[root@FedoraCore5 tmp]# cat a.c #include <stdio.h> float Q_rsqrt(float number) { long i; float x2, y; float threehalfs = 1.5f; x2 = number * 0.5f; y = number; i = *(long *)&y; i = 0x5f3759df-(i>>1); y = *(float *)&i; y = y * (threehalfs -(x2 *y *y)); y = y *(threehalfs - (x2 *y *y)); return y; } int main() { printf("%f\n", Q_rsqrt(305.0f)); return 0; } [root@FedoraCore5 tmp]# gcc a.c [root@FedoraCore5 tmp]# ./a.out 0.057260 [root@FedoraCore5 tmp]# gcc -O2 a.c [root@FedoraCore5 tmp]# ./a.out 0.000000 [root@FedoraCore5 tmp]# gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking =release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java, fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux Thread model: posix gcc version 4.1.0 20060304 (Red Hat 4.1.0-3)
You code violates the aliasing rules in this line: y = *(float *)&i; Please read http://gcc.gnu.org/bugs.html In the non-bug section you will find: "Casting does not work as expected when optimization is turned on." *** This bug has been marked as a duplicate of 21920 ***