1 /* 2 * Copyright (c) 2015, 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 /** 26 * @test 27 * @bug 8081778 28 * @summary Add C2 x86 intrinsic for BigInteger::mulAdd() method 29 * 30 * @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch 31 * -XX:+IgnoreUnrecognizedVMOptions -XX:-UseSquareToLenIntrinsic -XX:-UseMultiplyToLenIntrinsic 32 * -XX:+UseMulAddIntrinsic 33 * -XX:CompileCommand=dontinline,TestMulAdd::main 34 * -XX:CompileCommand=option,TestMulAdd::base_multiply,ccstr,DisableIntrinsic,_mulAdd 35 * -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_mulAdd 36 * -XX:CompileCommand=option,java.math.BigInteger::square,ccstr,DisableIntrinsic,_mulAdd 37 * -XX:CompileCommand=option,java.math.BigInteger::squareToLen,ccstr,DisableIntrinsic,_mulAdd 38 * -XX:CompileCommand=option,java.math.BigInteger::mulAdd,ccstr,DisableIntrinsic,_mulAdd 39 * -XX:CompileCommand=inline,java.math.BigInteger::multiply 40 * -XX:CompileCommand=inline,java.math.BigInteger::square 41 * -XX:CompileCommand=inline,java.math.BigInteger::squareToLen 42 * -XX:CompileCommand=inline,java.math.BigInteger::mulAdd TestMulAdd 43 */ 44 45 import java.util.Random; 46 import java.math.*; 47 48 public class TestMulAdd { 49 50 // Avoid intrinsic by preventing inlining multiply() and mulAdd(). base_multiply(BigInteger op1)51 public static BigInteger base_multiply(BigInteger op1) { 52 return op1.multiply(op1); 53 } 54 55 // Generate mulAdd() intrinsic by inlining multiply(). new_multiply(BigInteger op1)56 public static BigInteger new_multiply(BigInteger op1) { 57 return op1.multiply(op1); 58 } 59 bytecompare(BigInteger b1, BigInteger b2)60 public static boolean bytecompare(BigInteger b1, BigInteger b2) { 61 byte[] data1 = b1.toByteArray(); 62 byte[] data2 = b2.toByteArray(); 63 if (data1.length != data2.length) 64 return false; 65 for (int i = 0; i < data1.length; i++) { 66 if (data1[i] != data2[i]) 67 return false; 68 } 69 return true; 70 } 71 stringify(BigInteger b)72 public static String stringify(BigInteger b) { 73 String strout= ""; 74 byte [] data = b.toByteArray(); 75 for (int i = 0; i < data.length; i++) { 76 strout += (String.format("%02x",data[i]) + " "); 77 } 78 return strout; 79 } 80 main(String args[])81 public static void main(String args[]) throws Exception { 82 83 BigInteger oldsum = new BigInteger("0"); 84 BigInteger newsum = new BigInteger("0"); 85 86 BigInteger b1, b2, oldres, newres; 87 88 Random rand = new Random(); 89 long seed = System.nanoTime(); 90 Random rand1 = new Random(); 91 long seed1 = System.nanoTime(); 92 rand.setSeed(seed); 93 rand1.setSeed(seed1); 94 95 for (int j = 0; j < 100000; j++) { 96 int rand_int = rand1.nextInt(3136)+32; 97 b1 = new BigInteger(rand_int, rand); 98 99 oldres = base_multiply(b1); 100 newres = new_multiply(b1); 101 102 oldsum = oldsum.add(oldres); 103 newsum = newsum.add(newres); 104 105 if (!bytecompare(oldres,newres)) { 106 System.out.print("mismatch for:b1:" + stringify(b1) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres)); 107 System.out.println(b1); 108 throw new Exception("Failed"); 109 } 110 } 111 if (!bytecompare(oldsum,newsum)) { 112 System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum)); 113 throw new Exception("Failed"); 114 } else { 115 System.out.println("Success"); 116 } 117 } 118 } 119