[poincare] Handle allocation failures in NaturalIntegerAbstract::udiv

This commit is contained in:
Léa Saviot
2018-08-20 17:03:43 +02:00
parent 73ae2a8bd6
commit 199a4386ee
2 changed files with 9 additions and 2 deletions

View File

@@ -22,10 +22,10 @@ Integer Arithmetic::GCD(const Integer a, const Integer b) {
i.setNegative(false);
j.setNegative(false);
do {
if (i.isZero()) {
if (j.isAllocationFailure() || i.isZero()) {
return j;
}
if (j.isZero()) {
if (i.isAllocationFailure() || j.isZero()) {
return i;
}
if (Integer::NaturalOrder(i, j) > 0) {

View File

@@ -280,6 +280,13 @@ IntegerDivision NaturalIntegerAbstract::udiv(const NaturalIntegerAbstract * nume
Integer A(numerator);
Integer B(denominator);
if (A.isAllocationFailure()) {
return {.quotient = A, .remainder = A};
}
if (B.isAllocationFailure()) {
return {.quotient = B, .remainder = B};
}
native_int_t base = 1 << 16;
// TODO: optimize by just swifting digit and finding 2^kB that makes B normalized
native_int_t d = base/(native_int_t)(B.node()->halfDigit(B.node()->numberOfHalfDigits()-1)+1);