# Fast inverse square root

Fast inverse square root is an algorithm that estimates , the reciprocal (or multiplicative inverse) of the square root of a 32-bit floating-point number x in IEEE 754 floating-point format. Computing reciprocal square roots is necessary in many applications, such as vector normalization in video games and is mostly used in calculations involved in 3D programming. In 3D graphics, surface normals, 3-coordinate vectors of length 1 is used, to express lighting and reflection. There were a lot of surface normals. And calculating them involves normalizing a lot of vectors. Normalizing is often just a fancy term for division. The Pythagorean theorem computes distance between points, and dividing by distance helps normalize vectors:

This algorithm is best known for its implementation in 1999 in the source code of Quake III Arena Game, a first-person shooter video game that made heavy use of 3D graphics. At that time, it was generally computationally expensive to compute the reciprocal of a floating-point number, especially on a large scale; the fast inverse square root bypassed this step.

**Algorithm :**

**Step 1 : ** It reinterprets the bits of the floating-point input as an integer.

i = * ( long * ) &y;

**Step 2 : ** It takes the resulting value and does integer arithmetic on it which produces an approximation of the value we’re looking for.

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

**Step 3 : **The result is not the approximation itself though, it is an integer which happens to be, if you reinterpret the bits as a floating point number, the approximation. So the code does the reverse of the conversion in step 1 to get back to floating point:

y = * ( float * ) &i;

**Step 4 : ** And finally it runs a single iteration of Newton’s method to improve the approximation.

y = y * ( threehalfs - ( x2 * y * y ) ); //threehalfs = 1.5F;

The algorithm accepts a 32-bit floating-point number as the input and stores a halved value for later use. Then, treating the bits representing the floating-point number as a 32-bit integer, a logical shift right by one bit is performed and the result subtracted from the magic number 0x5F3759DF. This is the first approximation of the inverse square root of the input. Treating the bits again as a floating-point number, it runs one iteration of Newton’s approximation method, yielding a more precise approximation.

Let’s say there is a number in exponent form or scientific notation:

=100 million

Now, to find the regular square root, we’d just divide the exponent by 2:

And if, want to know the inverse square root, divide the exponent by -2 to flip the sign:

So, the code converts the floating-point number into an integer. It then shifts the bits by one, which means the exponent bits are divided by 2 (when we eventually turn the bits back into a float). And lastly, to negate the exponent, we subtract from the magic number 0x5f3759df. This does a few things: it preserves the mantissa (the non-exponent part, aka 5 in: 5 · ), handles odd-even exponents, shifting bits from the exponent into the mantissa, and all sorts of funky stuff.

The following code is the fast inverse square root implementation from Quake III Arena (exact original comment written in Quake III Arena Game).

`// CPP program for fast inverse square root. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find the inverse square root ` `float` `inverse_rsqrt( ` `float` `number ) ` `{ ` ` ` `const` `float` `threehalfs = 1.5F; ` ` ` ` ` `float` `x2 = number * 0.5F; ` ` ` `float` `y = number; ` ` ` ` ` `// evil floating point bit level hacking ` ` ` `long` `i = * ( ` `long` `* ) &y; ` ` ` ` ` `// value is pre-assumed ` ` ` `i = 0x5f3759df - ( i >> 1 ); ` ` ` `y = * ( ` `float` `* ) &i; ` ` ` ` ` `// 1st iteration ` ` ` `y = y * ( threehalfs - ( x2 * y * y ) ); ` ` ` ` ` `// 2nd iteration, this can be removed ` ` ` `// y = y * ( threehalfs - ( x2 * y * y ) ); ` ` ` ` ` `return` `y; ` `} ` ` ` `// driver code ` `int` `main(){ ` ` ` ` ` `int` `n = 256; ` ` ` `float` `f = inverse_rsqrt(n); ` ` ` `cout << f << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

0.0623942

## Recommended Posts:

- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Check if a number is perfect square without finding square root
- Square root of an integer
- Square root of a number using log
- Program to calculate Root Mean Square
- Babylonian method for square root
- Range Sum Queries and Update with Square Root
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Square root of a number without using sqrt() function
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Euler's criterion (Check if square root under modulo p exists)
- Find square root of number upto given precision using binary search
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Fast average of two numbers without division
- Exponential Squaring (Fast Modulo Multiplication)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.