Commit a852d85f authored by Jakob Kummerow's avatar Jakob Kummerow Committed by Commit Bot

[bigint] Small refactoring of DivLarge

Changing "DoubleDigitGreaterThan", which was consuming the result of a
multiplication, to "ProductGreaterThan", which performs both steps.

Bug: v8:6791
Change-Id: I7dbad350ff9b8228e11682d9691a1574ea5b0b58
Reviewed-on: https://chromium-review.googlesource.com/683614Reviewed-by: 's avatarDaniel Ehrenberg <littledan@chromium.org>
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48243}
parent 7cf29d8d
...@@ -446,18 +446,14 @@ void BigInt::AbsoluteDivLarge(Handle<BigInt> dividend, Handle<BigInt> divisor, ...@@ -446,18 +446,14 @@ void BigInt::AbsoluteDivLarge(Handle<BigInt> dividend, Handle<BigInt> divisor,
// Decrement the quotient estimate as needed by looking at the next // Decrement the quotient estimate as needed by looking at the next
// digit, i.e. by testing whether // digit, i.e. by testing whether
// qhat * v_{n-2} > (rhat << kDigitBits) + u_{j+n-2}. // qhat * v_{n-2} > (rhat << kDigitBits) + u_{j+n-2}.
// x1 | x2 = qhat * v_{n-2}.
digit_t vn2 = divisor->digit(n - 2); digit_t vn2 = divisor->digit(n - 2);
digit_t x1;
digit_t x2 = digit_mul(qhat, vn2, &x1);
digit_t ujn2 = u->digit(j + n - 2); digit_t ujn2 = u->digit(j + n - 2);
while (DoubleDigitGreaterThan(x1, x2, rhat, ujn2)) { while (ProductGreaterThan(qhat, vn2, rhat, ujn2)) {
qhat--; qhat--;
digit_t prev_rhat = rhat; digit_t prev_rhat = rhat;
rhat += vn1; rhat += vn1;
// v[n-1] >= 0, so this tests for overflow. // v[n-1] >= 0, so this tests for overflow.
if (rhat < prev_rhat) break; if (rhat < prev_rhat) break;
x2 = digit_mul(qhat, vn2, &x1);
} }
} }
...@@ -485,10 +481,12 @@ void BigInt::AbsoluteDivLarge(Handle<BigInt> dividend, Handle<BigInt> divisor, ...@@ -485,10 +481,12 @@ void BigInt::AbsoluteDivLarge(Handle<BigInt> dividend, Handle<BigInt> divisor,
} }
} }
// Returns (x_high << kDigitBits + x_low) > (y_high << kDigitBits + y_low). // Returns whether (factor1 * factor2) > (high << kDigitBits) + low.
bool BigInt::DoubleDigitGreaterThan(digit_t x_high, digit_t x_low, bool BigInt::ProductGreaterThan(digit_t factor1, digit_t factor2, digit_t high,
digit_t y_high, digit_t y_low) { digit_t low) {
return x_high > y_high || (x_high == y_high && x_low > y_low); digit_t result_high;
digit_t result_low = digit_mul(factor1, factor2, &result_high);
return result_high > high || (result_high == high && result_low > low);
} }
// Adds {summand} onto {this}, starting with {summand}'s 0th digit // Adds {summand} onto {this}, starting with {summand}'s 0th digit
......
...@@ -117,8 +117,8 @@ class BigInt : public HeapObject { ...@@ -117,8 +117,8 @@ class BigInt : public HeapObject {
static void AbsoluteDivLarge(Handle<BigInt> dividend, Handle<BigInt> divisor, static void AbsoluteDivLarge(Handle<BigInt> dividend, Handle<BigInt> divisor,
Handle<BigInt>* quotient, Handle<BigInt>* quotient,
Handle<BigInt>* remainder); Handle<BigInt>* remainder);
static bool DoubleDigitGreaterThan(digit_t x_high, digit_t x_low, static bool ProductGreaterThan(digit_t factor1, digit_t factor2, digit_t high,
digit_t y_high, digit_t y_low); digit_t low);
digit_t InplaceAdd(BigInt* summand, int start_index); digit_t InplaceAdd(BigInt* summand, int start_index);
digit_t InplaceSub(BigInt* subtrahend, int start_index); digit_t InplaceSub(BigInt* subtrahend, int start_index);
void InplaceRightShift(int shift); void InplaceRightShift(int shift);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment