r/programming • u/DataBaeBee • Sep 14 '26
How GCC Eliminates Unnecessary Integer Division
https://leetarxiv.substack.com/p/how-gcc-eliminates-unnecessary-integer
249
Upvotes
r/programming • u/DataBaeBee • Sep 14 '26
View all comments
Show parent comment
40
u/Jhavul Sep 14 '26 edited Sep 15 '26
yeah, when division is 80 cycles and multiplications are 1, you get insane performance gains even if it takes 20 multiplications to make the magic numbers
e: sorry apparently I should specify, 80 cycles idiv is for hardware >4 years old, it's closer to 20 cycles on modern hardware, and multiplication can take up to 3 cycles. so in the very worst case, it's a 3x speedup if you can swap one idiv for two imul, if you never do sequential operations, and have the newest CPU possible. it gets way better in every real life scenario.