mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-23 20:44:41 +02:00
8020641: Clean up some code style in recent BigInteger contributions
Some minor cleanup to adhere better to Java coding conventions. Reviewed-by: darcy
This commit is contained in:
parent
d59c1fac00
commit
4fe69c432f
3 changed files with 281 additions and 255 deletions
|
@ -372,8 +372,10 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
|
||||
// Skip leading zeros and compute number of digits in magnitude
|
||||
while (cursor < len &&
|
||||
Character.digit(val.charAt(cursor), radix) == 0)
|
||||
Character.digit(val.charAt(cursor), radix) == 0) {
|
||||
cursor++;
|
||||
}
|
||||
|
||||
if (cursor == len) {
|
||||
signum = 0;
|
||||
mag = ZERO.mag;
|
||||
|
@ -1074,8 +1076,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
powerCache = new BigInteger[Character.MAX_RADIX+1][];
|
||||
logCache = new double[Character.MAX_RADIX+1];
|
||||
|
||||
for (int i=Character.MIN_RADIX; i<=Character.MAX_RADIX; i++)
|
||||
{
|
||||
for (int i=Character.MIN_RADIX; i <= Character.MAX_RADIX; i++) {
|
||||
powerCache[i] = new BigInteger[] { BigInteger.valueOf(i) };
|
||||
logCache[i] = Math.log(i);
|
||||
}
|
||||
|
@ -1304,7 +1305,6 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
result[bigIndex] = (int)difference;
|
||||
}
|
||||
|
||||
|
||||
// Subtract remainder of longer number while borrow propagates
|
||||
boolean borrow = (difference >> 32 != 0);
|
||||
while (bigIndex > 0 && borrow)
|
||||
|
@ -1385,8 +1385,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
int xlen = mag.length;
|
||||
int ylen = val.mag.length;
|
||||
|
||||
if ((xlen < KARATSUBA_THRESHOLD) || (ylen < KARATSUBA_THRESHOLD))
|
||||
{
|
||||
if ((xlen < KARATSUBA_THRESHOLD) || (ylen < KARATSUBA_THRESHOLD)) {
|
||||
int resultSign = signum == val.signum ? 1 : -1;
|
||||
if (val.mag.length == 1) {
|
||||
return multiplyByInt(mag,val.mag[0], resultSign);
|
||||
|
@ -1398,13 +1397,14 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
val.mag, ylen, null);
|
||||
result = trustedStripLeadingZeroInts(result);
|
||||
return new BigInteger(result, resultSign);
|
||||
}
|
||||
else
|
||||
if ((xlen < TOOM_COOK_THRESHOLD) && (ylen < TOOM_COOK_THRESHOLD))
|
||||
} else {
|
||||
if ((xlen < TOOM_COOK_THRESHOLD) && (ylen < TOOM_COOK_THRESHOLD)) {
|
||||
return multiplyKaratsuba(this, val);
|
||||
else
|
||||
} else {
|
||||
return multiplyToomCook3(this, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static BigInteger multiplyByInt(int[] x, int y, int sign) {
|
||||
if (Integer.bitCount(y) == 1) {
|
||||
|
@ -1519,8 +1519,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
*
|
||||
* See: http://en.wikipedia.org/wiki/Karatsuba_algorithm
|
||||
*/
|
||||
private static BigInteger multiplyKaratsuba(BigInteger x, BigInteger y)
|
||||
{
|
||||
private static BigInteger multiplyKaratsuba(BigInteger x, BigInteger y) {
|
||||
int xlen = x.mag.length;
|
||||
int ylen = y.mag.length;
|
||||
|
||||
|
@ -1543,11 +1542,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
// result = p1 * 2^(32*2*half) + (p3 - p1 - p2) * 2^(32*half) + p2
|
||||
BigInteger result = p1.shiftLeft(32*half).add(p3.subtract(p1).subtract(p2)).shiftLeft(32*half).add(p2);
|
||||
|
||||
if (x.signum != y.signum)
|
||||
if (x.signum != y.signum) {
|
||||
return result.negate();
|
||||
else
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies two BigIntegers using a 3-way Toom-Cook multiplication
|
||||
|
@ -1577,8 +1577,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* LNCS #4547. Springer, Madrid, Spain, June 21-22, 2007.
|
||||
*
|
||||
*/
|
||||
private static BigInteger multiplyToomCook3(BigInteger a, BigInteger b)
|
||||
{
|
||||
private static BigInteger multiplyToomCook3(BigInteger a, BigInteger b) {
|
||||
int alen = a.mag.length;
|
||||
int blen = b.mag.length;
|
||||
|
||||
|
@ -1613,12 +1612,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
db1.add(b2).shiftLeft(1).subtract(b0));
|
||||
vinf = a2.multiply(b2);
|
||||
|
||||
/* The algorithm requires two divisions by 2 and one by 3.
|
||||
All divisions are known to be exact, that is, they do not produce
|
||||
remainders, and all results are positive. The divisions by 2 are
|
||||
implemented as right shifts which are relatively efficient, leaving
|
||||
only an exact division by 3, which is done by a specialized
|
||||
linear-time algorithm. */
|
||||
// The algorithm requires two divisions by 2 and one by 3.
|
||||
// All divisions are known to be exact, that is, they do not produce
|
||||
// remainders, and all results are positive. The divisions by 2 are
|
||||
// implemented as right shifts which are relatively efficient, leaving
|
||||
// only an exact division by 3, which is done by a specialized
|
||||
// linear-time algorithm.
|
||||
t2 = v2.subtract(vm1).exactDivideBy3();
|
||||
tm1 = v1.subtract(vm1).shiftRight(1);
|
||||
t1 = v1.subtract(v0);
|
||||
|
@ -1632,11 +1631,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
|
||||
BigInteger result = vinf.shiftLeft(ss).add(t2).shiftLeft(ss).add(t1).shiftLeft(ss).add(tm1).shiftLeft(ss).add(v0);
|
||||
|
||||
if (a.signum != b.signum)
|
||||
if (a.signum != b.signum) {
|
||||
return result.negate();
|
||||
else
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -1653,38 +1653,38 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* numbers.
|
||||
*/
|
||||
private BigInteger getToomSlice(int lowerSize, int upperSize, int slice,
|
||||
int fullsize)
|
||||
{
|
||||
int fullsize) {
|
||||
int start, end, sliceSize, len, offset;
|
||||
|
||||
len = mag.length;
|
||||
offset = fullsize - len;
|
||||
|
||||
if (slice == 0)
|
||||
{
|
||||
if (slice == 0) {
|
||||
start = 0 - offset;
|
||||
end = upperSize - 1 - offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
start = upperSize + (slice-1)*lowerSize - offset;
|
||||
end = start + lowerSize - 1;
|
||||
}
|
||||
|
||||
if (start < 0)
|
||||
if (start < 0) {
|
||||
start = 0;
|
||||
if (end < 0)
|
||||
}
|
||||
if (end < 0) {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
sliceSize = (end-start) + 1;
|
||||
|
||||
if (sliceSize <= 0)
|
||||
if (sliceSize <= 0) {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
// While performing Toom-Cook, all slices are positive and
|
||||
// the sign is adjusted when the final number is composed.
|
||||
if (start==0 && sliceSize >= len)
|
||||
if (start == 0 && sliceSize >= len) {
|
||||
return this.abs();
|
||||
}
|
||||
|
||||
int intSlice[] = new int[sliceSize];
|
||||
System.arraycopy(mag, start, intSlice, 0, sliceSize);
|
||||
|
@ -1700,20 +1700,19 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* undefined. Note that this is expected to be called with positive
|
||||
* arguments only.
|
||||
*/
|
||||
private BigInteger exactDivideBy3()
|
||||
{
|
||||
private BigInteger exactDivideBy3() {
|
||||
int len = mag.length;
|
||||
int[] result = new int[len];
|
||||
long x, w, q, borrow;
|
||||
borrow = 0L;
|
||||
for (int i=len-1; i>=0; i--)
|
||||
{
|
||||
for (int i=len-1; i >= 0; i--) {
|
||||
x = (mag[i] & LONG_MASK);
|
||||
w = x - borrow;
|
||||
if (borrow > x) // Did we make the number go negative?
|
||||
if (borrow > x) { // Did we make the number go negative?
|
||||
borrow = 1L;
|
||||
else
|
||||
} else {
|
||||
borrow = 0L;
|
||||
}
|
||||
|
||||
// 0xAAAAAAAB is the modular inverse of 3 (mod 2^32). Thus,
|
||||
// the effect of this is to divide by 3 (mod 2^32).
|
||||
|
@ -1723,8 +1722,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
|
||||
// Now check the borrow. The second check can of course be
|
||||
// eliminated if the first fails.
|
||||
if (q >= 0x55555556L)
|
||||
{
|
||||
if (q >= 0x55555556L) {
|
||||
borrow++;
|
||||
if (q >= 0xAAAAAAABL)
|
||||
borrow++;
|
||||
|
@ -1741,8 +1739,9 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
private BigInteger getLower(int n) {
|
||||
int len = mag.length;
|
||||
|
||||
if (len <= n)
|
||||
if (len <= n) {
|
||||
return this;
|
||||
}
|
||||
|
||||
int lowerInts[] = new int[n];
|
||||
System.arraycopy(mag, len-n, lowerInts, 0, n);
|
||||
|
@ -1758,8 +1757,9 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
private BigInteger getUpper(int n) {
|
||||
int len = mag.length;
|
||||
|
||||
if (len <= n)
|
||||
if (len <= n) {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
int upperLen = len - n;
|
||||
int upperInts[] = new int[upperLen];
|
||||
|
@ -1776,21 +1776,22 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* @return {@code this<sup>2</sup>}
|
||||
*/
|
||||
private BigInteger square() {
|
||||
if (signum == 0)
|
||||
if (signum == 0) {
|
||||
return ZERO;
|
||||
}
|
||||
int len = mag.length;
|
||||
|
||||
if (len < KARATSUBA_SQUARE_THRESHOLD)
|
||||
{
|
||||
if (len < KARATSUBA_SQUARE_THRESHOLD) {
|
||||
int[] z = squareToLen(mag, len, null);
|
||||
return new BigInteger(trustedStripLeadingZeroInts(z), 1);
|
||||
}
|
||||
else
|
||||
if (len < TOOM_COOK_SQUARE_THRESHOLD)
|
||||
} else {
|
||||
if (len < TOOM_COOK_SQUARE_THRESHOLD) {
|
||||
return squareKaratsuba();
|
||||
else
|
||||
} else {
|
||||
return squareToomCook3();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Squares the contents of the int array x. The result is placed into the
|
||||
|
@ -1866,8 +1867,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* has better asymptotic performance than the algorithm used in
|
||||
* squareToLen.
|
||||
*/
|
||||
private BigInteger squareKaratsuba()
|
||||
{
|
||||
private BigInteger squareKaratsuba() {
|
||||
int half = (mag.length+1) / 2;
|
||||
|
||||
BigInteger xl = getLower(half);
|
||||
|
@ -1887,8 +1887,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* that has better asymptotic performance than the algorithm used in
|
||||
* squareToLen or squareKaratsuba.
|
||||
*/
|
||||
private BigInteger squareToomCook3()
|
||||
{
|
||||
private BigInteger squareToomCook3() {
|
||||
int len = mag.length;
|
||||
|
||||
// k is the size (in ints) of the lower-order slices.
|
||||
|
@ -1913,13 +1912,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
vinf = a2.square();
|
||||
v2 = da1.add(a2).shiftLeft(1).subtract(a0).square();
|
||||
|
||||
/* The algorithm requires two divisions by 2 and one by 3.
|
||||
All divisions are known to be exact, that is, they do not produce
|
||||
remainders, and all results are positive. The divisions by 2 are
|
||||
implemented as right shifts which are relatively efficient, leaving
|
||||
only a division by 3.
|
||||
The division by 3 is done by an optimized algorithm for this case.
|
||||
*/
|
||||
// The algorithm requires two divisions by 2 and one by 3.
|
||||
// All divisions are known to be exact, that is, they do not produce
|
||||
// remainders, and all results are positive. The divisions by 2 are
|
||||
// implemented as right shifts which are relatively efficient, leaving
|
||||
// only a division by 3.
|
||||
// The division by 3 is done by an optimized algorithm for this case.
|
||||
t2 = v2.subtract(vm1).exactDivideBy3();
|
||||
tm1 = v1.subtract(vm1).shiftRight(1);
|
||||
t1 = v1.subtract(v0);
|
||||
|
@ -1944,11 +1942,13 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* @throws ArithmeticException if {@code val} is zero.
|
||||
*/
|
||||
public BigInteger divide(BigInteger val) {
|
||||
if (mag.length<BURNIKEL_ZIEGLER_THRESHOLD || val.mag.length<BURNIKEL_ZIEGLER_THRESHOLD)
|
||||
if (mag.length < BURNIKEL_ZIEGLER_THRESHOLD ||
|
||||
val.mag.length < BURNIKEL_ZIEGLER_THRESHOLD) {
|
||||
return divideKnuth(val);
|
||||
else
|
||||
} else {
|
||||
return divideBurnikelZiegler(val);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BigInteger whose value is {@code (this / val)} using an O(n^2) algorithm from Knuth.
|
||||
|
@ -1979,11 +1979,13 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* @throws ArithmeticException if {@code val} is zero.
|
||||
*/
|
||||
public BigInteger[] divideAndRemainder(BigInteger val) {
|
||||
if (mag.length<BURNIKEL_ZIEGLER_THRESHOLD || val.mag.length<BURNIKEL_ZIEGLER_THRESHOLD)
|
||||
if (mag.length < BURNIKEL_ZIEGLER_THRESHOLD ||
|
||||
val.mag.length < BURNIKEL_ZIEGLER_THRESHOLD) {
|
||||
return divideAndRemainderKnuth(val);
|
||||
else
|
||||
} else {
|
||||
return divideAndRemainderBurnikelZiegler(val);
|
||||
}
|
||||
}
|
||||
|
||||
/** Long division */
|
||||
private BigInteger[] divideAndRemainderKnuth(BigInteger val) {
|
||||
|
@ -2006,11 +2008,13 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* @throws ArithmeticException if {@code val} is zero.
|
||||
*/
|
||||
public BigInteger remainder(BigInteger val) {
|
||||
if (mag.length<BURNIKEL_ZIEGLER_THRESHOLD || val.mag.length<BURNIKEL_ZIEGLER_THRESHOLD)
|
||||
if (mag.length < BURNIKEL_ZIEGLER_THRESHOLD ||
|
||||
val.mag.length < BURNIKEL_ZIEGLER_THRESHOLD) {
|
||||
return remainderKnuth(val);
|
||||
else
|
||||
} else {
|
||||
return remainderBurnikelZiegler(val);
|
||||
}
|
||||
}
|
||||
|
||||
/** Long division */
|
||||
private BigInteger remainderKnuth(BigInteger val) {
|
||||
|
@ -2063,10 +2067,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* cause the operation to yield a non-integer value.)
|
||||
*/
|
||||
public BigInteger pow(int exponent) {
|
||||
if (exponent < 0)
|
||||
if (exponent < 0) {
|
||||
throw new ArithmeticException("Negative exponent");
|
||||
if (signum==0)
|
||||
}
|
||||
if (signum == 0) {
|
||||
return (exponent == 0 ? ONE : this);
|
||||
}
|
||||
|
||||
BigInteger partToSquare = this.abs();
|
||||
|
||||
|
@ -2079,25 +2085,26 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
int remainingBits;
|
||||
|
||||
// Factor the powers of two out quickly by shifting right, if needed.
|
||||
if (powersOfTwo > 0)
|
||||
{
|
||||
if (powersOfTwo > 0) {
|
||||
partToSquare = partToSquare.shiftRight(powersOfTwo);
|
||||
remainingBits = partToSquare.bitLength();
|
||||
if (remainingBits == 1) // Nothing left but +/- 1?
|
||||
if (signum<0 && (exponent&1)==1)
|
||||
if (remainingBits == 1) { // Nothing left but +/- 1?
|
||||
if (signum < 0 && (exponent&1) == 1) {
|
||||
return NEGATIVE_ONE.shiftLeft(powersOfTwo*exponent);
|
||||
else
|
||||
} else {
|
||||
return ONE.shiftLeft(powersOfTwo*exponent);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
} else {
|
||||
remainingBits = partToSquare.bitLength();
|
||||
if (remainingBits == 1) // Nothing left but +/- 1?
|
||||
if (signum<0 && (exponent&1)==1)
|
||||
if (remainingBits == 1) { // Nothing left but +/- 1?
|
||||
if (signum < 0 && (exponent&1) == 1) {
|
||||
return NEGATIVE_ONE;
|
||||
else
|
||||
} else {
|
||||
return ONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This is a quick way to approximate the size of the result,
|
||||
// similar to doing log2[n] * exponent. This will give an upper bound
|
||||
|
@ -2106,8 +2113,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
|
||||
// Use slightly different algorithms for small and large operands.
|
||||
// See if the result will safely fit into a long. (Largest 2^63-1)
|
||||
if (partToSquare.mag.length==1 && scaleFactor <= 62)
|
||||
{
|
||||
if (partToSquare.mag.length == 1 && scaleFactor <= 62) {
|
||||
// Small number algorithm. Everything fits into a long.
|
||||
int newSign = (signum <0 && (exponent&1) == 1 ? -1 : 1);
|
||||
long result = 1;
|
||||
|
@ -2117,27 +2123,28 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
|
||||
// Perform exponentiation using repeated squaring trick
|
||||
while (workingExponent != 0) {
|
||||
if ((workingExponent & 1)==1)
|
||||
if ((workingExponent & 1) == 1) {
|
||||
result = result * baseToPow2;
|
||||
}
|
||||
|
||||
if ((workingExponent >>>= 1) != 0)
|
||||
if ((workingExponent >>>= 1) != 0) {
|
||||
baseToPow2 = baseToPow2 * baseToPow2;
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply back the powers of two (quickly, by shifting left)
|
||||
if (powersOfTwo > 0)
|
||||
{
|
||||
if (powersOfTwo > 0) {
|
||||
int bitsToShift = powersOfTwo*exponent;
|
||||
if (bitsToShift + scaleFactor <= 62) // Fits in long?
|
||||
if (bitsToShift + scaleFactor <= 62) { // Fits in long?
|
||||
return valueOf((result << bitsToShift) * newSign);
|
||||
else
|
||||
} else {
|
||||
return valueOf(result*newSign).shiftLeft(bitsToShift);
|
||||
}
|
||||
else
|
||||
}
|
||||
else {
|
||||
return valueOf(result*newSign);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Large number algorithm. This is basically identical to
|
||||
// the algorithm above, but calls multiply() and square()
|
||||
// which may use more efficient algorithms for large numbers.
|
||||
|
@ -2146,23 +2153,27 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
int workingExponent = exponent;
|
||||
// Perform exponentiation using repeated squaring trick
|
||||
while (workingExponent != 0) {
|
||||
if ((workingExponent & 1)==1)
|
||||
if ((workingExponent & 1) == 1) {
|
||||
answer = answer.multiply(partToSquare);
|
||||
}
|
||||
|
||||
if ((workingExponent >>>= 1) != 0)
|
||||
if ((workingExponent >>>= 1) != 0) {
|
||||
partToSquare = partToSquare.square();
|
||||
}
|
||||
}
|
||||
// Multiply back the (exponentiated) powers of two (quickly,
|
||||
// by shifting left)
|
||||
if (powersOfTwo > 0)
|
||||
if (powersOfTwo > 0) {
|
||||
answer = answer.shiftLeft(powersOfTwo*exponent);
|
||||
}
|
||||
|
||||
if (signum<0 && (exponent&1)==1)
|
||||
if (signum < 0 && (exponent&1) == 1) {
|
||||
return answer.negate();
|
||||
else
|
||||
} else {
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BigInteger whose value is the greatest common divisor of
|
||||
|
@ -3427,8 +3438,9 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
|
||||
/** This method is used to perform toString when arguments are small. */
|
||||
private String smallToString(int radix) {
|
||||
if (signum == 0)
|
||||
if (signum == 0) {
|
||||
return "0";
|
||||
}
|
||||
|
||||
// Compute upper bound on number of digit groups and allocate space
|
||||
int maxNumDigitGroups = (4*mag.length + 6)/7;
|
||||
|
@ -3453,16 +3465,18 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
|
||||
// Put sign (if any) and first digit group into result buffer
|
||||
StringBuilder buf = new StringBuilder(numGroups*digitsPerLong[radix]+1);
|
||||
if (signum<0)
|
||||
if (signum < 0) {
|
||||
buf.append('-');
|
||||
}
|
||||
buf.append(digitGroup[numGroups-1]);
|
||||
|
||||
// Append remaining digit groups padded with leading zeros
|
||||
for (int i=numGroups-2; i >= 0; i--) {
|
||||
// Prepend (any) leading zeros for this digit group
|
||||
int numLeadingZeros = digitsPerLong[radix]-digitGroup[i].length();
|
||||
if (numLeadingZeros != 0)
|
||||
if (numLeadingZeros != 0) {
|
||||
buf.append(zeros[numLeadingZeros]);
|
||||
}
|
||||
buf.append(digitGroup[i]);
|
||||
}
|
||||
return buf.toString();
|
||||
|
@ -3490,9 +3504,11 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
|
||||
// Pad with internal zeros if necessary.
|
||||
// Don't pad if we're at the beginning of the string.
|
||||
if ((s.length() < digits) && (sb.length() > 0))
|
||||
for (int i=s.length(); i<digits; i++) // May be a faster way to
|
||||
if ((s.length() < digits) && (sb.length() > 0)) {
|
||||
for (int i=s.length(); i < digits; i++) { // May be a faster way to
|
||||
sb.append('0'); // do this?
|
||||
}
|
||||
}
|
||||
|
||||
sb.append(s);
|
||||
return;
|
||||
|
|
|
@ -523,11 +523,12 @@ class MutableBigInteger {
|
|||
* Like {@link #rightShift(int)} but {@code n} can be greater than the length of the number.
|
||||
*/
|
||||
void safeRightShift(int n) {
|
||||
if (n/32 >= intLen)
|
||||
if (n/32 >= intLen) {
|
||||
reset();
|
||||
else
|
||||
} else {
|
||||
rightShift(n);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Right shift this MutableBigInteger n bits. The MutableBigInteger is left
|
||||
|
@ -554,9 +555,10 @@ class MutableBigInteger {
|
|||
* Like {@link #leftShift(int)} but {@code n} can be zero.
|
||||
*/
|
||||
void safeLeftShift(int n) {
|
||||
if (n > 0)
|
||||
if (n > 0) {
|
||||
leftShift(n);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Left shift this MutableBigInteger n bits.
|
||||
|
@ -703,11 +705,11 @@ class MutableBigInteger {
|
|||
* low ints of this number.
|
||||
*/
|
||||
private BigInteger getLower(int n) {
|
||||
if (isZero())
|
||||
if (isZero()) {
|
||||
return BigInteger.ZERO;
|
||||
else if (intLen < n)
|
||||
} else if (intLen < n) {
|
||||
return toBigInteger(1);
|
||||
else {
|
||||
} else {
|
||||
// strip zeros
|
||||
int len = n;
|
||||
while (len > 0 && value[offset+intLen-len] == 0)
|
||||
|
@ -788,12 +790,13 @@ class MutableBigInteger {
|
|||
|
||||
/**
|
||||
* Adds the value of {@code addend} shifted {@code n} ints to the left.
|
||||
* Has the same effect as {@code addend.leftShift(32*ints); add(b);}
|
||||
* but doesn't change the value of {@code b}.
|
||||
* Has the same effect as {@code addend.leftShift(32*ints); add(addend);}
|
||||
* but doesn't change the value of {@code addend}.
|
||||
*/
|
||||
void addShifted(MutableBigInteger addend, int n) {
|
||||
if (addend.isZero())
|
||||
if (addend.isZero()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int x = intLen;
|
||||
int y = addend.intLen + n;
|
||||
|
@ -817,8 +820,9 @@ class MutableBigInteger {
|
|||
// Add remainder of the longer number
|
||||
while (x > 0) {
|
||||
x--;
|
||||
if (carry == 0 && result == value && rstart == (x + offset))
|
||||
if (carry == 0 && result == value && rstart == (x + offset)) {
|
||||
return;
|
||||
}
|
||||
sum = (value[x+offset] & LONG_MASK) + carry;
|
||||
result[rstart--] = (int)sum;
|
||||
carry = sum >>> 32;
|
||||
|
@ -1144,11 +1148,13 @@ class MutableBigInteger {
|
|||
}
|
||||
|
||||
MutableBigInteger divide(MutableBigInteger b, MutableBigInteger quotient, boolean needRemainder) {
|
||||
if (intLen<BigInteger.BURNIKEL_ZIEGLER_THRESHOLD || b.intLen<BigInteger.BURNIKEL_ZIEGLER_THRESHOLD)
|
||||
if (intLen < BigInteger.BURNIKEL_ZIEGLER_THRESHOLD ||
|
||||
b.intLen < BigInteger.BURNIKEL_ZIEGLER_THRESHOLD) {
|
||||
return divideKnuth(b, quotient, needRemainder);
|
||||
else
|
||||
} else {
|
||||
return divideAndRemainderBurnikelZiegler(b, quotient);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #divideKnuth(MutableBigInteger, MutableBigInteger, boolean)
|
||||
|
@ -1236,9 +1242,9 @@ class MutableBigInteger {
|
|||
int r = intLen;
|
||||
int s = b.intLen;
|
||||
|
||||
if (r < s)
|
||||
if (r < s) {
|
||||
return this;
|
||||
else {
|
||||
} else {
|
||||
// Unlike Knuth division, we don't check for common powers of two here because
|
||||
// BZ already runs faster if both numbers contain powers of two and cancelling them has no
|
||||
// additional benefit.
|
||||
|
@ -1256,8 +1262,9 @@ class MutableBigInteger {
|
|||
|
||||
// step 5: t is the number of blocks needed to accommodate this plus one additional bit
|
||||
int t = (bitLength()+n32) / n32;
|
||||
if (t < 2)
|
||||
if (t < 2) {
|
||||
t = 2;
|
||||
}
|
||||
|
||||
// step 6: conceptually split this into blocks a[t-1], ..., a[0]
|
||||
MutableBigInteger a1 = getBlock(t-1, t, n); // the most significant block of this
|
||||
|
@ -1302,8 +1309,9 @@ class MutableBigInteger {
|
|||
int n = b.intLen;
|
||||
|
||||
// step 1: base case
|
||||
if (n%2!=0 || n<BigInteger.BURNIKEL_ZIEGLER_THRESHOLD)
|
||||
if (n%2 != 0 || n < BigInteger.BURNIKEL_ZIEGLER_THRESHOLD) {
|
||||
return divideKnuth(b, quotient);
|
||||
}
|
||||
|
||||
// step 2: view this as [a1,a2,a3,a4] where each ai is n/2 ints or less
|
||||
MutableBigInteger aUpper = new MutableBigInteger(this);
|
||||
|
@ -1352,8 +1360,7 @@ class MutableBigInteger {
|
|||
|
||||
// step 4: d=quotient*b2
|
||||
d = new MutableBigInteger(quotient.toBigInteger().multiply(b2));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// step 3b: if a1>=b1, let quotient=beta^n-1 and r=a12-b1*2^n+b1
|
||||
quotient.ones(n);
|
||||
a12.add(b1);
|
||||
|
@ -1393,16 +1400,19 @@ class MutableBigInteger {
|
|||
*/
|
||||
private MutableBigInteger getBlock(int index, int numBlocks, int blockLength) {
|
||||
int blockStart = index * blockLength;
|
||||
if (blockStart >= intLen)
|
||||
if (blockStart >= intLen) {
|
||||
return new MutableBigInteger();
|
||||
}
|
||||
|
||||
int blockEnd;
|
||||
if (index == numBlocks-1)
|
||||
if (index == numBlocks-1) {
|
||||
blockEnd = intLen;
|
||||
else
|
||||
} else {
|
||||
blockEnd = (index+1) * blockLength;
|
||||
if (blockEnd > intLen)
|
||||
}
|
||||
if (blockEnd > intLen) {
|
||||
return new MutableBigInteger();
|
||||
}
|
||||
|
||||
int[] newVal = Arrays.copyOfRange(value, offset+intLen-blockEnd, offset+intLen-blockStart);
|
||||
return new MutableBigInteger(newVal);
|
||||
|
|
|
@ -48,7 +48,7 @@ import java.util.Random;
|
|||
*
|
||||
* The tests are performed on arrays of random numbers which are
|
||||
* generated by a Random class as well as special cases which
|
||||
* throw in boundary numbers such as 0, 1, maximum SIZEd, etc.
|
||||
* throw in boundary numbers such as 0, 1, maximum sized, etc.
|
||||
*
|
||||
*/
|
||||
public class BigIntegerTest {
|
||||
|
@ -1008,7 +1008,7 @@ public class BigIntegerTest {
|
|||
|
||||
/*
|
||||
* Get a random or boundary-case number. This is designed to provide
|
||||
* a lot of numbers that will find failure points, such as max SIZEd
|
||||
* a lot of numbers that will find failure points, such as max sized
|
||||
* numbers, empty BigIntegers, etc.
|
||||
*
|
||||
* If order is less than 2, order is changed to 2.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue