code
stringlengths 11
173k
| docstring
stringlengths 2
593k
| func_name
stringlengths 2
189
| language
stringclasses 1
value | repo
stringclasses 844
values | path
stringlengths 11
294
| url
stringlengths 60
339
| license
stringclasses 4
values |
---|---|---|---|---|---|---|---|
private static BigInteger divideAndRound(BigInteger bdividend, BigInteger bdivisor, int roundingMode) {
boolean isRemainderZero; // record remainder is zero or not
int qsign; // quotient sign
// Descend into mutables for faster remainder checks
MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag);
MutableBigInteger mq = new MutableBigInteger();
MutableBigInteger mdivisor = new MutableBigInteger(bdivisor.mag);
MutableBigInteger mr = mdividend.divide(mdivisor, mq);
isRemainderZero = mr.isZero();
qsign = (bdividend.signum != bdivisor.signum) ? -1 : 1;
if (!isRemainderZero) {
if (needIncrement(mdivisor, roundingMode, qsign, mq, mr)) {
mq.add(MutableBigInteger.ONE);
}
}
return mq.toBigInteger(qsign);
} |
Divides {@code BigInteger} value by {@code BigInteger} value and
do rounding based on the passed in roundingMode.
| StringBuilderHelper::divideAndRound | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal divideAndRound(BigInteger bdividend, BigInteger bdivisor, int scale, int roundingMode,
int preferredScale) {
boolean isRemainderZero; // record remainder is zero or not
int qsign; // quotient sign
// Descend into mutables for faster remainder checks
MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag);
MutableBigInteger mq = new MutableBigInteger();
MutableBigInteger mdivisor = new MutableBigInteger(bdivisor.mag);
MutableBigInteger mr = mdividend.divide(mdivisor, mq);
isRemainderZero = mr.isZero();
qsign = (bdividend.signum != bdivisor.signum) ? -1 : 1;
if (!isRemainderZero) {
if (needIncrement(mdivisor, roundingMode, qsign, mq, mr)) {
mq.add(MutableBigInteger.ONE);
}
return mq.toBigDecimal(qsign, scale);
} else {
if (preferredScale != scale) {
long compactVal = mq.toCompactValue(qsign);
if (compactVal != INFLATED) {
return createAndStripZerosToMatchScale(compactVal, scale, preferredScale);
}
BigInteger intVal = mq.toBigInteger(qsign);
return createAndStripZerosToMatchScale(intVal, scale, preferredScale);
} else {
return mq.toBigDecimal(qsign, scale);
}
}
} |
Internally used for division operation for division {@code BigInteger}
by {@code BigInteger}.
The returned {@code BigDecimal} object is the quotient whose scale is set
to the passed in scale. If the remainder is not zero, it will be rounded
based on the passed in roundingMode. Also, if the remainder is zero and
the last parameter, i.e. preferredScale is NOT equal to scale, the
trailing zeros of the result is stripped to match the preferredScale.
| StringBuilderHelper::divideAndRound | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static boolean needIncrement(MutableBigInteger mdivisor, int roundingMode,
int qsign, MutableBigInteger mq, MutableBigInteger mr) {
assert !mr.isZero();
int cmpFracHalf = mr.compareHalf(mdivisor);
return commonNeedIncrement(roundingMode, qsign, cmpFracHalf, mq.isOdd());
} |
Tests if quotient has to be incremented according the roundingMode
| StringBuilderHelper::needIncrement | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal createAndStripZerosToMatchScale(BigInteger intVal, int scale, long preferredScale) {
BigInteger qr[]; // quotient-remainder pair
while (intVal.compareMagnitude(BigInteger.TEN) >= 0
&& scale > preferredScale) {
if (intVal.testBit(0))
break; // odd number cannot end in 0
qr = intVal.divideAndRemainder(BigInteger.TEN);
if (qr[1].signum() != 0)
break; // non-0 remainder
intVal = qr[0];
scale = checkScale(intVal,(long) scale - 1); // could Overflow
}
return valueOf(intVal, scale, 0);
} |
Remove insignificant trailing zeros from this
{@code BigInteger} value until the preferred scale is reached or no
more zeros can be removed. If the preferred scale is less than
Integer.MIN_VALUE, all the trailing zeros will be removed.
@return new {@code BigDecimal} with a scale possibly reduced
to be closed to the preferred scale.
| StringBuilderHelper::createAndStripZerosToMatchScale | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal createAndStripZerosToMatchScale(long compactVal, int scale, long preferredScale) {
while (Math.abs(compactVal) >= 10L && scale > preferredScale) {
if ((compactVal & 1L) != 0L)
break; // odd number cannot end in 0
long r = compactVal % 10L;
if (r != 0L)
break; // non-0 remainder
compactVal /= 10;
scale = checkScale(compactVal, (long) scale - 1); // could Overflow
}
return valueOf(compactVal, scale);
} |
Remove insignificant trailing zeros from this
{@code long} value until the preferred scale is reached or no
more zeros can be removed. If the preferred scale is less than
Integer.MIN_VALUE, all the trailing zeros will be removed.
@return new {@code BigDecimal} with a scale possibly reduced
to be closed to the preferred scale.
| StringBuilderHelper::createAndStripZerosToMatchScale | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static long add(long xs, long ys){
long sum = xs + ys;
// See "Hacker's Delight" section 2-12 for explanation of
// the overflow test.
if ( (((sum ^ xs) & (sum ^ ys))) >= 0L) { // not overflowed
return sum;
}
return INFLATED;
} |
Remove insignificant trailing zeros from this
{@code long} value until the preferred scale is reached or no
more zeros can be removed. If the preferred scale is less than
Integer.MIN_VALUE, all the trailing zeros will be removed.
@return new {@code BigDecimal} with a scale possibly reduced
to be closed to the preferred scale.
private static BigDecimal createAndStripZerosToMatchScale(long compactVal, int scale, long preferredScale) {
while (Math.abs(compactVal) >= 10L && scale > preferredScale) {
if ((compactVal & 1L) != 0L)
break; // odd number cannot end in 0
long r = compactVal % 10L;
if (r != 0L)
break; // non-0 remainder
compactVal /= 10;
scale = checkScale(compactVal, (long) scale - 1); // could Overflow
}
return valueOf(compactVal, scale);
}
private static BigDecimal stripZerosToMatchScale(BigInteger intVal, long intCompact, int scale, int preferredScale) {
if(intCompact!=INFLATED) {
return createAndStripZerosToMatchScale(intCompact, scale, preferredScale);
} else {
return createAndStripZerosToMatchScale(intVal==null ? INFLATED_BIGINT : intVal,
scale, preferredScale);
}
}
/*
returns INFLATED if oveflow
| StringBuilderHelper::add | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal divideSmallFastPath(final long xs, int xscale,
final long ys, int yscale,
long preferredScale, MathContext mc) {
int mcp = mc.precision;
int roundingMode = mc.roundingMode.oldMode;
assert (xscale <= yscale) && (yscale < 18) && (mcp < 18);
int xraise = yscale - xscale; // xraise >=0
long scaledX = (xraise==0) ? xs :
longMultiplyPowerTen(xs, xraise); // can't overflow here!
BigDecimal quotient;
int cmp = longCompareMagnitude(scaledX, ys);
if(cmp > 0) { // satisfy constraint (b)
yscale -= 1; // [that is, divisor *= 10]
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
// assert newScale >= xscale
int raise = checkScaleNonZero((long) mcp + yscale - xscale);
long scaledXs;
if ((scaledXs = longMultiplyPowerTen(xs, raise)) == INFLATED) {
quotient = null;
if((mcp-1) >=0 && (mcp-1)<LONG_TEN_POWERS_TABLE.length) {
quotient = multiplyDivideAndRound(LONG_TEN_POWERS_TABLE[mcp-1], scaledX, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
}
if(quotient==null) {
BigInteger rb = bigMultiplyPowerTen(scaledX,mcp-1);
quotient = divideAndRound(rb, ys,
scl, roundingMode, checkScaleNonZero(preferredScale));
}
} else {
quotient = divideAndRound(scaledXs, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
}
} else {
int newScale = checkScaleNonZero((long) xscale - mcp);
// assert newScale >= yscale
if (newScale == yscale) { // easy case
quotient = divideAndRound(xs, ys, scl, roundingMode,checkScaleNonZero(preferredScale));
} else {
int raise = checkScaleNonZero((long) newScale - yscale);
long scaledYs;
if ((scaledYs = longMultiplyPowerTen(ys, raise)) == INFLATED) {
BigInteger rb = bigMultiplyPowerTen(ys,raise);
quotient = divideAndRound(BigInteger.valueOf(xs),
rb, scl, roundingMode,checkScaleNonZero(preferredScale));
} else {
quotient = divideAndRound(xs, scaledYs, scl, roundingMode,checkScaleNonZero(preferredScale));
}
}
}
} else {
// abs(scaledX) <= abs(ys)
// result is "scaledX * 10^msp / ys"
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
if(cmp==0) {
// abs(scaleX)== abs(ys) => result will be scaled 10^mcp + correct sign
quotient = roundedTenPower(((scaledX < 0) == (ys < 0)) ? 1 : -1, mcp, scl, checkScaleNonZero(preferredScale));
} else {
// abs(scaledX) < abs(ys)
long scaledXs;
if ((scaledXs = longMultiplyPowerTen(scaledX, mcp)) == INFLATED) {
quotient = null;
if(mcp<LONG_TEN_POWERS_TABLE.length) {
quotient = multiplyDivideAndRound(LONG_TEN_POWERS_TABLE[mcp], scaledX, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
}
if(quotient==null) {
BigInteger rb = bigMultiplyPowerTen(scaledX,mcp);
quotient = divideAndRound(rb, ys,
scl, roundingMode, checkScaleNonZero(preferredScale));
}
} else {
quotient = divideAndRound(scaledXs, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
}
}
}
// doRound, here, only affects 1000000000 case.
return doRound(quotient,mc);
} |
Returns a {@code BigDecimal} whose value is {@code (xs /
ys)}, with rounding according to the context settings.
Fast path - used only when (xscale <= yscale && yscale < 18
&& mc.presision<18) {
| StringBuilderHelper::divideSmallFastPath | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal divide(final long xs, int xscale, final long ys, int yscale, long preferredScale, MathContext mc) {
int mcp = mc.precision;
if(xscale <= yscale && yscale < 18 && mcp<18) {
return divideSmallFastPath(xs, xscale, ys, yscale, preferredScale, mc);
}
if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
yscale -= 1; // [that is, divisor *= 10]
}
int roundingMode = mc.roundingMode.oldMode;
// In order to find out whether the divide generates the exact result,
// we avoid calling the above divide method. 'quotient' holds the
// return BigDecimal object whose scale will be set to 'scl'.
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
BigDecimal quotient;
if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
int raise = checkScaleNonZero((long) mcp + yscale - xscale);
long scaledXs;
if ((scaledXs = longMultiplyPowerTen(xs, raise)) == INFLATED) {
BigInteger rb = bigMultiplyPowerTen(xs,raise);
quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
} else {
quotient = divideAndRound(scaledXs, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
}
} else {
int newScale = checkScaleNonZero((long) xscale - mcp);
// assert newScale >= yscale
if (newScale == yscale) { // easy case
quotient = divideAndRound(xs, ys, scl, roundingMode,checkScaleNonZero(preferredScale));
} else {
int raise = checkScaleNonZero((long) newScale - yscale);
long scaledYs;
if ((scaledYs = longMultiplyPowerTen(ys, raise)) == INFLATED) {
BigInteger rb = bigMultiplyPowerTen(ys,raise);
quotient = divideAndRound(BigInteger.valueOf(xs),
rb, scl, roundingMode,checkScaleNonZero(preferredScale));
} else {
quotient = divideAndRound(xs, scaledYs, scl, roundingMode,checkScaleNonZero(preferredScale));
}
}
}
// doRound, here, only affects 1000000000 case.
return doRound(quotient,mc);
} |
Returns a {@code BigDecimal} whose value is {@code (xs /
ys)}, with rounding according to the context settings.
| StringBuilderHelper::divide | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal divide(BigInteger xs, int xscale, long ys, int yscale, long preferredScale, MathContext mc) {
// Normalize dividend & divisor so that both fall into [0.1, 0.999...]
if ((-compareMagnitudeNormalized(ys, yscale, xs, xscale)) > 0) {// satisfy constraint (b)
yscale -= 1; // [that is, divisor *= 10]
}
int mcp = mc.precision;
int roundingMode = mc.roundingMode.oldMode;
// In order to find out whether the divide generates the exact result,
// we avoid calling the above divide method. 'quotient' holds the
// return BigDecimal object whose scale will be set to 'scl'.
BigDecimal quotient;
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
int raise = checkScaleNonZero((long) mcp + yscale - xscale);
BigInteger rb = bigMultiplyPowerTen(xs,raise);
quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
} else {
int newScale = checkScaleNonZero((long) xscale - mcp);
// assert newScale >= yscale
if (newScale == yscale) { // easy case
quotient = divideAndRound(xs, ys, scl, roundingMode,checkScaleNonZero(preferredScale));
} else {
int raise = checkScaleNonZero((long) newScale - yscale);
long scaledYs;
if ((scaledYs = longMultiplyPowerTen(ys, raise)) == INFLATED) {
BigInteger rb = bigMultiplyPowerTen(ys,raise);
quotient = divideAndRound(xs, rb, scl, roundingMode,checkScaleNonZero(preferredScale));
} else {
quotient = divideAndRound(xs, scaledYs, scl, roundingMode,checkScaleNonZero(preferredScale));
}
}
}
// doRound, here, only affects 1000000000 case.
return doRound(quotient, mc);
} |
Returns a {@code BigDecimal} whose value is {@code (xs /
ys)}, with rounding according to the context settings.
| StringBuilderHelper::divide | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal divide(long xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc) {
// Normalize dividend & divisor so that both fall into [0.1, 0.999...]
if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
yscale -= 1; // [that is, divisor *= 10]
}
int mcp = mc.precision;
int roundingMode = mc.roundingMode.oldMode;
// In order to find out whether the divide generates the exact result,
// we avoid calling the above divide method. 'quotient' holds the
// return BigDecimal object whose scale will be set to 'scl'.
BigDecimal quotient;
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
int raise = checkScaleNonZero((long) mcp + yscale - xscale);
BigInteger rb = bigMultiplyPowerTen(xs,raise);
quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
} else {
int newScale = checkScaleNonZero((long) xscale - mcp);
int raise = checkScaleNonZero((long) newScale - yscale);
BigInteger rb = bigMultiplyPowerTen(ys,raise);
quotient = divideAndRound(BigInteger.valueOf(xs), rb, scl, roundingMode,checkScaleNonZero(preferredScale));
}
// doRound, here, only affects 1000000000 case.
return doRound(quotient, mc);
} |
Returns a {@code BigDecimal} whose value is {@code (xs /
ys)}, with rounding according to the context settings.
| StringBuilderHelper::divide | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal divide(BigInteger xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc) {
// Normalize dividend & divisor so that both fall into [0.1, 0.999...]
if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
yscale -= 1; // [that is, divisor *= 10]
}
int mcp = mc.precision;
int roundingMode = mc.roundingMode.oldMode;
// In order to find out whether the divide generates the exact result,
// we avoid calling the above divide method. 'quotient' holds the
// return BigDecimal object whose scale will be set to 'scl'.
BigDecimal quotient;
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
int raise = checkScaleNonZero((long) mcp + yscale - xscale);
BigInteger rb = bigMultiplyPowerTen(xs,raise);
quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
} else {
int newScale = checkScaleNonZero((long) xscale - mcp);
int raise = checkScaleNonZero((long) newScale - yscale);
BigInteger rb = bigMultiplyPowerTen(ys,raise);
quotient = divideAndRound(xs, rb, scl, roundingMode,checkScaleNonZero(preferredScale));
}
// doRound, here, only affects 1000000000 case.
return doRound(quotient, mc);
} |
Returns a {@code BigDecimal} whose value is {@code (xs /
ys)}, with rounding according to the context settings.
| StringBuilderHelper::divide | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal multiplyDivideAndRound(long dividend0, long dividend1, long divisor, int scale, int roundingMode,
int preferredScale) {
int qsign = Long.signum(dividend0)*Long.signum(dividend1)*Long.signum(divisor);
dividend0 = Math.abs(dividend0);
dividend1 = Math.abs(dividend1);
divisor = Math.abs(divisor);
// multiply dividend0 * dividend1
long d0_hi = dividend0 >>> 32;
long d0_lo = dividend0 & LONG_MASK;
long d1_hi = dividend1 >>> 32;
long d1_lo = dividend1 & LONG_MASK;
long product = d0_lo * d1_lo;
long d0 = product & LONG_MASK;
long d1 = product >>> 32;
product = d0_hi * d1_lo + d1;
d1 = product & LONG_MASK;
long d2 = product >>> 32;
product = d0_lo * d1_hi + d1;
d1 = product & LONG_MASK;
d2 += product >>> 32;
long d3 = d2>>>32;
d2 &= LONG_MASK;
product = d0_hi*d1_hi + d2;
d2 = product & LONG_MASK;
d3 = ((product>>>32) + d3) & LONG_MASK;
final long dividendHi = make64(d3,d2);
final long dividendLo = make64(d1,d0);
// divide
return divideAndRound128(dividendHi, dividendLo, divisor, qsign, scale, roundingMode, preferredScale);
} |
Returns a {@code BigDecimal} whose value is {@code (xs /
ys)}, with rounding according to the context settings.
private static BigDecimal divide(BigInteger xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc) {
// Normalize dividend & divisor so that both fall into [0.1, 0.999...]
if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
yscale -= 1; // [that is, divisor *= 10]
}
int mcp = mc.precision;
int roundingMode = mc.roundingMode.oldMode;
// In order to find out whether the divide generates the exact result,
// we avoid calling the above divide method. 'quotient' holds the
// return BigDecimal object whose scale will be set to 'scl'.
BigDecimal quotient;
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
int raise = checkScaleNonZero((long) mcp + yscale - xscale);
BigInteger rb = bigMultiplyPowerTen(xs,raise);
quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
} else {
int newScale = checkScaleNonZero((long) xscale - mcp);
int raise = checkScaleNonZero((long) newScale - yscale);
BigInteger rb = bigMultiplyPowerTen(ys,raise);
quotient = divideAndRound(xs, rb, scl, roundingMode,checkScaleNonZero(preferredScale));
}
// doRound, here, only affects 1000000000 case.
return doRound(quotient, mc);
}
/*
performs divideAndRound for (dividend0*dividend1, divisor)
returns null if quotient can't fit into long value;
| StringBuilderHelper::multiplyDivideAndRound | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal divideAndRound128(final long dividendHi, final long dividendLo, long divisor, int sign,
int scale, int roundingMode, int preferredScale) {
if (dividendHi >= divisor) {
return null;
}
final int shift = Long.numberOfLeadingZeros(divisor);
divisor <<= shift;
final long v1 = divisor >>> 32;
final long v0 = divisor & LONG_MASK;
long q1, q0;
long r_tmp;
long tmp = dividendLo << shift;
long u1 = tmp >>> 32;
long u0 = tmp & LONG_MASK;
tmp = (dividendHi << shift) | (dividendLo >>> 64 - shift);
long u2 = tmp & LONG_MASK;
tmp = divWord(tmp,v1);
q1 = tmp & LONG_MASK;
r_tmp = tmp >>> 32;
while(q1 >= DIV_NUM_BASE || unsignedLongCompare(q1*v0, make64(r_tmp, u1))) {
q1--;
r_tmp += v1;
if (r_tmp >= DIV_NUM_BASE)
break;
}
tmp = mulsub(u2,u1,v1,v0,q1);
u1 = tmp & LONG_MASK;
tmp = divWord(tmp,v1);
q0 = tmp & LONG_MASK;
r_tmp = tmp >>> 32;
while(q0 >= DIV_NUM_BASE || unsignedLongCompare(q0*v0,make64(r_tmp,u0))) {
q0--;
r_tmp += v1;
if (r_tmp >= DIV_NUM_BASE)
break;
}
if((int)q1 < 0) {
// result (which is positive and unsigned here)
// can't fit into long due to sign bit is used for value
MutableBigInteger mq = new MutableBigInteger(new int[]{(int)q1, (int)q0});
if (roundingMode == ROUND_DOWN && scale == preferredScale) {
return mq.toBigDecimal(sign, scale);
}
long r = mulsub(u1, u0, v1, v0, q0) >>> shift;
if (r != 0) {
if(needIncrement(divisor >>> shift, roundingMode, sign, mq, r)){
mq.add(MutableBigInteger.ONE);
}
return mq.toBigDecimal(sign, scale);
} else {
if (preferredScale != scale) {
BigInteger intVal = mq.toBigInteger(sign);
return createAndStripZerosToMatchScale(intVal,scale, preferredScale);
} else {
return mq.toBigDecimal(sign, scale);
}
}
}
long q = make64(q1,q0);
q*=sign;
if (roundingMode == ROUND_DOWN && scale == preferredScale)
return valueOf(q, scale);
long r = mulsub(u1, u0, v1, v0, q0) >>> shift;
if (r != 0) {
boolean increment = needIncrement(divisor >>> shift, roundingMode, sign, q, r);
return valueOf((increment ? q + sign : q), scale);
} else {
if (preferredScale != scale) {
return createAndStripZerosToMatchScale(q, scale, preferredScale);
} else {
return valueOf(q, scale);
}
}
} |
Returns a {@code BigDecimal} whose value is {@code (xs /
ys)}, with rounding according to the context settings.
private static BigDecimal divide(BigInteger xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc) {
// Normalize dividend & divisor so that both fall into [0.1, 0.999...]
if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
yscale -= 1; // [that is, divisor *= 10]
}
int mcp = mc.precision;
int roundingMode = mc.roundingMode.oldMode;
// In order to find out whether the divide generates the exact result,
// we avoid calling the above divide method. 'quotient' holds the
// return BigDecimal object whose scale will be set to 'scl'.
BigDecimal quotient;
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
int raise = checkScaleNonZero((long) mcp + yscale - xscale);
BigInteger rb = bigMultiplyPowerTen(xs,raise);
quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
} else {
int newScale = checkScaleNonZero((long) xscale - mcp);
int raise = checkScaleNonZero((long) newScale - yscale);
BigInteger rb = bigMultiplyPowerTen(ys,raise);
quotient = divideAndRound(xs, rb, scl, roundingMode,checkScaleNonZero(preferredScale));
}
// doRound, here, only affects 1000000000 case.
return doRound(quotient, mc);
}
/*
performs divideAndRound for (dividend0*dividend1, divisor)
returns null if quotient can't fit into long value;
private static BigDecimal multiplyDivideAndRound(long dividend0, long dividend1, long divisor, int scale, int roundingMode,
int preferredScale) {
int qsign = Long.signum(dividend0)*Long.signum(dividend1)*Long.signum(divisor);
dividend0 = Math.abs(dividend0);
dividend1 = Math.abs(dividend1);
divisor = Math.abs(divisor);
// multiply dividend0 * dividend1
long d0_hi = dividend0 >>> 32;
long d0_lo = dividend0 & LONG_MASK;
long d1_hi = dividend1 >>> 32;
long d1_lo = dividend1 & LONG_MASK;
long product = d0_lo * d1_lo;
long d0 = product & LONG_MASK;
long d1 = product >>> 32;
product = d0_hi * d1_lo + d1;
d1 = product & LONG_MASK;
long d2 = product >>> 32;
product = d0_lo * d1_hi + d1;
d1 = product & LONG_MASK;
d2 += product >>> 32;
long d3 = d2>>>32;
d2 &= LONG_MASK;
product = d0_hi*d1_hi + d2;
d2 = product & LONG_MASK;
d3 = ((product>>>32) + d3) & LONG_MASK;
final long dividendHi = make64(d3,d2);
final long dividendLo = make64(d1,d0);
// divide
return divideAndRound128(dividendHi, dividendLo, divisor, qsign, scale, roundingMode, preferredScale);
}
private static final long DIV_NUM_BASE = (1L<<32); // Number base (32 bits).
/*
divideAndRound 128-bit value by long divisor.
returns null if quotient can't fit into long value;
Specialized version of Knuth's division
| StringBuilderHelper::divideAndRound128 | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal roundedTenPower(int qsign, int raise, int scale, int preferredScale) {
if (scale > preferredScale) {
int diff = scale - preferredScale;
if(diff < raise) {
return scaledTenPow(raise - diff, qsign, preferredScale);
} else {
return valueOf(qsign,scale-raise);
}
} else {
return scaledTenPow(raise, qsign, scale);
}
} |
Returns a {@code BigDecimal} whose value is {@code (xs /
ys)}, with rounding according to the context settings.
private static BigDecimal divide(BigInteger xs, int xscale, BigInteger ys, int yscale, long preferredScale, MathContext mc) {
// Normalize dividend & divisor so that both fall into [0.1, 0.999...]
if (compareMagnitudeNormalized(xs, xscale, ys, yscale) > 0) {// satisfy constraint (b)
yscale -= 1; // [that is, divisor *= 10]
}
int mcp = mc.precision;
int roundingMode = mc.roundingMode.oldMode;
// In order to find out whether the divide generates the exact result,
// we avoid calling the above divide method. 'quotient' holds the
// return BigDecimal object whose scale will be set to 'scl'.
BigDecimal quotient;
int scl = checkScaleNonZero(preferredScale + yscale - xscale + mcp);
if (checkScaleNonZero((long) mcp + yscale - xscale) > 0) {
int raise = checkScaleNonZero((long) mcp + yscale - xscale);
BigInteger rb = bigMultiplyPowerTen(xs,raise);
quotient = divideAndRound(rb, ys, scl, roundingMode, checkScaleNonZero(preferredScale));
} else {
int newScale = checkScaleNonZero((long) xscale - mcp);
int raise = checkScaleNonZero((long) newScale - yscale);
BigInteger rb = bigMultiplyPowerTen(ys,raise);
quotient = divideAndRound(xs, rb, scl, roundingMode,checkScaleNonZero(preferredScale));
}
// doRound, here, only affects 1000000000 case.
return doRound(quotient, mc);
}
/*
performs divideAndRound for (dividend0*dividend1, divisor)
returns null if quotient can't fit into long value;
private static BigDecimal multiplyDivideAndRound(long dividend0, long dividend1, long divisor, int scale, int roundingMode,
int preferredScale) {
int qsign = Long.signum(dividend0)*Long.signum(dividend1)*Long.signum(divisor);
dividend0 = Math.abs(dividend0);
dividend1 = Math.abs(dividend1);
divisor = Math.abs(divisor);
// multiply dividend0 * dividend1
long d0_hi = dividend0 >>> 32;
long d0_lo = dividend0 & LONG_MASK;
long d1_hi = dividend1 >>> 32;
long d1_lo = dividend1 & LONG_MASK;
long product = d0_lo * d1_lo;
long d0 = product & LONG_MASK;
long d1 = product >>> 32;
product = d0_hi * d1_lo + d1;
d1 = product & LONG_MASK;
long d2 = product >>> 32;
product = d0_lo * d1_hi + d1;
d1 = product & LONG_MASK;
d2 += product >>> 32;
long d3 = d2>>>32;
d2 &= LONG_MASK;
product = d0_hi*d1_hi + d2;
d2 = product & LONG_MASK;
d3 = ((product>>>32) + d3) & LONG_MASK;
final long dividendHi = make64(d3,d2);
final long dividendLo = make64(d1,d0);
// divide
return divideAndRound128(dividendHi, dividendLo, divisor, qsign, scale, roundingMode, preferredScale);
}
private static final long DIV_NUM_BASE = (1L<<32); // Number base (32 bits).
/*
divideAndRound 128-bit value by long divisor.
returns null if quotient can't fit into long value;
Specialized version of Knuth's division
private static BigDecimal divideAndRound128(final long dividendHi, final long dividendLo, long divisor, int sign,
int scale, int roundingMode, int preferredScale) {
if (dividendHi >= divisor) {
return null;
}
final int shift = Long.numberOfLeadingZeros(divisor);
divisor <<= shift;
final long v1 = divisor >>> 32;
final long v0 = divisor & LONG_MASK;
long q1, q0;
long r_tmp;
long tmp = dividendLo << shift;
long u1 = tmp >>> 32;
long u0 = tmp & LONG_MASK;
tmp = (dividendHi << shift) | (dividendLo >>> 64 - shift);
long u2 = tmp & LONG_MASK;
tmp = divWord(tmp,v1);
q1 = tmp & LONG_MASK;
r_tmp = tmp >>> 32;
while(q1 >= DIV_NUM_BASE || unsignedLongCompare(q1*v0, make64(r_tmp, u1))) {
q1--;
r_tmp += v1;
if (r_tmp >= DIV_NUM_BASE)
break;
}
tmp = mulsub(u2,u1,v1,v0,q1);
u1 = tmp & LONG_MASK;
tmp = divWord(tmp,v1);
q0 = tmp & LONG_MASK;
r_tmp = tmp >>> 32;
while(q0 >= DIV_NUM_BASE || unsignedLongCompare(q0*v0,make64(r_tmp,u0))) {
q0--;
r_tmp += v1;
if (r_tmp >= DIV_NUM_BASE)
break;
}
if((int)q1 < 0) {
// result (which is positive and unsigned here)
// can't fit into long due to sign bit is used for value
MutableBigInteger mq = new MutableBigInteger(new int[]{(int)q1, (int)q0});
if (roundingMode == ROUND_DOWN && scale == preferredScale) {
return mq.toBigDecimal(sign, scale);
}
long r = mulsub(u1, u0, v1, v0, q0) >>> shift;
if (r != 0) {
if(needIncrement(divisor >>> shift, roundingMode, sign, mq, r)){
mq.add(MutableBigInteger.ONE);
}
return mq.toBigDecimal(sign, scale);
} else {
if (preferredScale != scale) {
BigInteger intVal = mq.toBigInteger(sign);
return createAndStripZerosToMatchScale(intVal,scale, preferredScale);
} else {
return mq.toBigDecimal(sign, scale);
}
}
}
long q = make64(q1,q0);
q*=sign;
if (roundingMode == ROUND_DOWN && scale == preferredScale)
return valueOf(q, scale);
long r = mulsub(u1, u0, v1, v0, q0) >>> shift;
if (r != 0) {
boolean increment = needIncrement(divisor >>> shift, roundingMode, sign, q, r);
return valueOf((increment ? q + sign : q), scale);
} else {
if (preferredScale != scale) {
return createAndStripZerosToMatchScale(q, scale, preferredScale);
} else {
return valueOf(q, scale);
}
}
}
/*
calculate divideAndRound for ldividend*10^raise / divisor
when abs(dividend)==abs(divisor);
| StringBuilderHelper::roundedTenPower | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal multiplyAndRound(long x, long y, int scale, MathContext mc) {
long product = multiply(x, y);
if(product!=INFLATED) {
return doRound(product, scale, mc);
}
// attempt to do it in 128 bits
int rsign = 1;
if(x < 0) {
x = -x;
rsign = -1;
}
if(y < 0) {
y = -y;
rsign *= -1;
}
// multiply dividend0 * dividend1
long m0_hi = x >>> 32;
long m0_lo = x & LONG_MASK;
long m1_hi = y >>> 32;
long m1_lo = y & LONG_MASK;
product = m0_lo * m1_lo;
long m0 = product & LONG_MASK;
long m1 = product >>> 32;
product = m0_hi * m1_lo + m1;
m1 = product & LONG_MASK;
long m2 = product >>> 32;
product = m0_lo * m1_hi + m1;
m1 = product & LONG_MASK;
m2 += product >>> 32;
long m3 = m2>>>32;
m2 &= LONG_MASK;
product = m0_hi*m1_hi + m2;
m2 = product & LONG_MASK;
m3 = ((product>>>32) + m3) & LONG_MASK;
final long mHi = make64(m3,m2);
final long mLo = make64(m1,m0);
BigDecimal res = doRound128(mHi, mLo, rsign, scale, mc);
if(res!=null) {
return res;
}
res = new BigDecimal(BigInteger.valueOf(x).multiply(y*rsign), INFLATED, scale, 0);
return doRound(res,mc);
} |
Multiplies two long values and rounds according {@code MathContext}
| StringBuilderHelper::multiplyAndRound | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static BigDecimal doRound128(long hi, long lo, int sign, int scale, MathContext mc) {
int mcp = mc.precision;
int drop;
BigDecimal res = null;
if(((drop = precision(hi, lo) - mcp) > 0)&&(drop<LONG_TEN_POWERS_TABLE.length)) {
scale = checkScaleNonZero((long)scale - drop);
res = divideAndRound128(hi, lo, LONG_TEN_POWERS_TABLE[drop], sign, scale, mc.roundingMode.oldMode, scale);
}
if(res!=null) {
return doRound(res,mc);
}
return null;
} |
rounds 128-bit value according {@code MathContext}
returns null if result can't be repsented as compact BigDecimal.
| StringBuilderHelper::doRound128 | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static int precision(long hi, long lo){
if(hi==0) {
if(lo>=0) {
return longDigitLength(lo);
}
return (unsignedLongCompareEq(lo, LONGLONG_TEN_POWERS_TABLE[0][1])) ? 20 : 19;
// 0x8AC7230489E80000L = unsigned 2^19
}
int r = ((128 - Long.numberOfLeadingZeros(hi) + 1) * 1233) >>> 12;
int idx = r-19;
return (idx >= LONGLONG_TEN_POWERS_TABLE.length || longLongCompareMagnitude(hi, lo,
LONGLONG_TEN_POWERS_TABLE[idx][0], LONGLONG_TEN_POWERS_TABLE[idx][1])) ? r : r + 1;
} |
rounds 128-bit value according {@code MathContext}
returns null if result can't be repsented as compact BigDecimal.
private static BigDecimal doRound128(long hi, long lo, int sign, int scale, MathContext mc) {
int mcp = mc.precision;
int drop;
BigDecimal res = null;
if(((drop = precision(hi, lo) - mcp) > 0)&&(drop<LONG_TEN_POWERS_TABLE.length)) {
scale = checkScaleNonZero((long)scale - drop);
res = divideAndRound128(hi, lo, LONG_TEN_POWERS_TABLE[drop], sign, scale, mc.roundingMode.oldMode, scale);
}
if(res!=null) {
return doRound(res,mc);
}
return null;
}
private static final long[][] LONGLONG_TEN_POWERS_TABLE = {
{ 0L, 0x8AC7230489E80000L }, //10^19
{ 0x5L, 0x6bc75e2d63100000L }, //10^20
{ 0x36L, 0x35c9adc5dea00000L }, //10^21
{ 0x21eL, 0x19e0c9bab2400000L }, //10^22
{ 0x152dL, 0x02c7e14af6800000L }, //10^23
{ 0xd3c2L, 0x1bcecceda1000000L }, //10^24
{ 0x84595L, 0x161401484a000000L }, //10^25
{ 0x52b7d2L, 0xdcc80cd2e4000000L }, //10^26
{ 0x33b2e3cL, 0x9fd0803ce8000000L }, //10^27
{ 0x204fce5eL, 0x3e25026110000000L }, //10^28
{ 0x1431e0faeL, 0x6d7217caa0000000L }, //10^29
{ 0xc9f2c9cd0L, 0x4674edea40000000L }, //10^30
{ 0x7e37be2022L, 0xc0914b2680000000L }, //10^31
{ 0x4ee2d6d415bL, 0x85acef8100000000L }, //10^32
{ 0x314dc6448d93L, 0x38c15b0a00000000L }, //10^33
{ 0x1ed09bead87c0L, 0x378d8e6400000000L }, //10^34
{ 0x13426172c74d82L, 0x2b878fe800000000L }, //10^35
{ 0xc097ce7bc90715L, 0xb34b9f1000000000L }, //10^36
{ 0x785ee10d5da46d9L, 0x00f436a000000000L }, //10^37
{ 0x4b3b4ca85a86c47aL, 0x098a224000000000L }, //10^38
};
/*
returns precision of 128-bit value
| StringBuilderHelper::precision | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
private static boolean longLongCompareMagnitude(long hi0, long lo0, long hi1, long lo1) {
if(hi0!=hi1) {
return hi0<hi1;
}
return (lo0+Long.MIN_VALUE) <(lo1+Long.MIN_VALUE);
} |
rounds 128-bit value according {@code MathContext}
returns null if result can't be repsented as compact BigDecimal.
private static BigDecimal doRound128(long hi, long lo, int sign, int scale, MathContext mc) {
int mcp = mc.precision;
int drop;
BigDecimal res = null;
if(((drop = precision(hi, lo) - mcp) > 0)&&(drop<LONG_TEN_POWERS_TABLE.length)) {
scale = checkScaleNonZero((long)scale - drop);
res = divideAndRound128(hi, lo, LONG_TEN_POWERS_TABLE[drop], sign, scale, mc.roundingMode.oldMode, scale);
}
if(res!=null) {
return doRound(res,mc);
}
return null;
}
private static final long[][] LONGLONG_TEN_POWERS_TABLE = {
{ 0L, 0x8AC7230489E80000L }, //10^19
{ 0x5L, 0x6bc75e2d63100000L }, //10^20
{ 0x36L, 0x35c9adc5dea00000L }, //10^21
{ 0x21eL, 0x19e0c9bab2400000L }, //10^22
{ 0x152dL, 0x02c7e14af6800000L }, //10^23
{ 0xd3c2L, 0x1bcecceda1000000L }, //10^24
{ 0x84595L, 0x161401484a000000L }, //10^25
{ 0x52b7d2L, 0xdcc80cd2e4000000L }, //10^26
{ 0x33b2e3cL, 0x9fd0803ce8000000L }, //10^27
{ 0x204fce5eL, 0x3e25026110000000L }, //10^28
{ 0x1431e0faeL, 0x6d7217caa0000000L }, //10^29
{ 0xc9f2c9cd0L, 0x4674edea40000000L }, //10^30
{ 0x7e37be2022L, 0xc0914b2680000000L }, //10^31
{ 0x4ee2d6d415bL, 0x85acef8100000000L }, //10^32
{ 0x314dc6448d93L, 0x38c15b0a00000000L }, //10^33
{ 0x1ed09bead87c0L, 0x378d8e6400000000L }, //10^34
{ 0x13426172c74d82L, 0x2b878fe800000000L }, //10^35
{ 0xc097ce7bc90715L, 0xb34b9f1000000000L }, //10^36
{ 0x785ee10d5da46d9L, 0x00f436a000000000L }, //10^37
{ 0x4b3b4ca85a86c47aL, 0x098a224000000000L }, //10^38
};
/*
returns precision of 128-bit value
private static int precision(long hi, long lo){
if(hi==0) {
if(lo>=0) {
return longDigitLength(lo);
}
return (unsignedLongCompareEq(lo, LONGLONG_TEN_POWERS_TABLE[0][1])) ? 20 : 19;
// 0x8AC7230489E80000L = unsigned 2^19
}
int r = ((128 - Long.numberOfLeadingZeros(hi) + 1) * 1233) >>> 12;
int idx = r-19;
return (idx >= LONGLONG_TEN_POWERS_TABLE.length || longLongCompareMagnitude(hi, lo,
LONGLONG_TEN_POWERS_TABLE[idx][0], LONGLONG_TEN_POWERS_TABLE[idx][1])) ? r : r + 1;
}
/*
returns true if 128 bit number <hi0,lo0> is less then <hi1,lo1>
hi0 & hi1 should be non-negative
| StringBuilderHelper::longLongCompareMagnitude | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java | Apache-2.0 |
MutableBigInteger() {
value = new int[1];
intLen = 0;
} |
The default constructor. An empty MutableBigInteger is created with
a one word capacity.
| MutableBigInteger::MutableBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger(int val) {
value = new int[1];
intLen = 1;
value[0] = val;
} |
Construct a new MutableBigInteger with a magnitude specified by
the int val.
| MutableBigInteger::MutableBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger(int[] val) {
value = val;
intLen = val.length;
} |
Construct a new MutableBigInteger with the specified value array
up to the length of the array supplied.
| MutableBigInteger::MutableBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger(BigInteger b) {
intLen = b.mag.length;
value = Arrays.copyOf(b.mag, intLen);
} |
Construct a new MutableBigInteger with a magnitude equal to the
specified BigInteger.
| MutableBigInteger::MutableBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger(MutableBigInteger val) {
intLen = val.intLen;
value = Arrays.copyOfRange(val.value, val.offset, val.offset + intLen);
} |
Construct a new MutableBigInteger with a magnitude equal to the
specified MutableBigInteger.
| MutableBigInteger::MutableBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private void ones(int n) {
if (n > value.length)
value = new int[n];
Arrays.fill(value, -1);
offset = 0;
intLen = n;
} |
Makes this number an {@code n}-int number all of whose bits are ones.
Used by Burnikel-Ziegler division.
@param n number of ints in the {@code value} array
@return a number equal to {@code ((1<<(32*n)))-1}
| MutableBigInteger::ones | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int[] getMagnitudeArray() {
if (offset > 0 || value.length != intLen)
return Arrays.copyOfRange(value, offset, offset + intLen);
return value;
} |
Internal helper method to return the magnitude array. The caller is not
supposed to modify the returned array.
| MutableBigInteger::getMagnitudeArray | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private long toLong() {
assert (intLen <= 2) : "this MutableBigInteger exceeds the range of long";
if (intLen == 0)
return 0;
long d = value[offset] & LONG_MASK;
return (intLen == 2) ? d << 32 | (value[offset + 1] & LONG_MASK) : d;
} |
Convert this MutableBigInteger to a long value. The caller has to make
sure this MutableBigInteger can be fit into long.
| MutableBigInteger::toLong | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
BigInteger toBigInteger(int sign) {
if (intLen == 0 || sign == 0)
return BigInteger.ZERO;
return new BigInteger(getMagnitudeArray(), sign);
} |
Convert this MutableBigInteger to a BigInteger object.
| MutableBigInteger::toBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
BigInteger toBigInteger() {
normalize();
return toBigInteger(isZero() ? 0 : 1);
} |
Converts this number to a nonnegative {@code BigInteger}.
| MutableBigInteger::toBigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
BigDecimal toBigDecimal(int sign, int scale) {
if (intLen == 0 || sign == 0)
return BigDecimal.zeroValueOf(scale);
int[] mag = getMagnitudeArray();
int len = mag.length;
int d = mag[0];
// If this MutableBigInteger can't be fit into long, we need to
// make a BigInteger object for the resultant BigDecimal object.
if (len > 2 || (d < 0 && len == 2))
return new BigDecimal(new BigInteger(mag, sign), INFLATED, scale, 0);
long v = (len == 2) ?
((mag[1] & LONG_MASK) | (d & LONG_MASK) << 32) :
d & LONG_MASK;
return BigDecimal.valueOf(sign == -1 ? -v : v, scale);
} |
Convert this MutableBigInteger to BigDecimal object with the specified sign
and scale.
| MutableBigInteger::toBigDecimal | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
long toCompactValue(int sign) {
if (intLen == 0 || sign == 0)
return 0L;
int[] mag = getMagnitudeArray();
int len = mag.length;
int d = mag[0];
// If this MutableBigInteger can not be fitted into long, we need to
// make a BigInteger object for the resultant BigDecimal object.
if (len > 2 || (d < 0 && len == 2))
return INFLATED;
long v = (len == 2) ?
((mag[1] & LONG_MASK) | (d & LONG_MASK) << 32) :
d & LONG_MASK;
return sign == -1 ? -v : v;
} |
This is for internal use in converting from a MutableBigInteger
object into a long value given a specified sign.
returns INFLATED if value is not fit into long
| MutableBigInteger::toCompactValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void clear() {
offset = intLen = 0;
for (int index=0, n=value.length; index < n; index++)
value[index] = 0;
} |
Clear out a MutableBigInteger for reuse.
| MutableBigInteger::clear | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void reset() {
offset = intLen = 0;
} |
Set a MutableBigInteger to zero, removing its offset.
| MutableBigInteger::reset | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
final int compare(MutableBigInteger b) {
int blen = b.intLen;
if (intLen < blen)
return -1;
if (intLen > blen)
return 1;
// Add Integer.MIN_VALUE to make the comparison act as unsigned integer
// comparison.
int[] bval = b.value;
for (int i = offset, j = b.offset; i < intLen + offset; i++, j++) {
int b1 = value[i] + 0x80000000;
int b2 = bval[j] + 0x80000000;
if (b1 < b2)
return -1;
if (b1 > b2)
return 1;
}
return 0;
} |
Compare the magnitude of two MutableBigIntegers. Returns -1, 0 or 1
as this MutableBigInteger is numerically less than, equal to, or
greater than <tt>b</tt>.
| MutableBigInteger::compare | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int compareShifted(MutableBigInteger b, int ints) {
int blen = b.intLen;
int alen = intLen - ints;
if (alen < blen)
return -1;
if (alen > blen)
return 1;
// Add Integer.MIN_VALUE to make the comparison act as unsigned integer
// comparison.
int[] bval = b.value;
for (int i = offset, j = b.offset; i < alen + offset; i++, j++) {
int b1 = value[i] + 0x80000000;
int b2 = bval[j] + 0x80000000;
if (b1 < b2)
return -1;
if (b1 > b2)
return 1;
}
return 0;
} |
Returns a value equal to what {@code b.leftShift(32*ints); return compare(b);}
would return, but doesn't change the value of {@code b}.
| MutableBigInteger::compareShifted | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
final int compareHalf(MutableBigInteger b) {
int blen = b.intLen;
int len = intLen;
if (len <= 0)
return blen <= 0 ? 0 : -1;
if (len > blen)
return 1;
if (len < blen - 1)
return -1;
int[] bval = b.value;
int bstart = 0;
int carry = 0;
// Only 2 cases left:len == blen or len == blen - 1
if (len != blen) { // len == blen - 1
if (bval[bstart] == 1) {
++bstart;
carry = 0x80000000;
} else
return -1;
}
// compare values with right-shifted values of b,
// carrying shifted-out bits across words
int[] val = value;
for (int i = offset, j = bstart; i < len + offset;) {
int bv = bval[j++];
long hb = ((bv >>> 1) + carry) & LONG_MASK;
long v = val[i++] & LONG_MASK;
if (v != hb)
return v < hb ? -1 : 1;
carry = (bv & 1) << 31; // carray will be either 0x80000000 or 0
}
return carry == 0 ? 0 : -1;
} |
Compare this against half of a MutableBigInteger object (Needed for
remainder tests).
Assumes no leading unnecessary zeros, which holds for results
from divide().
| MutableBigInteger::compareHalf | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final int getLowestSetBit() {
if (intLen == 0)
return -1;
int j, b;
for (j=intLen-1; (j > 0) && (value[j+offset] == 0); j--)
;
b = value[j+offset];
if (b == 0)
return -1;
return ((intLen-1-j)<<5) + Integer.numberOfTrailingZeros(b);
} |
Return the index of the lowest set bit in this MutableBigInteger. If the
magnitude of this MutableBigInteger is zero, -1 is returned.
| MutableBigInteger::getLowestSetBit | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final int getInt(int index) {
return value[offset+index];
} |
Return the int in use in this MutableBigInteger at the specified
index. This method is not used because it is not inlined on all
platforms.
| MutableBigInteger::getInt | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final long getLong(int index) {
return value[offset+index] & LONG_MASK;
} |
Return a long which is equal to the unsigned value of the int in
use in this MutableBigInteger at the specified index. This method is
not used because it is not inlined on all platforms.
| MutableBigInteger::getLong | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
final void normalize() {
if (intLen == 0) {
offset = 0;
return;
}
int index = offset;
if (value[index] != 0)
return;
int indexBound = index+intLen;
do {
index++;
} while(index < indexBound && value[index] == 0);
int numZeros = index - offset;
intLen -= numZeros;
offset = (intLen == 0 ? 0 : offset+numZeros);
} |
Ensure that the MutableBigInteger is in normal form, specifically
making sure that there are no leading zeros, and that if the
magnitude is zero, then intLen is zero.
| MutableBigInteger::normalize | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final void ensureCapacity(int len) {
if (value.length < len) {
value = new int[len];
offset = 0;
intLen = len;
}
} |
If this MutableBigInteger cannot hold len words, increase the size
of the value array to len words.
| MutableBigInteger::ensureCapacity | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
int[] toIntArray() {
int[] result = new int[intLen];
for(int i=0; i < intLen; i++)
result[i] = value[offset+i];
return result;
} |
Convert this MutableBigInteger into an int array with no leading
zeros, of a length that is equal to this MutableBigInteger's intLen.
| MutableBigInteger::toIntArray | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void setInt(int index, int val) {
value[offset + index] = val;
} |
Sets the int at index+offset in this MutableBigInteger to val.
This does not get inlined on all platforms so it is not used
as often as originally intended.
| MutableBigInteger::setInt | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void setValue(int[] val, int length) {
value = val;
intLen = length;
offset = 0;
} |
Sets this MutableBigInteger's value array to the specified array.
The intLen is set to the specified length.
| MutableBigInteger::setValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void copyValue(MutableBigInteger src) {
int len = src.intLen;
if (value.length < len)
value = new int[len];
System.arraycopy(src.value, src.offset, value, 0, len);
intLen = len;
offset = 0;
} |
Sets this MutableBigInteger's value array to a copy of the specified
array. The intLen is set to the length of the new array.
| MutableBigInteger::copyValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void copyValue(int[] val) {
int len = val.length;
if (value.length < len)
value = new int[len];
System.arraycopy(val, 0, value, 0, len);
intLen = len;
offset = 0;
} |
Sets this MutableBigInteger's value array to a copy of the specified
array. The intLen is set to the length of the specified array.
| MutableBigInteger::copyValue | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isOne() {
return (intLen == 1) && (value[offset] == 1);
} |
Returns true iff this MutableBigInteger has a value of one.
| MutableBigInteger::isOne | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isZero() {
return (intLen == 0);
} |
Returns true iff this MutableBigInteger has a value of zero.
| MutableBigInteger::isZero | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isEven() {
return (intLen == 0) || ((value[offset + intLen - 1] & 1) == 0);
} |
Returns true iff this MutableBigInteger is even.
| MutableBigInteger::isEven | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isOdd() {
return isZero() ? false : ((value[offset + intLen - 1] & 1) == 1);
} |
Returns true iff this MutableBigInteger is odd.
| MutableBigInteger::isOdd | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
boolean isNormal() {
if (intLen + offset > value.length)
return false;
if (intLen == 0)
return true;
return (value[offset] != 0);
} |
Returns true iff this MutableBigInteger is in normal form. A
MutableBigInteger is in normal form if it has no leading zeros
after the offset, and intLen + offset <= value.length.
| MutableBigInteger::isNormal | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
public String toString() {
BigInteger b = toBigInteger(1);
return b.toString();
} |
Returns a String representation of this MutableBigInteger in radix 10.
| MutableBigInteger::toString | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void safeRightShift(int n) {
if (n/32 >= intLen) {
reset();
} else {
rightShift(n);
}
} |
Like {@link #rightShift(int)} but {@code n} can be greater than the length of the number.
| MutableBigInteger::safeRightShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void rightShift(int n) {
if (intLen == 0)
return;
int nInts = n >>> 5;
int nBits = n & 0x1F;
this.intLen -= nInts;
if (nBits == 0)
return;
int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
if (nBits >= bitsInHighWord) {
this.primitiveLeftShift(32 - nBits);
this.intLen--;
} else {
primitiveRightShift(nBits);
}
} |
Right shift this MutableBigInteger n bits. The MutableBigInteger is left
in normal form.
| MutableBigInteger::rightShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void safeLeftShift(int n) {
if (n > 0) {
leftShift(n);
}
} |
Like {@link #leftShift(int)} but {@code n} can be zero.
| MutableBigInteger::safeLeftShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void leftShift(int n) {
/*
* If there is enough storage space in this MutableBigInteger already
* the available space will be used. Space to the right of the used
* ints in the value array is faster to utilize, so the extra space
* will be taken from the right if possible.
*/
if (intLen == 0)
return;
int nInts = n >>> 5;
int nBits = n&0x1F;
int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
// If shift can be done without moving words, do so
if (n <= (32-bitsInHighWord)) {
primitiveLeftShift(nBits);
return;
}
int newLen = intLen + nInts +1;
if (nBits <= (32-bitsInHighWord))
newLen--;
if (value.length < newLen) {
// The array must grow
int[] result = new int[newLen];
for (int i=0; i < intLen; i++)
result[i] = value[offset+i];
setValue(result, newLen);
} else if (value.length - offset >= newLen) {
// Use space on right
for(int i=0; i < newLen - intLen; i++)
value[offset+intLen+i] = 0;
} else {
// Must use space on left
for (int i=0; i < intLen; i++)
value[i] = value[offset+i];
for (int i=intLen; i < newLen; i++)
value[i] = 0;
offset = 0;
}
intLen = newLen;
if (nBits == 0)
return;
if (nBits <= (32-bitsInHighWord))
primitiveLeftShift(nBits);
else
primitiveRightShift(32 -nBits);
} |
Left shift this MutableBigInteger n bits.
| MutableBigInteger::leftShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int divadd(int[] a, int[] result, int offset) {
long carry = 0;
for (int j=a.length-1; j >= 0; j--) {
long sum = (a[j] & LONG_MASK) +
(result[j+offset] & LONG_MASK) + carry;
result[j+offset] = (int)sum;
carry = sum >>> 32;
}
return (int)carry;
} |
A primitive used for division. This method adds in one multiple of the
divisor a back to the dividend result at a specified offset. It is used
when qhat was estimated too large, and must be adjusted.
| MutableBigInteger::divadd | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private final void primitiveLeftShift(int n) {
int[] val = value;
int n2 = 32 - n;
for (int i=offset, c=val[i], m=i+intLen-1; i < m; i++) {
int b = c;
c = val[i+1];
val[i] = (b << n) | (c >>> n2);
}
val[offset+intLen-1] <<= n;
} |
Left shift this MutableBigInteger n bits, where n is
less than 32.
Assumes that intLen > 0, n > 0 for speed
| MutableBigInteger::primitiveLeftShift | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private BigInteger getLower(int n) {
if (isZero()) {
return BigInteger.ZERO;
} else if (intLen < n) {
return toBigInteger(1);
} else {
// strip zeros
int len = n;
while (len > 0 && value[offset+intLen-len] == 0)
len--;
int sign = len > 0 ? 1 : 0;
return new BigInteger(Arrays.copyOfRange(value, offset+intLen-len, offset+intLen), sign);
}
} |
Returns a {@code BigInteger} equal to the {@code n}
low ints of this number.
| MutableBigInteger::getLower | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private void keepLower(int n) {
if (intLen >= n) {
offset += intLen - n;
intLen = n;
}
} |
Discards all ints whose index is greater than {@code n}.
| MutableBigInteger::keepLower | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void add(MutableBigInteger addend) {
int x = intLen;
int y = addend.intLen;
int resultLen = (intLen > addend.intLen ? intLen : addend.intLen);
int[] result = (value.length < resultLen ? new int[resultLen] : value);
int rstart = result.length-1;
long sum;
long carry = 0;
// Add common parts of both numbers
while(x > 0 && y > 0) {
x--; y--;
sum = (value[x+offset] & LONG_MASK) +
(addend.value[y+addend.offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
// Add remainder of the longer number
while(x > 0) {
x--;
if (carry == 0 && result == value && rstart == (x + offset))
return;
sum = (value[x+offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
while(y > 0) {
y--;
sum = (addend.value[y+addend.offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
if (carry > 0) { // Result must grow in length
resultLen++;
if (result.length < resultLen) {
int temp[] = new int[resultLen];
// Result one word longer from carry-out; copy low-order
// bits into new result.
System.arraycopy(result, 0, temp, 1, result.length);
temp[0] = 1;
result = temp;
} else {
result[rstart--] = 1;
}
}
value = result;
intLen = resultLen;
offset = result.length - resultLen;
} |
Adds the contents of two MutableBigInteger objects.The result
is placed within this MutableBigInteger.
The contents of the addend are not changed.
| MutableBigInteger::add | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void addShifted(MutableBigInteger addend, int n) {
if (addend.isZero()) {
return;
}
int x = intLen;
int y = addend.intLen + n;
int resultLen = (intLen > y ? intLen : y);
int[] result = (value.length < resultLen ? new int[resultLen] : value);
int rstart = result.length-1;
long sum;
long carry = 0;
// Add common parts of both numbers
while (x > 0 && y > 0) {
x--; y--;
int bval = y+addend.offset < addend.value.length ? addend.value[y+addend.offset] : 0;
sum = (value[x+offset] & LONG_MASK) +
(bval & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
// Add remainder of the longer number
while (x > 0) {
x--;
if (carry == 0 && result == value && rstart == (x + offset)) {
return;
}
sum = (value[x+offset] & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
while (y > 0) {
y--;
int bval = y+addend.offset < addend.value.length ? addend.value[y+addend.offset] : 0;
sum = (bval & LONG_MASK) + carry;
result[rstart--] = (int)sum;
carry = sum >>> 32;
}
if (carry > 0) { // Result must grow in length
resultLen++;
if (result.length < resultLen) {
int temp[] = new int[resultLen];
// Result one word longer from carry-out; copy low-order
// bits into new result.
System.arraycopy(result, 0, temp, 1, result.length);
temp[0] = 1;
result = temp;
} else {
result[rstart--] = 1;
}
}
value = result;
intLen = resultLen;
offset = result.length - resultLen;
} |
Adds the value of {@code addend} shifted {@code n} ints to the left.
Has the same effect as {@code addend.leftShift(32*ints); add(addend);}
but doesn't change the value of {@code addend}.
| MutableBigInteger::addShifted | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void addDisjoint(MutableBigInteger addend, int n) {
if (addend.isZero())
return;
int x = intLen;
int y = addend.intLen + n;
int resultLen = (intLen > y ? intLen : y);
int[] result;
if (value.length < resultLen)
result = new int[resultLen];
else {
result = value;
Arrays.fill(value, offset+intLen, value.length, 0);
}
int rstart = result.length-1;
// copy from this if needed
System.arraycopy(value, offset, result, rstart+1-x, x);
y -= x;
rstart -= x;
int len = Math.min(y, addend.value.length-addend.offset);
System.arraycopy(addend.value, addend.offset, result, rstart+1-y, len);
// zero the gap
for (int i=rstart+1-y+len; i < rstart+1; i++)
result[i] = 0;
value = result;
intLen = resultLen;
offset = result.length - resultLen;
} |
Like {@link #addShifted(MutableBigInteger, int)} but {@code this.intLen} must
not be greater than {@code n}. In other words, concatenates {@code this}
and {@code addend}.
| MutableBigInteger::addDisjoint | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void addLower(MutableBigInteger addend, int n) {
MutableBigInteger a = new MutableBigInteger(addend);
if (a.offset + a.intLen >= n) {
a.offset = a.offset + a.intLen - n;
a.intLen = n;
}
a.normalize();
add(a);
} |
Adds the low {@code n} ints of {@code addend}.
| MutableBigInteger::addLower | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
int subtract(MutableBigInteger b) {
MutableBigInteger a = this;
int[] result = value;
int sign = a.compare(b);
if (sign == 0) {
reset();
return 0;
}
if (sign < 0) {
MutableBigInteger tmp = a;
a = b;
b = tmp;
}
int resultLen = a.intLen;
if (result.length < resultLen)
result = new int[resultLen];
long diff = 0;
int x = a.intLen;
int y = b.intLen;
int rstart = result.length - 1;
// Subtract common parts of both numbers
while (y > 0) {
x--; y--;
diff = (a.value[x+a.offset] & LONG_MASK) -
(b.value[y+b.offset] & LONG_MASK) - ((int)-(diff>>32));
result[rstart--] = (int)diff;
}
// Subtract remainder of longer number
while (x > 0) {
x--;
diff = (a.value[x+a.offset] & LONG_MASK) - ((int)-(diff>>32));
result[rstart--] = (int)diff;
}
value = result;
intLen = resultLen;
offset = value.length - resultLen;
normalize();
return sign;
} |
Subtracts the smaller of this and b from the larger and places the
result into this MutableBigInteger.
| MutableBigInteger::subtract | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int difference(MutableBigInteger b) {
MutableBigInteger a = this;
int sign = a.compare(b);
if (sign == 0)
return 0;
if (sign < 0) {
MutableBigInteger tmp = a;
a = b;
b = tmp;
}
long diff = 0;
int x = a.intLen;
int y = b.intLen;
// Subtract common parts of both numbers
while (y > 0) {
x--; y--;
diff = (a.value[a.offset+ x] & LONG_MASK) -
(b.value[b.offset+ y] & LONG_MASK) - ((int)-(diff>>32));
a.value[a.offset+x] = (int)diff;
}
// Subtract remainder of longer number
while (x > 0) {
x--;
diff = (a.value[a.offset+ x] & LONG_MASK) - ((int)-(diff>>32));
a.value[a.offset+x] = (int)diff;
}
a.normalize();
return sign;
} |
Subtracts the smaller of a and b from the larger and places the result
into the larger. Returns 1 if the answer is in a, -1 if in b, 0 if no
operation was performed.
| MutableBigInteger::difference | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void multiply(MutableBigInteger y, MutableBigInteger z) {
int xLen = intLen;
int yLen = y.intLen;
int newLen = xLen + yLen;
// Put z into an appropriate state to receive product
if (z.value.length < newLen)
z.value = new int[newLen];
z.offset = 0;
z.intLen = newLen;
// The first iteration is hoisted out of the loop to avoid extra add
long carry = 0;
for (int j=yLen-1, k=yLen+xLen-1; j >= 0; j--, k--) {
long product = (y.value[j+y.offset] & LONG_MASK) *
(value[xLen-1+offset] & LONG_MASK) + carry;
z.value[k] = (int)product;
carry = product >>> 32;
}
z.value[xLen-1] = (int)carry;
// Perform the multiplication word by word
for (int i = xLen-2; i >= 0; i--) {
carry = 0;
for (int j=yLen-1, k=yLen+i; j >= 0; j--, k--) {
long product = (y.value[j+y.offset] & LONG_MASK) *
(value[i+offset] & LONG_MASK) +
(z.value[k] & LONG_MASK) + carry;
z.value[k] = (int)product;
carry = product >>> 32;
}
z.value[i] = (int)carry;
}
// Remove leading zeros from product
z.normalize();
} |
Multiply the contents of two MutableBigInteger objects. The result is
placed into MutableBigInteger z. The contents of y are not changed.
| MutableBigInteger::multiply | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
void mul(int y, MutableBigInteger z) {
if (y == 1) {
z.copyValue(this);
return;
}
if (y == 0) {
z.clear();
return;
}
// Perform the multiplication word by word
long ylong = y & LONG_MASK;
int[] zval = (z.value.length < intLen+1 ? new int[intLen + 1]
: z.value);
long carry = 0;
for (int i = intLen-1; i >= 0; i--) {
long product = ylong * (value[i+offset] & LONG_MASK) + carry;
zval[i+1] = (int)product;
carry = product >>> 32;
}
if (carry == 0) {
z.offset = 1;
z.intLen = intLen;
} else {
z.offset = 0;
z.intLen = intLen + 1;
zval[0] = (int)carry;
}
z.value = zval;
} |
Multiply the contents of this MutableBigInteger by the word y. The
result is placed into z.
| MutableBigInteger::mul | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
int divideOneWord(int divisor, MutableBigInteger quotient) {
long divisorLong = divisor & LONG_MASK;
// Special case of one word dividend
if (intLen == 1) {
long dividendValue = value[offset] & LONG_MASK;
int q = (int) (dividendValue / divisorLong);
int r = (int) (dividendValue - q * divisorLong);
quotient.value[0] = q;
quotient.intLen = (q == 0) ? 0 : 1;
quotient.offset = 0;
return r;
}
if (quotient.value.length < intLen)
quotient.value = new int[intLen];
quotient.offset = 0;
quotient.intLen = intLen;
// Normalize the divisor
int shift = Integer.numberOfLeadingZeros(divisor);
int rem = value[offset];
long remLong = rem & LONG_MASK;
if (remLong < divisorLong) {
quotient.value[0] = 0;
} else {
quotient.value[0] = (int)(remLong / divisorLong);
rem = (int) (remLong - (quotient.value[0] * divisorLong));
remLong = rem & LONG_MASK;
}
int xlen = intLen;
while (--xlen > 0) {
long dividendEstimate = (remLong << 32) |
(value[offset + intLen - xlen] & LONG_MASK);
int q;
if (dividendEstimate >= 0) {
q = (int) (dividendEstimate / divisorLong);
rem = (int) (dividendEstimate - q * divisorLong);
} else {
long tmp = divWord(dividendEstimate, divisor);
q = (int) (tmp & LONG_MASK);
rem = (int) (tmp >>> 32);
}
quotient.value[intLen - xlen] = q;
remLong = rem & LONG_MASK;
}
quotient.normalize();
// Unnormalize
if (shift > 0)
return rem % divisor;
else
return rem;
} |
This method is used for division of an n word dividend by a one word
divisor. The quotient is placed into quotient. The one word divisor is
specified by divisor.
@return the remainder of the division is returned.
| MutableBigInteger::divideOneWord | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger divide(MutableBigInteger b, MutableBigInteger quotient) {
return divide(b,quotient,true);
} |
Calculates the quotient of this div b and places the quotient in the
provided MutableBigInteger objects and the remainder object is returned.
| MutableBigInteger::divide | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger divideKnuth(MutableBigInteger b, MutableBigInteger quotient) {
return divideKnuth(b,quotient,true);
} |
@see #divideKnuth(MutableBigInteger, MutableBigInteger, boolean)
| MutableBigInteger::divideKnuth | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger divideKnuth(MutableBigInteger b, MutableBigInteger quotient, boolean needRemainder) {
if (b.intLen == 0)
throw new ArithmeticException("BigInteger divide by zero");
// Dividend is zero
if (intLen == 0) {
quotient.intLen = quotient.offset = 0;
return needRemainder ? new MutableBigInteger() : null;
}
int cmp = compare(b);
// Dividend less than divisor
if (cmp < 0) {
quotient.intLen = quotient.offset = 0;
return needRemainder ? new MutableBigInteger(this) : null;
}
// Dividend equal to divisor
if (cmp == 0) {
quotient.value[0] = quotient.intLen = 1;
quotient.offset = 0;
return needRemainder ? new MutableBigInteger() : null;
}
quotient.clear();
// Special case one word divisor
if (b.intLen == 1) {
int r = divideOneWord(b.value[b.offset], quotient);
if(needRemainder) {
if (r == 0)
return new MutableBigInteger();
return new MutableBigInteger(r);
} else {
return null;
}
}
// Cancel common powers of two if we're above the KNUTH_POW2_* thresholds
if (intLen >= KNUTH_POW2_THRESH_LEN) {
int trailingZeroBits = Math.min(getLowestSetBit(), b.getLowestSetBit());
if (trailingZeroBits >= KNUTH_POW2_THRESH_ZEROS*32) {
MutableBigInteger a = new MutableBigInteger(this);
b = new MutableBigInteger(b);
a.rightShift(trailingZeroBits);
b.rightShift(trailingZeroBits);
MutableBigInteger r = a.divideKnuth(b, quotient);
r.leftShift(trailingZeroBits);
return r;
}
}
return divideMagnitude(b, quotient, needRemainder);
} |
Calculates the quotient of this div b and places the quotient in the
provided MutableBigInteger objects and the remainder object is returned.
Uses Algorithm D in Knuth section 4.3.1.
Many optimizations to that algorithm have been adapted from the Colin
Plumb C library.
It special cases one word divisors for speed. The content of b is not
changed.
| MutableBigInteger::divideKnuth | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger divideAndRemainderBurnikelZiegler(MutableBigInteger b, MutableBigInteger quotient) {
int r = intLen;
int s = b.intLen;
// Clear the quotient
quotient.offset = quotient.intLen = 0;
if (r < s) {
return this;
} 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.
// step 1: let m = min{2^k | (2^k)*BURNIKEL_ZIEGLER_THRESHOLD > s}
int m = 1 << (32-Integer.numberOfLeadingZeros(s/BigInteger.BURNIKEL_ZIEGLER_THRESHOLD));
int j = (s+m-1) / m; // step 2a: j = ceil(s/m)
int n = j * m; // step 2b: block length in 32-bit units
long n32 = 32L * n; // block length in bits
int sigma = (int) Math.max(0, n32 - b.bitLength()); // step 3: sigma = max{T | (2^T)*B < beta^n}
MutableBigInteger bShifted = new MutableBigInteger(b);
bShifted.safeLeftShift(sigma); // step 4a: shift b so its length is a multiple of n
safeLeftShift(sigma); // step 4b: shift this by the same amount
// step 5: t is the number of blocks needed to accommodate this plus one additional bit
int t = (int) ((bitLength()+n32) / n32);
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
// step 7: z[t-2] = [a[t-1], a[t-2]]
MutableBigInteger z = getBlock(t-2, t, n); // the second to most significant block
z.addDisjoint(a1, n); // z[t-2]
// do schoolbook division on blocks, dividing 2-block numbers by 1-block numbers
MutableBigInteger qi = new MutableBigInteger();
MutableBigInteger ri;
for (int i=t-2; i > 0; i--) {
// step 8a: compute (qi,ri) such that z=b*qi+ri
ri = z.divide2n1n(bShifted, qi);
// step 8b: z = [ri, a[i-1]]
z = getBlock(i-1, t, n); // a[i-1]
z.addDisjoint(ri, n);
quotient.addShifted(qi, i*n); // update q (part of step 9)
}
// final iteration of step 8: do the loop one more time for i=0 but leave z unchanged
ri = z.divide2n1n(bShifted, qi);
quotient.add(qi);
ri.rightShift(sigma); // step 9: this and b were shifted, so shift back
return ri;
}
} |
Computes {@code this/b} and {@code this%b} using the
<a href="http://cr.yp.to/bib/1998/burnikel.ps"> Burnikel-Ziegler algorithm</a>.
This method implements algorithm 3 from pg. 9 of the Burnikel-Ziegler paper.
The parameter beta was chosen to b 2<sup>32</sup> so almost all shifts are
multiples of 32 bits.<br/>
{@code this} and {@code b} must be nonnegative.
@param b the divisor
@param quotient output parameter for {@code this/b}
@return the remainder
| MutableBigInteger::divideAndRemainderBurnikelZiegler | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger divide2n1n(MutableBigInteger b, MutableBigInteger quotient) {
int n = b.intLen;
// step 1: base case
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);
aUpper.safeRightShift(32*(n/2)); // aUpper = [a1,a2,a3]
keepLower(n/2); // this = a4
// step 3: q1=aUpper/b, r1=aUpper%b
MutableBigInteger q1 = new MutableBigInteger();
MutableBigInteger r1 = aUpper.divide3n2n(b, q1);
// step 4: quotient=[r1,this]/b, r2=[r1,this]%b
addDisjoint(r1, n/2); // this = [r1,this]
MutableBigInteger r2 = divide3n2n(b, quotient);
// step 5: let quotient=[q1,quotient] and return r2
quotient.addDisjoint(q1, n/2);
return r2;
} |
This method implements algorithm 1 from pg. 4 of the Burnikel-Ziegler paper.
It divides a 2n-digit number by a n-digit number.<br/>
The parameter beta is 2<sup>32</sup> so all shifts are multiples of 32 bits.
<br/>
{@code this} must be a nonnegative number such that {@code this.bitLength() <= 2*b.bitLength()}
@param b a positive number such that {@code b.bitLength()} is even
@param quotient output parameter for {@code this/b}
@return {@code this%b}
| MutableBigInteger::divide2n1n | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger divide3n2n(MutableBigInteger b, MutableBigInteger quotient) {
int n = b.intLen / 2; // half the length of b in ints
// step 1: view this as [a1,a2,a3] where each ai is n ints or less; let a12=[a1,a2]
MutableBigInteger a12 = new MutableBigInteger(this);
a12.safeRightShift(32*n);
// step 2: view b as [b1,b2] where each bi is n ints or less
MutableBigInteger b1 = new MutableBigInteger(b);
b1.safeRightShift(n * 32);
BigInteger b2 = b.getLower(n);
MutableBigInteger r;
MutableBigInteger d;
if (compareShifted(b, n) < 0) {
// step 3a: if a1<b1, let quotient=a12/b1 and r=a12%b1
r = a12.divide2n1n(b1, quotient);
// step 4: d=quotient*b2
d = new MutableBigInteger(quotient.toBigInteger().multiply(b2));
} else {
// step 3b: if a1>=b1, let quotient=beta^n-1 and r=a12-b1*2^n+b1
quotient.ones(n);
a12.add(b1);
b1.leftShift(32*n);
a12.subtract(b1);
r = a12;
// step 4: d=quotient*b2=(b2 << 32*n) - b2
d = new MutableBigInteger(b2);
d.leftShift(32 * n);
d.subtract(new MutableBigInteger(b2));
}
// step 5: r = r*beta^n + a3 - d (paper says a4)
// However, don't subtract d until after the while loop so r doesn't become negative
r.leftShift(32 * n);
r.addLower(this, n);
// step 6: add b until r>=d
while (r.compare(d) < 0) {
r.add(b);
quotient.subtract(MutableBigInteger.ONE);
}
r.subtract(d);
return r;
} |
This method implements algorithm 2 from pg. 5 of the Burnikel-Ziegler paper.
It divides a 3n-digit number by a 2n-digit number.<br/>
The parameter beta is 2<sup>32</sup> so all shifts are multiples of 32 bits.<br/>
<br/>
{@code this} must be a nonnegative number such that {@code 2*this.bitLength() <= 3*b.bitLength()}
@param quotient output parameter for {@code this/b}
@return {@code this%b}
| MutableBigInteger::divide3n2n | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger getBlock(int index, int numBlocks, int blockLength) {
int blockStart = index * blockLength;
if (blockStart >= intLen) {
return new MutableBigInteger();
}
int blockEnd;
if (index == numBlocks-1) {
blockEnd = intLen;
} else {
blockEnd = (index+1) * blockLength;
}
if (blockEnd > intLen) {
return new MutableBigInteger();
}
int[] newVal = Arrays.copyOfRange(value, offset+intLen-blockEnd, offset+intLen-blockStart);
return new MutableBigInteger(newVal);
} |
Returns a {@code MutableBigInteger} containing {@code blockLength} ints from
{@code this} number, starting at {@code index*blockLength}.<br/>
Used by Burnikel-Ziegler division.
@param index the block index
@param numBlocks the total number of blocks in {@code this} number
@param blockLength length of one block in units of 32 bits
@return
| MutableBigInteger::getBlock | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
long bitLength() {
if (intLen == 0)
return 0;
return intLen*32L - Integer.numberOfLeadingZeros(value[offset]);
} |
Returns a {@code MutableBigInteger} containing {@code blockLength} ints from
{@code this} number, starting at {@code index*blockLength}.<br/>
Used by Burnikel-Ziegler division.
@param index the block index
@param numBlocks the total number of blocks in {@code this} number
@param blockLength length of one block in units of 32 bits
@return
private MutableBigInteger getBlock(int index, int numBlocks, int blockLength) {
int blockStart = index * blockLength;
if (blockStart >= intLen) {
return new MutableBigInteger();
}
int blockEnd;
if (index == numBlocks-1) {
blockEnd = intLen;
} else {
blockEnd = (index+1) * blockLength;
}
if (blockEnd > intLen) {
return new MutableBigInteger();
}
int[] newVal = Arrays.copyOfRange(value, offset+intLen-blockEnd, offset+intLen-blockStart);
return new MutableBigInteger(newVal);
}
/** @see BigInteger#bitLength() | MutableBigInteger::bitLength | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
long divide(long v, MutableBigInteger quotient) {
if (v == 0)
throw new ArithmeticException("BigInteger divide by zero");
// Dividend is zero
if (intLen == 0) {
quotient.intLen = quotient.offset = 0;
return 0;
}
if (v < 0)
v = -v;
int d = (int)(v >>> 32);
quotient.clear();
// Special case on word divisor
if (d == 0)
return divideOneWord((int)v, quotient) & LONG_MASK;
else {
return divideLongMagnitude(v, quotient).toLong();
}
} |
Internally used to calculate the quotient of this div v and places the
quotient in the provided MutableBigInteger object and the remainder is
returned.
@return the remainder of the division will be returned.
| MutableBigInteger::divide | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger divideMagnitude(MutableBigInteger div,
MutableBigInteger quotient,
boolean needRemainder ) {
// assert div.intLen > 1
// D1 normalize the divisor
int shift = Integer.numberOfLeadingZeros(div.value[div.offset]);
// Copy divisor value to protect divisor
final int dlen = div.intLen;
int[] divisor;
MutableBigInteger rem; // Remainder starts as dividend with space for a leading zero
if (shift > 0) {
divisor = new int[dlen];
copyAndShift(div.value,div.offset,dlen,divisor,0,shift);
if (Integer.numberOfLeadingZeros(value[offset]) >= shift) {
int[] remarr = new int[intLen + 1];
rem = new MutableBigInteger(remarr);
rem.intLen = intLen;
rem.offset = 1;
copyAndShift(value,offset,intLen,remarr,1,shift);
} else {
int[] remarr = new int[intLen + 2];
rem = new MutableBigInteger(remarr);
rem.intLen = intLen+1;
rem.offset = 1;
int rFrom = offset;
int c=0;
int n2 = 32 - shift;
for (int i=1; i < intLen+1; i++,rFrom++) {
int b = c;
c = value[rFrom];
remarr[i] = (b << shift) | (c >>> n2);
}
remarr[intLen+1] = c << shift;
}
} else {
divisor = Arrays.copyOfRange(div.value, div.offset, div.offset + div.intLen);
rem = new MutableBigInteger(new int[intLen + 1]);
System.arraycopy(value, offset, rem.value, 1, intLen);
rem.intLen = intLen;
rem.offset = 1;
}
int nlen = rem.intLen;
// Set the quotient size
final int limit = nlen - dlen + 1;
if (quotient.value.length < limit) {
quotient.value = new int[limit];
quotient.offset = 0;
}
quotient.intLen = limit;
int[] q = quotient.value;
// Must insert leading 0 in rem if its length did not change
if (rem.intLen == nlen) {
rem.offset = 0;
rem.value[0] = 0;
rem.intLen++;
}
int dh = divisor[0];
long dhLong = dh & LONG_MASK;
int dl = divisor[1];
// D2 Initialize j
for (int j=0; j < limit-1; j++) {
// D3 Calculate qhat
// estimate qhat
int qhat = 0;
int qrem = 0;
boolean skipCorrection = false;
int nh = rem.value[j+rem.offset];
int nh2 = nh + 0x80000000;
int nm = rem.value[j+1+rem.offset];
if (nh == dh) {
qhat = ~0;
qrem = nh + nm;
skipCorrection = qrem + 0x80000000 < nh2;
} else {
long nChunk = (((long)nh) << 32) | (nm & LONG_MASK);
if (nChunk >= 0) {
qhat = (int) (nChunk / dhLong);
qrem = (int) (nChunk - (qhat * dhLong));
} else {
long tmp = divWord(nChunk, dh);
qhat = (int) (tmp & LONG_MASK);
qrem = (int) (tmp >>> 32);
}
}
if (qhat == 0)
continue;
if (!skipCorrection) { // Correct qhat
long nl = rem.value[j+2+rem.offset] & LONG_MASK;
long rs = ((qrem & LONG_MASK) << 32) | nl;
long estProduct = (dl & LONG_MASK) * (qhat & LONG_MASK);
if (unsignedLongCompare(estProduct, rs)) {
qhat--;
qrem = (int)((qrem & LONG_MASK) + dhLong);
if ((qrem & LONG_MASK) >= dhLong) {
estProduct -= (dl & LONG_MASK);
rs = ((qrem & LONG_MASK) << 32) | nl;
if (unsignedLongCompare(estProduct, rs))
qhat--;
}
}
}
// D4 Multiply and subtract
rem.value[j+rem.offset] = 0;
int borrow = mulsub(rem.value, divisor, qhat, dlen, j+rem.offset);
// D5 Test remainder
if (borrow + 0x80000000 > nh2) {
// D6 Add back
divadd(divisor, rem.value, j+1+rem.offset);
qhat--;
}
// Store the quotient digit
q[j] = qhat;
} // D7 loop on j
// D3 Calculate qhat
// estimate qhat
int qhat = 0;
int qrem = 0;
boolean skipCorrection = false;
int nh = rem.value[limit - 1 + rem.offset];
int nh2 = nh + 0x80000000;
int nm = rem.value[limit + rem.offset];
if (nh == dh) {
qhat = ~0;
qrem = nh + nm;
skipCorrection = qrem + 0x80000000 < nh2;
} else {
long nChunk = (((long) nh) << 32) | (nm & LONG_MASK);
if (nChunk >= 0) {
qhat = (int) (nChunk / dhLong);
qrem = (int) (nChunk - (qhat * dhLong));
} else {
long tmp = divWord(nChunk, dh);
qhat = (int) (tmp & LONG_MASK);
qrem = (int) (tmp >>> 32);
}
}
if (qhat != 0) {
if (!skipCorrection) { // Correct qhat
long nl = rem.value[limit + 1 + rem.offset] & LONG_MASK;
long rs = ((qrem & LONG_MASK) << 32) | nl;
long estProduct = (dl & LONG_MASK) * (qhat & LONG_MASK);
if (unsignedLongCompare(estProduct, rs)) {
qhat--;
qrem = (int) ((qrem & LONG_MASK) + dhLong);
if ((qrem & LONG_MASK) >= dhLong) {
estProduct -= (dl & LONG_MASK);
rs = ((qrem & LONG_MASK) << 32) | nl;
if (unsignedLongCompare(estProduct, rs))
qhat--;
}
}
}
// D4 Multiply and subtract
int borrow;
rem.value[limit - 1 + rem.offset] = 0;
if(needRemainder)
borrow = mulsub(rem.value, divisor, qhat, dlen, limit - 1 + rem.offset);
else
borrow = mulsubBorrow(rem.value, divisor, qhat, dlen, limit - 1 + rem.offset);
// D5 Test remainder
if (borrow + 0x80000000 > nh2) {
// D6 Add back
if(needRemainder)
divadd(divisor, rem.value, limit - 1 + 1 + rem.offset);
qhat--;
}
// Store the quotient digit
q[(limit - 1)] = qhat;
}
if (needRemainder) {
// D8 Unnormalize
if (shift > 0)
rem.rightShift(shift);
rem.normalize();
}
quotient.normalize();
return needRemainder ? rem : null;
} |
Divide this MutableBigInteger by the divisor.
The quotient will be placed into the provided quotient object &
the remainder object is returned.
| MutableBigInteger::divideMagnitude | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger divideLongMagnitude(long ldivisor, MutableBigInteger quotient) {
// Remainder starts as dividend with space for a leading zero
MutableBigInteger rem = new MutableBigInteger(new int[intLen + 1]);
System.arraycopy(value, offset, rem.value, 1, intLen);
rem.intLen = intLen;
rem.offset = 1;
int nlen = rem.intLen;
int limit = nlen - 2 + 1;
if (quotient.value.length < limit) {
quotient.value = new int[limit];
quotient.offset = 0;
}
quotient.intLen = limit;
int[] q = quotient.value;
// D1 normalize the divisor
int shift = Long.numberOfLeadingZeros(ldivisor);
if (shift > 0) {
ldivisor<<=shift;
rem.leftShift(shift);
}
// Must insert leading 0 in rem if its length did not change
if (rem.intLen == nlen) {
rem.offset = 0;
rem.value[0] = 0;
rem.intLen++;
}
int dh = (int)(ldivisor >>> 32);
long dhLong = dh & LONG_MASK;
int dl = (int)(ldivisor & LONG_MASK);
// D2 Initialize j
for (int j = 0; j < limit; j++) {
// D3 Calculate qhat
// estimate qhat
int qhat = 0;
int qrem = 0;
boolean skipCorrection = false;
int nh = rem.value[j + rem.offset];
int nh2 = nh + 0x80000000;
int nm = rem.value[j + 1 + rem.offset];
if (nh == dh) {
qhat = ~0;
qrem = nh + nm;
skipCorrection = qrem + 0x80000000 < nh2;
} else {
long nChunk = (((long) nh) << 32) | (nm & LONG_MASK);
if (nChunk >= 0) {
qhat = (int) (nChunk / dhLong);
qrem = (int) (nChunk - (qhat * dhLong));
} else {
long tmp = divWord(nChunk, dh);
qhat =(int)(tmp & LONG_MASK);
qrem = (int)(tmp>>>32);
}
}
if (qhat == 0)
continue;
if (!skipCorrection) { // Correct qhat
long nl = rem.value[j + 2 + rem.offset] & LONG_MASK;
long rs = ((qrem & LONG_MASK) << 32) | nl;
long estProduct = (dl & LONG_MASK) * (qhat & LONG_MASK);
if (unsignedLongCompare(estProduct, rs)) {
qhat--;
qrem = (int) ((qrem & LONG_MASK) + dhLong);
if ((qrem & LONG_MASK) >= dhLong) {
estProduct -= (dl & LONG_MASK);
rs = ((qrem & LONG_MASK) << 32) | nl;
if (unsignedLongCompare(estProduct, rs))
qhat--;
}
}
}
// D4 Multiply and subtract
rem.value[j + rem.offset] = 0;
int borrow = mulsubLong(rem.value, dh, dl, qhat, j + rem.offset);
// D5 Test remainder
if (borrow + 0x80000000 > nh2) {
// D6 Add back
divaddLong(dh,dl, rem.value, j + 1 + rem.offset);
qhat--;
}
// Store the quotient digit
q[j] = qhat;
} // D7 loop on j
// D8 Unnormalize
if (shift > 0)
rem.rightShift(shift);
quotient.normalize();
rem.normalize();
return rem;
} |
Divide this MutableBigInteger by the divisor represented by positive long
value. The quotient will be placed into the provided quotient object &
the remainder object is returned.
| MutableBigInteger::divideLongMagnitude | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private int divaddLong(int dh, int dl, int[] result, int offset) {
long carry = 0;
long sum = (dl & LONG_MASK) + (result[1+offset] & LONG_MASK);
result[1+offset] = (int)sum;
sum = (dh & LONG_MASK) + (result[offset] & LONG_MASK) + carry;
result[offset] = (int)sum;
carry = sum >>> 32;
return (int)carry;
} |
A primitive used for division by long.
Specialized version of the method divadd.
dh is a high part of the divisor, dl is a low part
| MutableBigInteger::divaddLong | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
static long divWord(long n, int d) {
long dLong = d & LONG_MASK;
long r;
long q;
if (dLong == 1) {
q = (int)n;
r = 0;
return (r << 32) | (q & LONG_MASK);
}
// Approximate the quotient and remainder
q = (n >>> 1) / (dLong >>> 1);
r = n - q*dLong;
// Correct the approximation
while (r < 0) {
r += dLong;
q--;
}
while (r >= dLong) {
r -= dLong;
q++;
}
// n - q*dlong == r && 0 <= r <dLong, hence we're done.
return (r << 32) | (q & LONG_MASK);
} |
This method divides a long quantity by an int to estimate
qhat for two multi precision numbers. It is used when
the signed value of n is less than zero.
Returns long value where high 32 bits contain remainder value and
low 32 bits contain quotient value.
| MutableBigInteger::divWord | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger hybridGCD(MutableBigInteger b) {
// Use Euclid's algorithm until the numbers are approximately the
// same length, then use the binary GCD algorithm to find the GCD.
MutableBigInteger a = this;
MutableBigInteger q = new MutableBigInteger();
while (b.intLen != 0) {
if (Math.abs(a.intLen - b.intLen) < 2)
return a.binaryGCD(b);
MutableBigInteger r = a.divide(b, q);
a = b;
b = r;
}
return a;
} |
Calculate GCD of this and b. This and b are changed by the computation.
| MutableBigInteger::hybridGCD | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger binaryGCD(MutableBigInteger v) {
// Algorithm B from Knuth section 4.5.2
MutableBigInteger u = this;
MutableBigInteger r = new MutableBigInteger();
// step B1
int s1 = u.getLowestSetBit();
int s2 = v.getLowestSetBit();
int k = (s1 < s2) ? s1 : s2;
if (k != 0) {
u.rightShift(k);
v.rightShift(k);
}
// step B2
boolean uOdd = (k == s1);
MutableBigInteger t = uOdd ? v: u;
int tsign = uOdd ? -1 : 1;
int lb;
while ((lb = t.getLowestSetBit()) >= 0) {
// steps B3 and B4
t.rightShift(lb);
// step B5
if (tsign > 0)
u = t;
else
v = t;
// Special case one word numbers
if (u.intLen < 2 && v.intLen < 2) {
int x = u.value[u.offset];
int y = v.value[v.offset];
x = binaryGcd(x, y);
r.value[0] = x;
r.intLen = 1;
r.offset = 0;
if (k > 0)
r.leftShift(k);
return r;
}
// step B6
if ((tsign = u.difference(v)) == 0)
break;
t = (tsign >= 0) ? u : v;
}
if (k > 0)
u.leftShift(k);
return u;
} |
Calculate GCD of this and v.
Assumes that this and v are not zero.
| MutableBigInteger::binaryGCD | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
static int binaryGcd(int a, int b) {
if (b == 0)
return a;
if (a == 0)
return b;
// Right shift a & b till their last bits equal to 1.
int aZeros = Integer.numberOfTrailingZeros(a);
int bZeros = Integer.numberOfTrailingZeros(b);
a >>>= aZeros;
b >>>= bZeros;
int t = (aZeros < bZeros ? aZeros : bZeros);
while (a != b) {
if ((a+0x80000000) > (b+0x80000000)) { // a > b as unsigned
a -= b;
a >>>= Integer.numberOfTrailingZeros(a);
} else {
b -= a;
b >>>= Integer.numberOfTrailingZeros(b);
}
}
return a<<t;
} |
Calculate GCD of a and b interpreted as unsigned integers.
| MutableBigInteger::binaryGcd | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger mutableModInverse(MutableBigInteger p) {
// Modulus is odd, use Schroeppel's algorithm
if (p.isOdd())
return modInverse(p);
// Base and modulus are even, throw exception
if (isEven())
throw new ArithmeticException("BigInteger not invertible.");
// Get even part of modulus expressed as a power of 2
int powersOf2 = p.getLowestSetBit();
// Construct odd part of modulus
MutableBigInteger oddMod = new MutableBigInteger(p);
oddMod.rightShift(powersOf2);
if (oddMod.isOne())
return modInverseMP2(powersOf2);
// Calculate 1/a mod oddMod
MutableBigInteger oddPart = modInverse(oddMod);
// Calculate 1/a mod evenMod
MutableBigInteger evenPart = modInverseMP2(powersOf2);
// Combine the results using Chinese Remainder Theorem
MutableBigInteger y1 = modInverseBP2(oddMod, powersOf2);
MutableBigInteger y2 = oddMod.modInverseMP2(powersOf2);
MutableBigInteger temp1 = new MutableBigInteger();
MutableBigInteger temp2 = new MutableBigInteger();
MutableBigInteger result = new MutableBigInteger();
oddPart.leftShift(powersOf2);
oddPart.multiply(y1, result);
evenPart.multiply(oddMod, temp1);
temp1.multiply(y2, temp2);
result.add(temp2);
return result.divide(p, temp1);
} |
Returns the modInverse of this mod p. This and p are not affected by
the operation.
| MutableBigInteger::mutableModInverse | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger modInverseMP2(int k) {
if (isEven())
throw new ArithmeticException("Non-invertible. (GCD != 1)");
if (k > 64)
return euclidModInverse(k);
int t = inverseMod32(value[offset+intLen-1]);
if (k < 33) {
t = (k == 32 ? t : t & ((1 << k) - 1));
return new MutableBigInteger(t);
}
long pLong = (value[offset+intLen-1] & LONG_MASK);
if (intLen > 1)
pLong |= ((long)value[offset+intLen-2] << 32);
long tLong = t & LONG_MASK;
tLong = tLong * (2 - pLong * tLong); // 1 more Newton iter step
tLong = (k == 64 ? tLong : tLong & ((1L << k) - 1));
MutableBigInteger result = new MutableBigInteger(new int[2]);
result.value[0] = (int)(tLong >>> 32);
result.value[1] = (int)tLong;
result.intLen = 2;
result.normalize();
return result;
} |
Returns the modInverse of this mod p. This and p are not affected by
the operation.
MutableBigInteger mutableModInverse(MutableBigInteger p) {
// Modulus is odd, use Schroeppel's algorithm
if (p.isOdd())
return modInverse(p);
// Base and modulus are even, throw exception
if (isEven())
throw new ArithmeticException("BigInteger not invertible.");
// Get even part of modulus expressed as a power of 2
int powersOf2 = p.getLowestSetBit();
// Construct odd part of modulus
MutableBigInteger oddMod = new MutableBigInteger(p);
oddMod.rightShift(powersOf2);
if (oddMod.isOne())
return modInverseMP2(powersOf2);
// Calculate 1/a mod oddMod
MutableBigInteger oddPart = modInverse(oddMod);
// Calculate 1/a mod evenMod
MutableBigInteger evenPart = modInverseMP2(powersOf2);
// Combine the results using Chinese Remainder Theorem
MutableBigInteger y1 = modInverseBP2(oddMod, powersOf2);
MutableBigInteger y2 = oddMod.modInverseMP2(powersOf2);
MutableBigInteger temp1 = new MutableBigInteger();
MutableBigInteger temp2 = new MutableBigInteger();
MutableBigInteger result = new MutableBigInteger();
oddPart.leftShift(powersOf2);
oddPart.multiply(y1, result);
evenPart.multiply(oddMod, temp1);
temp1.multiply(y2, temp2);
result.add(temp2);
return result.divide(p, temp1);
}
/*
Calculate the multiplicative inverse of this mod 2^k.
| MutableBigInteger::modInverseMP2 | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
static int inverseMod32(int val) {
// Newton's iteration!
int t = val;
t *= 2 - val*t;
t *= 2 - val*t;
t *= 2 - val*t;
t *= 2 - val*t;
return t;
} |
Returns the multiplicative inverse of val mod 2^32. Assumes val is odd.
| MutableBigInteger::inverseMod32 | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
static MutableBigInteger modInverseBP2(MutableBigInteger mod, int k) {
// Copy the mod to protect original
return fixup(new MutableBigInteger(1), new MutableBigInteger(mod), k);
} |
Calculate the multiplicative inverse of 2^k mod mod, where mod is odd.
| MutableBigInteger::modInverseBP2 | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
private MutableBigInteger modInverse(MutableBigInteger mod) {
MutableBigInteger p = new MutableBigInteger(mod);
MutableBigInteger f = new MutableBigInteger(this);
MutableBigInteger g = new MutableBigInteger(p);
SignedMutableBigInteger c = new SignedMutableBigInteger(1);
SignedMutableBigInteger d = new SignedMutableBigInteger();
MutableBigInteger temp = null;
SignedMutableBigInteger sTemp = null;
int k = 0;
// Right shift f k times until odd, left shift d k times
if (f.isEven()) {
int trailingZeros = f.getLowestSetBit();
f.rightShift(trailingZeros);
d.leftShift(trailingZeros);
k = trailingZeros;
}
// The Almost Inverse Algorithm
while (!f.isOne()) {
// If gcd(f, g) != 1, number is not invertible modulo mod
if (f.isZero())
throw new ArithmeticException("BigInteger not invertible.");
// If f < g exchange f, g and c, d
if (f.compare(g) < 0) {
temp = f; f = g; g = temp;
sTemp = d; d = c; c = sTemp;
}
// If f == g (mod 4)
if (((f.value[f.offset + f.intLen - 1] ^
g.value[g.offset + g.intLen - 1]) & 3) == 0) {
f.subtract(g);
c.signedSubtract(d);
} else { // If f != g (mod 4)
f.add(g);
c.signedAdd(d);
}
// Right shift f k times until odd, left shift d k times
int trailingZeros = f.getLowestSetBit();
f.rightShift(trailingZeros);
d.leftShift(trailingZeros);
k += trailingZeros;
}
while (c.sign < 0)
c.signedAdd(p);
return fixup(c, p, k);
} |
Calculate the multiplicative inverse of this mod mod, where mod is odd.
This and mod are not changed by the calculation.
This method implements an algorithm due to Richard Schroeppel, that uses
the same intermediate representation as Montgomery Reduction
("Montgomery Form"). The algorithm is described in an unpublished
manuscript entitled "Fast Modular Reciprocals."
| MutableBigInteger::modInverse | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
static MutableBigInteger fixup(MutableBigInteger c, MutableBigInteger p,
int k) {
MutableBigInteger temp = new MutableBigInteger();
// Set r to the multiplicative inverse of p mod 2^32
int r = -inverseMod32(p.value[p.offset+p.intLen-1]);
for (int i=0, numWords = k >> 5; i < numWords; i++) {
// V = R * c (mod 2^j)
int v = r * c.value[c.offset + c.intLen-1];
// c = c + (v * p)
p.mul(v, temp);
c.add(temp);
// c = c / 2^j
c.intLen--;
}
int numBits = k & 0x1f;
if (numBits != 0) {
// V = R * c (mod 2^j)
int v = r * c.value[c.offset + c.intLen-1];
v &= ((1<<numBits) - 1);
// c = c + (v * p)
p.mul(v, temp);
c.add(temp);
// c = c / 2^j
c.rightShift(numBits);
}
// In theory, c may be greater than p at this point (Very rare!)
while (c.compare(p) >= 0)
c.subtract(p);
return c;
} |
The Fixup Algorithm
Calculates X such that X = C * 2^(-k) (mod P)
Assumes C<P and P is odd.
| MutableBigInteger::fixup | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
MutableBigInteger euclidModInverse(int k) {
MutableBigInteger b = new MutableBigInteger(1);
b.leftShift(k);
MutableBigInteger mod = new MutableBigInteger(b);
MutableBigInteger a = new MutableBigInteger(this);
MutableBigInteger q = new MutableBigInteger();
MutableBigInteger r = b.divide(a, q);
MutableBigInteger swapper = b;
// swap b & r
b = r;
r = swapper;
MutableBigInteger t1 = new MutableBigInteger(q);
MutableBigInteger t0 = new MutableBigInteger(1);
MutableBigInteger temp = new MutableBigInteger();
while (!b.isOne()) {
r = a.divide(b, q);
if (r.intLen == 0)
throw new ArithmeticException("BigInteger not invertible.");
swapper = r;
a = swapper;
if (q.intLen == 1)
t1.mul(q.value[q.offset], temp);
else
q.multiply(t1, temp);
swapper = q;
q = temp;
temp = swapper;
t0.add(q);
if (a.isOne())
return t0;
r = b.divide(a, q);
if (r.intLen == 0)
throw new ArithmeticException("BigInteger not invertible.");
swapper = b;
b = r;
if (q.intLen == 1)
t0.mul(q.value[q.offset], temp);
else
q.multiply(t0, temp);
swapper = q; q = temp; temp = swapper;
t1.add(q);
}
mod.subtract(t1);
return mod;
} |
Uses the extended Euclidean algorithm to compute the modInverse of base
mod a modulus that is a power of 2. The modulus is 2^k.
| MutableBigInteger::euclidModInverse | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MutableBigInteger.java | Apache-2.0 |
public MathContext(int setPrecision) {
this(setPrecision, DEFAULT_ROUNDINGMODE);
return;
} |
Constructs a new {@code MathContext} with the specified
precision and the {@link RoundingMode#HALF_UP HALF_UP} rounding
mode.
@param setPrecision The non-negative {@code int} precision setting.
@throws IllegalArgumentException if the {@code setPrecision} parameter is less
than zero.
| MathContext::MathContext | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MathContext.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MathContext.java | Apache-2.0 |
public MathContext(int setPrecision,
RoundingMode setRoundingMode) {
if (setPrecision < MIN_DIGITS)
throw new IllegalArgumentException("Digits < 0");
if (setRoundingMode == null)
throw new NullPointerException("null RoundingMode");
precision = setPrecision;
roundingMode = setRoundingMode;
return;
} |
Constructs a new {@code MathContext} with a specified
precision and rounding mode.
@param setPrecision The non-negative {@code int} precision setting.
@param setRoundingMode The rounding mode to use.
@throws IllegalArgumentException if the {@code setPrecision} parameter is less
than zero.
@throws NullPointerException if the rounding mode argument is {@code null}
| MathContext::MathContext | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MathContext.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MathContext.java | Apache-2.0 |
public MathContext(String val) {
boolean bad = false;
int setPrecision;
if (val == null)
throw new NullPointerException("null String");
try { // any error here is a string format problem
if (!val.startsWith("precision=")) throw new RuntimeException();
int fence = val.indexOf(' '); // could be -1
int off = 10; // where value starts
setPrecision = Integer.parseInt(val.substring(10, fence));
if (!val.startsWith("roundingMode=", fence+1))
throw new RuntimeException();
off = fence + 1 + 13;
String str = val.substring(off, val.length());
roundingMode = RoundingMode.valueOf(str);
} catch (RuntimeException re) {
throw new IllegalArgumentException("bad string format");
}
if (setPrecision < MIN_DIGITS)
throw new IllegalArgumentException("Digits < 0");
// the other parameters cannot be invalid if we got here
precision = setPrecision;
} |
Constructs a new {@code MathContext} from a string.
The string must be in the same format as that produced by the
{@link #toString} method.
<p>An {@code IllegalArgumentException} is thrown if the precision
section of the string is out of range ({@code < 0}) or the string is
not in the format created by the {@link #toString} method.
@param val The string to be parsed
@throws IllegalArgumentException if the precision section is out of range
or of incorrect format
@throws NullPointerException if the argument is {@code null}
| MathContext::MathContext | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MathContext.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MathContext.java | Apache-2.0 |
public int getPrecision() {
return precision;
} |
Returns the {@code precision} setting.
This value is always non-negative.
@return an {@code int} which is the value of the {@code precision}
setting
| MathContext::getPrecision | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MathContext.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MathContext.java | Apache-2.0 |
public boolean equals(Object x){
MathContext mc;
if (!(x instanceof MathContext))
return false;
mc = (MathContext) x;
return mc.precision == this.precision
&& mc.roundingMode == this.roundingMode; // no need for .equals()
} |
Compares this {@code MathContext} with the specified
{@code Object} for equality.
@param x {@code Object} to which this {@code MathContext} is to
be compared.
@return {@code true} if and only if the specified {@code Object} is
a {@code MathContext} object which has exactly the same
settings as this object
| MathContext::equals | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MathContext.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MathContext.java | Apache-2.0 |
public int hashCode() {
return this.precision + roundingMode.hashCode() * 59;
} |
Returns the hash code for this {@code MathContext}.
@return hash code for this {@code MathContext}
| MathContext::hashCode | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MathContext.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MathContext.java | Apache-2.0 |
public java.lang.String toString() {
return "precision=" + precision + " " +
"roundingMode=" + roundingMode.toString();
} |
Returns the string representation of this {@code MathContext}.
The {@code String} returned represents the settings of the
{@code MathContext} object as two space-delimited words
(separated by a single space character, <tt>'\u0020'</tt>,
and with no leading or trailing white space), as follows:
<ol>
<li>
The string {@code "precision="}, immediately followed
by the value of the precision setting as a numeric string as if
generated by the {@link Integer#toString(int) Integer.toString}
method.
<li>
The string {@code "roundingMode="}, immediately
followed by the value of the {@code roundingMode} setting as a
word. This word will be the same as the name of the
corresponding public constant in the {@link RoundingMode}
enum.
</ol>
<p>
For example:
<pre>
precision=9 roundingMode=HALF_UP
</pre>
Additional words may be appended to the result of
{@code toString} in the future if more properties are added to
this class.
@return a {@code String} representing the context settings
| MathContext::toString | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MathContext.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MathContext.java | Apache-2.0 |
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject(); // read in all fields
// validate possibly bad fields
if (precision < MIN_DIGITS) {
String message = "MathContext: invalid digits in stream";
throw new java.io.StreamCorruptedException(message);
}
if (roundingMode == null) {
String message = "MathContext: null roundingMode in stream";
throw new java.io.StreamCorruptedException(message);
}
} |
Reconstitute the {@code MathContext} instance from a stream (that is,
deserialize it).
@param s the stream being read.
| MathContext::readObject | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/MathContext.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/MathContext.java | Apache-2.0 |
public BigInteger(byte[] val) {
if (val.length == 0)
throw new NumberFormatException("Zero length BigInteger");
if (val[0] < 0) {
mag = makePositive(val);
signum = -1;
} else {
mag = stripLeadingZeroBytes(val);
signum = (mag.length == 0 ? 0 : 1);
}
if (mag.length >= MAX_MAG_LENGTH) {
checkRange();
}
} |
Translates a byte array containing the two's-complement binary
representation of a BigInteger into a BigInteger. The input array is
assumed to be in <i>big-endian</i> byte-order: the most significant
byte is in the zeroth element.
@param val big-endian two's-complement binary representation of
BigInteger.
@throws NumberFormatException {@code val} is zero bytes long.
| BigInteger::BigInteger | java | google/j2objc | jre_emul/openjdk/src/share/classes/java/math/BigInteger.java | https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigInteger.java | Apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.