// Copyright 2014 The PDFium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Original code by Matt McCutchen, see the LICENSE file. #include "BigInteger.hh" BigInteger& BigInteger::operator =(const BigInteger &x) { … } BigInteger::BigInteger(const Blk *b, Index blen, Sign s) : … { … } BigInteger::BigInteger(const BigUnsigned &x, Sign s) : … { … } /* CONSTRUCTION FROM PRIMITIVE INTEGERS * Same idea as in BigUnsigned.cc, except that negative input results in a * negative BigInteger instead of an exception. */ // Done longhand to let us use initialization. BigInteger::BigInteger(unsigned long x) : … { … } BigInteger::BigInteger(unsigned int x) : … { … } BigInteger::BigInteger(unsigned short x) : … { … } // For signed input, determine the desired magnitude and sign separately. namespace { template <class X, class UX> BigInteger::Blk magOf(X x) { … } template <class X> BigInteger::Sign signOf(X x) { … } } BigInteger::BigInteger(long x) : … { … } BigInteger::BigInteger(int x) : … { … } BigInteger::BigInteger(short x) : … { … } // CONVERSION TO PRIMITIVE INTEGERS /* Reuse BigUnsigned's conversion to an unsigned primitive integer. * The friend is a separate function rather than * BigInteger::convertToUnsignedPrimitive to avoid requiring BigUnsigned to * declare BigInteger. */ template <class X> inline X convertBigUnsignedToPrimitiveAccess(const BigUnsigned &a) { … } template <class X> X BigInteger::convertToUnsignedPrimitive() const { … } /* Similar to BigUnsigned::convertToPrimitive, but split into two cases for * nonnegative and negative numbers. */ template <class X, class UX> X BigInteger::convertToSignedPrimitive() const { … } unsigned long BigInteger::toUnsignedLong () const { … } unsigned int BigInteger::toUnsignedInt () const { … } unsigned short BigInteger::toUnsignedShort() const { … } long BigInteger::toLong () const { … } int BigInteger::toInt () const { … } short BigInteger::toShort () const { … } // COMPARISON BigInteger::CmpRes BigInteger::compareTo(const BigInteger &x) const { … } /* COPY-LESS OPERATIONS * These do some messing around to determine the sign of the result, * then call one of BigUnsigned's copy-less operations. */ // See remarks about aliased calls in BigUnsigned.cc . #define DTRT_ALIASED(cond, op) … void BigInteger::add(const BigInteger &a, const BigInteger &b) { … } void BigInteger::subtract(const BigInteger &a, const BigInteger &b) { … } void BigInteger::multiply(const BigInteger &a, const BigInteger &b) { … } /* * DIVISION WITH REMAINDER * Please read the comments before the definition of * `BigUnsigned::divideWithRemainder' in `BigUnsigned.cc' for lots of * information you should know before reading this function. * * Following Knuth, I decree that x / y is to be * 0 if y==0 and floor(real-number x / y) if y!=0. * Then x % y shall be x - y*(integer x / y). * * Note that x = y * (x / y) + (x % y) always holds. * In addition, (x % y) is from 0 to y - 1 if y > 0, * and from -(|y| - 1) to 0 if y < 0. (x % y) = x if y = 0. * * Examples: (q = a / b, r = a % b) * a b q r * === === === === * 4 3 1 1 * -4 3 -2 2 * 4 -3 -2 -2 * -4 -3 1 -1 */ void BigInteger::divideWithRemainder(const BigInteger &b, BigInteger &q) { … } // Negation void BigInteger::negate(const BigInteger &a) { … } // INCREMENT/DECREMENT OPERATORS // Prefix increment BigInteger& BigInteger::operator ++() { … } // Postfix increment BigInteger BigInteger::operator ++(int) { … } // Prefix decrement BigInteger& BigInteger::operator --() { … } // Postfix decrement BigInteger BigInteger::operator --(int) { … }