The fast inverse square root alogorithim to compute the inverse sqaure root 1\sqrt(x)
of a number
x
. This algorithm was first used in the 1999 first person shooter Quake-III-Arena1
You can find the original here
near line 552.
We will not go in depth of how this algorithm works here, but basically we attempt inteprete a floating
point number x
as a integer in orger to approximate
where
And when we interprete it as an integer:
where
By substituting
We take
And this is the principal behind the quake algorithm.
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
Here I attempt to Calculate the magic number automatically for certain floating point types at compile time
using c++ templates and constexpr
context. This is the only idea behind this repo.
In Quake.hpp:
constexpr static typename int_type<_T>::type magic_constant(void);
Along with giving a template implementation of the fast inverse sqare root, I have created a main.cpp
to
test the function. We test the code on both IEC 559 single and
double precision. To build the test executable:
make
./build/quaketest 4.0
The syntax for quaktest
is:
quaketest [option] <number>
The available options are -d
for double precision and -f
for single precision.