Skip to content

Instantly share code, notes, and snippets.

@8-bit-pixel
Last active August 9, 2025 10:01
Show Gist options
  • Select an option

  • Save 8-bit-pixel/32d31692066c2a76cf1aedce8c2fc73e to your computer and use it in GitHub Desktop.

Select an option

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.
#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