Wed, 17 Feb 2016 13:40:12 +0300
8081778: Use Intel x64 CPU instructions for RSA acceleration
Summary: Add intrinsics for BigInteger squareToLen and mulAdd methods.
Reviewed-by: kvn, jrose
igerasim@8307 | 1 | /* |
igerasim@8307 | 2 | * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
igerasim@8307 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
igerasim@8307 | 4 | * |
igerasim@8307 | 5 | * This code is free software; you can redistribute it and/or modify it |
igerasim@8307 | 6 | * under the terms of the GNU General Public License version 2 only, as |
igerasim@8307 | 7 | * published by the Free Software Foundation. |
igerasim@8307 | 8 | * |
igerasim@8307 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
igerasim@8307 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
igerasim@8307 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
igerasim@8307 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
igerasim@8307 | 13 | * accompanied this code). |
igerasim@8307 | 14 | * |
igerasim@8307 | 15 | * You should have received a copy of the GNU General Public License version |
igerasim@8307 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
igerasim@8307 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
igerasim@8307 | 18 | * |
igerasim@8307 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
igerasim@8307 | 20 | * or visit www.oracle.com if you need additional information or have any |
igerasim@8307 | 21 | * questions. |
igerasim@8307 | 22 | * |
igerasim@8307 | 23 | */ |
igerasim@8307 | 24 | |
igerasim@8307 | 25 | /** |
igerasim@8307 | 26 | * @test |
igerasim@8307 | 27 | * @bug 8081778 |
igerasim@8307 | 28 | * @summary Add C2 x86 intrinsic for BigInteger::squareToLen() method |
igerasim@8307 | 29 | * |
igerasim@8307 | 30 | * @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch |
igerasim@8307 | 31 | * -XX:CompileCommand=exclude,TestSquareToLen::main |
igerasim@8307 | 32 | * -XX:CompileCommand=option,TestSquareToLen::base_multiply,ccstr,DisableIntrinsic,_squareToLen |
igerasim@8307 | 33 | * -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_squareToLen |
igerasim@8307 | 34 | * -XX:CompileCommand=option,java.math.BigInteger::square,ccstr,DisableIntrinsic,_squareToLen |
igerasim@8307 | 35 | * -XX:CompileCommand=option,java.math.BigInteger::squareToLen,ccstr,DisableIntrinsic,_squareToLen |
igerasim@8307 | 36 | * -XX:CompileCommand=inline,java.math.BigInteger::multiply |
igerasim@8307 | 37 | * -XX:CompileCommand=inline,java.math.BigInteger::square |
igerasim@8307 | 38 | * -XX:CompileCommand=inline,java.math.BigInteger::squareToLen TestSquareToLen |
igerasim@8307 | 39 | */ |
igerasim@8307 | 40 | |
igerasim@8307 | 41 | import java.util.Random; |
igerasim@8307 | 42 | import java.math.*; |
igerasim@8307 | 43 | |
igerasim@8307 | 44 | public class TestSquareToLen { |
igerasim@8307 | 45 | |
igerasim@8307 | 46 | // Avoid intrinsic by preventing inlining multiply() and squareToLen(). |
igerasim@8307 | 47 | public static BigInteger base_multiply(BigInteger op1) { |
igerasim@8307 | 48 | return op1.multiply(op1); |
igerasim@8307 | 49 | } |
igerasim@8307 | 50 | |
igerasim@8307 | 51 | // Generate squareToLen() intrinsic by inlining multiply(). |
igerasim@8307 | 52 | public static BigInteger new_multiply(BigInteger op1) { |
igerasim@8307 | 53 | return op1.multiply(op1); |
igerasim@8307 | 54 | } |
igerasim@8307 | 55 | |
igerasim@8307 | 56 | public static boolean bytecompare(BigInteger b1, BigInteger b2) { |
igerasim@8307 | 57 | byte[] data1 = b1.toByteArray(); |
igerasim@8307 | 58 | byte[] data2 = b2.toByteArray(); |
igerasim@8307 | 59 | if (data1.length != data2.length) |
igerasim@8307 | 60 | return false; |
igerasim@8307 | 61 | for (int i = 0; i < data1.length; i++) { |
igerasim@8307 | 62 | if (data1[i] != data2[i]) |
igerasim@8307 | 63 | return false; |
igerasim@8307 | 64 | } |
igerasim@8307 | 65 | return true; |
igerasim@8307 | 66 | } |
igerasim@8307 | 67 | |
igerasim@8307 | 68 | public static String stringify(BigInteger b) { |
igerasim@8307 | 69 | String strout= ""; |
igerasim@8307 | 70 | byte [] data = b.toByteArray(); |
igerasim@8307 | 71 | for (int i = 0; i < data.length; i++) { |
igerasim@8307 | 72 | strout += (String.format("%02x",data[i]) + " "); |
igerasim@8307 | 73 | } |
igerasim@8307 | 74 | return strout; |
igerasim@8307 | 75 | } |
igerasim@8307 | 76 | |
igerasim@8307 | 77 | public static void main(String args[]) throws Exception { |
igerasim@8307 | 78 | |
igerasim@8307 | 79 | BigInteger oldsum = new BigInteger("0"); |
igerasim@8307 | 80 | BigInteger newsum = new BigInteger("0"); |
igerasim@8307 | 81 | |
igerasim@8307 | 82 | BigInteger b1, b2, oldres, newres; |
igerasim@8307 | 83 | |
igerasim@8307 | 84 | Random rand = new Random(); |
igerasim@8307 | 85 | long seed = System.nanoTime(); |
igerasim@8307 | 86 | Random rand1 = new Random(); |
igerasim@8307 | 87 | long seed1 = System.nanoTime(); |
igerasim@8307 | 88 | rand.setSeed(seed); |
igerasim@8307 | 89 | rand1.setSeed(seed1); |
igerasim@8307 | 90 | |
igerasim@8307 | 91 | for (int j = 0; j < 100000; j++) { |
igerasim@8307 | 92 | int rand_int = rand1.nextInt(3136)+32; |
igerasim@8307 | 93 | b1 = new BigInteger(rand_int, rand); |
igerasim@8307 | 94 | |
igerasim@8307 | 95 | oldres = base_multiply(b1); |
igerasim@8307 | 96 | newres = new_multiply(b1); |
igerasim@8307 | 97 | |
igerasim@8307 | 98 | oldsum = oldsum.add(oldres); |
igerasim@8307 | 99 | newsum = newsum.add(newres); |
igerasim@8307 | 100 | |
igerasim@8307 | 101 | if (!bytecompare(oldres,newres)) { |
igerasim@8307 | 102 | System.out.print("mismatch for:b1:" + stringify(b1) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres)); |
igerasim@8307 | 103 | System.out.println(b1); |
igerasim@8307 | 104 | throw new Exception("Failed"); |
igerasim@8307 | 105 | } |
igerasim@8307 | 106 | } |
igerasim@8307 | 107 | if (!bytecompare(oldsum,newsum)) { |
igerasim@8307 | 108 | System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum)); |
igerasim@8307 | 109 | throw new Exception("Failed"); |
igerasim@8307 | 110 | } else { |
igerasim@8307 | 111 | System.out.println("Success"); |
igerasim@8307 | 112 | } |
igerasim@8307 | 113 | } |
igerasim@8307 | 114 | } |