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
11
u/tanner-gooding Aug 11 '26
I'm definitely not forgetting, it's just that this is not an optimization in most cases (most such cases aren't vectorizable in the first place, especially not safely) and even when you get into vectorization it is extremely tricky.
The best case scenario ends up being:
load 8x int(dividend, divisor - memory load)widen on AVX512 hardware to 8x long(2 + 2 cycles)convert to double(3 + 3 cycles)divide(13 cycles)convert to long(3 cycles)narrow to 8x int(4 cycles)store 8x int(quotient - memory store)So you're looking at 30 cycles here in the absolute best case (+ the load/store costs), the loss of atomicity guarantees, the additional loop complexity costs, etc
This is in contrast to what is, on paper, around 51 cycles for the scalar sequence (accounting for the 6 cycle reciprocal throughput and knowledge that it's 32/32 so an around 9-10 cycle retirement cost).
However, it misses the additional penalties from crossing domains, likely crossing cache lines, that it slows down certain loop operations, and has other SIMD unique penalties and considerations, etc. So it really ends up depending on what exactly the algorithm is doing on whether it will be faster, slower, or a wash.
It's certainly not something you want to be generally doing, especially without explicit measurements and considerations for the memory model and guarantees required. It may be something you want in specific contexts, if you have know the inputs and outputs are bounded, aligned, and exclusively single threaded.
-- Notably I'm the owner/maintainer for SIMD in .NET, including the integer division vectorization support, so this is an area I'm intimately familiar with.