Skip to content

Instantly share code, notes, and snippets.

@lh3
Created August 24, 2019 00:58
Show Gist options
  • Save lh3/9430fbb713f8a6171ab0befecfee015d to your computer and use it in GitHub Desktop.
Save lh3/9430fbb713f8a6171ab0befecfee015d to your computer and use it in GitHub Desktop.
Fast square root
// a combination of inverse square root (see wiki) and inversion: https://bits.stephan-brumme.com/inverse.html
static inline float mg_sqrtf(float x)
{
union { float f; uint32_t i; } z = { x };
z.i = 0x5f3759df - (z.i >> 1);
z.f *= (1.5f - (x * 0.5f * z.f * z.f));
z.i = 0x7EEEEEEE - z.i;
return z.f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment