r/learnprogramming • u/WasteKnowledge5318 • 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.
1.5k
Upvotes
501
u/theBarneyBus Sep 29 '25
To be pedantic, it doesn’t calculate an inverse square root, it approximates it (within a small single-digit percent margin of error).
The slight inaccuracies lead to a slightly non-perfect FOV & “framing” of each rendered frame, but it’s close enough to not matter.
268
u/light_switchy Sep 29 '25
This is the best analysis I've come across.
34
u/ruat_caelum Sep 29 '25
To be far the
was used twice (though using it once on the domain supplied is less than 1% error across the span, so it was actually:
24
u/zd_3dgy Sep 29 '25
hash functions and rng generators have code like this too. I wonder how they come up with the magic numbers tho in 1980 without plotting software
13
u/derpbynature Sep 29 '25
What exactly does the "magic number" do here?
132
u/inline_five Sep 29 '25
Back when men were men and programmers were programmers and knew what a pointer was
15
u/sellibitze Sep 29 '25
Yeah, it's cool. Just let me add two things:
10
u/scratch31415 Sep 29 '25
But why do: i = * (long *) &y And not just: i = y ?
Will probably be facepalming in 2 mins
3
u/FiTroSky Sep 29 '25
Wonder how much more faster some modern game would be if we allowed more "approximations".
3
u/batclocks Sep 29 '25
There’s this guy who only has one YouTube video on his whole channel, and it’s just a really detailed interesting breakdown of this algorithm.
2
u/crispyfunky Sep 29 '25
Give this guy an HPC performance optimization job and a custom ASIC
2
u/Xiten Sep 29 '25
Loved quake 3. id Tech 3 is amazing.
2
u/turtleXD Sep 29 '25
not too long ago i did an exercise where I basically applied this same black magic, but for the normal square root instead. forced me to really understand how it worked. awesome stuff
2
u/TrevorLaheyJim Oct 02 '25
All of us have at some point accidentally committed something like this by accident.
Code Reviews don't always catch it.
1
u/Sakkyoku-Sha Oct 02 '25
I always found it super interesting that this isn't true on modern CPUs and compilers.
You can benchmark this in C today, and it won't be faster than other commonly used methods.
1
u/Salty_Upstairs3151 Oct 16 '25
https://work.mercor.com/?referralCode=c216dd0d-1f8b-4a81-9c78-b3b5e7a4c2a6
0
u/Nixinova Sep 29 '25
Im still confused why they saved "three halfs" to a variable as if that's gonna change, meanwhile 0xNonsense gets to just sit as a magic number, lol
-4
u/RedditWishIHadnt Sep 29 '25
I think this caused problems when open sourcing the code as someone else had copyrighted this trick some time after Quake was released so it had to be rewritten.