The main loop in this code should loop from 'a' to 'z'. On gcc3.3.3 on fedora with -O2 and -funroll-loops, it loops the correct number of times but the key value alternates between 97 and 98. Compile line: g++ -o gccBug -O2 -funroll-loops main.cpp
Contents of main.cpp: #include <stdio.h> #define CP_ALPHA_STATE(x) (1<<((x)-'a')) class GptoKeyState { public: GptoKeyState() { for (int i=0; i< 256; ++i) { _keyDown[i] = 0; } } /// int IsDown(unsigned char key) const { return _keyDown[key]; } void SetDownKey(unsigned char key, int down) { _keyDown[(unsigned int)key] = down; } /// void SetDown(unsigned char key, int down) { _keyDown[(unsigned int)key] = down; } private: int _keyDown[256]; }; int main(int, char**) { GptoKeyState _keyState; char key; int status2 = 8; for (key = 'a'; key<= 'z'; ++key) { int keyDown; printf("Testing %d\n", (int) key); // the & can return a value greater than 1, we want a boolean if (status2 & CP_ALPHA_STATE(key)) keyDown = 1; else keyDown = 0; if (_keyState.IsDown((unsigned char)key) != keyDown) { _keyState.SetDown((unsigned char)key, keyDown); } } } Results of g++ -v: Reading specs from /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --disable-libunwind-exceptions --with-system-zlib --enable-__cxa_atexit --host=x86_64-redhat-linux Thread model: posix gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) Output from generating ii file: Reading specs from /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --disable-libunwind-exceptions --with-system-zlib --enable-__cxa_atexit --host=x86_64-redhat-linux Thread model: posix gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/cc1plus -E -D__GNUG__=3 -quiet -v -D__GNUC__=3 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=3 -D_GNU_SOURCE main.cpp -funroll-loops -O2 main.ii ignoring nonexistent directory "/usr/x86_64-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/include/c++/3.3.3 /usr/include/c++/3.3.3/x86_64-redhat-linux /usr/include/c++/3.3.3/backward /usr/local/include /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/include /usr/include End of search list. /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/cc1plus -fpreprocessed main.ii -quiet -dumpbase main.cpp -auxbase main -O2 -version -funroll-loops -o main.s GNU C++ version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) (x86_64-redhat-linux) compiled by GNU C version 3.3.3 20040412 (Red Hat Linux 3.3.3-7). GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 as -V -Qy -o main.o main.s GNU assembler version 2.15.90.0.3 (x86_64-redhat-linux) using BFD version 2.15.90.0.3 20040415 /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/collect2 --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o gccBug /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/../../../../lib64/crt1.o /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/../../../../lib64/crti.o /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/crtbegin.o -L/usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3 -L/usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/../../../../lib64 -L/usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/../../.. -L/lib/../lib64 -L/usr/lib/../lib64 main.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/crtend.o /usr/lib/gcc-lib/x86_64-redhat-linux/3.3.3/../../../../lib64/crtn.o I'll try and attach main.ii after submitting the bug. Sorry if this isn't quite the right format, this is my first g++ bug. -Dirk (reporting from Pixar Animation Studios software development group) -- Summary: -O2 -funroll-loops causes bad loop behavior on fedora Product: gcc Version: 3.3.3 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: anndirkvg at gmail dot com CC: anndirkvg at gmail dot com,gcc-bugs at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18120