Just wanted to stash this little tidbit here in case I needed it for something.
This Slashdot article bringing up Origin of Quake3’s Fast InvSqrt(). The original article is a bit of a sleuthing expedition looking for the origins of this little code snippet and makes for an interesting read along with pointing to some other interesting sources.
The algorithm is a remarkable in the way it hides a lot of complexity in a simple looking package. All it does is calculate 1/sqrt(x), but does it very quickly using a cleverly written Newton-Raphson method.
float InvSqrt(float x) { float xhalf = 0.5f*x; int i = *(int*)&x; // get bits for floating value i = 0x5f3759df - (i>>1); // gives initial guess y0 x = *(float*)&i; // convert bits back to float x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy return x; }
Discover more from Imablog
Subscribe to get the latest posts sent to your email.