1
1
/*
2
- * Copyright (c) 2016, 2017 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2016, 2021 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
26
26
* @library /test/lib
27
27
* @build jdk.test.lib.RandomFactory
28
28
* @run main MultiplicationTests
29
- * @bug 5100935
29
+ * @bug 5100935 8188044
30
30
* @summary Tests for multiplication methods (use -Dseed=X to set PRNG seed)
31
31
* @key randomness
32
32
*/
33
33
34
34
import java .math .BigInteger ;
35
+ import java .util .function .BiFunction ;
35
36
import jdk .test .lib .RandomFactory ;
36
37
37
38
public class MultiplicationTests {
@@ -49,10 +50,25 @@ private static long multiplyHighBigInt(long x, long y) {
49
50
.shiftRight (64 ).longValue ();
50
51
}
51
52
52
- // Check Math.multiplyHigh(x,y) against multiplyHighBigInt(x,y)
53
- private static boolean check (long x , long y ) {
54
- long p1 = multiplyHighBigInt (x , y );
55
- long p2 = Math .multiplyHigh (x , y );
53
+ // Calculate high 64 bits of unsigned 128 product using signed multiply
54
+ private static long unsignedMultiplyHigh (long x , long y ) {
55
+ long x0 = x & 0xffffffffL ;
56
+ long x1 = x >>> 32 ;
57
+ long y0 = y & 0xffffffffL ;
58
+ long y1 = y >>> 32 ;
59
+
60
+ long t = x1 * y0 + ((x0 * y0 ) >>> 32 );
61
+ long z0 = x0 * y1 + (t & 0xffffffffL );
62
+ long z1 = t >>> 32 ;
63
+
64
+ return x1 * y1 + z1 + (z0 >>> 32 );
65
+ }
66
+
67
+ // Compare results of two functions for a pair of values
68
+ private static boolean check (BiFunction <Long ,Long ,Long > reference ,
69
+ BiFunction <Long ,Long ,Long > multiply , long x , long y ) {
70
+ long p1 = reference .apply (x , y );
71
+ long p2 = multiply .apply (x , y );
56
72
if (p1 != p2 ) {
57
73
System .err .printf ("Error - x:%d y:%d p1:%d p2:%d\n " , x , y , p1 , p2 );
58
74
return false ;
@@ -61,7 +77,19 @@ private static boolean check(long x, long y) {
61
77
}
62
78
}
63
79
64
- private static int testMultiplyHigh () {
80
+ // Check Math.multiplyHigh(x,y) against multiplyHighBigInt(x,y)
81
+ private static boolean checkSigned (long x , long y ) {
82
+ return check ((a ,b ) -> multiplyHighBigInt (a ,b ),
83
+ (a ,b ) -> Math .multiplyHigh (a , b ), x , y );
84
+ }
85
+
86
+ // Check Math.unsignedMultiplyHigh(x,y) against unsignedMultiplyHigh(x,y)
87
+ private static boolean checkUnsigned (long x , long y ) {
88
+ return check ((a ,b ) -> unsignedMultiplyHigh (a ,b ),
89
+ (a ,b ) -> Math .unsignedMultiplyHigh (a , b ), x , y );
90
+ }
91
+
92
+ private static int test (BiFunction <Long ,Long ,Boolean > chk ) {
65
93
int failures = 0 ;
66
94
67
95
// check some boundary cases
@@ -84,23 +112,31 @@ private static int testMultiplyHigh() {
84
112
};
85
113
86
114
for (long [] xy : v ) {
87
- if (!check (xy [0 ], xy [1 ])) {
115
+ if (!chk . apply (xy [0 ], xy [1 ])) {
88
116
failures ++;
89
117
}
90
118
}
91
119
92
120
// check some random values
93
121
for (int i = 0 ; i < COUNT ; i ++) {
94
- if (!check (rnd .nextLong (), rnd .nextLong ())) {
122
+ if (!chk . apply (rnd .nextLong (), rnd .nextLong ())) {
95
123
failures ++;
96
124
}
97
125
}
98
126
99
127
return failures ;
100
128
}
101
129
130
+ private static int testMultiplyHigh () {
131
+ return test ((x ,y ) -> checkSigned (x ,y ));
132
+ }
133
+
134
+ private static int testUnsignedMultiplyHigh () {
135
+ return test ((x ,y ) -> checkUnsigned (x ,y ));
136
+ }
137
+
102
138
public static void main (String argv []) {
103
- int failures = testMultiplyHigh ();
139
+ int failures = testMultiplyHigh () + testUnsignedMultiplyHigh () ;
104
140
105
141
if (failures > 0 ) {
106
142
System .err .println ("Multiplication testing encountered "
0 commit comments