Fast Inverse Square Root — A Quake III Algorithm
Nemean3 HN recommenders · recommended in 3 months, 2021–2022
explainer · intermediate · evergreen · 2021–2022 on HN
A walkthrough of the Quake III fast inverse square root, explaining where the constant 0x5f3759df comes from. It covers IEEE 754 floats, the bit hack that approximates a logarithm, and a Newton's method refinement.
HN recommends it as an approachable explanation that makes the famous algorithm finally make sense.
Up next
3 mo on HNHow to achieve nanometer precision in space?
Breaking Taps
4 HN recommender · 2022–2026
3 mo on HNTWO Unpickable (?) Locks for Lock Picking Lawyer!
Stuff Made Here
3 HN recommender · 2021–2022
4 mo on HNThis is why we can't have nice things
Veritasium
4 HN recommender · 2021–2024
7 mo on HNThe Future of Mathematics?
Microsoft Research
9 HN recommender · 2019–2025
3 mo on HN[1299] Unpickable Locks From Stuff Made Here
LockPickingLawyer
5 HN recommender · 2021–2022
5 mo on HNWhy can't you multiply vectors?
Freya Holmér
9 HN recommender · 2023–2025
5 mo on HNFourier Optics used for Optical Pattern Recognition
Huygens Optics
5 HN recommender · 2021–2025
10 mo on HNRefterm Lecture Part 1 - Philosophies of Optimization
Molly Rocket
10 HN recommender · 2021–2026
10 mo on HNCppCon 2017: Matt Kulukundis “Designing a Fast, Efficient, Cache-friendly Hash Table, Step by Step”
CppCon
9 HN recommender · 2018–2026
8 mo on HNThe Fast Fourier Transform (FFT): Most Ingenious Algorithm Ever?
Reducible
7 HN recommender · 2021–2026
9 mo on HNModern Dictionaries by Raymond Hettinger
SF Python
9 HN recommender · 2016–2025
7 mo on HNCppCon 2014: Chandler Carruth "Efficiency with Algorithms, Performance with Data Structures"
CppCon
7 HN recommender · 2014–2024
4 comments from Hacker News
Very interesting that this was posted as I literally JUST watched an even MORE interesting youtube upload about this very bit of code just last weekend. Here's the very fun video if anyone wants to take a look: ▶ this video
Apparently the bit pattern of a float interpreted as an int is sorta-kinda it's own logarithm, which is why the famous fast inverse square root from quake 3 works: log(x^-½) = -½log(x) =~ -log(x) >> 1. This video is approachable and explains it pretty well: ▶ this video
> The bits of a float as an integer literal are proportional to its log2 value (plus a constant), … I watched a video[0] a while back about that. Once that coincidence(?) was explained, Q_rsqrt finally made sense. [0]: ▶ this video (~20 minutes)
print("Hello World") /s But I've heard good things about the Fast Inverse Square Root Calculation algorithm in the Doom 3 source code. ▶ this video