hotspot/test/compiler/intrinsics/bigInteger/MontgomeryMultiplyTest.java
author hseigel
Wed, 01 Mar 2017 08:00:02 -0500
changeset 46194 5596e6f63072
parent 43455 96560cffef4d
permissions -rw-r--r--
8172307: Remove ununsed JVM API JVM_GetModuleByPackageName() Summary: Remove get_module_by_package_name() etc., and unneeded test. Reviewed-by: sspitsyn, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
     1
/*
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40067
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
     3
 * Copyright (c) 2015, Red Hat Inc. All rights reserved.
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
     5
 *
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
     8
 * published by the Free Software Foundation.
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
     9
 *
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    14
 * accompanied this code).
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    15
 *
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    19
 *
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    22
 * questions.
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    23
 */
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    24
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    25
/**
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    26
 * @test
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    27
 * @bug 8130150 8131779 8139907
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    28
 * @summary Verify that the Montgomery multiply and square intrinsic works and correctly checks their arguments.
43455
96560cffef4d 8166002: Emulate client build on platforms with reduced virtual address space
jcm
parents: 42650
diff changeset
    29
 * @requires vm.flavor == "server" & !vm.emulatedClient
42307
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
    30
 * @modules java.base/jdk.internal.misc:open
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40631
diff changeset
    31
 * @modules java.base/java.math:open
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 42307
diff changeset
    32
 * @library /test/lib /
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    33
 *
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 40067
diff changeset
    34
 * @build sun.hotspot.WhiteBox
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    35
 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    36
 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    37
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    38
 *      compiler.intrinsics.bigInteger.MontgomeryMultiplyTest
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    39
 */
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    40
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    41
package compiler.intrinsics.bigInteger;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    42
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    43
import jdk.test.lib.Platform;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 38152
diff changeset
    44
import sun.hotspot.WhiteBox;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 42307
diff changeset
    45
import compiler.whitebox.CompilerWhiteBoxTest;
31583
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    46
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    47
import java.lang.invoke.MethodHandle;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    48
import java.lang.invoke.MethodHandles;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    49
import java.lang.reflect.Constructor;
33473
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
    50
