Profile photo for Ivan Latták

This line of code changed the world of low-level computer graphics:

  1. i = 0x5f3759df - ( i >> 1 ); // what the fuck?  

Appearing in the original source code of Quake 3 Arena, this line is part of a function in Quake’s graphical engine which handles the computation of the inverse square root of a floating point number. That is, for a number [math]x[/math], the function returns a number as close to [math]\frac{1}{\sqrt{x}}[/math] as feasible.

Previously, functions with this purpose would first draw an estimate of the result using table lookups, then they would refine the result using Newton’s approximation algorithm. The new function, dubbed fast inverse square root, took advantage of the IEEE 754 floating point format to compute the first estimate much faster, and using much less memory, than using a lookup table. It did this by first treating the bits of the 32-bit float as an integer, shifting that 1 bit to the right and then subtracting the result from a magic constant. The resulting bits would then be treated as a float again and refined by the approximation method.

The most baffling fact for me about this discovery of an unexpected and very fast solution to a common problem in low-level graphics is that this function was hidden in Quake’s source files for 5 years at least, and that its roots go back to the 90s. If this had been academia, the author could have had their dissertation based on this line alone. But this was the video game industry, which probably meant that this had been just another day in the author’s office.

Reference: Fast inverse square root - Wikipedia

View 48 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025