test/hotspot/jtreg/compiler/intrinsics/bigInteger/TestMulAdd.java
author iignatyev
Mon, 18 Feb 2019 09:31:07 -0800
changeset 53791 bec6c8739833
parent 53494 8fd43dacaef7
permissions -rw-r--r--
8219157: vm/mlvm/mixed/stress/java/findDeadlock should be problem-listed only on mac Reviewed-by: thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31129
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
     1
/*
53494
8fd43dacaef7 8216180: [AOT] compiler/intrinsics/bigInteger/TestMulAdd.java crashed with AOT enabled
iignatyev
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
31129
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
     4
 *
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
     8
 *
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    13
 * accompanied this code).
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    14
 *
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    18
 *
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    21
 * questions.
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    22
 */
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    23
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    24
/**
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    25
 * @test
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    26
 * @bug 8081778
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    27
 * @summary Add C2 x86 intrinsic for BigInteger::mulAdd() method
53494
8fd43dacaef7 8216180: [AOT] compiler/intrinsics/bigInteger/TestMulAdd.java crashed with AOT enabled
iignatyev
parents: 47216
diff changeset
    28
 * @comment the test disables intrinsics, so it can't be run w/ AOT'ed java.base
8fd43dacaef7 8216180: [AOT] compiler/intrinsics/bigInteger/TestMulAdd.java crashed with AOT enabled
iignatyev
parents: 47216
diff changeset
    29
 * @requires !vm.aot.enabled
31129
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    30
 *
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    31
 * @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch
38285
20b85a0ba796 8153655: Make intrinsics flags diagnostic and update intrinsics tests to enable diagnostic options
rraghavan
parents: 37268
diff changeset
    32
 *      -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-UseSquareToLenIntrinsic -XX:-UseMultiplyToLenIntrinsic
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38285
diff changeset
    33
 *      -XX:CompileCommand=dontinline,compiler.intrinsics.bigInteger.TestMulAdd::main
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38285
diff changeset
    34
 *      -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestMulAdd::base_multiply,ccstr,DisableIntrinsic,_mulAdd
31129
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    35
 *      -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_mulAdd
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    36
 *      -XX:CompileCommand=option,java.math.BigInteger::square,ccstr,DisableIntrinsic,_mulAdd
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    37
 *      -XX:CompileCommand=option,java.math.BigInteger::squareToLen,ccstr,DisableIntrinsic,_mulAdd
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    38
 *      -XX:CompileCommand=option,java.math.BigInteger::mulAdd,ccstr,DisableIntrinsic,_mulAdd
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    39
 *      -XX:CompileCommand=inline,java.math.BigInteger::multiply
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    40
 *      -XX:CompileCommand=inline,java.math.BigInteger::square
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    41
 *      -XX:CompileCommand=inline,java.math.BigInteger::squareToLen
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38285
diff changeset
    42
 *      -XX:CompileCommand=inline,java.math.BigInteger::mulAdd
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38285
diff changeset
    43
 *      compiler.intrinsics.bigInteger.TestMulAdd
31129
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    44
 */
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    45
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38285
diff changeset
    46
package compiler.intrinsics.bigInteger;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38285
diff changeset
    47
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38285
diff changeset
    48
import java.math.BigInteger;
31129
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    49
import java.util.Random;
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    50
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    51
public class TestMulAdd {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    52
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    53
    // Avoid intrinsic by preventing inlining multiply() and mulAdd().
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    54
    public static BigInteger base_multiply(BigInteger op1) {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    55
      return op1.multiply(op1);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    56
    }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    57
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    58
    // Generate mulAdd() intrinsic by inlining multiply().
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    59
    public static BigInteger new_multiply(BigInteger op1) {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    60
      return op1.multiply(op1);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    61
    }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    62
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    63
    public static boolean bytecompare(BigInteger b1, BigInteger b2) {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    64
      byte[] data1 = b1.toByteArray();
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    65
      byte[] data2 = b2.toByteArray();
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    66
      if (data1.length != data2.length)
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    67
        return false;
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    68
      for (int i = 0; i < data1.length; i++) {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    69
        if (data1[i] != data2[i])
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    70
          return false;
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    71
      }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    72
      return true;
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    73
    }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    74
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    75
    public static String stringify(BigInteger b) {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    76
      String strout= "";
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    77
      byte [] data = b.toByteArray();
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    78
      for (int i = 0; i < data.length; i++) {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    79
        strout += (String.format("%02x",data[i]) + " ");
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    80
      }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    81
      return strout;
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    82
    }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    83
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    84
    public static void main(String args[]) throws Exception {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    85
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    86
      BigInteger oldsum = new BigInteger("0");
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    87
      BigInteger newsum = new BigInteger("0");
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    88
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    89
      BigInteger b1, b2, oldres, newres;
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    90
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    91
      Random rand = new Random();
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    92
      long seed = System.nanoTime();
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    93
      Random rand1 = new Random();
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    94
      long seed1 = System.nanoTime();
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    95
      rand.setSeed(seed);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    96
      rand1.setSeed(seed1);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    97
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    98
      for (int j = 0; j < 100000; j++) {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
    99
        int rand_int = rand1.nextInt(3136)+32;
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   100
        b1 = new BigInteger(rand_int, rand);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   101
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   102
        oldres = base_multiply(b1);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   103
        newres = new_multiply(b1);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   104
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   105
        oldsum = oldsum.add(oldres);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   106
        newsum = newsum.add(newres);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   107
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   108
        if (!bytecompare(oldres,newres)) {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   109
          System.out.print("mismatch for:b1:" + stringify(b1) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres));
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   110
          System.out.println(b1);
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   111
          throw new Exception("Failed");
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   112
        }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   113
      }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   114
      if (!bytecompare(oldsum,newsum))  {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   115
        System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum));
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   116
        throw new Exception("Failed");
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   117
      } else {
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   118
        System.out.println("Success");
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   119
      }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   120
   }
02ee7609f0e1 8081778: Use Intel x64 CPU instructions for RSA acceleration
kvn
parents:
diff changeset
   121
}