0x5f3759df the magic of FastInvSqrt()

August 27, 2010

Ben Jones

No, that isn’t 1337 sp3@k, its a 32 bit integer. The magic number, widely disseminated after the Quake 3 source code was released, is part of a cryptic function: float InvSqrt(float x) { float xhalf = 0.5f*x; int i = *((int *)&f); i = 0x5f3759df - (i >> 1); x = *((float *)&i); return x * (1.5f - xhalf * x * x); } which somehow returns the 1/sqrt(x)… but how? I’ll talk about how this black magic works (to some degree) as well as the mystery of its origin.