Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8222029

Optimize Math.floorMod

XMLWordPrintable

    • b16

      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.

            redestad Claes Redestad
            redestad Claes Redestad
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: