Fast Inverse Square Root: Quake III — John Carmack
“0x5f3759df: The famous magic constant. A masterclass in IEEE 754 floating-point mechanics, bit-level hacking, and Newton-Raphson approximation.”
Executive Summary: Fast Inverse Square Root and the 0x5f3759df Hack
The Fast Inverse Square Root algorithm—immortalized in the 1999 Quake III Arena source code by id Software under the function name Q_rsqrt—is celebrated as one of the most brilliant and iconic algorithmic hacks in computer science history. In 3D graphics and physics engines, computing vector normalization requires the reciprocal root calculation y = 1 / sqrt(x), an operation that historically required floating-point division and square root instructions to find the vector root length. The fast inverse square root algorithm solves this reciprocal square root bottleneck. Q_rsqrt computes an extraordinarily precise square root approximation and reciprocal root in a tiny fraction of the time using three profound steps: interpreting a 32-bit IEEE 754 floating-point number as an integer, shifting it right by one bit, subtracting it from the enigmatic magic constant 0x5f3759df, and applying a single iteration of Newton-Raphson approximation. This deep dive systematically deconstructs the mathematical derivations behind the magic constant, demonstrating how the IEEE 754 exponent-mantissa bit layout naturally mimics the logarithm function log2(x) for square root derivation and root approximation.
Key Mental Models Derived from this Lecture:
- Vector Normalization Graphics Bottleneck: Every 3D graphics lighting calculation, ray reflection, and physics collision requires dividing a vector by its Euclidean square root length sqrt(x^2 + y^2 + z^2).
- The Logarithm Trick in IEEE 754: When an IEEE 754 float is cast directly to an integer, the resulting integer bit pattern is an affine approximation of the base-2 logarithm of the square root.
- Exponent Halving via Bit Shift: Computing x^(-1/2) in log-space is equivalent to multiplying by -1/2, mapping directly to integer division by 2 via (i >> 1) for the square root reciprocal.
- Derivation of the Magic Constant 0x5f3759df: The magic constant minimizes the maximum relative error of the piecewise log square root approximation.
- Newton-Raphson Quadratic Convergence: A single iteration of Newton-Raphson on f(y) = 1/y^2 - x = 0 computes the reciprocal square root with less than 0.175% relative error.
- The Legacy of Hardware-Aware Software Engineering: Understanding low-level bit representation unlocks orders of magnitude performance gains before dedicated silicon instructions exist.
Key Timestamps: Fast Inverse Square Root Algorithm Breakdown
The 3D Graphics Bottleneck: Vector Normalization
The Quake III Source Code: Deconstructing the Q_rsqrt Function
IEEE 754 Floating-Point Deep Dive: Sign, Biased Exponent, and Mantissa
The Logarithmic Identity: Why (i >> 1) Approximates 1/sqrt(x)
Deriving the Magic Number: How Mathematicians Solved 0x5f3759df
Newton-Raphson Refinement: Achieving 0.175% Error in One Step
Mathematical Derivations: IEEE 754 Log Approximation and Newton Method
IEEE 754 Bit Representation as Logarithm Approximation
Because log2(1 + m) is approximately m + mu for m in [0, 1] (where mu is a correction factor ~0.0450465), the integer bit pattern of an IEEE 754 float is directly proportional to the base-2 logarithm of the number.
# Fast Inverse Square Root C implementation from Quake III
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 hack
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration of Newton-Raphson
return y;
}Derivation of the Magic Constant 0x5f3759df
We want y = x^(-1/2), which means log2(y) = -1/2 * log2(x). Substituting the integer representation equation yields I_y = 3/2 * 2^23 * (127 - mu) - (1/2) * I_x. The constant term evaluates to 1597463007 in base-10, which in hexadecimal is 0x5f3759df.
# Python derivation of the magic constant import math mu = 0.0450465 # Minimax error parameter magic_dec = int(1.5 * (2**23) * (127 - mu)) magic_hex = hex(magic_dec) assert magic_hex in ["0x5f3759df", "0x5f375a86"]
Newton-Raphson Quadratic Convergence Step
To refine the initial integer approximation without division, we apply Newton's method to f(y) = 1/y^2 - x. Since f'(y) = -2/y^3, the update formula requires only multiplication and subtraction: y * (1.5 - 0.5 * x * y^2).
# Comparing Q_rsqrt precision against 1/sqrt(x)
import numpy as np
def fast_inv_sqrt(x):
# Pure Python simulation of the C pointer casting and Newton step
import struct
x2 = x * 0.5
i = struct.unpack('>I', struct.pack('>f', x))[0]
i = 0x5f3759df - (i >> 1)
y = struct.unpack('>f', struct.pack('>I', i))[0]
y = y * (1.5 - x2 * y * y)
return y
test_x = 16.0
approx = fast_inv_sqrt(test_x)
true_val = 1.0 / np.sqrt(test_x)
max_err = abs(approx - true_val) / true_val
assert max_err < 0.002 # Error < 0.2%Community Consensus: Hacker News Debates on Fast Inverse Square Root
Did John Carmack Actually Write Q_rsqrt in Quake III?
“Carmack's genius was not inventing every math trick, but having the encyclopedic memory and engineering courage to put them into a production 60 FPS graphics engine.” — @gl_historian
Undefined Behavior in C: Strict Aliasing and Modern Compilers
“In 1999, compilers did what you told them to. In 2026, compilers assume undefined behavior means you wanted them to delete your code.” — @ub_watchdog
Is Q_rsqrt Obsolete on Modern Hardware with SSE / AVX rsqrtss?
“It’s obsolete in production, but immortal in spirit. It’s the Mona Lisa of computer science hacks.” — @nemean_fan
Technical FAQ: IEEE 754 Magic Constant, SIMD rsqrtss and Graphics Math
In Simple Made Easy, what is the fundamental difference between Simple and Easy?
In the Simple Made Easy lecture by Rich Hickey, Simple is an objective design property (simplex: having a single role, unbraided), whereas Easy is subjective and relative to developer familiarity (facilis: near at hand).
What does the Simple Made Easy lecture mean by the verb “to complect”?
In Simple Made Easy, to complect means to braid, intertwine, or fold multiple distinct concerns together (from Latin complecti). Once concepts are complected, you cannot reason about or test one without dragging the other along.
Why does Simple Made Easy advocate for immutable values over mutable state?
Simple Made Easy proves that mutable state braids value and time together. Immutable values represent permanent historical facts that can be freely shared across concurrent threads without locks, cached indefinitely, and trivially reasoned about.
How does the Epochal Time Model in Simple Made Easy solve concurrency?
In Simple Made Easy, identity is an atomic reference pointing to an immutable value snapshot at time t. Updates apply a pure function to produce a new immutable value at t+1 and swap the reference atomically (e.g. CAS), allowing lock-free reads.
Why does Simple Made Easy recommend passing plain data maps instead of OOP classes?
In Simple Made Easy, plain associative maps represent information directly without braiding data representation with domain logic or class inheritance hierarchies, enabling universal composition.
What architectural constructs does Simple Made Easy recommend for decoupled systems?
Simple Made Easy recommends five foundational constructs: persistent immutable data structures, declarative data (like JSON or EDN), asynchronous queues to decouple space and time, pure functions, and runtime schemas.
