It is much older than Quake III, although the exact magic constant has varied a bit.
The encoding of a finite single-precision floating point number according to IEEE-754 (which is what most modern computers use) is
31 30 23 22 0
├─┼─┬─┬─┬─┬─┬─┬─┬─┼─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┤
│S│ Exponent │ Mantissa │
└─┴───────────────┴─────────────────────────────────────────────┘
The highest bit, S
is the sign. The value is negative if it is set, positive otherwise.
If Exponent
and Mantissa
are both zero, then the value is either 0.0 (if S
is zero) or -0.0 (if S
is 1).
If Exponent
is zero, but Mantissa
nonzero, you have a denormal number very close to zero.
Representations where Exponent
is 255 (all bits set) are reserved for infinity (if Mantissa
is zero), and not-a-number (if Mantissa
is nonzero).
For Exponent
values from 1 to 254, inclusive, Mantissa
contains the fractional bits of the mantissa, with an implicit 1 in the integers position (This is what the (1)
means in e.g. 0 10000001 (1)1011011011011011011011
.) In other words, the value Mantissa
represents for these Exponent
values, is from 1.000000000000000000000002 (1.0 in decimal) to 1.111111111111111111111112 (1.99999994 in decimal), inclusive.
Now, if we consider only nonnegative values (with S
clear), with E = Exponent
(between 1 and 254, inclusive), and M the logical value of Mantissa
(between 1.0 and 1.99999994), then the value V the single-precision floating point represents is
V = 2E - 127 × M
The 127 is the exponent bias. The square root of V is
√V = 2(E - 127)/2 × √M = 2E/2 - 127 × 263 × √2 × √M
and the inverse square root, which is the target operation here,
1/√V = 2(127 - E)/2 × 1/√M = 2-E/2 - 127 × 263 × √2 × 1/√M
noting that 2127/2 = 263.5 = 263 × √2.
If we take the integer representation, and shift it one bit to the right, we effectively halve E. To multiply by 263, we need to add 63 to the exponent; 63×223. However, for the square root operation, instead of multiplying by √2 × √M, we effectively just multiply by M/2. This means that that (shifting the integer representation one bit right, then adding 63 to the exponent) yields an estimate of a square root that is off by a factor between 0.7071067 and 0.75 for arguments greater than 1.0, and by a factor between 0.7071067 and 1448.155 for values between 0 and 1. (It yields 5.421×10-20 for √0, and 0.75 for √1.)
Note that for the inverse square root operation, we wish to negate E.
It turns out that if you shift the integer representation right by one bit, and then subtract that from (1 01111100 1101110101100111011111)2 (1597463007 in decimal, 0x5f3759df in hexadecimal, a single-precision floating-point approximation of √2127 ≈ 13043817825332782212), you get a pretty good approximation of the inverse square root (1/√V). For all finite normal arguments, the approximation is off by a factor of 0.965624 to 1.0339603 from the correct value; a very good approximation! All you need is one or two iterations of Newton's method for the inverse square root operation on top. (For f(X) = 0 iff X = 1/√V you need f(X) = 1/X^2 - V. So, each iteration in X is X - f(X)/f'(X) = X × (1.5 - 0.5 × V × X × X. Which should look quite familiar.)