Just about a year ago, someone identified just as "ryszard" wrote about five tight lines of C code that calculate an approximate inverse square root (1/sqrt(x)). Mark Rickerby pointed the article out to me.
Like many chapters of Beautiful Code, this article challenges some assumptions about good coding that the software engineering literature has repeated to us over the years--notably, assumptions about hardware independence that's important for portability and thus for long-term correctness. Luckily, because neither integer nor floating-point formats are likely to change in the forseeable future, this Quake code looks risk-free. Still, ryszard seems conscious that it occupies one of the lower rungs of beauty, calling it a "really cool hack."
The code is described as being useful for 'normalisation of 3D vectors," which I am guessing can be useful for comparing trajectories in games, whether of the sun's rays or your bullets.
ryszard's search for authorship was also pretty damned impressive. While he laments at the end that he can't quite find out who put those five lines in its final form, he does succeed in finding the computer science professor who probably created the algorithm, identifying the MIT department known for that style of coding, and finding one of the Quake team who worked on that particular function.


Comments (3)
Wow, I had a touch of synchronicity when I saw this come up on my RSS feeds - it was only a couple of days ago I was reading about this after following a link in a Slashdot story about dissecting old code.
To me these few lines of code are incredibly beautiful because they encompass the essence of what keeps programming interesting for me - thinking around limitations. When I was googling for the origins of the code I came across this article, which documents some other famous Id Software work-arounds.
Posted by Wayne | November 19, 2007 12:57 AM
Don't forget the follow-up article where the authorship is resolved.
Posted by Nicholas | November 19, 2007 12:25 PM
This piece of code has been discussed probably more than ever. brings back many memories :)
Posted by Chris | November 19, 2007 8:21 PM