Re:Bitwise Division
by int_19h on Wednesday March 01, 2023 @04:04PM
(#34988317)
The obvious question this invites: if this is generally faster for unknown values, why don't compilers use this optimization directly in emitted code?
[ Parent
| on HN ]
> Compilers usually do this, but only when the divisor is known at compile time
if the divisor is in a variable or otherwise 'hidden', the compiler can't deduce enought to get to it, is my guess.
Calculating the divisor values is also expensive, this works when you do this work once and then do the efficient divides multiple times.
They could, but it's less profitable, and cost models are, as always, a big problem. If we have:
loop for i from 0 to n
f(a[i]/y)
How big does n have to be before it makes sense to compute a reciprocal for y? And how frequently is it actually that big during the runtime of the code?