A new faster algorithm to compute 64-bit signed floor division
Posted by benjoffe 2 days ago
Comments
Comment by derf_ 10 hours ago
1. The divisor is not actually a compile-time constant, but comes from a small set. You can compute a table of corresponding constants and use the same code for every divisor.
2. You want to do many divisions by many different divisors (whether known at compile-time or drawn from a small set, like above, or pre-computable and applied many times... think of something like a quantization matrix for image coding, for example). Here Robison easily SIMDifies, whereas an algorithm that needs different code paths for every divisor is going to be much more difficult.
Regarding the lack of native signed-unsigned multiplies, as someone who does a lot of multimedia processing, I find the set of available multiplication instructions on x86 and ARM to be rather shamefully limited (especially x86). I recall a colleague who used to optimize for VideoCore (the GPU in a Raspberry Pi) saying that when he first started he was astounded that it had 80 different multiply instructions, but within a year or two he had used basically all of them.
Comment by bugake 5 hours ago
I'm kind of sad that all those algorithms have become obsolete, but at the same time, I'm happy that integer division has finally become fast enough in hardware.
Comment by nkurz 4 hours ago
If you've come up with something better, I'd encourage you to contact Daniel and see if he's interested in helping you write it up into a paper. He's friendly, and may be better positioned to get the optimizations into mainstream compilers.
Comment by sjrd 4 hours ago
I can't help but do a shameless plug for another recent result in that space: we developed fast algorithms for 64-bit integer divisions (by constants and non-constants) for JavaScript, where you don't even have 64-bit integers to start with. Paper [1] and slides [2] available. The Scala.js compiler uses all of that. The Kotlin/JS compiler recently accepted my PR for the non-constant divisors algorithms.
[1] https://arith2026.org/papers/64-bit%20Integer%20Division%20f...
Comment by dahart 3 hours ago
If the goal is wide vector divides, what about GPU? How does this algorithm compare on GPU? Are GPU uses less likely to rely on and benefit from throughput behavior if the vector of divides is spread across all threads?
Comment by rf15 11 hours ago
I'm one of those people I suppose. Yes, if you want to play with microoptimisations and part of a calculation are known, you can flip a division into a mult (and shift).
This might seem somewhat useless at first, but you can use this to e.g. speed up calculations in nested loops, where you can precompute this for relevant loop-layers.
Comment by benjoffe 10 hours ago
The `new` thing is the range-splitting which achieves the floor division for negative inputs (eg. sign-selected for 60% of divisors, and the negative/positive bump for the remaining 20%/20% of cases each).
Comment by rf15 9 hours ago
Comment by Sharlin 5 hours ago
Comment by yje07039 9 hours ago