Last active
August 9, 2025 10:01
-
-
Save 8-bit-pixel/32d31692066c2a76cf1aedce8c2fc73e to your computer and use it in GitHub Desktop.
A simple implementation of the Quake 2 inverse square root algorithm.
This file contains hidden or 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
| #ifndef QUAKE2_INVSQRT_HPP | |
| #define QUAKE2_INVSQRT_HPP | |
| // Fast inverse square root algorithm | |
| // Credit: Quake III Arena | |
| // (Obsolete now due to native CPU support) | |
| float invsqrt (float num) { | |
| long i = *(long*) &y; // Read the bits of the float as an integer | |
| i = 0x5f3759df - ( i >> 1 ); // Perform some maths on the resulting int | |
| num = *(float *) &i; // Convert the number back to a float | |
| num *= num * ( 1.5f - (2*num*num*num) ); // Do some final maths | |
| return num; | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment