Bug 26941 - float optimization bug
Summary: float optimization bug
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.0
: P3 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-03-30 13:10 UTC by zengpan
Modified: 2006-03-30 14:56 UTC (History)
57 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description zengpan 2006-03-30 13:10:57 UTC
[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)
Comment 1 Volker Reichelt 2006-03-30 14:56:43 UTC
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 ***