r/learnprogramming • • Sep 29 '25

TIL about Quake III's legendary "WTF?" code

This is a wild piece of optimization from Quake III Arena (1999):

float Q_rsqrt( float number )
{
    long i;
    float x2, y;
    const float threehalfs = 1.5F;
    
    x2 = number * 0.5F;
    y = number;
    i = * ( long * ) &y;                       
// evil floating point bit level hacking
    i = 0x5f3759df - ( i >> 1 );               
// what the fuck? 
    y = * ( float * ) &i;
    y = y * ( threehalfs - ( x2 * y * y ) );
    
    return y;
}

Those are the actual comments. It calculates inverse square roots 4x faster than normal by treating float bits as an integer and using a "magic number" (0x5F3759DF). Nobody knew who wrote it for years, turned out to be Greg Walsh from the late 1980s.

Modern CPUs have dedicated instructions now, but this remains one of the most elegant low-level hacks ever written.

https://en.wikipedia.org/wiki/Fast_inverse_square_root

1.5k Upvotes

131 comments sorted by

View all comments

10

u/scratch31415 Sep 29 '25

But why do: i = * (long *) &y And not just: i = y ?

Will probably be facepalming in 2 mins

27

u/DirkSwizzler Sep 29 '25

i is type long (32 but integer in this context) y is type float

Doing a direct assignment tells the compiler to round/truncate the decimal portion away to fit in an integer. I believe the exact conversion is controlled by CPU register settings at runtime.

The "*(long *)&y" tells the compiler to treat the raw bits as something that's already converted. it will most assuredly be some crazy value that does not reflect the floating point value at all. But it lets you do bit manipulation for real wizardy

15

u/risanaga Sep 29 '25 edited Sep 29 '25

It's called type punning. Just saying i = y takes the float value of y, truncates the decimal, and that becomes the integer. This reference/pointer cast effectively copies the bits as-is in the float. No truncating or type conversion.

As an actual example, the float value of -0 regularly converted to a long just becomes 0. This type pun gets you the value of -maxint.

Edit: just to add something. This is not normally something that should be done. It's a subversion of the type system that usually ends up being UB. It's occasionally necessary though

3

u/Lithl Sep 29 '25

Assume y = 3.5

If you do i = y, then i = 3.

If you do i = * (long *) &y, then i = 1080033280.