Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/bigInteger/TestMulAdd.java
41153 views
/*1* Copyright (c) 2015, 2021, 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 808177827* @summary Add C2 x86 intrinsic for BigInteger::mulAdd() method28*29* @library /test/lib30* @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch31* -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-UseSquareToLenIntrinsic -XX:-UseMultiplyToLenIntrinsic32* -XX:CompileCommand=dontinline,compiler.intrinsics.bigInteger.TestMulAdd::main33* -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestMulAdd::base_multiply,ccstr,DisableIntrinsic,_mulAdd34* -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_mulAdd35* -XX:CompileCommand=option,java.math.BigInteger::square,ccstr,DisableIntrinsic,_mulAdd36* -XX:CompileCommand=option,java.math.BigInteger::squareToLen,ccstr,DisableIntrinsic,_mulAdd37* -XX:CompileCommand=option,java.math.BigInteger::mulAdd,ccstr,DisableIntrinsic,_mulAdd38* -XX:CompileCommand=inline,java.math.BigInteger::multiply39* -XX:CompileCommand=inline,java.math.BigInteger::square40* -XX:CompileCommand=inline,java.math.BigInteger::squareToLen41* -XX:CompileCommand=inline,java.math.BigInteger::mulAdd42* compiler.intrinsics.bigInteger.TestMulAdd43*/4445package compiler.intrinsics.bigInteger;4647import java.math.BigInteger;48import java.util.Random;49import jdk.test.lib.Utils;5051public class TestMulAdd {5253// Avoid intrinsic by preventing inlining multiply() and mulAdd().54public static BigInteger base_multiply(BigInteger op1) {55return op1.multiply(op1);56}5758// Generate mulAdd() intrinsic by inlining multiply().59public static BigInteger new_multiply(BigInteger op1) {60return op1.multiply(op1);61}6263public static boolean bytecompare(BigInteger b1, BigInteger b2) {64byte[] data1 = b1.toByteArray();65byte[] data2 = b2.toByteArray();66if (data1.length != data2.length)67return false;68for (int i = 0; i < data1.length; i++) {69if (data1[i] != data2[i])70return false;71}72return true;73}7475public static String stringify(BigInteger b) {76String strout= "";77byte [] data = b.toByteArray();78for (int i = 0; i < data.length; i++) {79strout += (String.format("%02x",data[i]) + " ");80}81return strout;82}8384public static void main(String args[]) throws Exception {8586BigInteger oldsum = new BigInteger("0");87BigInteger newsum = new BigInteger("0");8889BigInteger b1, b2, oldres, newres;9091Random rand = new Random(Utils.getRandomInstance().nextLong());92Random rand1 = new Random(Utils.getRandomInstance().nextLong());9394for (int j = 0; j < 100000; j++) {95int rand_int = rand1.nextInt(3136)+32;96b1 = new BigInteger(rand_int, rand);9798oldres = base_multiply(b1);99newres = new_multiply(b1);100101oldsum = oldsum.add(oldres);102newsum = newsum.add(newres);103104if (!bytecompare(oldres,newres)) {105System.out.print("mismatch for:b1:" + stringify(b1) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));106System.out.println(b1);107throw new Exception("Failed");108}109}110if (!bytecompare(oldsum,newsum)) {111System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum));112throw new Exception("Failed");113} else {114System.out.println("Success");115}116}117}118119120