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:
Brian Burkhalter 2013-07-26 17:09:30 -07:00 committed by Brian Burkhalter
parent d59c1fac00
commit 4fe69c432f
3 changed files with 281 additions and 255 deletions

File diff suppressed because it is too large Load diff

View file

@ -313,7 +313,7 @@ class MutableBigInteger {
int blen = b.intLen; int blen = b.intLen;
int len = intLen; int len = intLen;
if (len <= 0) if (len <= 0)
return blen <=0 ? 0 : -1; return blen <= 0 ? 0 : -1;
if (len > blen) if (len > blen)
return 1; return 1;
if (len < blen - 1) if (len < blen - 1)
@ -340,7 +340,7 @@ class MutableBigInteger {
return v < hb ? -1 : 1; return v < hb ? -1 : 1;
carry = (bv & 1) << 31; // carray will be either 0x80000000 or 0 carry = (bv & 1) << 31; // carray will be either 0x80000000 or 0
} }
return carry == 0? 0 : -1; return carry == 0 ? 0 : -1;
} }
/** /**
@ -351,10 +351,10 @@ class MutableBigInteger {
if (intLen == 0) if (intLen == 0)
return -1; return -1;
int j, b; int j, b;
for (j=intLen-1; (j>0) && (value[j+offset]==0); j--) for (j=intLen-1; (j > 0) && (value[j+offset] == 0); j--)
; ;
b = value[j+offset]; b = value[j+offset];
if (b==0) if (b == 0)
return -1; return -1;
return ((intLen-1-j)<<5) + Integer.numberOfTrailingZeros(b); return ((intLen-1-j)<<5) + Integer.numberOfTrailingZeros(b);
} }
@ -395,11 +395,11 @@ class MutableBigInteger {
int indexBound = index+intLen; int indexBound = index+intLen;
do { do {
index++; index++;
} while(index < indexBound && value[index]==0); } while(index < indexBound && value[index] == 0);
int numZeros = index - offset; int numZeros = index - offset;
intLen -= numZeros; intLen -= numZeros;
offset = (intLen==0 ? 0 : offset+numZeros); offset = (intLen == 0 ? 0 : offset+numZeros);
} }
/** /**
@ -420,7 +420,7 @@ class MutableBigInteger {
*/ */
int[] toIntArray() { int[] toIntArray() {
int[] result = new int[intLen]; int[] result = new int[intLen];
for(int i=0; i<intLen; i++) for(int i=0; i < intLen; i++)
result[i] = value[offset+i]; result[i] = value[offset+i];
return result; return result;
} }
@ -506,7 +506,7 @@ class MutableBigInteger {
boolean isNormal() { boolean isNormal() {
if (intLen + offset > value.length) if (intLen + offset > value.length)
return false; return false;
if (intLen ==0) if (intLen == 0)
return true; return true;
return (value[offset] != 0); return (value[offset] != 0);
} }
@ -523,11 +523,12 @@ class MutableBigInteger {
* Like {@link #rightShift(int)} but {@code n} can be greater than the length of the number. * Like {@link #rightShift(int)} but {@code n} can be greater than the length of the number.
*/ */
void safeRightShift(int n) { void safeRightShift(int n) {
if (n/32 >= intLen) if (n/32 >= intLen) {
reset(); reset();
else } else {
rightShift(n); rightShift(n);
} }
}
/** /**
* Right shift this MutableBigInteger n bits. The MutableBigInteger is left * 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. * Like {@link #leftShift(int)} but {@code n} can be zero.
*/ */
void safeLeftShift(int n) { void safeLeftShift(int n) {
if (n > 0) if (n > 0) {
leftShift(n); leftShift(n);
} }
}
/** /**
* Left shift this MutableBigInteger n bits. * Left shift this MutableBigInteger n bits.
@ -586,18 +588,18 @@ class MutableBigInteger {
if (value.length < newLen) { if (value.length < newLen) {
// The array must grow // The array must grow
int[] result = new int[newLen]; int[] result = new int[newLen];
for (int i=0; i<intLen; i++) for (int i=0; i < intLen; i++)
result[i] = value[offset+i]; result[i] = value[offset+i];
setValue(result, newLen); setValue(result, newLen);
} else if (value.length - offset >= newLen) { } else if (value.length - offset >= newLen) {
// Use space on right // Use space on right
for(int i=0; i<newLen - intLen; i++) for(int i=0; i < newLen - intLen; i++)
value[offset+intLen+i] = 0; value[offset+intLen+i] = 0;
} else { } else {
// Must use space on left // Must use space on left
for (int i=0; i<intLen; i++) for (int i=0; i < intLen; i++)
value[i] = value[offset+i]; value[i] = value[offset+i];
for (int i=intLen; i<newLen; i++) for (int i=intLen; i < newLen; i++)
value[i] = 0; value[i] = 0;
offset = 0; offset = 0;
} }
@ -674,7 +676,7 @@ class MutableBigInteger {
private final void primitiveRightShift(int n) { private final void primitiveRightShift(int n) {
int[] val = value; int[] val = value;
int n2 = 32 - n; int n2 = 32 - n;
for (int i=offset+intLen-1, c=val[i]; i>offset; i--) { for (int i=offset+intLen-1, c=val[i]; i > offset; i--) {
int b = c; int b = c;
c = val[i-1]; c = val[i-1];
val[i] = (c << n2) | (b >>> n); val[i] = (c << n2) | (b >>> n);
@ -690,7 +692,7 @@ class MutableBigInteger {
private final void primitiveLeftShift(int n) { private final void primitiveLeftShift(int n) {
int[] val = value; int[] val = value;
int n2 = 32 - n; int n2 = 32 - n;
for (int i=offset, c=val[i], m=i+intLen-1; i<m; i++) { for (int i=offset, c=val[i], m=i+intLen-1; i < m; i++) {
int b = c; int b = c;
c = val[i+1]; c = val[i+1];
val[i] = (b << n) | (c >>> n2); val[i] = (b << n) | (c >>> n2);
@ -703,16 +705,16 @@ class MutableBigInteger {
* low ints of this number. * low ints of this number.
*/ */
private BigInteger getLower(int n) { private BigInteger getLower(int n) {
if (isZero()) if (isZero()) {
return BigInteger.ZERO; return BigInteger.ZERO;
else if (intLen < n) } else if (intLen < n) {
return toBigInteger(1); return toBigInteger(1);
else { } else {
// strip zeros // strip zeros
int len = n; int len = n;
while (len>0 && value[offset+intLen-len]==0) while (len > 0 && value[offset+intLen-len] == 0)
len--; len--;
int sign = len>0 ? 1 : 0; int sign = len > 0 ? 1 : 0;
return new BigInteger(Arrays.copyOfRange(value, offset+intLen-len, offset+intLen), sign); return new BigInteger(Arrays.copyOfRange(value, offset+intLen-len, offset+intLen), sign);
} }
} }
@ -743,7 +745,7 @@ class MutableBigInteger {
long carry = 0; long carry = 0;
// Add common parts of both numbers // Add common parts of both numbers
while(x>0 && y>0) { while(x > 0 && y > 0) {
x--; y--; x--; y--;
sum = (value[x+offset] & LONG_MASK) + sum = (value[x+offset] & LONG_MASK) +
(addend.value[y+addend.offset] & LONG_MASK) + carry; (addend.value[y+addend.offset] & LONG_MASK) + carry;
@ -752,7 +754,7 @@ class MutableBigInteger {
} }
// Add remainder of the longer number // Add remainder of the longer number
while(x>0) { while(x > 0) {
x--; x--;
if (carry == 0 && result == value && rstart == (x + offset)) if (carry == 0 && result == value && rstart == (x + offset))
return; return;
@ -760,7 +762,7 @@ class MutableBigInteger {
result[rstart--] = (int)sum; result[rstart--] = (int)sum;
carry = sum >>> 32; carry = sum >>> 32;
} }
while(y>0) { while(y > 0) {
y--; y--;
sum = (addend.value[y+addend.offset] & LONG_MASK) + carry; sum = (addend.value[y+addend.offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum; result[rstart--] = (int)sum;
@ -788,12 +790,13 @@ class MutableBigInteger {
/** /**
* Adds the value of {@code addend} shifted {@code n} ints to the left. * 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);} * Has the same effect as {@code addend.leftShift(32*ints); add(addend);}
* but doesn't change the value of {@code b}. * but doesn't change the value of {@code addend}.
*/ */
void addShifted(MutableBigInteger addend, int n) { void addShifted(MutableBigInteger addend, int n) {
if (addend.isZero()) if (addend.isZero()) {
return; return;
}
int x = intLen; int x = intLen;
int y = addend.intLen + n; int y = addend.intLen + n;
@ -805,9 +808,9 @@ class MutableBigInteger {
long carry = 0; long carry = 0;
// Add common parts of both numbers // Add common parts of both numbers
while(x>0 && y>0) { while (x > 0 && y > 0) {
x--; y--; x--; y--;
int bval = y+addend.offset<addend.value.length ? addend.value[y+addend.offset] : 0; int bval = y+addend.offset < addend.value.length ? addend.value[y+addend.offset] : 0;
sum = (value[x+offset] & LONG_MASK) + sum = (value[x+offset] & LONG_MASK) +
(bval & LONG_MASK) + carry; (bval & LONG_MASK) + carry;
result[rstart--] = (int)sum; result[rstart--] = (int)sum;
@ -815,17 +818,18 @@ class MutableBigInteger {
} }
// Add remainder of the longer number // Add remainder of the longer number
while(x>0) { while (x > 0) {
x--; x--;
if (carry == 0 && result == value && rstart == (x + offset)) if (carry == 0 && result == value && rstart == (x + offset)) {
return; return;
}
sum = (value[x+offset] & LONG_MASK) + carry; sum = (value[x+offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum; result[rstart--] = (int)sum;
carry = sum >>> 32; carry = sum >>> 32;
} }
while(y>0) { while (y > 0) {
y--; y--;
int bval = y+addend.offset<addend.value.length ? addend.value[y+addend.offset] : 0; int bval = y+addend.offset < addend.value.length ? addend.value[y+addend.offset] : 0;
sum = (bval & LONG_MASK) + carry; sum = (bval & LONG_MASK) + carry;
result[rstart--] = (int)sum; result[rstart--] = (int)sum;
carry = sum >>> 32; carry = sum >>> 32;
@ -881,7 +885,7 @@ class MutableBigInteger {
System.arraycopy(addend.value, addend.offset, result, rstart+1-y, len); System.arraycopy(addend.value, addend.offset, result, rstart+1-y, len);
// zero the gap // zero the gap
for (int i=rstart+1-y+len; i<rstart+1; i++) for (int i=rstart+1-y+len; i < rstart+1; i++)
result[i] = 0; result[i] = 0;
value = result; value = result;
@ -932,7 +936,7 @@ class MutableBigInteger {
int rstart = result.length - 1; int rstart = result.length - 1;
// Subtract common parts of both numbers // Subtract common parts of both numbers
while (y>0) { while (y > 0) {
x--; y--; x--; y--;
diff = (a.value[x+a.offset] & LONG_MASK) - diff = (a.value[x+a.offset] & LONG_MASK) -
@ -940,7 +944,7 @@ class MutableBigInteger {
result[rstart--] = (int)diff; result[rstart--] = (int)diff;
} }
// Subtract remainder of longer number // Subtract remainder of longer number
while (x>0) { while (x > 0) {
x--; x--;
diff = (a.value[x+a.offset] & LONG_MASK) - ((int)-(diff>>32)); diff = (a.value[x+a.offset] & LONG_MASK) - ((int)-(diff>>32));
result[rstart--] = (int)diff; result[rstart--] = (int)diff;
@ -961,7 +965,7 @@ class MutableBigInteger {
private int difference(MutableBigInteger b) { private int difference(MutableBigInteger b) {
MutableBigInteger a = this; MutableBigInteger a = this;
int sign = a.compare(b); int sign = a.compare(b);
if (sign ==0) if (sign == 0)
return 0; return 0;
if (sign < 0) { if (sign < 0) {
MutableBigInteger tmp = a; MutableBigInteger tmp = a;
@ -974,14 +978,14 @@ class MutableBigInteger {
int y = b.intLen; int y = b.intLen;
// Subtract common parts of both numbers // Subtract common parts of both numbers
while (y>0) { while (y > 0) {
x--; y--; x--; y--;
diff = (a.value[a.offset+ x] & LONG_MASK) - diff = (a.value[a.offset+ x] & LONG_MASK) -
(b.value[b.offset+ y] & LONG_MASK) - ((int)-(diff>>32)); (b.value[b.offset+ y] & LONG_MASK) - ((int)-(diff>>32));
a.value[a.offset+x] = (int)diff; a.value[a.offset+x] = (int)diff;
} }
// Subtract remainder of longer number // Subtract remainder of longer number
while (x>0) { while (x > 0) {
x--; x--;
diff = (a.value[a.offset+ x] & LONG_MASK) - ((int)-(diff>>32)); diff = (a.value[a.offset+ x] & LONG_MASK) - ((int)-(diff>>32));
a.value[a.offset+x] = (int)diff; a.value[a.offset+x] = (int)diff;
@ -1050,7 +1054,7 @@ class MutableBigInteger {
// Perform the multiplication word by word // Perform the multiplication word by word
long ylong = y & LONG_MASK; long ylong = y & LONG_MASK;
int[] zval = (z.value.length<intLen+1 ? new int[intLen + 1] int[] zval = (z.value.length < intLen+1 ? new int[intLen + 1]
: z.value); : z.value);
long carry = 0; long carry = 0;
for (int i = intLen-1; i >= 0; i--) { for (int i = intLen-1; i >= 0; i--) {
@ -1144,11 +1148,13 @@ class MutableBigInteger {
} }
MutableBigInteger divide(MutableBigInteger b, MutableBigInteger quotient, boolean needRemainder) { 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); return divideKnuth(b, quotient, needRemainder);
else } else {
return divideAndRemainderBurnikelZiegler(b, quotient); return divideAndRemainderBurnikelZiegler(b, quotient);
} }
}
/** /**
* @see #divideKnuth(MutableBigInteger, MutableBigInteger, boolean) * @see #divideKnuth(MutableBigInteger, MutableBigInteger, boolean)
@ -1236,9 +1242,9 @@ class MutableBigInteger {
int r = intLen; int r = intLen;
int s = b.intLen; int s = b.intLen;
if (r < s) if (r < s) {
return this; return this;
else { } else {
// Unlike Knuth division, we don't check for common powers of two here because // 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 // BZ already runs faster if both numbers contain powers of two and cancelling them has no
// additional benefit. // additional benefit.
@ -1256,8 +1262,9 @@ class MutableBigInteger {
// step 5: t is the number of blocks needed to accommodate this plus one additional bit // step 5: t is the number of blocks needed to accommodate this plus one additional bit
int t = (bitLength()+n32) / n32; int t = (bitLength()+n32) / n32;
if (t < 2) if (t < 2) {
t = 2; t = 2;
}
// step 6: conceptually split this into blocks a[t-1], ..., a[0] // 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 MutableBigInteger a1 = getBlock(t-1, t, n); // the most significant block of this
@ -1270,7 +1277,7 @@ class MutableBigInteger {
MutableBigInteger qi = new MutableBigInteger(); MutableBigInteger qi = new MutableBigInteger();
MutableBigInteger ri; MutableBigInteger ri;
quotient.offset = quotient.intLen = 0; quotient.offset = quotient.intLen = 0;
for (int i=t-2; i>0; i--) { for (int i=t-2; i > 0; i--) {
// step 8a: compute (qi,ri) such that z=b*qi+ri // step 8a: compute (qi,ri) such that z=b*qi+ri
ri = z.divide2n1n(bShifted, qi); ri = z.divide2n1n(bShifted, qi);
@ -1302,8 +1309,9 @@ class MutableBigInteger {
int n = b.intLen; int n = b.intLen;
// step 1: base case // 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); return divideKnuth(b, quotient);
}
// step 2: view this as [a1,a2,a3,a4] where each ai is n/2 ints or less // step 2: view this as [a1,a2,a3,a4] where each ai is n/2 ints or less
MutableBigInteger aUpper = new MutableBigInteger(this); MutableBigInteger aUpper = new MutableBigInteger(this);
@ -1352,8 +1360,7 @@ class MutableBigInteger {
// step 4: d=quotient*b2 // step 4: d=quotient*b2
d = new MutableBigInteger(quotient.toBigInteger().multiply(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 // step 3b: if a1>=b1, let quotient=beta^n-1 and r=a12-b1*2^n+b1
quotient.ones(n); quotient.ones(n);
a12.add(b1); a12.add(b1);
@ -1393,16 +1400,19 @@ class MutableBigInteger {
*/ */
private MutableBigInteger getBlock(int index, int numBlocks, int blockLength) { private MutableBigInteger getBlock(int index, int numBlocks, int blockLength) {
int blockStart = index * blockLength; int blockStart = index * blockLength;
if (blockStart >= intLen) if (blockStart >= intLen) {
return new MutableBigInteger(); return new MutableBigInteger();
}
int blockEnd; int blockEnd;
if (index == numBlocks-1) if (index == numBlocks-1) {
blockEnd = intLen; blockEnd = intLen;
else } else {
blockEnd = (index+1) * blockLength; blockEnd = (index+1) * blockLength;
if (blockEnd > intLen) }
if (blockEnd > intLen) {
return new MutableBigInteger(); return new MutableBigInteger();
}
int[] newVal = Arrays.copyOfRange(value, offset+intLen-blockEnd, offset+intLen-blockStart); int[] newVal = Arrays.copyOfRange(value, offset+intLen-blockEnd, offset+intLen-blockStart);
return new MutableBigInteger(newVal); return new MutableBigInteger(newVal);
@ -1473,7 +1483,7 @@ class MutableBigInteger {
if (shift > 0) { if (shift > 0) {
divisor = new int[dlen]; divisor = new int[dlen];
copyAndShift(div.value,div.offset,dlen,divisor,0,shift); copyAndShift(div.value,div.offset,dlen,divisor,0,shift);
if(Integer.numberOfLeadingZeros(value[offset])>=shift) { if (Integer.numberOfLeadingZeros(value[offset]) >= shift) {
int[] remarr = new int[intLen + 1]; int[] remarr = new int[intLen + 1];
rem = new MutableBigInteger(remarr); rem = new MutableBigInteger(remarr);
rem.intLen = intLen; rem.intLen = intLen;
@ -1526,7 +1536,7 @@ class MutableBigInteger {
int dl = divisor[1]; int dl = divisor[1];
// D2 Initialize j // D2 Initialize j
for(int j=0; j<limit-1; j++) { for (int j=0; j < limit-1; j++) {
// D3 Calculate qhat // D3 Calculate qhat
// estimate qhat // estimate qhat
int qhat = 0; int qhat = 0;
@ -1650,7 +1660,7 @@ class MutableBigInteger {
} }
if(needRemainder) { if (needRemainder) {
// D8 Unnormalize // D8 Unnormalize
if (shift > 0) if (shift > 0)
rem.rightShift(shift); rem.rightShift(shift);
@ -1892,7 +1902,7 @@ class MutableBigInteger {
} }
// step B2 // step B2
boolean uOdd = (k==s1); boolean uOdd = (k == s1);
MutableBigInteger t = uOdd ? v: u; MutableBigInteger t = uOdd ? v: u;
int tsign = uOdd ? -1 : 1; int tsign = uOdd ? -1 : 1;
@ -1934,9 +1944,9 @@ class MutableBigInteger {
* Calculate GCD of a and b interpreted as unsigned integers. * Calculate GCD of a and b interpreted as unsigned integers.
*/ */
static int binaryGcd(int a, int b) { static int binaryGcd(int a, int b) {
if (b==0) if (b == 0)
return a; return a;
if (a==0) if (a == 0)
return b; return b;
// Right shift a & b till their last bits equal to 1. // Right shift a & b till their last bits equal to 1.
@ -2087,7 +2097,7 @@ class MutableBigInteger {
} }
// The Almost Inverse Algorithm // The Almost Inverse Algorithm
while(!f.isOne()) { while (!f.isOne()) {
// If gcd(f, g) != 1, number is not invertible modulo mod // If gcd(f, g) != 1, number is not invertible modulo mod
if (f.isZero()) if (f.isZero())
throw new ArithmeticException("BigInteger not invertible."); throw new ArithmeticException("BigInteger not invertible.");
@ -2132,7 +2142,7 @@ class MutableBigInteger {
// Set r to the multiplicative inverse of p mod 2^32 // Set r to the multiplicative inverse of p mod 2^32
int r = -inverseMod32(p.value[p.offset+p.intLen-1]); int r = -inverseMod32(p.value[p.offset+p.intLen-1]);
for(int i=0, numWords = k >> 5; i<numWords; i++) { for (int i=0, numWords = k >> 5; i < numWords; i++) {
// V = R * c (mod 2^j) // V = R * c (mod 2^j)
int v = r * c.value[c.offset + c.intLen-1]; int v = r * c.value[c.offset + c.intLen-1];
// c = c + (v * p) // c = c + (v * p)

View file

@ -48,7 +48,7 @@ import java.util.Random;
* *
* The tests are performed on arrays of random numbers which are * The tests are performed on arrays of random numbers which are
* generated by a Random class as well as special cases which * 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 { public class BigIntegerTest {
@ -1008,7 +1008,7 @@ public class BigIntegerTest {
/* /*
* Get a random or boundary-case number. This is designed to provide * 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. * numbers, empty BigIntegers, etc.
* *
* If order is less than 2, order is changed to 2. * If order is less than 2, order is changed to 2.