1 /* 2 * Copyright (c) 2016, 2017, 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 * @library /test/lib 27 * @run main MultiplicationTests 28 * @bug 5100935 29 * @summary Tests for multiplication methods (use -Dseed=X to set PRNG seed) 30 * @key randomness 31 */ 32 33 import java.math.BigInteger; 34 import jdk.test.lib.RandomFactory; 35 36 public class MultiplicationTests { 37 private MultiplicationTests(){} 38 39 // Number of random products to test. 40 private static final int COUNT = 1 << 16; 41 42 // Initialize shared random number generator 43 private static java.util.Random rnd = RandomFactory.getRandom(); 44 45 // Calculate high 64 bits of 128 product using BigInteger. 46 private static long multiplyHighBigInt(long x, long y) { 47 return BigInteger.valueOf(x).multiply(BigInteger.valueOf(y)) 48 .shiftRight(64).longValue(); 49 } 50 51 // Check Math.multiplyHigh(x,y) against multiplyHighBigInt(x,y) 52 private static boolean check(long x, long y) { 53 long p1 = multiplyHighBigInt(x, y); 54 long p2 = Math.multiplyHigh(x, y); 55 if (p1 != p2) { 56 System.err.printf("Error - x:%d y:%d p1:%d p2:%d\n", x, y, p1, p2); 57 return false; 58 } else { 59 return true; 60 } 61 } 62 63 private static int testMultiplyHigh() { 64 int failures = 0; 65 66 // check some boundary cases 67 long[][] v = new long[][]{ 68 {0L, 0L}, 69 {-1L, 0L}, 70 {0L, -1L}, 71 {1L, 0L}, 72 {0L, 1L}, 73 {-1L, -1L}, 74 {-1L, 1L}, 75 {1L, -1L}, 76 {1L, 1L}, 77 {Long.MAX_VALUE, Long.MAX_VALUE}, 78 {Long.MAX_VALUE, -Long.MAX_VALUE}, 79 {-Long.MAX_VALUE, Long.MAX_VALUE}, 80 {Long.MAX_VALUE, Long.MIN_VALUE}, 81 {Long.MIN_VALUE, Long.MAX_VALUE}, 82 {Long.MIN_VALUE, Long.MIN_VALUE} 83 }; 84 85 for (long[] xy : v) { 86 if(!check(xy[0], xy[1])) { 87 failures++; 88 } 89 } 90 91 // check some random values 92 for (int i = 0; i < COUNT; i++) { 93 if (!check(rnd.nextLong(), rnd.nextLong())) { 94 failures++; 95 } 96 } 97 98 return failures; 99 } 100 101 public static void main(String argv[]) { 102 int failures = testMultiplyHigh(); 103 104 if (failures > 0) { 105 System.err.println("Multiplication testing encountered " 106 + failures + " failures."); 107 throw new RuntimeException(); 108 } else { 109 System.out.println("MultiplicationTests succeeded"); 110 } 111 } 112 }