The radix 2^51 trick
Starting from the “ones” position, we add 6 + 6 = 12, write down a 2 and carry a 1. We proceed to the left, one position at a time, until there are no more digits to add. When implementing addition for large integers (e.g. 264 and above), it’s common to write code that looks quite similar to this algorithm. Interestingly, there’s a straightforward trick that can speed up this process enormously on modern CPUs. But first, a question: why do we start long addition with the “ones”? Why not start on the left? The answer, of course, is the carries. We can’t figure out for sure what a given digit of the answer will be until we’ve completed all of the additions to the right of that digit. Imagine if we tried to add left-to-right instead: 6 + 3 = 9. So the first digit is 9. 8 + 4 = 12. OK, the second digit is 2… but carry a 1, so the first digit was actually 9 + 1 = 10… now carry back that 1… For mental math, this isn’t too bad (and some people actually prefer it when working with small enough
The radix 2^51 trick The radix 2^51 trick Faster addition and subtraction on modern CPUs Do you remember how to do long addition on paper? ¹¹ ¹ 6876 + 3406 ------ 10282 Starting from the “ones” position, we add 6 + 6 = 12, write down a 2 and carry a 1. We proceed to the left, one position at a time, until there are no more digits to add. When implementing addition for large integers (e.g. 2 64 and above), it’s common to write code that looks quite similar to this algorithm. Interestingly, there’s a straightforward trick that can speed up this process enormously on modern CPUs. But first, a que
Explore this link on the map →saved by
related reading
- FatNums: An Alternate Representation for School Arithmeticparentheticallyspeaking.org
- abseil / Performance Hintsabseil.io
- Algorithms for Modern Hardware - Algorithmicaen.algorithmica.org
- The Aggregate Magic Algorithmsaggregate.org
- Binius: highly efficient proofs over binary fieldsvitalik.eth.limo
- Math & Engineeringxn--2-umb.com
- Fastest Possible Processing Speed of the Human Brain – World Mental Calculationworldmentalcalculation.com
- Floating point from scratch: Hard Mode · Tales on the wireessenceia.github.io
- Labor of Division (Episode IV): Algorithm Dridiculousfish.com
- Amdahl's law - Wikipediaen.wikipedia.org
- thesis-FIXED.pdfhjaltman.github.io
- What's new | Updates on my research and expository papers, discussion of open problems, and other maths-related topics. By Terence Taoterrytao.wordpress.com