# Fast Inverse Square Root in Delphi’s Object Pascal

Does Delphi make you a better programmer? Is Object Pascal code more readable?

There is an algorithm with a mystery constant that rose to fame in John Carmack’s Quake III Arena C code for quickly estimating the inverse square root of a 32-bit floating-point number.

```
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 f**k?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
```

## Understanding the Code

It is based on Newton’s method for estimating roots. Additionally, it converts the floating-point number to an integer, uses bit shifting, and starts with an approximation of √2^127. The commented out line allows for an additional iteration to improve the estimate, which was not used in Quate III Arena. You can read more about it in Wikipedia or watch some YouTube videos on the topic [including a very deep dive]. Here is a nice high-level video:

## Can Delphi Make it Better?

Facebook user Toon Krijthe showed how much clearer and simpler the code would be if it were implemented in Object Pascal/Delphi.

```
function rsqrt(const ANumber: Single): Single;
var
ResultAsInt: UInt32 absolute Result;
begin
Result := ANumber;
ResultAsInt := $5F3759DF - (ResultAsInt shr 1);
Result := Result * ( 1.5 - (ANumber * 0.5 * Result * Result)); // 1st iteration
// Result := Result * ( 1.5 - (ANumber * 0.5 * Result * Result)); // 2nd iteration, this can be removed
end;
```

It makes use of the **absolute** keyword to map the floating-point number to the integer, which avoids all the *“evil floating point bit level hacking.” *This is something I love about Delphi and Object Pascal: It gives you access to pointers, and raw memory, etc. but doesn’t force you to use it when you don’t want/need to. Shorter code isn’t always easier to understand, just take a look at any regular expression, but this is an improvement because it removes so much extraneous code. Much more readable.

*“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”*

Object Pascal is so readable it makes programmers better as it makes their code more readable and maintainable. Don’t get me wrong, you can write spaghetti in any programming language/syntax, but starting with a readable one helps. This is why there are so many “Legacy” Delphi programs: They are successful and maintainable. Code that doesn’t work, or isn’t maintainable gets thrown out or re-written.

## Need More High-Performance Math?

If you are looking for more fast math routines for Delphi, check out the High-Performance FastMath Library by Embarcadero MVP **Erik van Bilsen** of Grijjy fame.

### FastMath – Fast Math Library for Delphi

FastMath is a Delphi math library that is optimized for fast performance (sometimes at the cost of not performing error checking or losing a little accuracy).

This makes FastMath ideal for high-performance math-intensive applications such as multimedia applications and games. For even better performance, the library provides a variety of “approximate” functions (which all start with a `Fast`

-prefix). These can be very fast, but you will lose some (sometimes surprisingly little) accuracy. For gaming and animation, this loss in accuracy is usually perfectly acceptable and outweighed by the increase in speed. Don’t use them for scientific calculations though…

## Leave Your Comment