final int dividendLength = arrayValidLength(dividend); final int divisorLength = arrayValidLength(divisor); Arrays.fill(quotient, 0); throwZeroDivisionException(); int rem = divideMultiPrecision(remainder, divisor[0]); System.arraycopy(remainder, 0, quotient, 0, quotient.length); Arrays.fill(remainder, 0); multiplyMultiPrecision(newDivisor, d1); divisor = newDivisor; multiplyMultiPrecision(remainder, d1); long accum = combineInts(remainder[rIndex - 1], remainder[rIndex]); int qhat; if (dHigh == (remainder[rIndex] & LONG_MASK)) { qhat = (int) (BASE - 1); } else { qhat = (int) divideUnsignedLong(accum, dHigh); while (compareUnsignedLong(dLow * (qhat & LONG_MASK), combineInts(remainder[rIndex - 2], rhat)) > 0) { qhat--; if ((rhat & LONG_MASK) >= -((int) dHigh)) { dwlMulAccum += (qhat & LONG_MASK) * (divisor[dIndex] & LONG_MASK); accum += (remainder[iulRwork] & LONG_MASK) - (extractLowInt(dwlMulAccum) & LONG_MASK);
private short bitLength() { return SqlMathUtil.bitLength(v[0], v[1], v[2], v[3]); }
/** * Returns the minimal number of bits to represent the words. * * @param v0 * v0 * @param v1 * v1 * @param v2 * v2 * @param v3 * v3 * @return the minimal number of bits to represent the words */ public static short bitLength(int v0, int v1, int v2, int v3) { if (v3 != 0) { return (short) (bitLengthInWord(v3) + 96); } if (v2 != 0) { return (short) (bitLengthInWord(v2) + 64); } if (v1 != 0) { return (short) (bitLengthInWord(v1) + 32); } return bitLengthInWord(v0); }
/** * Scale up this object for 10**tenScale and then divides this value with the * given value. This version is destructive, meaning it modifies this object. * * @param right * the value to divide * @param tenScale * ten-based scale up distance * @param remainder * object to receive remainder */ public void divideScaleUpTenDestructive(UnsignedInt128 right, short tenScale, UnsignedInt128 remainder) { // in this case, we have to scale up _BEFORE_ division. otherwise we // might lose precision. if (tenScale > SqlMathUtil.MAX_POWER_TEN_INT128) { // in this case, the result will be surely more than 128 bit even // after division SqlMathUtil.throwOverflowException(); } int[] scaledUp = multiplyConstructive256(SqlMathUtil.POWER_TENS_INT128[tenScale]); int[] quotient = new int[5]; int[] rem = SqlMathUtil.divideMultiPrecision(scaledUp, right.v, quotient); update(quotient[0], quotient[1], quotient[2], quotient[3]); remainder.update(rem[0], rem[1], rem[2], rem[3]); }
/** * Divides this value with the given value. This version is destructive, * meaning it modifies this object. * * @param right * the value to divide * @param remainder * object to receive remainder */ public void divideDestructive(UnsignedInt128 right, UnsignedInt128 remainder) { if (right.isZero()) { assert (right.isZero()); SqlMathUtil.throwZeroDivisionException(); } if (right.count == 1) { assert (right.v[1] == 0); assert (right.v[2] == 0); assert (right.v[3] == 0); int rem = divideDestructive(right.v[0]); remainder.update(rem); return; } int[] quotient = new int[5]; int[] rem = SqlMathUtil.divideMultiPrecision(this.v, right.v, quotient); update(quotient[0], quotient[1], quotient[2], quotient[3]); remainder.update(rem[0], rem[1], rem[2], rem[3]); }
return (compareUnsignedLong(dividend, divisor)) < 0 ? dividend : dividend - divisor; if (compareUnsignedLong(remainder, divisor) >= 0) { return remainder - divisor;
/** * @see #compareTo(UnsignedInt128) */ private static int compareTo(int l0, int l1, int l2, int l3, int r0, int r1, int r2, int r3) { if (l3 != r3) { return SqlMathUtil.compareUnsignedInt(l3, r3); } if (l2 != r2) { return SqlMathUtil.compareUnsignedInt(l2, r2); } if (l1 != r1) { return SqlMathUtil.compareUnsignedInt(l1, r1); } if (l0 != r0) { return SqlMathUtil.compareUnsignedInt(l0, r0); } return 0; }
int[] divisor = new int[] { 1, 2, 3, 4 }; int[] quotient = new int[5]; int[] remainder = SqlMathUtil.divideMultiPrecision(dividend, divisor, quotient); assertArrayEquals(new int[] { 1, 0, 0, 10, 0 }, quotient); int[] divisor = new int[] { 0xF700, 0, 0x3900, 0 }; int[] quotient = new int[5]; int[] remainder = SqlMathUtil.divideMultiPrecision(dividend, divisor, quotient); assertArrayEquals(new int[] { 0x10000, 0, 0, 0, 0 }, quotient); int[] divisor = new int[] { 0xF700, 0, 0x3900, 0 }; int[] quotient = new int[5]; int[] remainder = SqlMathUtil.divideMultiPrecision(dividend, divisor, quotient); assertArrayEquals(new int[] { 0, 0, 0, 0, 0 }, quotient);
@Override public long longValue() { long unsigned = SqlMathUtil.combineInts(this.mag.getV0(), this.mag.getV1()) & SqlMathUtil.FULLBITS_63; return this.negative ? -unsigned : unsigned; }
/** * Scale up this object for 10**tenScale and then divides this value with the * given value. This version is destructive, meaning it modifies this object. * * @param right * the value to divide * @param tenScale * ten-based scale up distance * @param remainder * object to receive remainder */ public void divideScaleUpTenDestructive(UnsignedInt128 right, short tenScale, UnsignedInt128 remainder) { // in this case, we have to scale up _BEFORE_ division. otherwise we // might lose precision. if (tenScale > SqlMathUtil.MAX_POWER_TEN_INT128) { // in this case, the result will be surely more than 128 bit even // after division SqlMathUtil.throwOverflowException(); } int[] scaledUp = multiplyConstructive256(SqlMathUtil.POWER_TENS_INT128[tenScale]); int[] quotient = new int[5]; int[] rem = SqlMathUtil.divideMultiPrecision(scaledUp, right.v, quotient); update(quotient[0], quotient[1], quotient[2], quotient[3]); remainder.update(rem[0], rem[1], rem[2], rem[3]); }
/** * Divides this value with the given value. This version is destructive, * meaning it modifies this object. * * @param right * the value to divide * @param remainder * object to receive remainder */ public void divideDestructive(UnsignedInt128 right, UnsignedInt128 remainder) { if (right.isZero()) { assert (right.isZero()); SqlMathUtil.throwZeroDivisionException(); } if (right.count == 1) { assert (right.v[1] == 0); assert (right.v[2] == 0); assert (right.v[3] == 0); int rem = divideDestructive(right.v[0]); remainder.update(rem); return; } int[] quotient = new int[5]; int[] rem = SqlMathUtil.divideMultiPrecision(this.v, right.v, quotient); update(quotient[0], quotient[1], quotient[2], quotient[3]); remainder.update(rem[0], rem[1], rem[2], rem[3]); }
return (compareUnsignedLong(dividend, divisor)) < 0 ? 0L : 1L; if (compareUnsignedLong(remainder, divisor) >= 0) { return quotient + 1;
/** * @see #compareTo(UnsignedInt128) * @param o0 * o0 * @param o1 * o1 * @param o2 * o2 * @param o3 * o3 * @return a negative integer, zero, or a positive integer as this object is * less than, equal to, or greater than the specified object. */ public int compareTo(int o0, int o1, int o2, int o3) { if (v[3] != o3) { return SqlMathUtil.compareUnsignedInt(v[3], o3); } else if (v[2] != o2) { return SqlMathUtil.compareUnsignedInt(v[2], o2); } else if (v[1] != o1) { return SqlMathUtil.compareUnsignedInt(v[1], o1); } else { return SqlMathUtil.compareUnsignedInt(v[0], o0); } }
@Override public long longValue() { long unsigned = SqlMathUtil.combineInts(this.mag.getV0(), this.mag.getV1()) & SqlMathUtil.FULLBITS_63; return this.negative ? -unsigned : unsigned; }
final int dividendLength = arrayValidLength(dividend); final int divisorLength = arrayValidLength(divisor); Arrays.fill(quotient, 0); throwZeroDivisionException(); int rem = divideMultiPrecision(remainder, divisor[0]); System.arraycopy(remainder, 0, quotient, 0, quotient.length); Arrays.fill(remainder, 0); multiplyMultiPrecision(newDivisor, d1); divisor = newDivisor; multiplyMultiPrecision(remainder, d1); long accum = combineInts(remainder[rIndex - 1], remainder[rIndex]); int qhat; if (dHigh == (remainder[rIndex] & LONG_MASK)) { qhat = (int) (BASE - 1); } else { qhat = (int) divideUnsignedLong(accum, dHigh); while (compareUnsignedLong(dLow * (qhat & LONG_MASK), combineInts(remainder[rIndex - 2], rhat)) > 0) { qhat--; if ((rhat & LONG_MASK) >= -((int) dHigh)) { dwlMulAccum += (qhat & LONG_MASK) * (divisor[dIndex] & LONG_MASK); accum += (remainder[iulRwork] & LONG_MASK) - (extractLowInt(dwlMulAccum) & LONG_MASK);
/** * Scale up this object for 10**tenScale and then divides this value with the * given value. This version is destructive, meaning it modifies this object. * * @param right * the value to divide * @param tenScale * ten-based scale up distance * @param remainder * object to receive remainder */ public void divideScaleUpTenDestructive(UnsignedInt128 right, short tenScale, UnsignedInt128 remainder) { // in this case, we have to scale up _BEFORE_ division. otherwise we // might lose precision. if (tenScale > SqlMathUtil.MAX_POWER_TEN_INT128) { // in this case, the result will be surely more than 128 bit even // after division SqlMathUtil.throwOverflowException(); } int[] scaledUp = multiplyConstructive256(SqlMathUtil.POWER_TENS_INT128[tenScale]); int[] quotient = new int[5]; int[] rem = SqlMathUtil.divideMultiPrecision(scaledUp, right.v, quotient); update(quotient[0], quotient[1], quotient[2], quotient[3]); remainder.update(rem[0], rem[1], rem[2], rem[3]); }
/** * Divides this value with the given value. This version is destructive, * meaning it modifies this object. * * @param right * the value to divide * @param remainder * object to receive remainder */ public void divideDestructive(UnsignedInt128 right, UnsignedInt128 remainder) { if (right.isZero()) { assert (right.isZero()); SqlMathUtil.throwZeroDivisionException(); } if (right.count == 1) { assert (right.v[1] == 0); assert (right.v[2] == 0); assert (right.v[3] == 0); int rem = divideDestructive(right.v[0]); remainder.update(rem); return; } int[] quotient = new int[5]; int[] rem = SqlMathUtil.divideMultiPrecision(this.v, right.v, quotient); update(quotient[0], quotient[1], quotient[2], quotient[3]); remainder.update(rem[0], rem[1], rem[2], rem[3]); }
return SqlMathUtil.compareUnsignedLong(v0Long, o0);
/** * @see #compareTo(UnsignedInt128) */ private static int compareTo(int l0, int l1, int l2, int l3, int r0, int r1, int r2, int r3) { if (l3 != r3) { return SqlMathUtil.compareUnsignedInt(l3, r3); } if (l2 != r2) { return SqlMathUtil.compareUnsignedInt(l2, r2); } if (l1 != r1) { return SqlMathUtil.compareUnsignedInt(l1, r1); } if (l0 != r0) { return SqlMathUtil.compareUnsignedInt(l0, r0); } return 0; }
@Override public long longValue() { long unsigned = SqlMathUtil.combineInts(this.mag.getV0(), this.mag.getV1()) & SqlMathUtil.FULLBITS_63; return this.negative ? -unsigned : unsigned; }