src/java.base/share/classes/java/math/BigInteger.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8076112 Cdiff src/java.base/share/classes/java/math/BigInteger.java

src/java.base/share/classes/java/math/BigInteger.java

Print this page

        

*** 32,45 **** --- 32,48 ---- import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.ObjectStreamField; import java.util.Arrays; + import java.util.Objects; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; + import sun.misc.DoubleConsts; import sun.misc.FloatConsts; + import jdk.internal.HotSpotIntrinsicCandidate; /** * Immutable arbitrary-precision integers. All operations behave as if * BigIntegers were represented in two's-complement notation (like Java's * primitive integer types). BigInteger provides analogues to all of Java's
*** 1638,1647 **** --- 1641,1657 ---- /** * Multiplies int arrays x and y to the specified lengths and places * the result into z. There will be no leading zeros in the resultant array. */ private int[] multiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z) { + multiplyToLenCheck(x, xlen); + multiplyToLenCheck(y, ylen); + return implMultiplyToLen(x, xlen, y, ylen, z); + } + + @HotSpotIntrinsicCandidate + private int[] implMultiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z) { int xstart = xlen - 1; int ystart = ylen - 1; if (z == null || z.length < (xlen+ ylen)) z = new int[xlen+ylen];
*** 1667,1676 **** --- 1677,1698 ---- z[i] = (int)carry; } return z; } + private static void multiplyToLenCheck(int[] array, int length) { + if (length <= 0) { + return; // not an error because multiplyToLen won't execute if len <= 0 + } + + Objects.requireNonNull(array); + + if (length > array.length) { + throw new ArrayIndexOutOfBoundsException(length - 1); + } + } + /** * Multiplies two BigIntegers using the Karatsuba multiplication * algorithm. This is a recursive divide-and-conquer algorithm which is * more efficient for large numbers than what is commonly called the * "grade-school" algorithm used in multiplyToLen. If the numbers to be
*** 1997,2006 **** --- 2019,2029 ---- } /** * Java Runtime may use intrinsic for this method. */ + @HotSpotIntrinsicCandidate private static final int[] implSquareToLen(int[] x, int len, int[] z, int zlen) { /* * The algorithm used here is adapted from Colin Plumb's C library. * Technique: Consider the partial products in the multiplication * of "abcde" by itself:
*** 2914,2923 **** --- 2937,2947 ---- } /** * Java Runtime may use intrinsic for this method. */ + @HotSpotIntrinsicCandidate private static int implMulAdd(int[] out, int[] in, int offset, int len, int k) { long kLong = k & LONG_MASK; long carry = 0; offset = out.length-offset - 1;
src/java.base/share/classes/java/math/BigInteger.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File