r/programming • • 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

34 comments sorted by

View all comments

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.

15

u/-Redstoneboi- Aug 11 '26

compilers automatically do this optimization i think

19

u/Dragdu Aug 11 '26

Only if the divisor is real constant, but not if divisor is constant adjacent (think function with the signature of divide_all_by(std::span<uint> in, uint divisor)), they won't and you have to use libdivide.

2

u/[deleted] Aug 12 '26

[removed] — view removed comment

3

u/Dragdu Aug 12 '26

I didn't say can't, I said won't.

1

u/-Redstoneboi- Aug 12 '26

depends if it's inlined or not

if it's a constant and happens to get inlined, it'll probably be optimized

if it comes from user input, probably not

1

u/Dwedit Aug 11 '26

Yep, I even mentioned that in my other post.

1

u/Determinant Aug 12 '26

They don't do this for "constants" that are not hard coded

1

u/mikeblas Aug 11 '26

How would that work?

27

u/taw Aug 11 '26

ELI5 version.

Computers are really fast at two things:

  • multiplying two numbers, giving you number with twice as many digits
  • dropping last N digits

Now this trick isn't great in decimals, but we can sort of make it work.

If you want to calculate x/3, that's same as x*0.333333..., which is then the same as x * 333.333... / 1000.

So since we can multiply fast, do x * 334 (rounding that 333.333... up), getting a 6 digit number, then drop the last three digits, which is also super fast.

For this decimal example, it only works for every x=0 to 499, and for other divisors you also don't get perfect range.

But it works even better with binary 32bit x 32bit to 64bit.

1

u/jsdodgers Aug 12 '26

is it faster to calculate 1/3 than x/3?

2

u/taw Aug 12 '26

The key part was "If your divisor doesn't change", so you only calculate this once ever, not for every number.

None of it works if you only divide once.

11

u/Dwedit Aug 11 '26 edited Aug 11 '26

Example of dividing by 13:

0x100000000 / 13 = 0x13B13B13

Add 1 to the number to prevent truncation errors: 0x13B13B14

Let's try 160485 / 13 by using multiplication:

160485 * 0x13B13B14 = 0x30390000C0E4

Discard low 32 bits:

0x3039 = 12345


And compilers have done this for a long time, when you divide by a constant, it will generate reciprocal multiplication code instead.

3

u/vytah Aug 13 '26

A minor nitpick: to actually prevent truncation errors, you need to prepare a little bigger constant than that.

For example, (0x4000'0000*0x13B1'3B14)>>32 is not 0x4000'0000/13, it's 0x4000'0000/13+1.

The compilers will use (x*0x4EC4'EC4F)>>34 instead (0x4EC4'EC4F being 0x4'0000'0000/13+1)

1

u/mikeblas Aug 11 '26

Ah, I see now. Thanks!

3

u/Madsy9 Aug 11 '26

a/b = a*(1/b). If b is constant, just compute its reciprocal. If 1/b is a fraction, do the multiplication in fixedpoint.