Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Fast Inverse Square Root in Delphi’s Object Pascal

delphi fast inverse square root quake 3 arena 3

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.

[crayon-673fb80323ed2299288313/]

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.

[crayon-673fb80323ed7369751836/]

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.”
Martin Fowler
Software Engineer
Author of “Refactoring”

 

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?

Erik van Bilsen
Embarcadero MVP
Co-Founder of Grijjy, inc.
Author of neslib/FastMath

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.

Check out FastMath if you need faster calculations that don’t need full accuracy.

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…

Exit mobile version