JACOB ROBINSONOpen.
← Blog ArchiveARTICLE 249 / 265

Blog Archive / JACOB ROBINSON

Why the Fast Inverse Square Root Matters

The Fast Inverse Square Root algorithm is among the most famous algorithms in computer science history. In this post, I want to go over what it is and why its so important in words anyone can understand.

The Background

The late 90s and early 2000s saw the world move into 3D computer graphics at a scale never before seen. This effected industries ranging from architecture, mechanical engineering, physics, and — you guessed it — video games.

3D computer graphics explains the world in terms of vectors. A vector is a point in 3d space that has a given direction and magnitude. For example, if I was trying to explain the vector of a wind current, I might describe its source (say a fan on the table), the direction its blowing, and its wind speed. The location of the source would be its point in xyz coordinates — if it were in the middle of my 6 foot room on a table 2 feet high (please stay with me I don’t know anything about lengths) we might say it is located at (3,3,2).

In general, when we’re simulating actions in a 3D computer space, there are two types of calculations we can make: direction calculations and distance calculations. Distance calculations are pretty simple — if I wanted to find the magnitude, I could do it by taking the square root of x^2 + y^2 + z^2 and working from there. But if I want to do a direction calculation, that means I need to normalize magnitude in order to solve for angle. In other words, I need divide x, y, and z by the magnitude… which just happens to be a square root.

As to why dividing by a square root is so hard for a computer to do… its complicated. Square rooting by itself is rather trivial, and in fact we’ll see how it works when we get to the FISR algorithm proper. Inverse square rooting, on the other hand, requires a lot of background calculations to do properly and thus takes up a lot of compute time and power. Considering that 3D engines were performing these calculations not once, but millions of times a second… it led to major slow down. The progress in the 3D industry had become bricked, and the choice was between waiting for better hardware to come out to perform these calculations faster, or to rebuild the calculations from the ground up.

Id Software chose the latter.

The Algorithm

Id Software is a company that represents what this blog — and most of my career — is all about: the intersection between art and technology.

Id was, first and foremost, a game company. It knew how to make games fun — titles like Doom and Quake still have thousands of players and get regular reboots/releases every year. This is something that is purely built into the design of these games: in other words, the art.

Yet at the same time, Id Software has a prestigious history of technological advances. Id’s cofounder John Carmack is considered by many to be one of the greatest programmers of all time, let alone within the gaming space. And while I (don’t believe) Carmack made FISR, it stands that the culture he created at Id incentivized making breakthrough technological innovations, of which FISR is no exception.

The genius of FISR is in its simplicity. I call this an algorithm, but in reality it is barely one — really its just a formula that occurs at the bit level.

We of course know that the problem with existing inverse square root calculations is the amount of time it takes a typical programming language to do the necessary prerequisite functions. So, theoretically — if we could limit that process to one or two simple functions — it could speed things up dramatically.

Let’s start with just a normal square root. You might be surprised by how fast this is given the issues with an inverse square, but this should go away once you look at how a number is calculated on most computers. Most of them, in bit form, have the following structure:

{sign}{exponent}{mantissa}

A sign is pretty obvious — it is either positive, or negative. As for the exponent and mantissa, it may be important to mention that all processes on a computer are calculated as a power of 2 (since binary is either 1 or 0, two choices, and we are calculating combinations of these numbers, thus the exponent). So an exponent gets us most of the way to the number, and the mantissa (fancy word for saying decimals) provide us the exact float, up to a certain limit. It might help you to see a real number calculated in binary to signify what this segmentation looks like:

{0}{10000011}{00000000000000000000000}

This is “16.0” in binary, segmented into our float structure. 0 says its positive (1 is negative), 10000011 happens to translate into 4 (so 2^4)[1] and the mantissa section is all zeroes to say that we have no decimal places we are using.

