1/* 2 * Copyright (c) 2014, 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 8055494 27 * @summary Add C2 x86 intrinsic for BigInteger::multiplyToLen() method 28 * 29 * @run main/othervm/timeout=600 -XX:-TieredCompilation -Xbatch 30 * -XX:CompileCommand=exclude,compiler.intrinsics.bigInteger.TestMultiplyToLen::main 31 * -XX:CompileCommand=option,compiler.intrinsics.bigInteger.TestMultiplyToLen::base_multiply,ccstr,DisableIntrinsic,_multiplyToLen 32 * -XX:CompileCommand=option,java.math.BigInteger::multiply,ccstr,DisableIntrinsic,_multiplyToLen 33 * -XX:CompileCommand=inline,java.math.BigInteger::multiply 34 * compiler.intrinsics.bigInteger.TestMultiplyToLen 35 */ 36 37package compiler.intrinsics.bigInteger; 38 39import java.math.BigInteger; 40import java.util.Arrays; 41import java.util.Random; 42 43public class TestMultiplyToLen { 44 45 // Avoid intrinsic by preventing inlining multiply() and multiplyToLen(). 46 public static BigInteger base_multiply(BigInteger op1, BigInteger op2) { 47 return op1.multiply(op2); 48 } 49 50 // Generate multiplyToLen() intrinsic by inlining multiply(). 51 public static BigInteger new_multiply(BigInteger op1, BigInteger op2) { 52 return op1.multiply(op2); 53 } 54 55 public static boolean bytecompare(BigInteger b1, BigInteger b2) { 56 byte[] data1 = b1.toByteArray(); 57 byte[] data2 = b2.toByteArray(); 58 if (data1.length != data2.length) 59 return false; 60 for (int i = 0; i < data1.length; i++) { 61 if (data1[i] != data2[i]) 62 return false; 63 } 64 return true; 65 } 66 67 public static String stringify(BigInteger b) { 68 String strout= ""; 69 byte [] data = b.toByteArray(); 70 for (int i = 0; i < data.length; i++) { 71 strout += (String.format("%02x",data[i]) + " "); 72 } 73 return strout; 74 } 75 76 public static void main(String args[]) throws Exception { 77 78 BigInteger oldsum = new BigInteger("0"); 79 BigInteger newsum = new BigInteger("0"); 80 81 BigInteger b1, b2, oldres, newres; 82 83 Random rand = new Random(); 84 long seed = System.nanoTime(); 85 Random rand1 = new Random(); 86 long seed1 = System.nanoTime(); 87 rand.setSeed(seed); 88 rand1.setSeed(seed1); 89 90 for (int j = 0; j < 1000000; j++) { 91 int rand_int = rand1.nextInt(3136)+32; 92 int rand_int1 = rand1.nextInt(3136)+32; 93 b1 = new BigInteger(rand_int, rand); 94 b2 = new BigInteger(rand_int1, rand); 95 96 oldres = base_multiply(b1,b2); 97 newres = new_multiply(b1,b2); 98 99 oldsum = oldsum.add(oldres); 100 newsum = newsum.add(newres); 101 102 if (!bytecompare(oldres,newres)) { 103 System.out.println(b1); 104 System.out.println(b2); 105 System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres)); 106 throw new Exception("Failed"); 107 } 108 } 109 110 // Test carry propagation. Multiple carries during bignum 111 // multiplication are rare (especially when using 64-bit 112 // arithmetic) so we have to provoke them deliberately. 113 for (int j = 4; j <= 396; j += 4) { 114 byte[] bytes = new byte[j]; 115 Arrays.fill(bytes, (byte)255); 116 b1 = new BigInteger(bytes); 117 b2 = new BigInteger(bytes); 118 119 oldres = base_multiply(b1,b2); 120 newres = new_multiply(b1,b2); 121 122 oldsum = oldsum.add(oldres); 123 newsum = newsum.add(newres); 124 125 if (!bytecompare(oldres,newres)) { 126 System.out.print("mismatch for:b1:" + stringify(b1) + " :b2:" + stringify(b2) + " :oldres:" + stringify(oldres) + " :newres:" + stringify(newres)); 127 System.out.println(b1); 128 System.out.println(b2); 129 throw new Exception("Failed"); 130 } 131 } 132 133 if (!bytecompare(oldsum,newsum)) { 134 System.out.println("Failure: oldsum:" + stringify(oldsum) + " newsum:" + stringify(newsum)); 135 throw new Exception("Failed"); 136 } else { 137 System.out.println("Success"); 138 } 139 } 140} 141