yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BigInteger Class Reference

#include <BigInteger.hh>

+ Collaboration diagram for BigInteger:

Public Types

enum  Sign { negative = -1, zero = 0, positive = 1 }
 
typedef BigUnsigned::Blk Blk
 
typedef BigUnsigned::Index Index
 
typedef BigUnsigned::CmpRes CmpRes
 

Public Member Functions

 BigInteger ()
 
 BigInteger (const BigInteger &x)
 
void operator= (const BigInteger &x)
 
 BigInteger (const Blk *b, Index blen, Sign s)
 
 BigInteger (const Blk *b, Index blen)
 
 BigInteger (const BigUnsigned &x, Sign s)
 
 BigInteger (const BigUnsigned &x)
 
 BigInteger (unsigned long x)
 
 BigInteger (long x)
 
 BigInteger (unsigned int x)
 
 BigInteger (int x)
 
 BigInteger (unsigned short x)
 
 BigInteger (short x)
 
unsigned long toUnsignedLong () const
 
long toLong () const
 
unsigned int toUnsignedInt () const
 
int toInt () const
 
unsigned short toUnsignedShort () const
 
short toShort () const
 
Sign getSign () const
 
const BigUnsignedgetMagnitude () const
 
Index getLength () const
 
Index getCapacity () const
 
Blk getBlock (Index i) const
 
bool isZero () const
 
CmpRes compareTo (const BigInteger &x) const
 
bool operator== (const BigInteger &x) const
 
bool operator!= (const BigInteger &x) const
 
bool operator< (const BigInteger &x) const
 
bool operator<= (const BigInteger &x) const
 
bool operator>= (const BigInteger &x) const
 
bool operator> (const BigInteger &x) const
 
void add (const BigInteger &a, const BigInteger &b)
 
void subtract (const BigInteger &a, const BigInteger &b)
 
void multiply (const BigInteger &a, const BigInteger &b)
 
void divideWithRemainder (const BigInteger &b, BigInteger &q)
 
void negate (const BigInteger &a)
 
BigInteger operator+ (const BigInteger &x) const
 
BigInteger operator- (const BigInteger &x) const
 
BigInteger operator* (const BigInteger &x) const
 
BigInteger operator/ (const BigInteger &x) const
 
BigInteger operator% (const BigInteger &x) const
 
BigInteger operator- () const
 
void operator+= (const BigInteger &x)
 
void operator-= (const BigInteger &x)
 
void operator*= (const BigInteger &x)
 
void operator/= (const BigInteger &x)
 
void operator%= (const BigInteger &x)
 
void flipSign ()
 
void operator++ ()
 
void operator++ (int)
 
void operator-- ()
 
void operator-- (int)
 

Static Public Attributes

static const CmpRes less = BigUnsigned::less
 
static const CmpRes equal = BigUnsigned::equal
 
static const CmpRes greater = BigUnsigned::greater
 

Protected Member Functions

template<class X >
X convertToUnsignedPrimitive () const
 
template<class X , class UX >
X convertToSignedPrimitive () const
 

Protected Attributes

Sign sign
 
BigUnsigned mag
 

Detailed Description

Definition at line 13 of file BigInteger.hh.

Member Typedef Documentation

Definition at line 16 of file BigInteger.hh.

Definition at line 18 of file BigInteger.hh.

Definition at line 17 of file BigInteger.hh.

Member Enumeration Documentation

Enumerator
negative 
zero 
positive 

Definition at line 24 of file BigInteger.hh.

Constructor & Destructor Documentation

BigInteger::BigInteger ( )
inline

Definition at line 32 of file BigInteger.hh.

32 : sign(zero), mag() {}
BigUnsigned mag
Definition: BigInteger.hh:28
BigInteger::BigInteger ( const BigInteger x)
inline

Definition at line 35 of file BigInteger.hh.

