I've written this little sample program to illustrate the problem:
It is supposed to shift the bit-representation of "a" to the right and to the left again, which works when compiling without optimization or with level 1 optimization (-O switch for gcc). But with level 2 optimization (-O2) the program outputs zero. My problem is also that I can't really debug with optimization, somehow the instructions are mixed or seem to be skipped.Code:1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() 5 { 6 double a = 2.3492e8,b; 7 extern double test(double); 8 9 b = test(a); 10 11 printf("%e\n",b); 12 13 } 14 15 double test(double a) 16 { 17 long tmp; 18 double ret; 19 20 tmp = *(long*)&a; // copy ieee-754 representation of "a" to "tmp" 21 tmp = tmp >> 5; 22 tmp = tmp << 5; 23 ret = *(double*)&tmp; 24 25 return ret; 26 }
Does anybody have an idea why this is? I really need to use second level optimization.
My system is Debian squeeze x86-64 with gcc 4.3.3