import java.lang.reflect.Executable;
31583
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    51
import java.lang.reflect.Field;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    52
import java.lang.reflect.Method;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    53
import java.math.BigInteger;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    54
import java.util.Arrays;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    55
import java.util.Random;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    56
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    57
public class MontgomeryMultiplyTest {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    58
33473
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
    59
    private static final WhiteBox wb = WhiteBox.getWhiteBox();
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
    60
31583
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    61
    static final MethodHandles.Lookup lookup = MethodHandles.lookup();
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    62
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    63
    static final MethodHandle montgomeryMultiplyHandle, montgomerySquareHandle;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    64
    static final MethodHandle bigIntegerConstructorHandle;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    65
    static final Field bigIntegerMagField;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    66
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    67
    static {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    68
       // Use reflection to gain access to the methods we want to test.
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    69
        try {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    70
            Method m = BigInteger.class.getDeclaredMethod("montgomeryMultiply",
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    71
                /*a*/int[].class, /*b*/int[].class, /*n*/int[].class, /*len*/int.class,
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    72
                /*inv*/long.class, /*product*/int[].class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    73
            m.setAccessible(true);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    74
            montgomeryMultiplyHandle = lookup.unreflect(m);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    75
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    76
            m = BigInteger.class.getDeclaredMethod("montgomerySquare",
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    77
                /*a*/int[].class, /*n*/int[].class, /*len*/int.class,
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    78
                /*inv*/long.class, /*product*/int[].class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    79
            m.setAccessible(true);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    80
            montgomerySquareHandle = lookup.unreflect(m);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    81
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    82
            Constructor c
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    83
                = BigInteger.class.getDeclaredConstructor(int.class, int[].class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    84
            c.setAccessible(true);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    85
            bigIntegerConstructorHandle = lookup.unreflectConstructor(c);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    86
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    87
            bigIntegerMagField = BigInteger.class.getDeclaredField("mag");
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    88
            bigIntegerMagField.setAccessible(true);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    89
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    90
        } catch (Throwable ex) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    91
            throw new RuntimeException(ex);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    92
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    93
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
    94
33473
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
    95
    /* Obtain executable for the intrinsics tested. Depending on the
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
    96
     * value of 'isMultiply', the executable corresponding to either
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
    97
     * implMontgomerMultiply or implMontgomerySqure is returned. */
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
    98
    static Executable getExecutable(boolean isMultiply) throws RuntimeException {
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
    99
        try {
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   100
            Class aClass = Class.forName("java.math.BigInteger");
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   101
            Method aMethod;
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   102
            if (isMultiply) {
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   103
                aMethod = aClass.getDeclaredMethod("implMontgomeryMultiply",
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   104
                                                   int[].class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   105
                                                   int[].class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   106
                                                   int[].class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   107
                                                   int.class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   108
                                                   long.class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   109
                                                   int[].class);
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   110
            } else {
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   111
                aMethod = aClass.getDeclaredMethod("implMontgomerySquare",
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   112
                                                   int[].class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   113
                                                   int[].class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   114
                                                   int.class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   115
                                                   long.class,
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   116
                                                   int[].class);
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   117
            }
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   118
            return aMethod;
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   119
        } catch (NoSuchMethodException e) {
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   120
            throw new RuntimeException("Test bug, method is unavailable. " + e);
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   121
        } catch (ClassNotFoundException e) {
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   122
            throw new RuntimeException("Test bug, class is unavailable. " + e);
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   123
        }
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   124
    }
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   125
31583
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   126
    // Invoke either BigInteger.montgomeryMultiply or BigInteger.montgomerySquare.
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   127
    int[] montgomeryMultiply(int[] a, int[] b, int[] n, int len, long inv,
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   128
                             int[] product) throws Throwable {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   129
        int[] result =
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   130
            (a == b) ? (int[]) montgomerySquareHandle.invokeExact(a, n, len, inv, product)
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   131
                     : (int[]) montgomeryMultiplyHandle.invokeExact(a, b, n, len, inv, product);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   132
        return Arrays.copyOf(result, len);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   133
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   134
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   135
    // Invoke the private constructor BigInteger(int[]).
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   136
    BigInteger newBigInteger(int[] val) throws Throwable {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   137
        return (BigInteger) bigIntegerConstructorHandle.invokeExact(1, val);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   138
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   139
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   140
    // Get the private field BigInteger.mag
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   141
    int[] mag(BigInteger n) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   142
        try {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   143
            return (int[]) bigIntegerMagField.get(n);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   144
        } catch (Exception ex) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   145
            throw new RuntimeException(ex);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   146
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   147
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   148
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   149
    // Montgomery multiplication
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   150
    // Calculate a * b * r^-1 mod n)
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   151
    //
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   152
    // R is a power of the word size
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   153
    // N' = R^-1 mod N
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   154
    //
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   155
    // T := ab
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   156
    // m := (T mod R)N' mod R [so 0 <= m < R]
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   157
    // t := (T + mN)/R
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   158
    // if t >= N then return t - N else return t
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   159
    //
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   160
    BigInteger montgomeryMultiply(BigInteger a, BigInteger b, BigInteger N,
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   161
            int len, BigInteger n_prime)
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   162
            throws Throwable {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   163
        BigInteger T = a.multiply(b);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   164
        BigInteger R = BigInteger.ONE.shiftLeft(len*32);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   165
        BigInteger mask = R.subtract(BigInteger.ONE);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   166
        BigInteger m = (T.and(mask)).multiply(n_prime);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   167
        m = m.and(mask); // i.e. m.mod(R)
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   168
        T = T.add(m.multiply(N));
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   169
        T = T.shiftRight(len*32); // i.e. T.divide(R)
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   170
        if (T.compareTo(N) > 0) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   171
            T = T.subtract(N);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   172
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   173
        return T;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   174
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   175
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   176
    // Call the Montgomery multiply intrinsic.
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   177
    BigInteger montgomeryMultiply(int[] a_words, int[] b_words, int[] n_words,
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   178
            int len, BigInteger inv)
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   179
            throws Throwable {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   180
        BigInteger t = montgomeryMultiply(
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   181
                newBigInteger(a_words),
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   182
                newBigInteger(b_words),
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   183
                newBigInteger(n_words),
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   184
                len, inv);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   185
        return t;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   186
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   187
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   188
    // Check that the Montgomery multiply intrinsic returns the same
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   189
    // result as the longhand calculation.
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   190
    void check(int[] a_words, int[] b_words, int[] n_words, int len, BigInteger inv)
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   191
            throws Throwable {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   192
        BigInteger n = newBigInteger(n_words);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   193
        BigInteger slow = montgomeryMultiply(a_words, b_words, n_words, len, inv);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   194
        BigInteger fast
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   195
            = newBigInteger(montgomeryMultiply
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   196
                            (a_words, b_words, n_words, len, inv.longValue(), null));
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   197
        // The intrinsic may not return the same value as the longhand
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   198
        // calculation but they must have the same residue mod N.
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   199
        if (!slow.mod(n).equals(fast.mod(n))) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   200
            throw new RuntimeException();
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   201
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   202
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   203
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   204
    Random rnd = new Random(0);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   205
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   206
    // Return a random value of length <= bits in an array of even length
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   207
    int[] random_val(int bits) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   208
        int len = (bits+63)/64;  // i.e. length in longs
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   209
        int[] val = new int[len*2];
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   210
        for (int i = 0; i < val.length; i++)
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   211
            val[i] = rnd.nextInt();
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   212
        int leadingZeros = 64 - (bits & 64);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   213
        if (leadingZeros >= 32) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   214
            val[0] = 0;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   215
            val[1] &= ~(-1l << (leadingZeros & 31));
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   216
        } else {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   217
            val[0] &= ~(-1l << leadingZeros);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   218
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   219
        return val;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   220
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   221
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   222
    void testOneLength(int lenInBits, int lenInInts) throws Throwable {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   223
        BigInteger mod = new BigInteger(lenInBits, 2, rnd);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   224
        BigInteger r = BigInteger.ONE.shiftLeft(lenInInts * 32);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   225
        BigInteger n_prime = mod.modInverse(r).negate();
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   226
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   227
        // Make n.length even, padding with a zero if necessary
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   228
        int[] n = mag(mod);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   229
        if (n.length < lenInInts) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   230
            int[] x = new int[lenInInts];
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   231
            System.arraycopy(n, 0, x, lenInInts-n.length, n.length);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   232
            n = x;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   233
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   234
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   235
        for (int i = 0; i < 10000; i++) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   236
            // multiply
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   237
            check(random_val(lenInBits), random_val(lenInBits), n, lenInInts, n_prime);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   238
            // square
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   239
            int[] tmp = random_val(lenInBits);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   240
            check(tmp, tmp, n, lenInInts, n_prime);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   241
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   242
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   243
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   244
    // Test the Montgomery multiply intrinsic with a bunch of random
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   245
    // values of varying lengths.  Do this for long enough that the
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   246
    // caller of the intrinsic is C2-compiled.
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   247
    void testResultValues() throws Throwable {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   248
        // Test a couple of interesting edge cases.
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   249
        testOneLength(1024, 32);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   250
        testOneLength(1025, 34);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   251
        for (int j = 10; j > 0; j--) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   252
            // Construct a random prime whose length in words is even
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   253
            int lenInBits = rnd.nextInt(2048) + 64;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   254
            int lenInInts = (lenInBits + 63)/64*2;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   255
            testOneLength(lenInBits, lenInInts);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   256
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   257
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   258
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   259
    // Range checks
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   260
    void testOneMontgomeryMultiplyCheck(int[] a, int[] b, int[] n, int len, long inv,
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   261
                                        int[] product, Class klass) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   262
        try {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   263
            montgomeryMultiply(a, b, n, len, inv, product);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   264
        } catch (Throwable ex) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   265
            if (klass.isAssignableFrom(ex.getClass()))
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   266
                return;
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   267
            throw new RuntimeException(klass + " expected, " + ex + " was thrown");
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   268
        }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   269
        throw new RuntimeException(klass + " expected, was not thrown");
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   270
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   271
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   272
    void testOneMontgomeryMultiplyCheck(int[] a, int[] b, BigInteger n, int len, BigInteger inv,
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   273
            Class klass) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   274
        testOneMontgomeryMultiplyCheck(a, b, mag(n), len, inv.longValue(), null, klass);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   275
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   276
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   277
    void testOneMontgomeryMultiplyCheck(int[] a, int[] b, BigInteger n, int len, BigInteger inv,
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   278
            int[] product, Class klass) {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   279
        testOneMontgomeryMultiplyCheck(a, b, mag(n), len, inv.longValue(), product, klass);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   280
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   281
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   282
    void testMontgomeryMultiplyChecks() {
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   283
        int[] blah = random_val(40);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   284
        int[] small = random_val(39);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   285
        BigInteger mod = new BigInteger(40*32 , 2, rnd);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   286
        BigInteger r = BigInteger.ONE.shiftLeft(40*32);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   287
        BigInteger n_prime = mod.modInverse(r).negate();
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   288
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   289
        // Length out of range: square
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   290
        testOneMontgomeryMultiplyCheck(blah, blah, mod, 41, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   291
        testOneMontgomeryMultiplyCheck(blah, blah, mod, 0, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   292
        testOneMontgomeryMultiplyCheck(blah, blah, mod, -1, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   293
        // As above, but for multiply
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   294
        testOneMontgomeryMultiplyCheck(blah, blah.clone(), mod, 41, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   295
        testOneMontgomeryMultiplyCheck(blah, blah.clone(), mod, 0, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   296
        testOneMontgomeryMultiplyCheck(blah, blah.clone(), mod, 0, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   297
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   298
        // Length odd
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   299
        testOneMontgomeryMultiplyCheck(small, small, mod, 39, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   300
        testOneMontgomeryMultiplyCheck(small, small, mod, 0, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   301
        testOneMontgomeryMultiplyCheck(small, small, mod, -1, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   302
        // As above, but for multiply
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   303
        testOneMontgomeryMultiplyCheck(small, small.clone(), mod, 39, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   304
        testOneMontgomeryMultiplyCheck(small, small.clone(), mod, 0, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   305
        testOneMontgomeryMultiplyCheck(small, small.clone(), mod, -1, n_prime, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   306
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   307
        // array too small
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   308
        testOneMontgomeryMultiplyCheck(blah, blah, mod, 40, n_prime, small, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   309
        testOneMontgomeryMultiplyCheck(blah, blah.clone(), mod, 40, n_prime, small, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   310
        testOneMontgomeryMultiplyCheck(small, blah, mod, 40, n_prime, blah, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   311
        testOneMontgomeryMultiplyCheck(blah, small, mod, 40, n_prime, blah, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   312
        testOneMontgomeryMultiplyCheck(blah, blah, mod, 40, n_prime, small, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   313
        testOneMontgomeryMultiplyCheck(small, small, mod, 40, n_prime, blah, IllegalArgumentException.class);
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   314
    }
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   315
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   316
    public static void main(String args[]) {
43455
96560cffef4d 8166002: Emulate client build on platforms with reduced virtual address space
jcm
parents: 42650
diff changeset
   317
        if (!Platform.isServer() || Platform.isEmulatedClient()) {
96560cffef4d 8166002: Emulate client build on platforms with reduced virtual address space
jcm
parents: 42650
diff changeset
   318
            throw new Error("TESTBUG: Not server mode");
40067
db6c74a53556 8151280: update hotspot tests to use vm.compMode instead of their own logic
tpivovarova
parents: 40059
diff changeset
   319
        }
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 42307
diff changeset
   320
        if (wb.isIntrinsicAvailable(getExecutable(true), CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION) &&
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 42307
diff changeset
   321
                wb.isIntrinsicAvailable(getExecutable(false), CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION)) {
33473
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   322
            try {
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   323
                new MontgomeryMultiplyTest().testMontgomeryMultiplyChecks();
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   324
                new MontgomeryMultiplyTest().testResultValues();
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   325
            } catch (Throwable ex) {
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   326
                throw new RuntimeException(ex);
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   327
            }
31583
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   328
        }
33473
4511002b3632 8139907: compiler/intrinsics/montgomerymultiply/MontgomeryMultiplyTest.java fails with timeout
zmajo
parents: 31583
diff changeset
   329
    }
31583
eb5bea7b4835 8130150: Implement BigInteger.montgomeryMultiply intrinsic
aph
parents:
diff changeset
   330
}