------- Comment #5 from gdr at cs dot tamu dot edu 2006-12-18 10:16 ------- Subject: Re: New: -O2 generates bad code
"dcb314 at hotmail dot com" <[EMAIL PROTECTED]> writes: | I compiled the following C++ code on a x86_64 machine | without optimisation. | | #include <iostream.h> | | int | main() | { | long long n = 1; | | cout << sizeof( n) << endl; | for (int i = 0; i < 100; ++i) | { | cout << n << ' ' << (float) n << '\n'; | n *= 2; | if (n == 0) | { | break; | } | } | return 0; | } | | When the compiled code runs, it is fine. | There are 64 executions of the loop. | | However, I added flag -O2, and the code runs differently. | There are 100 executions of the loop. | | Suspect optimiser bug. Rather, user bug. Your program contains an undefined behaviour which is provoked by the overflow of n *= 2; the compiler is therefore technically allowed to do whatever it likes. In this case, the optimizer assumes that you can never get from 1 to 0 by successive exponentiation. Therefore the test n == 0 is never executed. -- Gaby -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30245