Hacker News new | ask | show | jobs
by sushshshsh 2031 days ago
Insert obligatory reference to the fast inverse square root calculation which, thanks to its ability to more quickly generate (1/x) by using a bit shift with a magic number 0x5F3759DF, allowed performant lighting calculations for use in games. This number was known for this purpose for long before Quake (shout out SGI), but was popularized for the first time there
4 comments

It's not the same without the original comments. Also, I don't know which is crazier, the magic number, or the fact that the code is casting a float to long bitwise and then working on it.

  i = * (long * ) &y; // evil floating point bit level hacking
  i = 0x5f3759df - (i >> 1); // what the fuck?
The bit manipulation already generates an approximation of 1/sqrt(x), not just 1/x.
Fast inverse square root wasn't in Quake until Quake III though, right?
That's correct, though there was plenty of other asm and bit level evil graphics voodoo that made quake able to run so fast on consumer hardware a few generations earlier than if they were stuck using slower division:)
The most notable trick in Quake I (at least IMHO) is the triangle rasterizer line-loop which schedules the divide needed for perspective-correct texture mapping to the FPU so that this expensive divide can run in parallel to pixel-span rasterization on the integer ALU. To my young brain who's only done 8- and 16-bit assembly on simple CPUs before this was nothing short of rocket science :)
Damn, that's awesome