Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/bigInteger/TestMultiplyToLen.java
41153 views
1
/*
2
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @key randomness
27
* @bug 8055494
28
* @summary Add C2 x86 intrinsic for BigInteger::multiplyToLen() method
29
*
30
* @library /test/lib
31
* @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch
32
* -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestMultiplyToLen::main
33
* -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestMultiplyToLen::base_multiply,ccstr,DisableIntrinsic,_multiplyToLen
34
* -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_multiplyToLen
35
* -XX:CompileCommand=inline,java.math.BigInteger::multiply
36
* compiler.intrinsics.bigInteger.TestMultiplyToLen
37
*/
38
39
package compiler.intrinsics.bigInteger;
40
41
import java.math.BigInteger;
42
import java.util.Arrays;
43
import java.util.Random;
44
import jdk.test.lib.Utils;
45
46
public class TestMultiplyToLen {
47
48
// Avoid intrinsic by preventing inlining multiply() and multiplyToLen().
49
public static BigInteger base_multiply(BigInteger op1, BigInteger op2) {
50
return op1.multiply(op2);
51
}
52
53
// Generate multiplyToLen() intrinsic by inlining multiply().
54
public static BigInteger new_multiply(BigInteger op1, BigInteger op2) {
55
return op1.multiply(op2);
56
}
57
58
public static boolean bytecompare(BigInteger b1, BigInteger b2) {
59
byte[] data1 = b1.toByteArray();
60
byte[] data2 = b2.toByteArray();
61
if (data1.length != data2.length)
62
return false;
63
for (int i = 0; i < data1.length; i++) {
64
if (data1[i] != data2[i])
65
return false;
66
}
67
return true;
68
}
69
70
public static String stringify(BigInteger b) {
71
String strout= "";
72
byte [] data = b.toByteArray();
73
for (int i = 0; i < data.length; i++) {
74
strout += (String.format("%02x",data[i]) + " ");
75
}
76
return strout;
77
}
78
79
public static void main(String args[]) throws Exception {
80
81
BigInteger oldsum = new BigInteger("0");
82
BigInteger newsum = new BigInteger("0");
83
84
BigInteger b1, b2, oldres, newres;
85
86
Random rand = new Random(Utils.getRandomInstance().nextLong());
87
Random rand1 = new Random(Utils.getRandomInstance().nextLong());
88
89
for (int j = 0; j < 1000000; j++) {
90
int rand_int = rand1.nextInt(3136)+32;
91
int rand_int1 = rand1.nextInt(3136)+32;
92
b1 = new BigInteger(rand_int, rand);
93
b2 = new BigInteger(rand_int1, rand);
94
95
oldres = base_multiply(b1,b2);
96
newres = new_multiply(b1,b2);
97
98
oldsum = oldsum.add(oldres);
99
newsum = newsum.add(newres);
100
101
if (!bytecompare(oldres,newres)) {
102
System.out.println(b1);
103
System.out.println(b2);
104
System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));
105
throw new Exception("Failed");
106
}
107
}
108
109
// Test carry propagation. Multiple carries during bignum
110
// multiplication are rare (especially when using 64-bit
111
// arithmetic) so we have to provoke them deliberately.
112
for (int j = 4; j <= 396; j += 4) {
113
byte[] bytes = new byte[j];
114
Arrays.fill(bytes, (byte)255);
115
b1 = new BigInteger(bytes);
116
b2 = new BigInteger(bytes);
117
118
oldres = base_multiply(b1,b2);
119
newres = new_multiply(b1,b2);
120
121
oldsum = oldsum.add(oldres);
122
newsum = newsum.add(newres);
123
124
if (!bytecompare(oldres,newres)) {
125
System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));
126
System.out.println(b1);
127
System.out.println(b2);
128
throw new Exception("Failed");
129
}
130
}
131
132
if (!bytecompare(oldsum,newsum)) {
133
System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum));
134
throw new Exception("Failed");
135
} else {
136
System.out.println("Success");
137
}
138
}
139
}
140
141