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
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.