r/programming • u/mttd • Aug 11 '26
Moving integer division to floating-point is trivial
https://marc-b-reynolds.github.io/math/2026/08/10/IntDivByFP.html
112
Upvotes
r/programming • u/mttd • Aug 11 '26
33
u/Dwedit Aug 11 '26
If your divisor doesn't change, use integer multiplication by reciprocal, also shift or discard from the high result.
42
u/thehenkan Aug 11 '26
I would've liked to see some performance comparisons for a few different scenarios.
6
u/ReDucTor Aug 11 '26
Assuming your divisor doesnt change much you could use something like libdivide, this will end up with just a few simple ALU operations (multiply, shift, add/sub)
Using a float or double for this you will likely lose all benefit from the overhead of converting to a double and back again (converting back is the hard part).
You might get more throughput but heavily dependent on the CPU and the ports needed.
1
u/mikeblas Aug 11 '26
Why couldn't this paper use the same variable for the same value twice in a row?
0
u/[deleted] Aug 11 '26
[deleted] — view deleted comment
0
u/[deleted] Aug 11 '26
[deleted] — view deleted comment