Back
Slide comparing two perfect magic numbers with the exact curve

What I built

  • The C and x86-64 SIMD implementations
  • The first magic-number derivation

Normalizing vectors and normal maps needs 1/√x constantly. Quake III’s famous trick approximates it by reading the bits of an IEEE-754 float as an integer, halving that with a bit shift and subtracting the result from a “magic number”.

What we did

  • Explained why the bit-level trick works and how a magic number can be derived.
  • Implemented 32-bit and 64-bit variants in C11 and x86-64 assembly, including SSE/SIMD versions.
  • Compared custom magic numbers with Quake’s original, and benchmarked speed and precision against conventional square-root implementations.

Course: Aspekte der systemnahen Programmierung bei der Spieleentwicklung (TUM).

Report

The derivation and benchmarks are written up as a report in German, Fast Inverse Square Root: Magic Numbers, SIMD and Precision (Y. R. Cui, S. Winter, J. Wagner). The main results:

  • Precision. For inputs between 4 and 9, our derived magic number (0x5f300000) is more accurate than Quake’s 0x5f3759df. Over larger ranges the error of a single derived number grows to around 10%.
  • Speed (float). The SIMD assembly version of the fast inverse square root was the fastest: about 16 times faster than the plain C version using sqrt at 100 inputs.
  • Speed (double). With 128-bit XMM registers, SIMD can only process two doubles at once, so the gain shrinks, but the assembly SIMD version is still about twice as fast as the C version.
  • Compiler optimization. At -O3, GCC’s C version overtakes our SIMD assembly above about 100 inputs. At -O2, our assembly version stays almost three times faster.
Up nextPenGO!Gameplay · Serious GamesA wellness game where a virtual penguin's health depends on your real habits: steps, water, friends and getting out of the house.

Trophies

Settings

Preferences

System information

Display
Frame rate
–
Built with
Astro, TypeScript, View Transitions, Web Audio