fix bigint remainder division

See #405
This commit is contained in:
Andrew Kelley 2018-01-16 03:09:44 -05:00
parent 84d8584c5b
commit 6a95b88d1b
2 changed files with 17 additions and 8 deletions

View File

@ -1015,21 +1015,29 @@ static void bigint_unsigned_division(const BigInt *op1, const BigInt *op2, BigIn
// If the caller wants the quotient
if (Quotient) {
Quotient->digit_count = lhsWords;
Quotient->data.digits = allocate<uint64_t>(lhsWords);
Quotient->is_negative = false;
for (size_t i = 0; i < lhsWords; i += 1) {
Quotient->data.digits[i] = Make_64(Q[i*2+1], Q[i*2]);
Quotient->digit_count = lhsWords;
if (lhsWords == 1) {
Quotient->data.digit = Make_64(Q[1], Q[0]);
} else {
Quotient->data.digits = allocate<uint64_t>(lhsWords);
for (size_t i = 0; i < lhsWords; i += 1) {
Quotient->data.digits[i] = Make_64(Q[i*2+1], Q[i*2]);
}
}
}
// If the caller wants the remainder
if (Remainder) {
Remainder->digit_count = rhsWords;
Remainder->data.digits = allocate<uint64_t>(rhsWords);
Remainder->is_negative = false;
for (size_t i = 0; i < rhsWords; i += 1) {
Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]);
Remainder->digit_count = rhsWords;
if (rhsWords == 1) {
Remainder->data.digit = Make_64(R[1], R[0]);
} else {
Remainder->data.digits = allocate<uint64_t>(rhsWords);
for (size_t i = 0; i < rhsWords; i += 1) {
Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]);
}
}
}
}

View File

@ -47,6 +47,7 @@ fn testDivision() {
assert(@divTrunc(-1194735857077236777412821811143690633098347576,
-508740759824825164163191790951174292733114988) ==
2);
assert(4126227191251978491697987544882340798050766755606969681711 % 10 == 1);
}
}
fn div(comptime T: type, a: T, b: T) -> T {