static int Compare(Bignum a, Bignum b) { int bigit_length_a = a.BigitLength(); int bigit_length_b = b.BigitLength(); if (bigit_length_a < bigit_length_b) { return(-1); } if (bigit_length_a > bigit_length_b) { return(+1); } for (int i = bigit_length_a - 1; i >= System.Math.Min(a.exponent_, b.exponent_); --i) { uint bigit_a = a.BigitAt(i); uint bigit_b = b.BigitAt(i); if (bigit_a < bigit_b) { return(-1); } if (bigit_a > bigit_b) { return(+1); } // Otherwise they are equal up to this digit. Try the next digit. } return(0); }
internal static int PlusCompare(Bignum a, Bignum b, Bignum c) { Debug.Assert(a.IsClamped()); Debug.Assert(b.IsClamped()); Debug.Assert(c.IsClamped()); if (a.BigitLength() < b.BigitLength()) { return(PlusCompare(b, a, c)); } if (a.BigitLength() + 1 < c.BigitLength()) { return(-1); } if (a.BigitLength() > c.BigitLength()) { return(+1); } // The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than // 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one // of 'a'. if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) { return(-1); } uint borrow = 0; // Starting at min_exponent all digits are == 0. So no need to compare them. int min_exponent = System.Math.Min(System.Math.Min(a.exponent_, b.exponent_), c.exponent_); for (int i = c.BigitLength() - 1; i >= min_exponent; --i) { uint chunk_a = a.BigitAt(i); uint chunk_b = b.BigitAt(i); uint chunk_c = c.BigitAt(i); uint sum = chunk_a + chunk_b; if (sum > chunk_c + borrow) { return(+1); } else { borrow = chunk_c + borrow - sum; if (borrow > 1) { return(-1); } borrow <<= kBigitSize; } } if (borrow == 0) { return(0); } return(-1); }