TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Quake's Fast Inverse Square Root

107 pointsby pjinover 13 years ago

13 comments

bermanoidover 13 years ago
This is a good thing to know about, but bear in mind that these days it's often slower than just using whatever built in square root you have access to. Especially beware in languages where you need to do something more than a cast to get the raw bits out of the floating point number, oftentimes that alone is enough to kill any performance gains you might otherwise see. Profile your code before and after, and make sure you've got an easy way to switch back if you realize that you've gone and made things slower (or worse, too inaccurate for your use case).<p>If this was a universally effective optimization with no potential downside, it would already be built in to your standard math library...
评论 #3115761 未加载
评论 #3115778 未加载
评论 #3116132 未加载
评论 #3117073 未加载
评论 #3115554 未加载
achilleover 13 years ago
Also see: <a href="http://en.wikipedia.org/wiki/Fast_inverse_square_root" rel="nofollow">http://en.wikipedia.org/wiki/Fast_inverse_square_root</a><p>And for more analysis and improvements see those papers:<p><a href="http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf" rel="nofollow">http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf</a><p><a href="http://www.daxia.com/bibis/upload/406Fast_Inverse_Square_Root.pdf" rel="nofollow">http://www.daxia.com/bibis/upload/406Fast_Inverse_Square_Roo...</a><p><a href="http://www.geometrictools.com/Documentation/FastInverseSqrt.pdf" rel="nofollow">http://www.geometrictools.com/Documentation/FastInverseSqrt....</a>
psykoticover 13 years ago
Huh. The only really interesting part of the algorithm is the initial guess with the magic number, and he doesn't go into that part at all. Read Chris Lamont's paper if you care enough to want to understand it. The only downside to Lamont's paper is that it's a post mortem analysis rather than a pedagogical derivation. I worked out a step-by-step development of the algorithm last year, but I've never gotten around to writing it up in tutorial form.
thretover 13 years ago
Obligatory XKCD reference:<p><a href="http://xkcd.com/664/" rel="nofollow">http://xkcd.com/664/</a><p>Image text: Some engineer out there has solved P=NP and it's locked up in an electric eggbeater calibration routine. For every 0x5f375a86 we learn about, there are thousands we never see.
ch0wnover 13 years ago
This is the kind of article I come to Hacker News for. A clever hack for a real-world problem with an excellent explanation.
评论 #3116978 未加载
mtkleinover 13 years ago
Every time I read the headline for a fast algorithm for inverse square root I think "Well, yeah, you just multiply the input by itself. Can't get much faster to undo a square root than that." Silly me.
评论 #3115415 未加载
derwikiover 13 years ago
I love that this comes up regularly in this community. It's one of those things that everyone seems to discover at some point.
palishover 13 years ago
The most useful modern-day knowledge you could gain from this is here: <a href="http://www.sosmath.com/calculus/diff/der07/der07.html" rel="nofollow">http://www.sosmath.com/calculus/diff/der07/der07.html</a><p>(Newton-Raphson Method)
rogerallenover 13 years ago
Relevant link for those who want to know more about who came up with the magic number. <a href="http://www.beyond3d.com/content/articles/15/" rel="nofollow">http://www.beyond3d.com/content/articles/15/</a>
0x12over 13 years ago
<a href="http://news.ycombinator.com/item?id=419166" rel="nofollow">http://news.ycombinator.com/item?id=419166</a>
ajessupover 13 years ago
Awesome writeup, thanks!<p>Came across something similar a couple of days ago while browsing the source code of Marathon (one of the early games of Jason Jones and Bungie, later of Halo fame). At the time, Bungie were the Mac equivalent of id software, and Jason Jones the equivalent of John Carmack.<p><a href="http://marathon.svn.sourceforge.net/viewvc/marathon/branches/aleph-source/Source_Files/Misc/world.cpp?revision=3&#38;view=markup" rel="nofollow">http://marathon.svn.sourceforge.net/viewvc/marathon/branches...</a> (look for isqrt())<p>There's a great explanation in the comments.<p>This code was most likely written by Jason, probably around '93-'94.
mathattackover 13 years ago
Which came first? Quake 3 or the papers? This defines how interesting it is. I've seen a lot of folks working through Quake's code lately. Hard to tell if they are originators or appliers, but either way it's much more interesting than reading bland examples from a book.
aw3c2over 13 years ago
Not Quake, but Quake 3. That's one and half generation of engines ahead.