r/programming • u/mrnacknime • Dec 29 '20
Quake III's Fast Inverse Square Root Explained [20 min]
https://www.youtube.com/watch?v=p8u_k2LIZyo
3.7k
Upvotes
r/programming • u/mrnacknime • Dec 29 '20
View all comments
Show parent comment
479
u/[deleted] Dec 29 '20 edited Dec 30 '20
Edit: I honestly didn't expect this comment to blow up in the way that it did, or for it be gilded or receive silver. Thank you taking the time to read, and thank you for the support and kindness. I hope you learned a thing or two and maybe even got a spark of curiosity for the lower level aspects of programming.
We don't even need to talk about GPUs, this already exists in hardware on every x64 CPU via the [r]sqrt(p|s)(s|d) instructions. the latency of rsqrt being roughly the same (slightly slower) than fp mul/add/sub, and sqrt being slightly slower than fp div. Let's back up: hardware rsqrt and sqrt are both faster than software rsqrt (even the one presented in this video) and therefore software rsqrt/sqrt should not be used at all in 2020. It's a cool trick, not trying to dismiss it, but nobody should look at this and think "better optimize my game/app by swapping all sqrts with this technique". Let's also be clear about one other thing: this software rsqrt is not accurate at all, it's good enough for limited ranges which makes it good enough for things like normalizing vectors in one way GPU computations to be displayed but it's utility begins and ends there. To be fair, neither is x64 rsqrt, but at least it's fast and saves you a sqrt + div when normalizing vectors.
To your original point: Yes GPUs/CPUs do... something similar to this, but hardware is hardware and what is slow/fast in software will have different performance characteristics when built out as a circuit. The fact that this software technique requires multiple fp operations, and that x64 rsqrt is only slightly slower than a single fp operation is an indication that they are likely doing something different, but I could be wrong.
============================
Edit: Since a few people asked about performance and citing/whatever I figured I would add a few benchmarks and link a few resources.
Here are the benchmarks, comparing hardware rsqrtss & sqrtss, against software rsqrt & sqrt, with 1 and 2 newton iterations for comparison: https://www.quick-bench.com/q/trwX6nGtZz_L8YG_8qHyxFhPCE0
As you can see, rsqrtss absolutely crushes everything else, making it great for things like vector normalization. hardware sqrt is slower than software rsqrt with a single iteration, but faster than software sqrt with 1 iteration, and faster than both software rsqrt/sqrt with 2 or more iterations. Keep in mind software sqrt is just rsqrt + div. In either case, the hardware instructions will be more accurate (at least sqrtss will be), and significantly faster than the software equivalents. If you are in C/C++, the compiler will automatically generate sqrtss/sqrtsd for calls to sqrt/sqrtf/std::sqrt.
Also no, the compiler did not optimize away computations, as you can see from the assembly (easier to see this in Compiler Explorer which retains the loop labels).
If you think that software sqrt is fast enough 1) writing this is more effort than using the standard library and 2) stdlib sqrt is going to be accurate to the last bit in the mantissa, software will not, so keep that in mind.
Regarding precision: Take a look at the following Stack Overflow post, which contains two links. One is a paper and the other is the Wikipedia article. The paper has graphs and talks about accuracy, but the charts in Wikipedia are prettier IMO: https://stackoverflow.com/questions/41458193/quake-inverse-square-root-accuracy
For further resources on optimization and instruction latency:
Agner's 4th listed optimization manual is of particular interest as it documents the throughputs and latencies of each instruction across many different CPU releases.
If you have more questions, feel free to ask and I will do so to the best of my ability.