Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Also related: Quake III fast inverse square root[1].

[1]: https://github.com/id-Software/Quake-III-Arena/blob/master/c...



sort by: page size:

Here's a link to the infamous fast inverse square root function: https://github.com/id-Software/Quake-III-Arena/blob/master/c...

The Fast Inverse Square Root function from Quake III:

    i  = 0x5f3759df - ( i >> 1 );               // what the fuck?
https://en.wikipedia.org/wiki/Fast_inverse_square_root

I was just talking about this paper with a coworker on Friday.

One of the best parts about this code is its initial implementation in Quake III. The comments are especially funny. It's on Wikipedia - https://en.wikipedia.org/wiki/Fast_inverse_square_root


Of course there's "Fast Inverse Square Root — A Quake III Algorithm"

[0] https://www.youtube.com/watch?v=p8u_k2LIZyo


Fast inverse square root for computer graphics programming! [1] Popularized by leaked Quake III code (attributed to John Carmack), but dates back much further.

[1] https://en.wikipedia.org/wiki/Fast_inverse_square_root


The famous "fast inverse square root" was in Quake 3.


finding an approximate root for these purposes can be really quite fast, as was demonstrated in Quake III. https://en.wikipedia.org/wiki/Fast_inverse_square_root

Fast inverse square root as seen in Quake (as mentioned in other comments)

https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overv...


Relevant Quake engine trivia, for those that are interested:

https://en.m.wikipedia.org/wiki/Fast_inverse_square_root


Here’s your fast inverse square root (totally not copied from Quake II Arena). By the way, did you know that I’m a real boy and I’m stuck in a code factory?


As mentioned in the article, it reminds me of the 0x5f3759df fast inverse square root used in Quake III Arena!

https://medium.com/hard-mode/the-legendary-fast-inverse-squa...

This is where I really see CS as being close to mathematics.


It's a shame you picked q3's fast inverse square root. It's not a great example as it turns out someone else implemented it in Quake 3 and someone else entirely devised the algorithm itself. Though you're right - he's a bit of a hero and deservedly so.

edit: however ... I've just read that article and it is actually the most in-depth and interesting analysis I've read on the subject so thanks!


GPT4:

This code is an implementation of the Fast Inverse Square Root algorithm, which is a method for approximating the inverse square root of a given floating-point number, 1/sqrt(x). This method is well-known for its use in the 3D computer graphics of the video game Quake III Arena, developed by id Software in 1999.

Btw: I don’t quite get the need for these frontends. I literally input this to gpt: “ Wxplain this code:” (Yes, with typo)


I love the story of the fast inverse square root. A bizzare piece of code from quake 3 shows up on usenet with a magic constant that calculates the inverse square root faster than table lookups and approximately four times faster than regular floating point division. Inverse square roots are used to compute angles of incidence and reflection for lighting and shading in computer graphics. Author unknown but was once thought as of Carmack.

https://en.m.wikipedia.org/wiki/Fast_inverse_square_root


Wasn't Quake the one where they did a really good approximation of he inverse square root because they needed it?

Hm interesting, I knew it from Quake and implicitly assumed that it was Carmack's trick. But Wikipedia has some more history:

https://en.wikipedia.org/wiki/Fast_inverse_square_root#Histo...


Fast inverse square root[0] is something that I encountered in the mid-2000s in the Q3A source code. It took me a really long time to understand it, and I eventually had to show it to some professors before I really understood what was going on and why this worked.

That's really an example of how arbitrary human thought processes are. When you release the constraint that your code has to have some human-comprehensible analog, you might arrive at interesting results.

[0] https://en.wikipedia.org/wiki/Fast_inverse_square_root

next

Legal | privacy