The single C++ line that is worth millions of dollars.

Many developers has already heard about the Ariane5 bug , it was one of the most infamous software failures in the history of aerospace engineering. It occurred on June 4, 1996, during the maiden flight of the Ariane 5 rocket, which was intended to launch four Cluster satellites into orbit to study Earth’s magnetosphere.

The bug itself was a software issue related to the guidance system of the rocket. The software component responsible for converting a 64-bit floating-point value to a 16-bit signed integer for use in the guidance system caused an unhandled exception due to an overflow error. So finally one line of code costs millions of dollars and make the Ariane 5 launch is widely acknowledged as one of the most expensive software failures in history.

But in the other side do you know which line of code worth million of dollars?

It was this magic C++ line : i = 0x5f3759df – ( i >> 1 );

This line was introduced first in the code of Quake to optimize the calculation of the inverse square root.

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, this can be removed

  return y;
}

Why optimizing Inverse square root is so important?

Inverse square roots find utility in video game graphics, particularly within the realm of 3D game engines. Various aspects of game programming, such as pathfinding, lighting, and reflections, heavily rely on vector normalization, a process that necessitates an inverse square root operation. However, performing inverse square roots, which involve floating-point division, can be computationally costly for processors. In fast-paced and visually immersive games like Quake III Arena, these computations occur millions of times per second. Therefore, even a slight enhancement in the performance of such calculations could notably augment the speed of graphics computation and ultimately enhance the game’s frame rate. To circumvent the resource-intensive nature of the inverse square root function, the programmers of the id Tech 3 engine devised an exceptionally precise and rapid approximation.

Without this clever optimization, perhaps Quake would not have achieved its status as a benchmark in the gaming industry.

Who is the brilliant mind behind this innovative solution?

Rys Sommerfeldt, Senior Manager of the European Game Engineering Team at AMD RTG, launched an investigation into the function’s origins in 2004. And finally it appears that Greg Walsh is the author. Greg Walsh is a monument in the world of computing. He helped engineer the first WYSIWYG (“what you see is what you get”) word processor at Xerox PARC and helped found Ardent Computer. Greg worked closely with Cleve Moler, author of Matlab, while at Ardent and it was Cleve who Greg called the inspiration for the Fast Inverse Square Root function.

Morale of the story

For some problems, take your time and think outside the box. Thinking outside the box is about breaking away from conventional or traditional ways of thinking and exploring unconventional, innovative, and creative solutions to problems. So when the problem is difficult to resolve, take your time to try exploring it from different sides, maybe you could find a solution that surpass your exceptations.