Ask a room of programmers who wrote the most famous hack in video game history, and many will give the same answer: John Carmack.
The story goes like this. Quake III Arena needed to compute lighting and reflections fast on 1999 hardware. Somewhere in its source code sits a tiny function that calculates 1/√x with a bizarre trick: it treats a floating-point number as an integer, subtracts it from the hexadecimal constant 0x5f3759df, and gets an answer that is almost right. The comment next to that line is a single exasperated profanity. It feels like the work of a lone genius, and Carmack was id Software’s genius.
There is just one problem. Carmack said he didn’t write it.
The code everyone shares
When id Software released the Quake III Arena source code under the GNU General Public License in 2005, programmers found this function in a math file. With the original comments removed, it looks like this:
float Q_rsqrt( float number )
{
long i;
float x2, y;
const 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 ) );
return y;
}
It became one of the most shared snippets on the internet, and the legend attached itself to the most famous name in the building.
The crack in the legend
Rys Sommefeldt of the graphics site Beyond3D did what most retellings skip: he asked. Carmack replied that it was not his code, and that he did not think Michael Abrash, id’s other optimisation expert, had written it either. He suggested a different name: Terje Mathisen, a well-known assembly-language optimiser.
Mathisen had written fast inverse square root routines of his own, but he said this one was not the same as what he wrote. The trail moved on to Gary Tarolli, a co-founder of the graphics-card company 3dfx. Tarolli recognised the code. He had used it heavily and even tweaked the constant years earlier, while working on projects for Silicon Graphics’ Indigo workstations, but he said he could not take credit for inventing it.
At that point the investigation seemed to stall. Then, after the story spread online, a programmer named Greg Walsh got in touch.
What the record shows
According to Beyond3D’s follow-up, Walsh wrote the fast reciprocal square root while working at Ardent Computer, a company that built graphics minicomputers with custom vector processors. He needed a faster path for software that could not use the vector hardware. The seed of the idea, the article says, came from working with Cleve Moler, the mathematician who created MATLAB and was exploring Newton-Raphson approximation at the time.
So the best available account runs like this: an engineer at a short-lived hardware company wrote it, colleagues and the graphics industry passed it around, and it ended up in a game engine that made it famous. That is less cinematic than a lone genius. It is also how most clever code actually travels.
Why the trick works
The real story is better because the trick itself is not magic. A 32-bit float stores an exponent and a fraction. If you read those same 32 bits as an integer, you get a rough, scaled approximation of the number’s base-2 logarithm.
That matters because 1/√x is simply x raised to the power of minus one-half. In logarithms, that is “multiply the log by minus one-half.” Shifting the integer right by one bit halves it. Subtracting it from a carefully chosen constant flips the sign and corrects the scaling. Reinterpreting the bits as a float gives a first guess that is surprisingly close. One step of Newton’s method then sharpens it.
In 2003, Chris Lomont analysed the function and found that, after that single refinement step, its relative error stays below about 0.18 percent. He also searched for the best possible constant and found one that performs very slightly better, 0x5f375a86. Later researchers derived the constant analytically. The famous number was good engineering, not a mystical value.
Why the myth survives
The Carmack version persists because it has everything a legend needs: a famous name, a famous game, an incomprehensible constant, and a swear word in a comment. “Someone at Ardent Computer, inspired by Cleve Moler” is harder to tweet.
But the myth hides the part that is useful to working programmers. Good tricks are rarely invented in one place. They are found, shared, tuned, and reused, and the person whose name ends up attached is often just the last link in the chain.
The code has also aged. Modern processors have dedicated instructions for approximate reciprocal square roots, and today’s C compilers treat that pointer cast between float and long as undefined behaviour, so new code should use a safe bit copy instead. The hack belongs in a museum of good ideas, not in a new codebase.
If you want to retell the story correctly in one sentence: Quake III made the fast inverse square root famous, Carmack didn’t write it, and the best evidence points to Greg Walsh, building on ideas he picked up from Cleve Moler.
Sources
- id Software, Quake III Arena source code (GPL), q_math.c
- Rys Sommefeldt, Beyond3D: Origin of Quake3’s Fast InvSqrt()
- Rys Sommefeldt, Beyond3D: Origin of Quake3’s Fast InvSqrt(), Part Two
- Chris Lomont, “Fast Inverse Square Root” (2003)
- Moroz et al., “Fast calculation of inverse square root with the use of magic constant: analytical approach” (arXiv, 2016)
- Featured image: an SGI Indigo workstation from the 1990s. Wolfgang Stief, Wikimedia Commons, CC0

Leave a Reply