File: TestShift.java

package info (click to toggle)
openjdk-21 21.0.9%2B10-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 821,840 kB
  • sloc: java: 5,620,360; xml: 1,643,607; cpp: 1,297,809; ansic: 421,231; asm: 404,850; objc: 21,018; sh: 15,299; javascript: 11,217; python: 6,895; makefile: 2,367; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (131 lines) | stat: -rw-r--r-- 5,585 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @key randomness
 * @bug 8234692
 * @summary Add C2 x86 intrinsic for BigInteger::shiftLeft() and BigInteger::shiftRight() method
 * @library /test/lib
 * @requires vm.compiler2.enabled
 *
 * @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch
 *      -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestShift::main
 *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_left_shift,ccstrlist,DisableIntrinsic,_bigIntegerLeftShiftWorker
 *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_right_shift,ccstrlist,DisableIntrinsic,_bigIntegerRightShiftWorker
 *      -XX:CompileCommand=inline,java.math.BigInteger::shiftLeft
 *      -XX:CompileCommand=inline,java.math.BigInteger::shiftRight
 *      compiler.intrinsics.bigInteger.TestShift
 *
 * @run main/othervm/timeout=600
 *      -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestShift::main
 *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_left_shift,ccstrlist,DisableIntrinsic,_bigIntegerLeftShiftWorker
 *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestShift::base_right_shift,ccstrlist,DisableIntrinsic,_bigIntegerRightShiftWorker
 *      -XX:CompileCommand=inline,java.math.BigInteger::shiftLeft
 *      -XX:CompileCommand=inline,java.math.BigInteger::shiftRight
 *      compiler.intrinsics.bigInteger.TestShift
 *
 */

package compiler.intrinsics.bigInteger;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Random;
import jdk.test.lib.Utils;

public class TestShift {

    public static BigInteger base_left_shift(BigInteger op1, int shift) {
      return op1.shiftLeft(shift);
    }

    public static BigInteger new_left_shift(BigInteger op1, int shift) {
      return op1.shiftLeft(shift);
    }

    public static BigInteger base_right_shift(BigInteger op1, int shift) {
      return op1.shiftRight(shift);
    }

    public static BigInteger new_right_shift(BigInteger op1, int shift) {
      return op1.shiftRight(shift);
    }

    public static boolean bytecompare(BigInteger b1, BigInteger b2) {
      byte[] data1 = b1.toByteArray();
      byte[] data2 = b2.toByteArray();
      if (data1.length != data2.length)
        return false;
      for (int i = 0; i < data1.length; i++) {
        if (data1[i] != data2[i])
          return false;
      }
      return true;
    }

    public static String stringify(BigInteger b) {
      String strout= "";
      byte [] data = b.toByteArray();
      for (int i = 0; i < data.length; i++) {
        strout += (String.format("%02x",data[i]) + " ");
      }
      return strout;
    }

    public static void main(String args[]) throws Exception {
      BigInteger [] inputbuffer = new BigInteger[10];
      BigInteger [] oldLeftShiftResult = new BigInteger[10];
      BigInteger [] newLeftShiftResult = new BigInteger[10];
      BigInteger [] oldRightShiftResult = new BigInteger[10];
      BigInteger [] newRightShiftResult = new BigInteger[10];

      Random rand = Utils.getRandomInstance();
      int shiftCount = rand.nextInt(30) + 1;

      for(int i = 0; i < inputbuffer.length; i++) {
        int numbits = rand.nextInt(4096)+32;
        inputbuffer[i] = new BigInteger(numbits, rand);
      }

      for (int j = 0; j < 100000; j++) {
        for(int i = 0; i < inputbuffer.length; i++) {
           oldLeftShiftResult[i] = base_left_shift(inputbuffer[i], shiftCount);
           newLeftShiftResult[i] = new_left_shift(inputbuffer[i], shiftCount);
           if (!bytecompare(oldLeftShiftResult[i], newLeftShiftResult[i])) {
            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]));
            throw new Exception("Failed");
          }

          oldRightShiftResult[i] = base_right_shift(inputbuffer[i], shiftCount);
          newRightShiftResult[i] = new_right_shift(inputbuffer[i], shiftCount);
          if (!bytecompare(oldRightShiftResult[i], newRightShiftResult[i])) {
            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]));
            throw new Exception("Failed");
          }
        }
      }
    }
}