Created
January 11, 2023 17:15
-
-
Save b1ek/02d15030148405a97cae4a63698a0e4b to your computer and use it in GitHub Desktop.
Fast square root in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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