Fast Inverse Square Root

One of the most famous optimization techniques in game programming, originally popularized by Quake III Arena.
...existing code...

invLength = fastInvSqrt(x*x + y*y)

x *= invLength
y *= invLength

This is much faster on old CPUs.

The Famous Quake III Algorithm


#include <stdint.h>

inline float fastInvSqrt(float number) {

    union {
        float f;
        uint32_t i;
    } conv;

    conv.f = number;

    const float x2 = number * 0.5f;
    const float threehalfs = 1.5f;

    conv.i = 0x5f3759df - (conv.i >> 1);

    conv.f = conv.f * (
        threehalfs - (x2 * conv.f * conv.f)
    );

    return conv.f;
}

How It Works

Step Description
Bit-level hack Approximates inverse square root using floating-point bits
Magic number 0x5f3759df gives an initial approximation
Newton-Raphson Improves approximation accuracy

Usage Example

Vector Normalization


float vx = 10.0f;
float vy = 5.0f;

float invLength = fastInvSqrt(vx*vx + vy*vy);

vx *= invLength;
vy *= invLength;

Distance Approximation


float dx = x2 - x1;
float dy = y2 - y1;

float distSq = dx*dx + dy*dy;

float distance = distSq * fastInvSqrt(distSq);

Advantages

Benefit Why It Matters
Very fast Much faster than sqrt + division on old hardware
Low CPU usage Good for consoles with low clock speeds
Great for hot loops Useful in rendering, physics and particles
Classic game optimization Used in famous game engines

Disadvantages

Problem Description
Approximation Not perfectly accurate
Hard to understand Bit-level tricks are confusing
Modern CPUs improved Modern hardware sqrt is much faster today

Where To Use It

Where NOT To Use It

Fast inverse square root is an approximation. Always benchmark before replacing all sqrt() calls.

Extra Optimization Tips

Retro Hardware Philosophy

Old hardware rewards:

Fast inverse square root became legendary because it represents the mindset of old-school engine optimization.