On Sun, Jan 16, 2022 at 4:11 PM Terry Reedy <tjreedy@udel.edu> wrote:
https://stackoverflow.com/questions/41183935/why-does-gcc-use-multiplication...
and
https://stackoverflow.com/questions/30790184/perform-integer-division-using-...
have multiple discussions of the technique for machine division invariant (small) ints and GCC's use thereof (only suppressed with -0s?).
Yes, it's an old and well-known technique, and compilers have been using it for division by a known-at-compile-time constant for many decades. What's surprising here is the use by GCC in a situation where the divisor is *not* known at compile time - that GCC essentially guesses that a divisor of 10 is common enough to justify special-casing. There's also the libdivide library[1], which caters to situations where you have a divisor not known at compile time but you know you're going to be using it often enough to compensate for the cost of computing the magic multiplier dynamically at run time. [1] https://libdivide.com -- Mark