8222029: Optimize Math.floorMod

Reviewed-by: aph, darcy
This commit is contained in:
Claes Redestad 2019-04-10 20:03:07 +02:00
parent 0f4b0947ff
commit 8ee30d4fbe
3 changed files with 120 additions and 3 deletions

View file

@ -1274,7 +1274,12 @@ public final class Math {
* @since 1.8
*/
public static int floorMod(int x, int y) {
return x - floorDiv(x, y) * y;
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;
}
/**
@ -1301,7 +1306,7 @@ public final class Math {
*/
public static int floorMod(long x, int y) {
// Result cannot overflow the range of int.
return (int)(x - floorDiv(x, y) * y);
return (int)floorMod(x, (long)y);
}
/**
@ -1327,7 +1332,12 @@ public final class Math {
* @since 1.8
*/
public static long floorMod(long x, long y) {
return x - floorDiv(x, y) * y;
long mod = x % y;
// if the signs are different and modulo not zero, adjust result
if ((x ^ y) < 0 && mod != 0) {
mod += y;
}
return mod;
}
/**