Hacker News new | ask | show | jobs
by int_19h 1211 days ago
The obvious question this invites: if this is generally faster for unknown values, why don't compilers use this optimization directly in emitted code?
3 comments

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