35 : sign(x.sign), mag(x.mag) {};
BigUnsigned mag
Definition: BigInteger.hh:28
BigInteger::BigInteger ( const Blk b,
Index  blen,
Sign  s 
)

Definition at line 13 of file BigInteger.cc.

13  : mag(b, blen) {
14  switch (s) {
15  case zero:
16  if (!mag.isZero())
17  throw "BigInteger::BigInteger(const Blk *, Index, Sign): Cannot use a sign of zero with a nonzero magnitude";
18  sign = zero;
19  break;
20  case positive:
21  case negative:
22  // If the magnitude is zero, force the sign to zero.
23  sign = mag.isZero() ? zero : s;
24  break;
25  default:
26  /* g++ seems to be optimizing out this case on the assumption
27  * that the sign is a valid member of the enumeration. Oh well. */
28  throw "BigInteger::BigInteger(const Blk *, Index, Sign): Invalid sign";
29  }
30 }
BigUnsigned mag
Definition: BigInteger.hh:28
bool isZero() const
Definition: BigUnsigned.hh:97

+ Here is the call graph for this function:

BigInteger::BigInteger ( const Blk b,
Index  blen 
)
inline

Definition at line 44 of file BigInteger.hh.

44  : mag(b, blen) {
45  sign = mag.isZero() ? zero : positive;
46  }
BigUnsigned mag
Definition: BigInteger.hh:28
bool isZero() const
Definition: BigUnsigned.hh:97

+ Here is the call graph for this function:

BigInteger::BigInteger ( const BigUnsigned x,
Sign  s 
)

Definition at line 32 of file BigInteger.cc.

32  : mag(x) {
33  switch (s) {
34  case zero:
35  if (!mag.isZero())
36  throw "BigInteger::BigInteger(const BigUnsigned &, Sign): Cannot use a sign of zero with a nonzero magnitude";
37  sign = zero;
38  break;
39  case positive:
40  case negative:
41  // If the magnitude is zero, force the sign to zero.
42  sign = mag.isZero() ? zero : s;
43  break;
44  default:
45  /* g++ seems to be optimizing out this case on the assumption
46  * that the sign is a valid member of the enumeration. Oh well. */
47  throw "BigInteger::BigInteger(const BigUnsigned &, Sign): Invalid sign";
48  }
49 }
BigUnsigned mag
Definition: BigInteger.hh:28
bool isZero() const
Definition: BigUnsigned.hh:97

+ Here is the call graph for this function:

BigInteger::BigInteger ( const BigUnsigned x)
inline

Definition at line 52 of file BigInteger.hh.

52  : mag(x) {
53  sign = mag.isZero() ? zero : positive;
54  }
BigUnsigned mag
Definition: BigInteger.hh:28
bool isZero() const
Definition: BigUnsigned.hh:97

+ Here is the call graph for this function:

BigInteger::BigInteger ( unsigned long  x)

Definition at line 56 of file BigInteger.cc.

56 : mag(x) { sign = mag.isZero() ? zero : positive; }
BigUnsigned mag
Definition: BigInteger.hh:28
bool isZero() const
Definition: BigUnsigned.hh:97

+ Here is the call graph for this function:

BigInteger::BigInteger ( long  x)

Definition at line 77 of file BigInteger.cc.

77 : sign(signOf(x)), mag(magOf<long , unsigned long >(x)) {}
BigUnsigned mag
Definition: BigInteger.hh:28
BigInteger::BigInteger ( unsigned int  x)

Definition at line 57 of file BigInteger.cc.

57 : mag(x) { sign = mag.isZero() ? zero : positive; }
BigUnsigned mag
Definition: BigInteger.hh:28
bool isZero() const
Definition: BigUnsigned.hh:97

+ Here is the call graph for this function:

BigInteger::BigInteger ( int  x)

Definition at line 78 of file BigInteger.cc.

