Fundamentals 17 min read

Unveiling the Magic of Fast Inverse Square Root: How 0x5f3759df Powers Game Physics

This article demystifies the fast inverse square root algorithm used in games, explaining the origin of the infamous 0x5f3759df magic number, the underlying IEEE‑754 floating‑point representation, the evil bit‑hack, Newton iteration steps, and how these tricks accelerate vector normalization.

ELab Team
ELab Team
ELab Team
Unveiling the Magic of Fast Inverse Square Root: How 0x5f3759df Powers Game Physics

Introduction

You might wonder how to write the following formula in code. A one‑line solution is: float y = 1 / sqrt(x); The game Quake III Arena uses a much more interesting implementation:

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 - ( i >> 1 );          // what the fuck?
  y  = * ( float * ) &i;
  y  = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  y  = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, can be removed

  return y;
}

If you see the number 0x5f3759df, you may feel a bit confused – the three questions begin:

What is it?

Where does it come from?

What is it used for?

The following sections explain the magic number, its origin, and its purpose.

Why This Algorithm Is Needed

In games, many physical effects such as lighting and reflections depend on a vector's direction rather than its length. Normalizing vectors simplifies many calculations. Computing the inverse square root of a number is a frequent operation, but division and sqrt are slow. The algorithm replaces them with fast multiplication, addition, and bit‑shifts, achieving about three times the speed with only ~1% error.

Initialization Parameters

long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y  = number;

The function receives a 32‑bit float, declares a 32‑bit integer i, two 32‑bit floats x2 and y, and a constant threehalfs. The variables occupy the same 32‑bit memory layout, which is essential for the bit‑level hack.

Binary Number Representation

Computers represent numbers using only 0 and 1. Common representations include sign‑magnitude, one's complement, two's complement, and bias (offset) code. For an 8‑bit example (decimal 4 → 0000 0100), the ranges are:

Sign‑magnitude: [-127, 127] with +0 and -0.

One's complement: same range, also with +0 and -0.

Two's complement: [-128, 127] (no negative zero).

Bias code: offset 128, range [0, 255] → [-128, 127] after bias.

Floating‑Point Numbers (IEEE‑754)

A 32‑bit float consists of a sign bit, an 8‑bit exponent (biased by 127), and a 23‑bit mantissa. The exponent uses bias code, allowing simple integer comparison. For example, the decimal 4 has exponent bits 1000 0011 after adding the bias.

Binary Conversion of a Float

Interpreting the bits of a float as an unsigned integer enables arithmetic on its binary representation. This is the basis of the "evil bit hack" used in the algorithm.

The Three‑Step Process

evil bit hack

By casting the address of a float to a long* and dereferencing, we obtain the raw 32‑bit pattern without changing the memory content:

i = * ( long * ) &y;

what the fuck

The expression 0x5f3759df - (i >> 1) approximates the logarithm of the float, effectively providing an initial guess for the inverse square root. The constant 0x5f3759df was derived mathematically to minimize error over a useful range.

Newton Iteration

Newton's method refines the approximation: y = y * ( threehalfs - ( x2 * y * y ) ); Each iteration roughly halves the error; the original code performs two iterations, but one is often sufficient.

Reference

0x5f3759df – see http://h14s.p5r.org/2012/09/0x5f3759df.html FAST INVERSE SQUARE ROOT – see http://www.matrix67.com/data/InvSqrt.pdf

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmfloating-pointFast Inverse Square Rootbit hack
ELab Team
Written by

ELab Team

Sharing fresh technical insights

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.