Merge "Gracefully handle integer overflows."

This commit is contained in:
Junyu Lai 2020-07-29 01:19:07 +00:00 • committed by Gerrit Code Review
commit 71fdf4ab8d

View file

@ -476,4 +476,35 @@ public class NetworkUtils {
return true;
}
/**
* Safely multiple a value by a rational.
* <p>
* Internally it uses integer-based math whenever possible, but switches
* over to double-based math if values would overflow.
* @hide
*/
public static long multiplySafeByRational(long value, long num, long den) {
if (den == 0) {
throw new ArithmeticException("Invalid Denominator");
}
long x = value;
long y = num;
// Logic shamelessly borrowed from Math.multiplyExact()
long r = x * y;
long ax = Math.abs(x);
long ay = Math.abs(y);
if (((ax | ay) >>> 31 != 0)) {
// Some bits greater than 2^31 that might cause overflow
// Check the result using the divide operator
// and check for the special case of Long.MIN_VALUE * -1
if (((y != 0) && (r / y != x)) ||
(x == Long.MIN_VALUE && y == -1)) {
// Use double math to avoid overflowing
return (long) (((double) num / den) * value);
}
}
return r / den;
}
}