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

Show parent comment

4

u/FrogNoPants Aug 11 '26 edited Aug 11 '26

If I'm working on SIMD data already I don't need any load/stores, and Marcs code doesn't appear to be doing any widening/narrowing, though if that is required for > 24 bit integers, it still would be worth it

So for 32 bit integers it looks it is: 2x convert to float, float div, trunc integer.. so 4 instructions.. they are all fairly slow instructions, but it will still wipe the floor with a scalar variant. If you have to widen/narrow, that will double the cost more or less.

Your cycle counts look way off, and most CPUs do not do integer division in 6 cycles(Zen2 == 30 cycles)

To take a AVX512 register, load each lane individually into a scalar integer register, divide, then put it back into its appropriate SIMD lane, is a shitload of instructions, easily 50+, and 16 of those are slow ass divisions.

10

u/tanner-gooding Aug 11 '26

You still require loads/stores, the key is you don't need additional loads/stores. However, there is then higher latency loading into SIMD registers, even just for scalar floats, as compared to the same data loaded into general-purpose registers. This is generally offset a bit from handling multiple elements at once, but it isn't free. Crossing cache lines or other considerations then add additional costs on top which tends to be a unique consideration for SIMD given standard languages.

Conversion of int to float then isn't safe, it is lossy for integers over 24-bits, so for arbitrary inputs you must widen to long and then convert to double instead, which is higher cost. If you know its in range then you can save some perf, but then there's also much better alternatives available that can save cycles as well.

I then didn't say CPUs do integer division in 6 cycles, I said that's the reciprocal throughput. That is, the amount of time an instruction has to execute before you can dispatch an instruction of the same categorization. Instructions like add typically have 0.25, meaning you can dispatch 4 of them in the same cycle. div on the latest hardware with AVX-512 support (i.e. the optimal scenario I described) has a reciprocal throughput of 6 cycles and a latency of 9-14 cycles, where 14 is the worst case for a full 64/32 and closer to 9 is what you get for 32/32 cases (i.e. the scenario I was detailing). So, while the raw latency of 8 divisions is around 9*8 == 72 cycles; due to reciprocal throughput and pipelining you can get it in about (8*6) + 3 or 51 cycles.

I then provided numbers in the very first post detailing how this has evolved over time and massively reduced to the point where it is, particularly for scalar values, faster to do plain integer division even if you know both sides are in range.

It's then not 30 cycles for zen2; but 8..25 for 32/32 and 64/32, with 8 being the more optimal case of 32/32 with fewer dividend bits. The typical reciprocal throughput is then 14. This is slower and can be impactful for truly arbitrary division, but in that case you're often adjusting the algorithm to account for this.

You have many alternative tricks that are applicable, especially if divisor is constant or the same for all dividends; which is the most typical case and then is significantly faster than doing conversion to floating-point.

If you're on Zen4, instead of Zen5, then you also have to deal with it being double pumped itself. You have to contend that there is at best 2 ALU ports available for floating-point division and its shared with the conversion and many other instructions. You have to contend with the longer dependency chains, more instructions impacting the icache, and many other scenarios.

This is not a "its a free or easy win" scenario, which is why compilers tend to not do this type of thing, not even LLVM does it extensively and it has a broad range of optimizations. It is a very scenario dependent case where utilizing it for scalars is almost certainly net neutral or a regression and for SIMD, it comes with added nuance, risk, and semantic changes that can make it undesirable even when it is a win but where that win isn't an absolute, it is dependent on the surrounding code in question and what the algorithm is doing; where there is often a far better alternative that compilers will optimize to instead.