1 /* 2 * Copyright (c) 2016, 2021, 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 * @build jdk.test.lib.RandomFactory 28 * @run main MultiplicationTests 29 * @bug 5100935 8188044 30 * @summary Tests for multiplication methods (use -Dseed=X to set PRNG seed) 31 * @key randomness 32 */ 33 34 package test.java.lang.Math; 35 36 import java.math.BigInteger; 37 import java.util.function.BiFunction; 38 import jdk.test.lib.RandomFactory; 39 40 public class MultiplicationTests { 41 // Android-changed: Needs a public constructor in order to initialize the test on Android. 42 // private MultiplicationTests(){} 43 44 // Number of random products to test. 45 private static final int COUNT = 1 << 16; 46 47 // Initialize shared random number generator 48 private static java.util.Random rnd = RandomFactory.getRandom(); 49 50 // Calculate high 64 bits of 128 product using BigInteger. multiplyHighBigInt(long x, long y)51 private static long multiplyHighBigInt(long x, long y) { 52 return BigInteger.valueOf(x).multiply(BigInteger.valueOf(y)) 53 .shiftRight(64).longValue(); 54 } 55 56 // Calculate high 64 bits of unsigned 128 product using signed multiply unsignedMultiplyHigh(long x, long y)57 private static long unsignedMultiplyHigh(long x, long y) { 58 long x0 = x & 0xffffffffL; 59 long x1 = x >>> 32; 60 long y0 = y & 0xffffffffL; 61 long y1 = y >>> 32; 62 63 long t = x1 * y0 + ((x0 * y0) >>> 32); 64 long z0 = x0 * y1 + (t & 0xffffffffL); 65 long z1 = t >>> 32; 66 67 return x1 * y1 + z1 + (z0 >>> 32); 68 } 69 70 // Compare results of two functions for a pair of values check(BiFunction<Long,Long,Long> reference, BiFunction<Long,Long,Long> multiply, long x, long y)71 private static boolean check(BiFunction<Long,Long,Long> reference, 72 BiFunction<Long,Long,Long> multiply, long x, long y) { 73 long p1 = reference.apply(x, y); 74 long p2 = multiply.apply(x, y); 75 if (p1 != p2) { 76 System.err.printf("Error - x:%d y:%d p1:%d p2:%d\n", x, y, p1, p2); 77 return false; 78 } else { 79 return true; 80 } 81 } 82 83 // Check Math.multiplyHigh(x,y) against multiplyHighBigInt(x,y) checkSigned(long x, long y)84 private static boolean checkSigned(long x, long y) { 85 return check((a,b) -> multiplyHighBigInt(a,b), 86 (a,b) -> Math.multiplyHigh(a, b), x, y); 87 } 88 89 // Check Math.unsignedMultiplyHigh(x,y) against unsignedMultiplyHigh(x,y) checkUnsigned(long x, long y)90 private static boolean checkUnsigned(long x, long y) { 91 return check((a,b) -> unsignedMultiplyHigh(a,b), 92 (a,b) -> Math.unsignedMultiplyHigh(a, b), x, y); 93 } 94 95 // Android-added: Test functionally-equivalent StrictMath version. 96 // Check StrictMath.unsignedMultiplyHigh(x,y) against unsignedMultiplyHigh(x,y) checkStrictMathUnsigned(long x, long y)97 private static boolean checkStrictMathUnsigned(long x, long y) { 98 return check((a,b) -> unsignedMultiplyHigh(a,b), 99 (a,b) -> StrictMath.unsignedMultiplyHigh(a, b), x, y); 100 } 101 test(BiFunction<Long,Long,Boolean> chk)102 private static int test(BiFunction<Long,Long,Boolean> chk) { 103 int failures = 0; 104 105 // check some boundary cases 106 long[][] v = new long[][]{ 107 {0L, 0L}, 108 {-1L, 0L}, 109 {0L, -1L}, 110 {1L, 0L}, 111 {0L, 1L}, 112 {-1L, -1L}, 113 {-1L, 1L}, 114 {1L, -1L}, 115 {1L, 1L}, 116 {Long.MAX_VALUE, Long.MAX_VALUE}, 117 {Long.MAX_VALUE, -Long.MAX_VALUE}, 118 {-Long.MAX_VALUE, Long.MAX_VALUE}, 119 {Long.MAX_VALUE, Long.MIN_VALUE}, 120 {Long.MIN_VALUE, Long.MAX_VALUE}, 121 {Long.MIN_VALUE, Long.MIN_VALUE} 122 }; 123 124 for (long[] xy : v) { 125 if(!chk.apply(xy[0], xy[1])) { 126 failures++; 127 } 128 } 129 130 // check some random values 131 for (int i = 0; i < COUNT; i++) { 132 if (!chk.apply(rnd.nextLong(), rnd.nextLong())) { 133 failures++; 134 } 135 } 136 137 return failures; 138 } 139 testMultiplyHigh()140 private static int testMultiplyHigh() { 141 return test((x,y) -> checkSigned(x,y)); 142 } 143 testUnsignedMultiplyHigh()144 private static int testUnsignedMultiplyHigh() { 145 return test((x,y) -> checkUnsigned(x,y) 146 // Android-added: Test functionally-equivalent StrictMath version. 147 && checkStrictMathUnsigned(x,y)); 148 } 149 main(String argv[])150 public static void main(String argv[]) { 151 int failures = testMultiplyHigh() + testUnsignedMultiplyHigh(); 152 153 if (failures > 0) { 154 System.err.println("Multiplication testing encountered " 155 + failures + " failures."); 156 throw new RuntimeException(); 157 } else { 158 System.out.println("MultiplicationTests succeeded"); 159 } 160 } 161 } 162