Now, back to square rooting. Since we have a dedicated exponent section, you can now see why it’s so easy to square root. All we’d have to do is divide the exponent section by two, which can be done by shifting the entire number one bit to the right (which happens to halve the exponent section of the bit). But the inverse square root requires division — more specifically, it requires floating point division. The problem with floating point division on computers is that performs a lot of back-end iterations to make sure that the answer is exactly right. But if we’re performing vector normalization, we don’t always have to be exact — we can be good enough, and choose to iterate if needed. Unfortunately, the built-in math of computers doesn’t give us that choice.

This is where we need to rebuild the calculation.

Now, theoretically, if we know how a float is calculated in bit-terms, we could jerry-rig a solution that always changes the correct segments of the float to simulate an inverse square root. Finding this magical bit which simulates an ISR was no doubt the source of much pain for many an Id software engineer, and probably took them months to figure out. I won’t go in depth here, since I think you have the important details of this down and don’t want to get too in the weeds for a single article. Instead, we’ll skip the blood sweat and tears and go right to the ending:

0x5f3759df. It’s 0x5f3759df.

If you subtract 0x5f3759df off of a bit-shifted right number, it will give you an inverse square root. And that inverse square root is good enough for graphics programming. If you choose, you can run a Newton-Raphson iterator on it once or twice to get more exact (funnily enough this is the same iterator that happened way too often on floating point division and caused the slow down) but we have succeeded in now finding the FISR algorithm!

The Legacy

You might wonder if FISR is now the de-facto standard for how computers compute inverse square roots. The truth is, it never needed to be — about a year later we got CPUs good enough to do floating point division quickly and accurately. Some might look at this as a tragic result for FISR as it essentially became a waste of time, but this is not so. The graphical fidelity of Quake III Arena made it leagues better than its peers, and that combined with its fast-paced addictive gameplay (gameplay that would’ve not been nearly as fast if FISR wasn’t used) led it to sell gangbusters and become a staple of gaming history. Had Id simply waited a year, they would’ve been on the same level as their competitors and therefore might have released Quake III to an already saturated market. Things moved fast back then!

But FISR also found a home somewhere else: Computer Science courses. Virtually every Computer Science degree teaches FISR at some point, as an example of low-level code being used to improve the speed of processing. And while FISR specifically might not be used, similar hacks are occurring each and every day at large corporations. For example, the fundamentals of FISR are how companies like Deepseek were able to reduce the cost of AI training on GPUs versus competitors like OpenAI or Meta. And this was just last week! Because of this I hope to return to low-level programming someday, but its most famous algorithm is a great place to start.

[Image omitted]

[1] – Some more astute of you might notice this does not actually translate to 4 in raw binary. It has to do with the IEEE standardization for floats, which is outside the scope of this article — but you can look it up if you wish.

Select text in the article to highlight it.

