RFR: 8222029: Optimize Math.floorMod

Claes Redestad claes.redestad at oracle.com
Fri Apr 5 13:44:26 UTC 2019


Hi,

currently Math.floorMod is implemented as: return x - floorDiv(x, y) * y;

This can be optimized as:

         int mod = x % y;
         // if the signs are different and modulo not zero, adjust result
         if ((mod ^ y) < 0 && mod != 0) {
             mod += y;
         }
         return mod;

While the JIT does a reasonably good job at the current implementation,
this ensures we only do a single integer division and no subsequent
integer multiplications, speeding up execution by ~1.3x in interpreter
(mainly from removing the floorDiv method call overhead) and ~1.1x with
C1 and C2.

Testing: tier1-2, all Math tests run locally, -prof perfasm verification
on the provided microbenchmark.

Thanks!

/Claes


More information about the core-libs-dev mailing list