Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/bigInteger/TestMultiplyToLen.java
41153 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key randomness26* @bug 805549427* @summary Add C2 x86 intrinsic for BigInteger::multiplyToLen() method28*29* @library /test/lib30* @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch31* -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestMultiplyToLen::main32* -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestMultiplyToLen::base_multiply,ccstr,DisableIntrinsic,_multiplyToLen33* -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_multiplyToLen34* -XX:CompileCommand=inline,java.math.BigInteger::multiply35* compiler.intrinsics.bigInteger.TestMultiplyToLen36*/3738package compiler.intrinsics.bigInteger;3940import java.math.BigInteger;41import java.util.Arrays;42import java.util.Random;43import jdk.test.lib.Utils;4445public class TestMultiplyToLen {4647// Avoid intrinsic by preventing inlining multiply() and multiplyToLen().48public static BigInteger base_multiply(BigInteger op1, BigInteger op2) {49return op1.multiply(op2);50}5152// Generate multiplyToLen() intrinsic by inlining multiply().53public static BigInteger new_multiply(BigInteger op1, BigInteger op2) {54return op1.multiply(op2);55}5657public static boolean bytecompare(BigInteger b1, BigInteger b2) {58byte[] data1 = b1.toByteArray();59byte[] data2 = b2.toByteArray();60if (data1.length != data2.length)61return false;62for (int i = 0; i < data1.length; i++) {63if (data1[i] != data2[i])64return false;65}66return true;67}6869public static String stringify(BigInteger b) {70String strout= "";71byte [] data = b.toByteArray();72for (int i = 0; i < data.length; i++) {73strout += (String.format("%02x",data[i]) + " ");74}75return strout;76}7778public static void main(String args[]) throws Exception {7980BigInteger oldsum = new BigInteger("0");81BigInteger newsum = new BigInteger("0");8283BigInteger b1, b2, oldres, newres;8485Random rand = new Random(Utils.getRandomInstance().nextLong());86Random rand1 = new Random(Utils.getRandomInstance().nextLong());8788for (int j = 0; j < 1000000; j++) {89int rand_int = rand1.nextInt(3136)+32;90int rand_int1 = rand1.nextInt(3136)+32;91b1 = new BigInteger(rand_int, rand);92b2 = new BigInteger(rand_int1, rand);9394oldres = base_multiply(b1,b2);95newres = new_multiply(b1,b2);9697oldsum = oldsum.add(oldres);98newsum = newsum.add(newres);99100if (!bytecompare(oldres,newres)) {101System.out.println(b1);102System.out.println(b2);103System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));104throw new Exception("Failed");105}106}107108// Test carry propagation. Multiple carries during bignum109// multiplication are rare (especially when using 64-bit110// arithmetic) so we have to provoke them deliberately.111for (int j = 4; j <= 396; j += 4) {112byte[] bytes = new byte[j];113Arrays.fill(bytes, (byte)255);114b1 = new BigInteger(bytes);115b2 = new BigInteger(bytes);116117oldres = base_multiply(b1,b2);118newres = new_multiply(b1,b2);119120oldsum = oldsum.add(oldres);121newsum = newsum.add(newres);122123if (!bytecompare(oldres,newres)) {124System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));125System.out.println(b1);126System.out.println(b2);127throw new Exception("Failed");128}129}130131if (!bytecompare(oldsum,newsum)) {132System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum));133throw new Exception("Failed");134} else {135System.out.println("Success");136}137}138}139140141