Skip to content

Commit cb79589

Browse files
author
Brian Burkhalter
committedJul 2, 2021
8188046: java.lang.Math.mutliplyHigh does not run in constant time
Reviewed-by: rriggs, darcy
1 parent ca4bea4 commit cb79589

File tree

1 file changed

+14
-25
lines changed
  • src/java.base/share/classes/java/lang

1 file changed

+14
-25
lines changed
 

‎src/java.base/share/classes/java/lang/Math.java

+14-25
Original file line numberDiff line numberDiff line change
@@ -1161,31 +1161,20 @@ public static long multiplyFull(int x, int y) {
11611161
*/
11621162
@IntrinsicCandidate
11631163
public static long multiplyHigh(long x, long y) {
1164-
if (x < 0 || y < 0) {
1165-
// Use technique from section 8-2 of Henry S. Warren, Jr.,
1166-
// Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 173-174.
1167-
long x1 = x >> 32;
1168-
long x2 = x & 0xFFFFFFFFL;
1169-
long y1 = y >> 32;
1170-
long y2 = y & 0xFFFFFFFFL;
1171-
long z2 = x2 * y2;
1172-
long t = x1 * y2 + (z2 >>> 32);
1173-
long z1 = t & 0xFFFFFFFFL;
1174-
long z0 = t >> 32;
1175-
z1 += x2 * y1;
1176-
return x1 * y1 + z0 + (z1 >> 32);
1177-
} else {
1178-
// Use Karatsuba technique with two base 2^32 digits.
1179-
long x1 = x >>> 32;
1180-
long y1 = y >>> 32;
1181-
long x2 = x & 0xFFFFFFFFL;
1182-
long y2 = y & 0xFFFFFFFFL;
1183-
long A = x1 * y1;
1184-
long B = x2 * y2;
1185-
long C = (x1 + x2) * (y1 + y2);
1186-
long K = C - A - B;
1187-
return (((B >>> 32) + K) >>> 32) + A;
1188-
}
1164+
// Use technique from section 8-2 of Henry S. Warren, Jr.,
1165+
// Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 173-174.
1166+
long x1 = x >> 32;
1167+
long x2 = x & 0xFFFFFFFFL;
1168+
long y1 = y >> 32;
1169+
long y2 = y & 0xFFFFFFFFL;
1170+
1171+
long z2 = x2 * y2;
1172+
long t = x1 * y2 + (z2 >>> 32);
1173+
long z1 = t & 0xFFFFFFFFL;
1174+
long z0 = t >> 32;
1175+
z1 += x2 * y1;
1176+
1177+
return x1 * y1 + z0 + (z1 >> 32);
11891178
}
11901179

11911180
/**

0 commit comments

Comments
 (0)
Please sign in to comment.