Skip to content

Instantly share code, notes, and snippets.

@b1ek
Created January 11, 2023 17:15
Show Gist options
  • Save b1ek/02d15030148405a97cae4a63698a0e4b to your computer and use it in GitHub Desktop.
Save b1ek/02d15030148405a97cae4a63698a0e4b to your computer and use it in GitHub Desktop.
Fast square root in c
// Source: https://en.wikipedia.org/wiki/Fast_inverse_square_root
#include <stdint.h> // uint32_t
float Q_rsqrt(float number)
{
union {
float f;
uint32_t i;
}
conv = { .f = number };
conv.i = 0x5f3759df - (conv.i >> 1);
conv.f *= 1.5F - (number * 0.5F * conv.f * conv.f);
return conv.f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment