Created
June 22, 2017 17:49
-
-
Save rnique/6a953af6c8a3e1c1819503d2c5f70a48 to your computer and use it in GitHub Desktop.
Fast Inverse Square Root
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
// from https://pizer.wordpress.com/2008/10/12/fast-inverse-square-root/ | |
float InvSqrt(float x){ | |
uint32_t i = 0x5F1F1412 - (*(uint32_t*)&x >> 1); | |
float tmp = *(float*)&i; | |
return tmp * (1.69000231f - 0.714158168f * x * tmp * tmp); | |
} | |
// from http://rrrola.wz.cz/inv_sqrt.html | |
float inv_sqrt(float x) | |
{ union { float f; uint32 u; } y = {x}; | |
y.u = 0x5F1FFF77ul – (y.u >> 1); | |
return 0.703974056f * y.f * (2.38919526f – x * y.f * y.f); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment