Fast Square Root Approximation
matmul.net·1d·
Discuss: Hacker News
Preview
Report Post

Fast Square Root Approximation Published on 18.06.2025


Almost a year ago I came across an old article named Understanding Quake's Fast Inverse Square Root. It blew my mind. A very fascinating approach to cut down the execution time by a good margin.

# $ ./a.out
number: 1337

+ result of math.h sqrt 36.565
* math.h took 15 ticks(1.5e-05s)

+ result of fast sqrt 36.5668
* fast sqrt took 1 ticks(1e-06s)

Note that this is an approximation, which was good enough for quake. In this post, we'll re-create this algorithm from scratch.

The original code looks like the following.

float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y  = number;
i  = * ( long * ) &y;						// evil floating point bit level hacking
i  = 0x5f3759df...

Similar Posts

Loading similar posts...