1 /*
   2  * Copyright (c) 2019, 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  * @bug 8234692
  27  * @summary Add C2 x86 intrinsic for BigInteger::shiftLeft() and BigInteger::shiftRight() method
  28  * @requires vm.compiler2.enabled
  29  *
  30  * @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch
  31  *      -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestShift::main
  32  *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_left_shift,ccstr,DisableIntrinsic,_bigIntegerLeftShiftWorker
  33  *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_right_shift,ccstr,DisableIntrinsic,_bigIntegerRightShiftWorker
  34  *      -XX:CompileCommand=inline,java.math.BigInteger::shiftLeft
  35  *      -XX:CompileCommand=inline,java.math.BigInteger::shiftRight
  36  *      compiler.intrinsics.bigInteger.TestShift
  37  *
  38  * @run main/othervm/timeout=600
  39  *      -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestShift::main
  40  *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_left_shift,ccstr,DisableIntrinsic,_bigIntegerLeftShiftWorker
  41  *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_right_shift,ccstr,DisableIntrinsic,_bigIntegerRightShiftWorker
  42  *      -XX:CompileCommand=inline,java.math.BigInteger::shiftLeft
  43  *      -XX:CompileCommand=inline,java.math.BigInteger::shiftRight
  44  *      compiler.intrinsics.bigInteger.TestShift
  45  *  
  46  */
  47 
  48 package compiler.intrinsics.bigInteger;
  49 
  50 import java.math.BigInteger;
  51 import java.util.Arrays;
  52 import java.util.Random;
  53 
  54 public class TestShift {
  55 
  56     public static BigInteger base_left_shift(BigInteger op1, int shift) {
  57       return op1.shiftLeft(shift);
  58     }
  59 
  60     public static BigInteger new_left_shift(BigInteger op1, int shift) {
  61       return op1.shiftLeft(shift);
  62     }
  63 
  64     public static BigInteger base_right_shift(BigInteger op1, int shift) {
  65       return op1.shiftRight(shift);
  66     }
  67 
  68     public static BigInteger new_right_shift(BigInteger op1, int shift) {
  69       return op1.shiftRight(shift);
  70     }
  71 
  72     public static boolean bytecompare(BigInteger b1, BigInteger b2) {
  73       byte[] data1 = b1.toByteArray();
  74       byte[] data2 = b2.toByteArray();
  75       if (data1.length != data2.length)
  76         return false;
  77       for (int i = 0; i < data1.length; i++) {
  78         if (data1[i] != data2[i])
  79           return false;
  80       }
  81       return true;
  82     }
  83 
  84     public static String stringify(BigInteger b) {
  85       String strout= "";
  86       byte [] data = b.toByteArray();
  87       for (int i = 0; i < data.length; i++) {
  88         strout += (String.format("%02x",data[i]) + " ");
  89       }
  90       return strout;
  91     }
  92 
  93     public static void main(String args[]) throws Exception {
  94       BigInteger [] inputbuffer = new BigInteger[10];
  95       BigInteger [] oldLeftShiftResult = new BigInteger[10];
  96       BigInteger [] newLeftShiftResult = new BigInteger[10];
  97       BigInteger [] oldRightShiftResult = new BigInteger[10];
  98       BigInteger [] newRightShiftResult = new BigInteger[10];
  99 
 100       Random rand = new Random();
 101       long seed = System.nanoTime();
 102       rand.setSeed(seed);
 103       int shiftCount = rand.nextInt(30) + 1;
 104 
 105       for(int i = 0; i < inputbuffer.length; i++) {
 106         int numbits = rand.nextInt(4096)+32;
 107         inputbuffer[i] = new BigInteger(numbits, rand);
 108       }
 109 
 110       for (int j = 0; j < 100000; j++) {
 111         for(int i = 0; i < inputbuffer.length; i++) {
 112            oldLeftShiftResult[i] = base_left_shift(inputbuffer[i], shiftCount);
 113            newLeftShiftResult[i] = new_left_shift(inputbuffer[i], shiftCount);
 114            if (!bytecompare(oldLeftShiftResult[i], newLeftShiftResult[i])) {
 115             System.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]));
 117             throw new Exception("Failed");
 118           }
 119 
 120           oldRightShiftResult[i] = base_right_shift(inputbuffer[i], shiftCount);
 121           newRightShiftResult[i] = new_right_shift(inputbuffer[i], shiftCount);
 122           if (!bytecompare(oldRightShiftResult[i], newRightShiftResult[i])) {
 123             System.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]));
 125             throw new Exception("Failed");
 126           }
 127         }
 128       }
 129     }
 130 }