GCC Bugzilla – Bug 44098
-O3 causes quickInvSqrt() to return -inf.
Last modified: 2010-05-12 15:29:18 UTC
I have tested the famous quickInvSqrt() algorithm uses in Quake, and it does not compile correctly when compiling with -O3. It does however work completely fine when compiling for -O0, -O1 and -O2.
The function goes like so:
float quickInvSqrt(float x)
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
Created attachment 20645 [details]
The test case.
Actually, it's quite absurd, but trying to print out from 1 to 10 rather than 1 to 100 seemed to work, even with -O3.
Created attachment 20646 [details]
ASM output with -O2
Assembly output using gcc -S -O2 invsqrt.c
Created attachment 20647 [details]
ASM output with -O3
Assembly output with -O3.
First blush, I can see some pretty serious aliasing violations... try -Wall
and then try -O3 -fno-strict-aliasing
That worked (-O3 -fno-strict-aliasing). But why does this work on -O2 and not -O3?
You are violating the aliasing rules, anything can happen, at any optimization level...
*** This bug has been marked as a duplicate of 21920 ***