Fast inverse square root

0x5f3759df - Better magic value (HN)

Originaly from quake 3/nvidia source code

The bits of a float as an integer literal are proportional to its log2 value (plus a constant), which is handy for computing a reciprocal square root in logspace, which when converted back to a float gets exponentiated, yielding the answer (after a couple more refinement steps). If you actually take the time to manually hammer out the bitwise manipulations, the fast inverse square root is quite simple, but it is nonetheless regarded as black magic, likely due to the totally non-explanatory comments in the Quake source code. - HN / Fast Inverse Square Root — A Quake III Algorithm

float inv_sqrt(float x) { 
  union { float f; uint32 u; } y = {x};
  y.u = 0x5F3759DFul - (y.u >> 1);  // Magic!
  return 0.5f * y.f * (3.0f - x * y.f * y.f);
}

Intel impl

It is worth noting that with AVX-512, Intel has introduced a native inverse sqrt approximation (VRSQRT14). Inverse sqrt approximation is available since SSE1 with rsqrtss & rsqrtps instructions. VRSQRT28 too, which has max 2^-28 rel error.

Written on July 9, 2018, Last update on August 23, 2021
math c++ fastware quake