Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/bigInteger/TestShift.java
41153 views
/*1* Copyright (c) 2019, 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 823469227* @summary Add C2 x86 intrinsic for BigInteger::shiftLeft() and BigInteger::shiftRight() method28* @library /test/lib29* @requires vm.compiler2.enabled30*31* @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch32* -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestShift::main33* -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_left_shift,ccstr,DisableIntrinsic,_bigIntegerLeftShiftWorker34* -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_right_shift,ccstr,DisableIntrinsic,_bigIntegerRightShiftWorker35* -XX:CompileCommand=inline,java.math.BigInteger::shiftLeft36* -XX:CompileCommand=inline,java.math.BigInteger::shiftRight37* compiler.intrinsics.bigInteger.TestShift38*39* @run main/othervm/timeout=60040* -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestShift::main41* -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_left_shift,ccstr,DisableIntrinsic,_bigIntegerLeftShiftWorker42* -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_right_shift,ccstr,DisableIntrinsic,_bigIntegerRightShiftWorker43* -XX:CompileCommand=inline,java.math.BigInteger::shiftLeft44* -XX:CompileCommand=inline,java.math.BigInteger::shiftRight45* compiler.intrinsics.bigInteger.TestShift46*47*/4849package compiler.intrinsics.bigInteger;5051import java.math.BigInteger;52import java.util.Arrays;53import java.util.Random;54import jdk.test.lib.Utils;5556public class TestShift {5758public static BigInteger base_left_shift(BigInteger op1, int shift) {59return op1.shiftLeft(shift);60}6162public static BigInteger new_left_shift(BigInteger op1, int shift) {63return op1.shiftLeft(shift);64}6566public static BigInteger base_right_shift(BigInteger op1, int shift) {67return op1.shiftRight(shift);68}6970public static BigInteger new_right_shift(BigInteger op1, int shift) {71return op1.shiftRight(shift);72}7374public static boolean bytecompare(BigInteger b1, BigInteger b2) {75byte[] data1 = b1.toByteArray();76byte[] data2 = b2.toByteArray();77if (data1.length != data2.length)78return false;79for (int i = 0; i < data1.length; i++) {80if (data1[i] != data2[i])81return false;82}83return true;84}8586public static String stringify(BigInteger b) {87String strout= "";88byte [] data = b.toByteArray();89for (int i = 0; i < data.length; i++) {90strout += (String.format("%02x",data[i]) + " ");91}92return strout;93}9495public static void main(String args[]) throws Exception {96BigInteger [] inputbuffer = new BigInteger[10];97BigInteger [] oldLeftShiftResult = new BigInteger[10];98BigInteger [] newLeftShiftResult = new BigInteger[10];99BigInteger [] oldRightShiftResult = new BigInteger[10];100BigInteger [] newRightShiftResult = new BigInteger[10];101102Random rand = Utils.getRandomInstance();103int shiftCount = rand.nextInt(30) + 1;104105for(int i = 0; i < inputbuffer.length; i++) {106int numbits = rand.nextInt(4096)+32;107inputbuffer[i] = new BigInteger(numbits, rand);108}109110for (int j = 0; j < 100000; j++) {111for(int i = 0; i < inputbuffer.length; i++) {112oldLeftShiftResult[i] = base_left_shift(inputbuffer[i], shiftCount);113newLeftShiftResult[i] = new_left_shift(inputbuffer[i], shiftCount);114if (!bytecompare(oldLeftShiftResult[i], newLeftShiftResult[i])) {115System.out.println("mismatch for input:" + stringify(inputbuffer[i]) + "\n" + "expected left shift result:" + stringify(oldLeftShiftResult[i]) + "\n" +116"calculated left shift result:" + stringify(newLeftShiftResult[i]));117throw new Exception("Failed");118}119120oldRightShiftResult[i] = base_right_shift(inputbuffer[i], shiftCount);121newRightShiftResult[i] = new_right_shift(inputbuffer[i], shiftCount);122if (!bytecompare(oldRightShiftResult[i], newRightShiftResult[i])) {123System.out.println("mismatch for input:" + stringify(inputbuffer[i]) + "\n" + "expected right shift result:" + stringify(oldRightShiftResult[i]) + "\n" +124"calculated right shift result:" + stringify(newRightShiftResult[i]));125throw new Exception("Failed");126}127}128}129}130}131132133