5 ms·
John Carmack's Fast Inverse Square Root: https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overview_of_the_code https://en.wikipedia.org/wiki/Fast_inverse_
by cepp 7y ago
John Carmack's Fast Inverse Square Root: https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overview_of_the_code https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overv.... The first time I really and truly felt that people approach problems differently from how I, by default, go about them.
- vatys 7y agoThis is still one of my favorites. In a list of magic numbers, sorted by magic-ness, 0x5F3759DF would sit pretty high.
- jmcgough 7y agoIIRC Carmack didn't write that code, as brilliant as he is.
- dkersten 7y agoIt even says that he didn’t write it in that wikipedia page.
- deleted 7y ago[deleted]
- Const-me 7y agoCPUs have much faster (2-5 cycles latency) and much more precise (<0.04% relative error) version in hardware, since 1999: https://www.felixcloutier.com/x86/rsqrtps https://www.felixcloutier.com/x86/rsqrtps
- hwillis 7y agoI wonder which one used less energy in 1999?
- chii 7y agoit's the antithesis of maintainable code. But given that one is unlikely to want to change from calculating the inverse square root to inverse cube root or any other variation....
- enriquto 7y agoI disagree strongly. This code is very maintainable: it does not have dependencies, it is trivial to test, it is wickedly short, and with the appropriate comment there is no confusion regarding its purpose. Also, its field of applicability is clear from the context: replace this code with a call to fsqrt if you happen to have a fast hardware implementation of it. It is the most easily maintainable code, ever!
- fteem 7y agoBy "the appropriate comment" you mean, and I quote: "// what the fuck? "
- chii 7y agothe one maintainability metric, and i think is the most important one, is whether it's easy to modify (to make it do something slightly different). You'd be hard pressed to write an inverse square cube without basically rewriting the whole function. There's nothing that can be reused. The only saving grace is that it is side-effect free, so replacement is trivial, unlike a lot of other code that's not maintainable.
- quickthrower2 7y agoThe question is how likely are you to need it to do something different, and what are the different things it might need to do? This will depend on the domain, but making things too flexible hampers maintainability too.
- wruza 7y ago>inverse square cube You mean inverse cube root? I believe the geometry ain’t easily upgradeable this way.
- munchbunny 7y agoYour metric is only applicable within the context of the problem domain. In 3-d graphics, the inverse square root (1 divided by the square root of x) is an extremely common operation (such as for normalizing vectors), but the inverse cube root pretty much isn't used, so it's safe to assume you will not need to extend it to arbitrary powers. As the designer of the code, you would understand that the inverse square root is a standalone problem.
- lbj 7y agoI came here to post this. Remember back in the day when it was wildly discussed. Still impressive to this day.
- quaquaqua1 7y agoAnd it also made the guys a ton of money :) They found a way to take something useful in theory and other practical applications and use it to make something never before seen for the massmarket. That one magic number alone is responsible for a good portion of his net worth and of course his Ferrari
- fxleach 7y agoThe most beautiful piece of code you've ever read contains a magic number with the comments "evil floating point bit level hacking" and "what the fuck?"... you're a madman.
- Erwin 7y agoIf you enjoy this sort of thing, this book is full of bit twiddling algorithms you never thought possible: https://www.amazon.com/Hackers-Delight-2nd-Henry-Warren/dp/0321842685 https://www.amazon.com/Hackers-Delight-2nd-Henry-Warren/dp/0...
- QuincyJazz 7y agoHow do I get a job in /this/ area? I couldn't care much to do JavaScript programming, but this is awesome.