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