1/*
2 * Copyright (c) 2016, 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 */
23package org.graalvm.compiler.hotspot.replacements;
24
25import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.arrayStart;
26
27import org.graalvm.compiler.api.directives.GraalDirectives;
28import org.graalvm.compiler.api.replacements.ClassSubstitution;
29import org.graalvm.compiler.api.replacements.MethodSubstitution;
30import org.graalvm.compiler.hotspot.HotSpotBackend;
31
32@ClassSubstitution(className = "java.math.BigInteger", optional = true)
33public class BigIntegerSubstitutions {
34
35    @MethodSubstitution(isStatic = false)
36    static int[] multiplyToLen(@SuppressWarnings("unused") Object receiver, int[] x, int xlen, int[] y, int ylen, int[] zIn) {
37        return multiplyToLenStatic(x, xlen, y, ylen, zIn);
38    }
39
40    @MethodSubstitution(isStatic = true)
41    static int[] multiplyToLenStatic(int[] x, int xlen, int[] y, int ylen, int[] zIn) {
42        int[] zResult = zIn;
43        int zLen;
44        if (zResult == null || zResult.length < (xlen + ylen)) {
45            zLen = xlen + ylen;
46            zResult = new int[xlen + ylen];
47        } else {
48            zLen = zIn.length;
49        }
50        HotSpotBackend.multiplyToLenStub(arrayStart(x), xlen, arrayStart(y), ylen, arrayStart(zResult), zLen);
51        return zResult;
52    }
53
54    @MethodSubstitution(isStatic = true)
55    static int mulAdd(int[] out, int[] in, int offset, int len, int k) {
56        int[] outNonNull = GraalDirectives.guardingNonNull(out);
57        int newOffset = outNonNull.length - offset;
58        return HotSpotBackend.mulAddStub(arrayStart(outNonNull), arrayStart(in), newOffset, len, k);
59    }
60
61    @MethodSubstitution(isStatic = true)
62    static int implMulAdd(int[] out, int[] in, int offset, int len, int k) {
63        int[] outNonNull = GraalDirectives.guardingNonNull(out);
64        int newOffset = outNonNull.length - offset;
65        return HotSpotBackend.mulAddStub(arrayStart(outNonNull), arrayStart(in), newOffset, len, k);
66    }
67
68    @MethodSubstitution(isStatic = true)
69    static int[] implMontgomeryMultiply(int[] a, int[] b, int[] n, int len, long inv, int[] product) {
70        HotSpotBackend.implMontgomeryMultiply(arrayStart(a), arrayStart(b), arrayStart(n), len, inv, arrayStart(product));
71        return product;
72
73    }
74
75    @MethodSubstitution(isStatic = true)
76    static int[] implMontgomerySquare(int[] a, int[] n, int len, long inv, int[] product) {
77        HotSpotBackend.implMontgomerySquare(arrayStart(a), arrayStart(n), len, inv, arrayStart(product));
78        return product;
79    }
80
81    @MethodSubstitution(isStatic = true)
82    static int[] implSquareToLen(int[] x, int len, int[] z, int zLen) {
83        HotSpotBackend.implSquareToLen(arrayStart(x), len, arrayStart(z), zLen);
84        return z;
85    }
86
87}
88