Here is a small test case, with output and version information in the comments.
There are enough ways of stating this problem that it was not evident to me within the existing bug list. One skilled in the art would know if this is a duplicate. I'm not sure where to upload my .i file, so I will try to attach it a few minutes after I post this. ------- /* * Illustration of GCC bug * Marc W. Abel, August 2, 2010 * * gcc version information follows * ------------------------------------ * Using built-in specs. * Target: x86_64-linux-gnu * Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu * Thread model: posix * gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) * ------------------------------------ * * Invocation and correct output (using -O2): * * $ gcc -O2 o3bug.c && ./a.out | head -n 6 * 7ffffffffffffffd fdffffffffffff7f * 7ffffffffffffffe feffffffffffff7f * 7fffffffffffffff ffffffffffffff7f * 8000000000000000 0000000000000080 * 8000000000000001 0100000000000080 * 8000000000000002 0200000000000080 * * Invocation and incorrect output (using -O3): * * $ gcc -O3 o3bug.c && ./a.out | head -n 6 * 7ffffffffffffffd fdffffffffffff7f * 7ffffffffffffffe feffffffffffff7f * 7fffffffffffffff ffffffffffffff7f * 8000000000000000 100ffffffffffff7f * 8000000000000001 101ffffffffffff7f * 8000000000000002 102ffffffffffff7f */ #include <stdio.h> /* * Convert an integer into an eight-byte format for persistance, LSB first. */ static void int_to_persist(unsigned char *eight_bytes, long long n) { int i; for (i=0; i<8; i++, n >>= 8) eight_bytes[i] = n & 255u; } int main(int argc, char *argv[]) { unsigned char bytes[8]; int i; long long m; for (m = 0x7ffffffffffffffdLL; ; ++m) { printf("%llx ", m); int_to_persist(bytes, m); for (i=0; i<8; i++) printf("%02x", bytes[i]); printf("\n"); } } -- Summary: -O3 optimization problematic on "long long" types near 64 bit wraparound Product: gcc Version: 4.4.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: robots at marcabel dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45164