src/share/classes/java/math/BigInteger.java
Print this page
*** 29,38 ****
--- 29,39 ----
package java.math;
import java.util.Random;
import java.io.*;
+ import java.util.Arrays;
/**
* 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
*** 1610,1627 ****
primitiveLeftShift(a, len, nBits);
return a;
} else { // Array must be resized
if (nBits <= (32-bitsInHighWord)) {
int result[] = new int[nInts+len];
! for (int i=0; i<len; i++)
! result[i] = a[i];
primitiveLeftShift(result, result.length, nBits);
return result;
} else {
int result[] = new int[nInts+len+1];
! for (int i=0; i<len; i++)
! result[i] = a[i];
primitiveRightShift(result, result.length, 32 - nBits);
return result;
}
}
}
--- 1611,1626 ----
primitiveLeftShift(a, len, nBits);
return a;
} else { // Array must be resized
if (nBits <= (32-bitsInHighWord)) {
int result[] = new int[nInts+len];
! System.arraycopy(a, 0, result, 0, len);
primitiveLeftShift(result, result.length, nBits);
return result;
} else {
int result[] = new int[nInts+len+1];
! System.arraycopy(a, 0, result, 0, len);
primitiveRightShift(result, result.length, 32 - nBits);
return result;
}
}
}
*** 1905,1917 ****
// Set b to the square of the base
int[] b = squareToLen(table[0], modLen, null);
b = montReduce(b, mod, modLen, inv);
// Set t to high half of b
! int[] t = new int[modLen];
! for(int i=0; i<modLen; i++)
! t[i] = b[i];
// Fill in the table with odd powers of the base
for (int i=1; i<tblmask; i++) {
int[] prod = multiplyToLen(t, modLen, table[i-1], modLen, null);
table[i] = montReduce(prod, mod, modLen, inv);
--- 1904,1914 ----
// Set b to the square of the base
int[] b = squareToLen(table[0], modLen, null);
b = montReduce(b, mod, modLen, inv);
// Set t to high half of b
! int[] t = Arrays.copyOf(b, modLen);
// Fill in the table with odd powers of the base
for (int i=1; i<tblmask; i++) {
int[] prod = multiplyToLen(t, modLen, table[i-1], modLen, null);
table[i] = montReduce(prod, mod, modLen, inv);
*** 2004,2021 ****
}
}
// Convert result out of Montgomery form and return
int[] t2 = new int[2*modLen];
! for(int i=0; i<modLen; i++)
! t2[i+modLen] = b[i];
b = montReduce(t2, mod, modLen, inv);
! t2 = new int[modLen];
! for(int i=0; i<modLen; i++)
! t2[i] = b[i];
return new BigInteger(1, t2);
}
/**
--- 2001,2015 ----
}
}
// Convert result out of Montgomery form and return
int[] t2 = new int[2*modLen];
! System.arraycopy(b, 0, t2, modLen, modLen);
b = montReduce(t2, mod, modLen, inv);
! t2 = Arrays.copyOf(b, modLen);
return new BigInteger(1, t2);
}
/**
*** 2152,2163 ****
return this;
// Copy remaining ints of mag
int numInts = (p + 31) >>> 5;
int[] mag = new int[numInts];
! for (int i=0; i<numInts; i++)
! mag[i] = this.mag[i + (this.mag.length - numInts)];
// Mask out any excess bits
int excessBits = (numInts << 5) - p;
mag[0] &= (1L << (32-excessBits)) - 1;
--- 2146,2156 ----
return this;
// Copy remaining ints of mag
int numInts = (p + 31) >>> 5;
int[] mag = new int[numInts];
! System.arraycopy(this.mag, (this.mag.length - numInts), mag, 0, numInts);
// Mask out any excess bits
int excessBits = (numInts << 5) - p;
mag[0] &= (1L << (32-excessBits)) - 1;
*** 2219,2229 ****
throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported.");
} else {
return shiftRight(-n);
}
}
! int[] newMag = shiftLeft(mag,n);
return new BigInteger(newMag, signum);
}
private static int[] shiftLeft(int[] mag, int n) {
--- 2212,2222 ----
throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported.");
} else {
return shiftRight(-n);
}
}
! int[] newMag = shiftLeft(mag, n);
return new BigInteger(newMag, signum);
}
private static int[] shiftLeft(int[] mag, int n) {
*** 2232,2243 ****
int magLen = mag.length;
int newMag[] = null;
if (nBits == 0) {
newMag = new int[magLen + nInts];
! for (int i=0; i<magLen; i++)
! newMag[i] = mag[i];
} else {
int i = 0;
int nBits2 = 32 - nBits;
int highBits = mag[0] >>> nBits2;
if (highBits != 0) {
--- 2225,2235 ----
int magLen = mag.length;
int newMag[] = null;
if (nBits == 0) {
newMag = new int[magLen + nInts];
! System.arraycopy(mag, 0, newMag, 0, magLen);
} else {
int i = 0;
int nBits2 = 32 - nBits;
int highBits = mag[0] >>> nBits2;
if (highBits != 0) {
*** 2286,2298 ****
if (nInts >= magLen)
return (signum >= 0 ? ZERO : negConst[1]);
if (nBits == 0) {
int newMagLen = magLen - nInts;
! newMag = new int[newMagLen];
! for (int i=0; i<newMagLen; i++)
! newMag[i] = mag[i];
} else {
int i = 0;
int highBits = mag[0] >>> nBits;
if (highBits != 0) {
newMag = new int[magLen - nInts];
--- 2278,2288 ----
if (nInts >= magLen)
return (signum >= 0 ? ZERO : negConst[1]);
if (nBits == 0) {
int newMagLen = magLen - nInts;
! newMag = Arrays.copyOf(mag, newMagLen);
} else {
int i = 0;
int highBits = mag[0] >>> nBits;
if (highBits != 0) {
newMag = new int[magLen - nInts];
*** 2559,2569 ****
// Calculate the bit length of the magnitude
int magBitLength = ((len - 1) << 5) + bitLengthForInt(mag[0]);
if (signum < 0) {
// Check if magnitude is a power of two
boolean pow2 = (Integer.bitCount(mag[0]) == 1);
! for(int i=1; i< len && pow2; i++)
pow2 = (mag[i] == 0);
n = (pow2 ? magBitLength -1 : magBitLength);
} else {
n = magBitLength;
--- 2549,2559 ----
// Calculate the bit length of the magnitude
int magBitLength = ((len - 1) << 5) + bitLengthForInt(mag[0]);
if (signum < 0) {
// Check if magnitude is a power of two
boolean pow2 = (Integer.bitCount(mag[0]) == 1);
! for (int i=1; i< len && pow2; i++)
pow2 = (mag[i] == 0);
n = (pow2 ? magBitLength -1 : magBitLength);
} else {
n = magBitLength;