←back to thread

420 points gnabgib | 5 comments | | HN request time: 0.692s | source
Show context
qingcharles ◴[] No.43999955[source]
I love these incomprehensible magic number optimizations. Every time I see one I wonder how many optimizations like this we missed back in the old days when we were writing all our inner loops in assembly?

Does anyone have a collection of these things?

replies(4): >>43999992 #>>44000134 #>>44000173 #>>44000633 #
masfuerte ◴[] No.43999992[source]
We didn't miss them. In those days they weren't optimizations. Multiplications were really expensive.
replies(6): >>44000045 #>>44001019 #>>44001612 #>>44001634 #>>44001978 #>>44008837 #
1. kurthr ◴[] No.44000045[source]
and divides were worse. (1 cycle add, 10 cycle mult, 60 cycle div)
replies(3): >>44000122 #>>44000234 #>>44000838 #
2. genewitch ◴[] No.44000122[source]
That's fair but mod is division, or no? So realistically the new magic number version would be faster. Assuming there is 32 bit int support. Sorry, this is above my paygrade.
replies(1): >>44001372 #
3. qingcharles ◴[] No.44000234[source]
Yeah, I'm thinking more of ones that remove all the divs from some crazy math functions for graphics rendering and replace them all with bit shifts or boolean ops.
4. ryao ◴[] No.44000838[source]
Division still is worse:

https://github.com/ridiculousfish/libdivide

5. bobmcnamara ◴[] No.44001372[source]
Many compiles will compute div-by-a-constant using the invert, multiply, and shift off the remainder trick. Once you have that, you can do mod-by-a-constant as a derivative and usually still beat 1-bit or 2-bit division.