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