Bug 1599465 - Part 10: Avoid calling absoluteCompare() twice in BigInt subtraction. r=jwalden

`absoluteCompare()` is currently called twice when subtracting two BigInts,
avoiding the second call gives a slight speed-up.

Differential Revision: https://phabricator.services.mozilla.com/D54767

--HG--
extra : moz-landing-system : lando
This commit is contained in:
André Bargull 2019-12-02 17:37:08 +00:00
Родитель 28319c9e07
Коммит a3067a0c3b
1 изменённых файлов: 15 добавлений и 13 удалений

Просмотреть файл

@ -584,22 +584,13 @@ BigInt* BigInt::absoluteAdd(JSContext* cx, HandleBigInt x, HandleBigInt y,
BigInt* BigInt::absoluteSub(JSContext* cx, HandleBigInt x, HandleBigInt y,
bool resultNegative) {
MOZ_ASSERT(x->digitLength() >= y->digitLength());
if (x->isZero()) {
MOZ_ASSERT(y->isZero());
return x;
}
MOZ_ASSERT(absoluteCompare(x, y) > 0);
MOZ_ASSERT(!x->isZero());
if (y->isZero()) {
return resultNegative == x->isNegative() ? x : neg(cx, x);
}
int8_t comparisonResult = absoluteCompare(x, y);
MOZ_ASSERT(comparisonResult >= 0);
if (comparisonResult == 0) {
return zero(cx);
}
RootedBigInt result(
cx, createUninitialized(cx, x->digitLength(), resultNegative));
if (!result) {
@ -1800,7 +1791,12 @@ BigInt* BigInt::add(JSContext* cx, HandleBigInt x, HandleBigInt y) {
// x + -y == x - y == -(y - x)
// -x + y == y - x == -(x - y)
if (absoluteCompare(x, y) >= 0) {
int8_t compare = absoluteCompare(x, y);
if (compare == 0) {
return zero(cx);
}
if (compare > 0) {
return absoluteSub(cx, x, y, xNegative);
}
@ -1815,9 +1811,15 @@ BigInt* BigInt::sub(JSContext* cx, HandleBigInt x, HandleBigInt y) {
// (-x) - y == -(x + y)
return absoluteAdd(cx, x, y, xNegative);
}
// x - y == -(y - x)
// (-x) - (-y) == y - x == -(x - y)
if (absoluteCompare(x, y) >= 0) {
int8_t compare = absoluteCompare(x, y);
if (compare == 0) {
return zero(cx);
}
if (compare > 0) {
return absoluteSub(cx, x, y, xNegative);
}