Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/bigInteger/TestSquareToLen.java
41153 views
/*1* Copyright (c) 2015, 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 808177827* @summary Add C2 x86 intrinsic for BigInteger::squareToLen() method28* @library /test/lib29*30* @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch31* -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestSquareToLen::main32* -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestSquareToLen::base_multiply,ccstr,DisableIntrinsic,_squareToLen33* -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_squareToLen34* -XX:CompileCommand=option,java.math.BigInteger::square,ccstr,DisableIntrinsic,_squareToLen35* -XX:CompileCommand=option,java.math.BigInteger::squareToLen,ccstr,DisableIntrinsic,_squareToLen36* -XX:CompileCommand=inline,java.math.BigInteger::multiply37* -XX:CompileCommand=inline,java.math.BigInteger::square38* -XX:CompileCommand=inline,java.math.BigInteger::squareToLen39* compiler.intrinsics.bigInteger.TestSquareToLen40*/4142package compiler.intrinsics.bigInteger;4344import java.math.BigInteger;45import java.util.Random;46import jdk.test.lib.Utils;4748public class TestSquareToLen {4950// Avoid intrinsic by preventing inlining multiply() and squareToLen().51public static BigInteger base_multiply(BigInteger op1) {52return op1.multiply(op1);53}5455// Generate squareToLen() intrinsic by inlining multiply().56public static BigInteger new_multiply(BigInteger op1) {57return op1.multiply(op1);58}5960public static boolean bytecompare(BigInteger b1, BigInteger b2) {61byte[] data1 = b1.toByteArray();62byte[] data2 = b2.toByteArray();63if (data1.length != data2.length)64return false;65for (int i = 0; i < data1.length; i++) {66if (data1[i] != data2[i])67return false;68}69return true;70}7172public static String stringify(BigInteger b) {73String strout= "";74byte [] data = b.toByteArray();75for (int i = 0; i < data.length; i++) {76strout += (String.format("%02x",data[i]) + " ");77}78return strout;79}8081public static void main(String args[]) throws Exception {8283BigInteger oldsum = new BigInteger("0");84BigInteger newsum = new BigInteger("0");8586BigInteger b1, b2, oldres, newres;8788Random rand = new Random(Utils.getRandomInstance().nextLong());89Random rand1 = new Random(Utils.getRandomInstance().nextLong());9091for (int j = 0; j < 100000; j++) {92int rand_int = rand1.nextInt(3136)+32;93b1 = new BigInteger(rand_int, rand);9495oldres = base_multiply(b1);96newres = new_multiply(b1);9798oldsum = oldsum.add(oldres);99newsum = newsum.add(newres);100101if (!bytecompare(oldres,newres)) {102System.out.print("mismatch for:b1:" + stringify(b1) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));103System.out.println(b1);104throw new Exception("Failed");105}106}107if (!bytecompare(oldsum,newsum)) {108System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum));109throw new Exception("Failed");110} else {111System.out.println("Success");112}113}114}115116117