Your saved highlights

    Article contents

    Blog Archive · 265 articles

    1. 01 Five Takeaways from #100DaysofCode
    2. 02 Getting Past the Bump
    3. 03 How Important are Principles?
    4. 04 How to Avoid Writer’s Fatigue
    5. 05 The Dangers of Catastrophizing
    6. 06 State of the Union 2019
    7. 07 The Rise of the Personal Brand
    8. 08 Finding Adventure in 2019
    9. 09 5 Ways to Use Automation to Vastly Increase Your Efficiency
    10. 10 What you Should be Doing Instead of Networking
    11. 11 Creatives vs. Technicals: Which Should You Focus On?
    12. 12 The Difference between Quitting and Giving Up
    13. 13 3 Ways To Get Value Out Of Nature
    14. 14 What is the Purpose of Dreams?
    15. 15 How to develop intellectual success in future generations
    16. 16 What the Definition of Luck Really Is
    17. 17 Who Makes the Culture?
    18. 18 Sexual Frustration and Trying to Find Love in the Age of the Internet
    19. 19 The Lazy Man’s Guide to More Efficient Research
    20. 20 How One Joke Can Ruin Your Life
    21. 21 Why IQ is BS
    22. 22 Why it’s Impossible for the Illuminati to Exist
    23. 23 Some of my Favorite Blogs
    24. 24 The Most Important Indicator on Why a Company Succeeds
    25. 25 Returning to Tesla
    26. 26 Living in a Post-Truth World
    27. 27 The Function of Success
    28. 28 Lessons at 20
    29. 29 If the Recession Happened This Year…
    30. 30 Why People Don’t Read
    31. 31 Social Media and Truth
    32. 32 Making Things 1% Better
    33. 33 What the Book of Job Really Means
    34. 34 Life as a Game
    35. 35 The Function of Opportunity
    36. 36 Exponential Growth
    37. 37 The Machine Learning Revolution
    38. 38 Practical Thoughts on Blockchain
    39. 39 Why the Humanities Are Just as Important as STEM
    40. 40 Dow 30,000
    41. 41 The Shame Economy
    42. 42 You Can’t Do It All Alone
    43. 43 Favorites of 2019
    44. 44 State of the Union 2020
    45. 45 The Tolerance Paradox
    46. 46 Null
    47. 47 Social Game Theory
    48. 48 3 Tips for Developing a Positive Mindset
    49. 49 Why Social Gaming is So Popular
    50. 50 A Design Study on YourStack
    51. 51 Relationships, Careers, and Investments
    52. 52 The Fall of GAFA
    53. 53 Wartime CEO
    54. 54 Mass Shooting Cycles
    55. 55 But Who’s Right?
    56. 56 What Matters in the End
    57. 57 Is AR/VR too “weird”?
    58. 58 Convenience + Efficiency
    59. 59 Vicious Cycles
    60. 60 With Smart People
    61. 61 The Third Moat
    62. 62 The Secret to Success
    63. 63 The End of Education
    64. 64 The Ben Carson Effect
    65. 65 Biological Evolution, Cultural Evolution
    66. 66 Suicide and the Self
    67. 67 The Life Cycle of a Business
    68. 68 How to Get into Harvard
    69. 69 Managing Your Personal KPIs
    70. 70 Fog of War
    71. 71 Make-or-Break vs. Nice-to-Have
    72. 72 Identity and Political Groups
    73. 73 Nationalism’s Pros and Cons
    74. 74 How to Win an Election
    75. 75 Why You Shouldn’t Network
    76. 76 On Relative Morality
    77. 77 Death in the Age of the Internet
    78. 78 Audience of One
    79. 79 The Rise of Anxiety
    80. 80 Stop Getting Scared By AI
    81. 81 If You Build It, They Will Come
    82. 82 Building Knowledge Advantage
    83. 83 The Quest to be Kinder
    84. 84 Everything You Do Sucks
    85. 85 Niche Monopolies
    86. 86 Up Shit Creek
    87. 87 Emotional Equivalent Salary
    88. 88 Uncertainty Principle
    89. 89 Building a Startup with Anxiety
    90. 90 Punishment and Forgiveness
    91. 91 Favorites of 2020
    92. 92 State of the Union 2021
    93. 93 Random Factor Social Suicide
    94. 94 Old World Business, New World Business
    95. 95 The Customer Takes Care of Itself
    96. 96 How to Use Reddit as a Business
    97. 97 Genetic Obsolescence
    98. 98 A Social Capital Theory of Management
    99. 99 If I Could Do My Education Over Again
    100. 100 Aggregation and Decoupling
    101. 101 The Crazies, Part II
    102. 102 Art as Inspiration, Art as Education
    103. 103 Free to Play Models for SaaS Services
    104. 104 A Recap on my 2020 Politics Posts
    105. 105 Making Mistakes, Post-Internet
    106. 106 Taste is Not Tech
    107. 107 Immortality
    108. 108 Biases Against Consistency
    109. 109 The Mythical Self
    110. 110 Faulty Mental Models
    111. 111 Honor and Glory
    112. 112 Momentum, and the Value of Cold Showers
    113. 113 The Prestige Myth: Providing Structure to Structureless Forms
    114. 114 The Unluckiest Man Alive
    115. 115 Those Who Make, And Those Who Work
    116. 116 Post-Truth, Revisited
    117. 117 Conscience Questions, Unchristianized Edition
    118. 118 On Being You
    119. 119 Effective versus Efficient
    120. 120 The Experimental Method of Learning
    121. 121 A New Model for Teaching Humanities
    122. 122 A Dialogue on Mindset
    123. 123 I Am Not Your Friend
    124. 124 My 5 Tips Towards Reading Books
    125. 125 Buying Immortality
    126. 126 Do this, but…
    127. 127 Leverage and Wealth: A Simplification
    128. 128 Mind’s Eye and Creative Application
    129. 129 Intersection of Technology and Art
    130. 130 Blog Writing for Robots
    131. 131 10 Tips on Writing
    132. 132 The Cost of Staying Alive in America
    133. 133 Taste on the Margin
    134. 134 Top 10 Tools I Use in 2021
    135. 135 Income Generators 101
    136. 136 Strategies for David, Versus Goliath
    137. 137 Progress in the Long Term
    138. 138 The End of Journalism
    139. 139 Myths of People
    140. 140 Heroism
    141. 141 External Mentors
    142. 142 Favorites of 2021
    143. 143 State of the Union 2022
    144. 144 The End of Gender
    145. 145 The Work After Work
    146. 146 There Is No Metaverse
    147. 147 On Dealing With Chaos
    148. 148 Risk/Return Bets in Entrepreneurship
    149. 149 The Weird, Magical World of Modern Day Quora
    150. 150 On Business
    151. 151 What’s “Hivemind”?
    152. 152 Life as a War
    153. 153 On Culture
    154. 154 My Approach to Investing
    155. 155 Stop Caring About Your Reputation
    156. 156 On Humility
    157. 157 Is Relative Morality Helpful?
    158. 158 Achievement-Based Thinking
    159. 159 On Success
    160. 160 Moral Implications in Maximums and Minimums
    161. 161 Sentience and Suffering
    162. 162 On Improving
    163. 163 The Suicide Disease
    164. 164 Social Diversification
    165. 165 On Meaning
    166. 166 Convenience and Pareto Optimality
    167. 167 Mental Illness is Not a Permanent State
    168. 168 Biased Science
    169. 169 People as Means
    170. 170 Being Wise and Being Smart
    171. 171 The New Working Class
    172. 172 Fitness, Suffering, Self
    173. 173 Political Theory and Evolution
    174. 174 Explaining my Rating System
    175. 175 Why You Might Want A Trading Account Over An IRA
    176. 176 Why NFTs Don’t Work For Games
    177. 177 A Few Thoughts on MMOs
    178. 178 A Review of MyMind
    179. 179 Read, Think, Make, Share, Monetize
    180. 180 Is Science Perfect?
    181. 181 A Guide to Fiction Writing
    182. 182 The Productivity Tool World Tour 2022
    183. 183 My Daily Routine
    184. 184 The Curse of The Conqueror
    185. 185 Some Brief Notes on Programming
    186. 186 My Life Principles
    187. 187 Cyberpunk in 2022
    188. 188 What Decides Good Art?
    189. 189 Trends in Mass Murder Techniques
    190. 190 Changing to Fit the Times
    191. 191 Why Did Photographers Embrace NFTs, but Artists Didn’t?
    192. 192 Favorites of 2022
    193. 193 State of the Union 2023
    194. 194 New Blog Post Schedule
    195. 195 Was Lex Fridman’s “Reading List” Controversy Caused by Mass Psychosis?
    196. 196 Is Notion “Too Good”?
    197. 197 Mastodon is Social Media PvP with Permadeath Enabled
    198. 198 Arc, Vivaldi, and the Next Generation of Browsers
    199. 199 Is the Four-Hour Workweek Actually Possible?
    200. 200 Do Not Obsess Over Your Customer. Obsess Over Your Community.
    201. 201 Why Big Entertainment Failed to Bring in Diversity
    202. 202 My All-Time Favorite Tools
    203. 203 Museums on the Computer
    204. 204 How to use AI to get you motivated to work
    205. 205 The Best Way to Focus: Do Nothing
    206. 206 Can Buddhism and Ego Intertwine?
    207. 207 How I Write My Blog Posts Now
    208. 208 Practice and Mysticism
    209. 209 Automate the Boring Stuff with ChatGPT
    210. 210 State of the Union 2024
    211. 211 The Secret to Great Products You Aren’t Doing
    212. 212 How to build a $5 billion dollar business in 5 minutes
    213. 213 How To Easily Automate Investments, Beat Big Tech, and more!
    214. 214 Can I Use AI To Solve The World’s Unsolvable Problems?
    215. 215 How A “Taste Matrix” Can Improve Your Art And Kill Writer’s Block
    216. 216 My Bizarre, Counterintuitive, and Lifechanging Approach to Work
    217. 217 Top 3 Recommended Browsers (They Aren’t What You Think)
    218. 218 How Hard Is It To Make Money On Steam?
    219. 219 What To Do If You’re Socially Anxious
    220. 220 My New New Way of Reading Books (I Abandoned AI)
    221. 221 References Are The Best Way To Learn Anything
    222. 222 How To Quit Planning Goals And Start Achieving Them
    223. 223 The Only Good Self-Help Book
    224. 224 Be Nice To Everyone. It Just Might Save Your Life.
    225. 225 I Learned An Ancient 3-Step Process And Became A Master Meditator
    226. 226 How Canva Became The Home of Everything
    227. 227 Has The Internet Made More People Mentally Ill?
    228. 228 The Biology Behind Why We Like Stories
    229. 229 This Tape Haunted An Entire Nation. But Was It Even Real?
    230. 230 We Are All Most Certainly Going To Make It
    231. 231 What Makes Good Taste?
    232. 232 In a World of Cancel Culture, Reputation Doesn’t Matter Anymore
    233. 233 Why You Hate Diversity In Media
    234. 234 I Used Readwise for 200 days. Here’s what happened.
    235. 235 Lessons on Bootstrapping Solo with Little Income
    236. 236 This Ancient Story Was Drowned In Lies. The Truth is Much More Interesting.
    237. 237 How To Get Actionable, Memorable Advice Out Of Anything You Read
    238. 238 You Are A Coward. And That’s Not So Bad, Really.
    239. 239 Time Travel is Real. Here’s How to Use It.
    240. 240 How I Got Into Old Games
    241. 241 Is Every Story Political?
    242. 242 Your Culture Decides Your Fate
    243. 243 In Defense of the Jumpscare
    244. 244 Final Fantasy XIV’s Story Is A Mess
    245. 245 Markov Chains and You!
    246. 246 I used Obsidian for 90 days. Here’s what happened.
    247. 247 Ideas That Can Kill You
    248. 248 Porting A 250k+ Item Database from Notion to Obsidian
    249. 249 Why the Fast Inverse Square Root Matters
    250. 250 A Deep Dive on Networking
    251. 251 A Deep Dive on Cybersecurity
    252. 252 Converting a $100k+ Trading Algorithm from Excel to Python
    253. 253 How To Make A (Super) Hard Drive From Scratch
    254. 254 So, How Do Computer Graphics Work?
    255. 255 The Quantum Physics Rabbit Hole, Part I
    256. 256 The Quantum Physics Rabbit Hole, Part II
    257. 257 The Story (and Meaning) of Humanity’s Greatest Achievement
    258. 258 Explaining Tonal Indicators
    259. 259 AI should have you thinking MORE, not less
    260. 260 Forbidden Knowledge: The Vela Incident
    261. 261 All success in life comes back to pain tolerance
    262. 262 The AI Revolution rewards the curious
    263. 263 Forbidden Knowledge: Marco Evaristti
    264. 264 Forbidden Knowledge: Casket Failure
    265. 265 Forbidden Knowledge: Coriander Theory

    Share

    Make it your own before sharing.

    Page link

    Send to

    EmailWhatsAppBlueskyFacebookX

    Facebook shares the link only; add your message there.