78 : sign(signOf(x)), mag(magOf<int , unsigned int >(x)) {}
BigUnsigned mag
Definition: BigInteger.hh:28
BigInteger::BigInteger ( unsigned short  x)

Definition at line 58 of file BigInteger.cc.

58 : mag(x) { sign = mag.isZero() ? zero : positive; }
BigUnsigned mag
Definition: BigInteger.hh:28
bool isZero() const
Definition: BigUnsigned.hh:97

+ Here is the call graph for this function:

BigInteger::BigInteger ( short  x)

Definition at line 79 of file BigInteger.cc.

79 : sign(signOf(x)), mag(magOf<short, unsigned short>(x)) {}
BigUnsigned mag
Definition: BigInteger.hh:28

Member Function Documentation

void BigInteger::add ( const BigInteger a,
const BigInteger b 
)

Definition at line 169 of file BigInteger.cc.

169  {
170  DTRT_ALIASED(this == &a || this == &b, add(a, b));
171  // If one argument is zero, copy the other.
172  if (a.sign == zero)
173  operator =(b);
174  else if (b.sign == zero)
175  operator =(a);
176  // If the arguments have the same sign, take the
177  // common sign and add their magnitudes.
178  else if (a.sign == b.sign) {
179  sign = a.sign;
180  mag.add(a.mag, b.mag);
181  } else {
182  // Otherwise, their magnitudes must be compared.
183  switch (a.mag.compareTo(b.mag)) {
184  case equal:
185  // If their magnitudes are the same, copy zero.
186  mag = 0;
187  sign = zero;
188  break;
189  // Otherwise, take the sign of the greater, and subtract
190  // the lesser magnitude from the greater magnitude.
191  case greater:
192  sign = a.sign;
193  mag.subtract(a.mag, b.mag);
194  break;
195  case less:
196  sign = b.sign;
197  mag.subtract(b.mag, a.mag);
198  break;
199  }
200  }
201 }
BigUnsigned mag
Definition: BigInteger.hh:28
CmpRes compareTo(const BigUnsigned &x) const
Definition: BigUnsigned.cc:69
static const CmpRes greater
Definition: BigInteger.hh:22
void operator=(const BigInteger &x)
Definition: BigInteger.cc:3
void add(const BigUnsigned &a, const BigUnsigned &b)
Definition: BigUnsigned.cc:124
void subtract(const BigUnsigned &a, const BigUnsigned &b)
Definition: BigUnsigned.cc:184
static const CmpRes equal
Definition: BigInteger.hh:21
void add(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:169
#define DTRT_ALIASED(cond, op)
Definition: BigInteger.cc:161
static const CmpRes less
Definition: BigInteger.hh:20

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

BigInteger::CmpRes BigInteger::compareTo ( const BigInteger x) const

Definition at line 135 of file BigInteger.cc.

135  {
136  // A greater sign implies a greater number
137  if (sign < x.sign)
138  return less;
139  else if (sign > x.sign)
140  return greater;
141  else switch (sign) {
142  // If the signs are the same...
143  case zero:
144  return equal; // Two zeros are equal
145  case positive:
146  // Compare the magnitudes
147  return mag.compareTo(x.mag);
148  case negative:
149  // Compare the magnitudes, but return the opposite result
150  return CmpRes(-mag.compareTo(x.mag));
151  default:
152  throw "BigInteger internal error";
153  }
154 }
BigUnsigned mag
Definition: BigInteger.hh:28
BigUnsigned::CmpRes CmpRes
Definition: BigInteger.hh:18
CmpRes compareTo(const BigUnsigned &x) const
Definition: BigUnsigned.cc:69
static const CmpRes greater
Definition: BigInteger.hh:22
static const CmpRes equal
Definition: BigInteger.hh:21
static const CmpRes less
Definition: BigInteger.hh:20

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class X , class UX >
X BigInteger::convertToSignedPrimitive ( ) const
protected

Definition at line 104 of file BigInteger.cc.

104  {
105  if (sign == zero)
106  return 0;
107  else if (mag.getLength() == 1) {
108  // The single block might fit in an X. Try the conversion.
109  Blk b = mag.getBlock(0);
110  if (sign == positive) {
111  X x = X(b);
112  if (x >= 0 && Blk(x) == b)
113  return x;
114  } else {
115  X x = -X(b);
116  /* UX(...) needed to avoid rejecting conversion of
117  * -2^15 to a short. */
118  if (x < 0 && Blk(UX(-x)) == b)
119  return x;
120  }
121  // Otherwise fall through.
122  }
123  throw "BigInteger::to<Primitive>: "
124  "Value is too big to fit in the requested type";
125 }
BigUnsigned mag
Definition: BigInteger.hh:28
#define X(_item)
Index getLength() const
Blk getBlock(Index i) const
Definition: BigUnsigned.hh:92
BigUnsigned::Blk Blk
Definition: BigInteger.hh:16

+ Here is the call graph for this function:

template<class X >
X BigInteger::convertToUnsignedPrimitive ( ) const
protected

Definition at line 93 of file BigInteger.cc.

93  {
94  if (sign == negative)
95  throw "BigInteger::to<Primitive>: "
96  "Cannot convert a negative integer to an unsigned type";
97  else
98  return convertBigUnsignedToPrimitiveAccess<X>(mag);
99 }
BigUnsigned mag
Definition: BigInteger.hh:28
void BigInteger::divideWithRemainder ( const BigInteger b,
BigInteger q 
)

Definition at line 280 of file BigInteger.cc.

280  {
281  // Defend against aliased calls;
282  // same idea as in BigUnsigned::divideWithRemainder .
283  if (this == &q)
284  throw "BigInteger::divideWithRemainder: Cannot write quotient and remainder into the same variable";
285  if (this == &b || &q == &b) {
286  BigInteger tmpB(b);
287  divideWithRemainder(tmpB, q);
288  return;
289  }
290 
291  // Division by zero gives quotient 0 and remainder *this
292  if (b.sign == zero) {
293  q.mag = 0;
294  q.sign = zero;
295  return;
296  }
297  // 0 / b gives quotient 0 and remainder 0
298  if (sign == zero) {
299  q.mag = 0;
300  q.sign = zero;
301  return;
302  }
303 
304  // Here *this != 0, b != 0.
305 
306  // Do the operands have the same sign?
307  if (sign == b.sign) {
308  // Yes: easy case. Quotient is zero or positive.
309  q.sign = positive;
310  } else {
311  // No: harder case. Quotient is negative.
312  q.sign = negative;
313  // Decrease the magnitude of the dividend by one.
314  mag--;
315  /*
316  * We tinker with the dividend before and with the
317  * quotient and remainder after so that the result
318  * comes out right. To see why it works, consider the following
319  * list of examples, where A is the magnitude-decreased
320  * a, Q and R are the results of BigUnsigned division
321  * with remainder on A and |b|, and q and r are the
322  * final results we want:
323  *
324  * a A b Q R q r
325  * -3 -2 3 0 2 -1 0
326  * -4 -3 3 1 0 -2 2
327  * -5 -4 3 1 1 -2 1
328  * -6 -5 3 1 2 -2 0
329  *
330  * It appears that we need a total of 3 corrections:
331  * Decrease the magnitude of a to get A. Increase the
332  * magnitude of Q to get q (and make it negative).
333  * Find r = (b - 1) - R and give it the desired sign.
334  */
335  }
336 
337  // Divide the magnitudes.
339 
340  if (sign != b.sign) {
341  // More for the harder case (as described):
342  // Increase the magnitude of the quotient by one.
343  q.mag++;
344  // Modify the remainder.
345  mag.subtract(b.mag, mag);
346  mag--;
347  }
348 
349  // Sign of the remainder is always the sign of the divisor b.
350  sign = b.sign;
351 
352  // Set signs to zero as necessary. (Thanks David Allen!)
353  if (mag.isZero())
354  sign = zero;
355  if (q.mag.isZero())
356  q.sign = zero;
357 
358  // WHEW!!!
359 }
BigUnsigned mag
Definition: BigInteger.hh:28
void divideWithRemainder(const BigUnsigned &b, BigUnsigned &q)
Definition: BigUnsigned.cc:382
bool isZero() const
Definition: BigUnsigned.hh:97
void subtract(const BigUnsigned &a, const BigUnsigned &b)
Definition: BigUnsigned.cc:184
void divideWithRemainder(const BigInteger &b, BigInteger &q)
Definition: BigInteger.cc:280

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void BigInteger::flipSign ( )
inline

Definition at line 211 of file BigInteger.hh.

211  {
212  sign = Sign(-sign);
213 }
Blk BigInteger::getBlock ( Index  i) const
inline

Definition at line 88 of file BigInteger.hh.

88 { return mag.getBlock(i); }
BigUnsigned mag
Definition: BigInteger.hh:28
Blk getBlock(Index i) const
Definition: BigUnsigned.hh:92

+ Here is the call graph for this function:

Index BigInteger::getCapacity ( ) const
inline

Definition at line 87 of file BigInteger.hh.

87 { return mag.getCapacity(); }
BigUnsigned mag
Definition: BigInteger.hh:28
Index getCapacity() const

+ Here is the call graph for this function:

Index BigInteger::getLength ( ) const
inline

Definition at line 86 of file BigInteger.hh.

86 { return mag.getLength(); }
BigUnsigned mag
Definition: BigInteger.hh:28
Index getLength() const

+ Here is the call graph for this function:

const BigUnsigned& BigInteger::getMagnitude ( ) const
inline

Definition at line 83 of file BigInteger.hh.

83 { return mag; }
BigUnsigned mag
Definition: BigInteger.hh:28

+ Here is the caller graph for this function:

Sign BigInteger::getSign ( ) const
inline

Definition at line 80 of file BigInteger.hh.

80 { return sign; }

+ Here is the caller graph for this function:

bool BigInteger::isZero ( ) const
inline

Definition at line 89 of file BigInteger.hh.

89 { return sign == zero; } // A bit special

+ Here is the caller graph for this function:

void BigInteger::multiply ( const BigInteger a,
const BigInteger b 
)

Definition at line 243 of file BigInteger.cc.

243  {
244  DTRT_ALIASED(this == &a || this == &b, multiply(a, b));
245  // If one object is zero, copy zero and return.
246  if (a.sign == zero || b.sign == zero) {
247  sign = zero;
248  mag = 0;
249  return;
250  }
251  // If the signs of the arguments are the same, the result
252  // is positive, otherwise it is negative.
253  sign = (a.sign == b.sign) ? positive : negative;
254  // Multiply the magnitudes.
255  mag.multiply(a.mag, b.mag);
256 }
BigUnsigned mag
Definition: BigInteger.hh:28
#define DTRT_ALIASED(cond, op)
Definition: BigInteger.cc:161
void multiply(const BigUnsigned &a, const BigUnsigned &b)
Definition: BigUnsigned.cc:300
void multiply(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:243

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void BigInteger::negate ( const BigInteger a)

Definition at line 362 of file BigInteger.cc.

362  {
363  DTRT_ALIASED(this == &a, negate(a));
364  // Copy a's magnitude
365  mag = a.mag;
366  // Copy the opposite of a.sign
367  sign = Sign(-a.sign);
368 }
BigUnsigned mag
Definition: BigInteger.hh:28
void negate(const BigInteger &a)
Definition: BigInteger.cc:362
#define DTRT_ALIASED(cond, op)
Definition: BigInteger.cc:161

+ Here is the caller graph for this function:

bool BigInteger::operator!= ( const BigInteger x) const
inline

Definition at line 100 of file BigInteger.hh.

100 { return !operator ==(x); };
bool operator==(const BigInteger &x) const
Definition: BigInteger.hh:97

+ Here is the call graph for this function:

BigInteger BigInteger::operator% ( const BigInteger x) const
inline

Definition at line 166 of file BigInteger.hh.

166  {
167  if (x.isZero()) throw "BigInteger::operator %: division by zero";
168  BigInteger q, r;
169  r = *this;
170  r.divideWithRemainder(x, q);
171  return r;
172 }
bool isZero() const
Definition: BigInteger.hh:89
void divideWithRemainder(const BigInteger &b, BigInteger &q)
Definition: BigInteger.cc:280

+ Here is the call graph for this function:

void BigInteger::operator%= ( const BigInteger x)
inline

Definition at line 204 of file BigInteger.hh.

204  {
205  if (x.isZero()) throw "BigInteger::operator %=: division by zero";
206  BigInteger q;
207  // Mods *this by x. Don't care about quotient left in q.
208  divideWithRemainder(x, q);
209 }
bool isZero() const
Definition: BigInteger.hh:89
void divideWithRemainder(const BigInteger &b, BigInteger &q)
Definition: BigInteger.cc:280

+ Here is the call graph for this function:

BigInteger BigInteger::operator* ( const BigInteger x) const
inline

Definition at line 154 of file BigInteger.hh.

154  {
155  BigInteger ans;
156  ans.multiply(*this, x);
157  return ans;
158 }
void multiply(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:243

+ Here is the call graph for this function:

void BigInteger::operator*= ( const BigInteger x)
inline

Definition at line 192 of file BigInteger.hh.

192  {
193  multiply(*this, x);
194 }
void multiply(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:243

+ Here is the call graph for this function:

BigInteger BigInteger::operator+ ( const BigInteger x) const
inline

Definition at line 144 of file BigInteger.hh.

144  {
145  BigInteger ans;
146  ans.add(*this, x);
147  return ans;
148 }
void add(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:169

+ Here is the call graph for this function:

void BigInteger::operator++ ( )

Definition at line 373 of file BigInteger.cc.

373  {
374  if (sign == negative) {
375  mag--;
376  if (mag == 0)
377  sign = zero;
378  } else {
379  mag++;
380  sign = positive; // if not already
381  }
382 }
BigUnsigned mag
Definition: BigInteger.hh:28

+ Here is the caller graph for this function:

void BigInteger::operator++ ( int  )

Definition at line 385 of file BigInteger.cc.

385  {
386  operator ++();
387 }
void operator++()
Definition: BigInteger.cc:373

+ Here is the call graph for this function:

void BigInteger::operator+= ( const BigInteger x)
inline

Definition at line 186 of file BigInteger.hh.

186  {
187  add(*this, x);
188 }
void add(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:169

+ Here is the call graph for this function:

BigInteger BigInteger::operator- ( const BigInteger x) const
inline

Definition at line 149 of file BigInteger.hh.

149  {
150  BigInteger ans;
151  ans.subtract(*this, x);
152  return ans;
153 }
void subtract(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:203

+ Here is the call graph for this function:

BigInteger BigInteger::operator- ( ) const
inline

Definition at line 173 of file BigInteger.hh.

173  {
174  BigInteger ans;
175  ans.negate(*this);
176  return ans;
177 }
void negate(const BigInteger &a)
Definition: BigInteger.cc:362

+ Here is the call graph for this function:

void BigInteger::operator-- ( )

Definition at line 390 of file BigInteger.cc.

390  {
391  if (sign == positive) {
392  mag--;
393  if (mag == 0)
394  sign = zero;
395  } else {
396  mag++;
397  sign = negative;
398  }
399 }
BigUnsigned mag
Definition: BigInteger.hh:28

+ Here is the caller graph for this function:

void BigInteger::operator-- ( int  )

Definition at line 402 of file BigInteger.cc.

402  {
403  operator --();
404 }
void operator--()
Definition: BigInteger.cc:390

+ Here is the call graph for this function:

void BigInteger::operator-= ( const BigInteger x)
inline

Definition at line 189 of file BigInteger.hh.

189  {
190  subtract(*this, x);
191 }
void subtract(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:203

+ Here is the call graph for this function:

BigInteger BigInteger::operator/ ( const BigInteger x) const
inline

Definition at line 159 of file BigInteger.hh.

159  {
160  if (x.isZero()) throw "BigInteger::operator /: division by zero";
161  BigInteger q, r;
162  r = *this;
163  r.divideWithRemainder(x, q);
164  return q;
165 }
bool isZero() const
Definition: BigInteger.hh:89
void divideWithRemainder(const BigInteger &b, BigInteger &q)
Definition: BigInteger.cc:280

+ Here is the call graph for this function:

void BigInteger::operator/= ( const BigInteger x)
inline

Definition at line 195 of file BigInteger.hh.

195  {
196  if (x.isZero()) throw "BigInteger::operator /=: division by zero";
197  /* The following technique is slightly faster than copying *this first
198  * when x is large. */
199  BigInteger q;
200  divideWithRemainder(x, q);
201  // *this contains the remainder, but we overwrite it with the quotient.
202  *this = q;
203 }
bool isZero() const
Definition: BigInteger.hh:89
void divideWithRemainder(const BigInteger &b, BigInteger &q)
Definition: BigInteger.cc:280

+ Here is the call graph for this function:

bool BigInteger::operator< ( const BigInteger x) const
inline

Definition at line 101 of file BigInteger.hh.

101 { return compareTo(x) == less ; }
CmpRes compareTo(const BigInteger &x) const
Definition: BigInteger.cc:135
static const CmpRes less
Definition: BigInteger.hh:20

+ Here is the call graph for this function:

bool BigInteger::operator<= ( const BigInteger x) const
inline

Definition at line 102 of file BigInteger.hh.

102 { return compareTo(x) != greater; }
static const CmpRes greater
Definition: BigInteger.hh:22
CmpRes compareTo(const BigInteger &x) const
Definition: BigInteger.cc:135

+ Here is the call graph for this function:

void BigInteger::operator= ( const BigInteger x)

Definition at line 3 of file BigInteger.cc.

3  {
4  // Calls like a = a have no effect
5  if (this == &x)
6  return;
7  // Copy sign
8  sign = x.sign;
9  // Copy the rest
10  mag = x.mag;
11 }
BigUnsigned mag
Definition: BigInteger.hh:28

+ Here is the caller graph for this function:

bool BigInteger::operator== ( const BigInteger x) const
inline

Definition at line 97 of file BigInteger.hh.

97  {
98  return sign == x.sign && mag == x.mag;
99  }
BigUnsigned mag
Definition: BigInteger.hh:28

+ Here is the caller graph for this function:

bool BigInteger::operator> ( const BigInteger x) const
inline

Definition at line 104 of file BigInteger.hh.

104 { return compareTo(x) == greater; }
static const CmpRes greater
Definition: BigInteger.hh:22
CmpRes compareTo(const BigInteger &x) const
Definition: BigInteger.cc:135

+ Here is the call graph for this function:

bool BigInteger::operator>= ( const BigInteger x) const
inline

Definition at line 103 of file BigInteger.hh.

103 { return compareTo(x) != less ; }
CmpRes compareTo(const BigInteger &x) const
Definition: BigInteger.cc:135
static const CmpRes less
Definition: BigInteger.hh:20

+ Here is the call graph for this function:

void BigInteger::subtract ( const BigInteger a,
const BigInteger b 
)

Definition at line 203 of file BigInteger.cc.

203  {
204  // Notice that this routine is identical to BigInteger::add,
205  // if one replaces b.sign by its opposite.
206  DTRT_ALIASED(this == &a || this == &b, subtract(a, b));
207  // If a is zero, copy b and flip its sign. If b is zero, copy a.
208  if (a.sign == zero) {
209  mag = b.mag;
210  // Take the negative of _b_'s, sign, not ours.
211  // Bug pointed out by Sam Larkin on 2005.03.30.
212  sign = Sign(-b.sign);
213  } else if (b.sign == zero)
214  operator =(a);
215  // If their signs differ, take a.sign and add the magnitudes.
216  else if (a.sign != b.sign) {
217  sign = a.sign;
218  mag.add(a.mag, b.mag);
219  } else {
220  // Otherwise, their magnitudes must be compared.
221  switch (a.mag.compareTo(b.mag)) {
222  // If their magnitudes are the same, copy zero.
223  case equal:
224  mag = 0;
225  sign = zero;
226  break;
227  // If a's magnitude is greater, take a.sign and
228  // subtract a from b.
229  case greater:
230  sign = a.sign;
231  mag.subtract(a.mag, b.mag);
232  break;
233  // If b's magnitude is greater, take the opposite
234  // of b.sign and subtract b from a.
235  case less:
236  sign = Sign(-b.sign);
237  mag.subtract(b.mag, a.mag);
238  break;
239  }
240  }
241 }
BigUnsigned mag
Definition: BigInteger.hh:28
CmpRes compareTo(const BigUnsigned &x) const
Definition: BigUnsigned.cc:69
static const CmpRes greater
Definition: BigInteger.hh:22
void operator=(const BigInteger &x)
Definition: BigInteger.cc:3
void add(const BigUnsigned &a, const BigUnsigned &b)
Definition: BigUnsigned.cc:124
void subtract(const BigUnsigned &a, const BigUnsigned &b)
Definition: BigUnsigned.cc:184
static const CmpRes equal
Definition: BigInteger.hh:21
#define DTRT_ALIASED(cond, op)
Definition: BigInteger.cc:161
static const CmpRes less
Definition: BigInteger.hh:20
void subtract(const BigInteger &a, const BigInteger &b)
Definition: BigInteger.cc:203

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int BigInteger::toInt ( ) const

Definition at line 131 of file BigInteger.cc.

131 { return convertToSignedPrimitive <int , unsigned int> (); }

+ Here is the caller graph for this function:

long BigInteger::toLong ( ) const

Definition at line 130 of file BigInteger.cc.

130 { return convertToSignedPrimitive <long , unsigned long> (); }
short BigInteger::toShort ( ) const

Definition at line 132 of file BigInteger.cc.

132 { return convertToSignedPrimitive <short, unsigned short>(); }
unsigned int BigInteger::toUnsignedInt ( ) const

Definition at line 128 of file BigInteger.cc.

128 { return convertToUnsignedPrimitive<unsigned int > (); }
unsigned long BigInteger::toUnsignedLong ( ) const

Definition at line 127 of file BigInteger.cc.

127 { return convertToUnsignedPrimitive<unsigned long > (); }
unsigned short BigInteger::toUnsignedShort ( ) const

Definition at line 129 of file BigInteger.cc.

129 { return convertToUnsignedPrimitive<unsigned short> (); }

Field Documentation

const CmpRes BigInteger::equal = BigUnsigned::equal
static

Definition at line 21 of file BigInteger.hh.

const CmpRes BigInteger::greater = BigUnsigned::greater
static

Definition at line 22 of file BigInteger.hh.

const CmpRes BigInteger::less = BigUnsigned::less
static

Definition at line 20 of file BigInteger.hh.

BigUnsigned BigInteger::mag
protected

Definition at line 28 of file BigInteger.hh.

Sign BigInteger::sign
protected

Definition at line 27 of file BigInteger.hh.


The documentation for this class was generated from the following files: