Commit | Line | Data |
---|---|---|
e67d6049 MM |
1 | /* |
2 | * Matt McCutchen's Big Integer Library | |
e67d6049 MM |
3 | */ |
4 | ||
05780f4b | 5 | #include "BigInteger.hh" |
e67d6049 MM |
6 | |
7 | // MANAGEMENT | |
8 | ||
9 | // Assignment operator | |
10 | void BigInteger::operator =(const BigInteger &x) { | |
11 | // Calls like a = a have no effect | |
12 | if (this == &x) | |
13 | return; | |
14 | // Copy sign | |
15 | sign = x.sign; | |
16 | // Copy the rest | |
17 | BigUnsigned::operator =(x); | |
18 | } | |
19 | ||
20 | // Constructor from an array of blocks and a sign | |
05780f4b | 21 | BigInteger::BigInteger(const Blk *b, Index l, Sign s) : BigUnsigned(b, l) { |
e67d6049 MM |
22 | switch (s) { |
23 | case zero: | |
24 | case positive: | |
25 | case negative: | |
26 | sign = (len == 0) ? zero : s; | |
27 | break; | |
28 | default: | |
05780f4b | 29 | throw "BigInteger::BigInteger(Blk *, Index, Sign): Invalid sign"; |
e67d6049 MM |
30 | } |
31 | } | |
32 | ||
33 | // Constructor from a BigUnsigned and a sign | |
34 | BigInteger::BigInteger(const BigUnsigned &x, Sign s) : BigUnsigned(x) { | |
35 | switch (s) { | |
36 | case zero: | |
37 | case positive: | |
38 | case negative: | |
39 | sign = (len == 0) ? zero : s; | |
40 | break; | |
41 | default: | |
05780f4b | 42 | throw "BigInteger::BigInteger(Blk *, Index, Sign): Invalid sign"; |
e67d6049 MM |
43 | } |
44 | } | |
45 | ||
46 | /* | |
47 | * The steps for construction of a BigInteger | |
48 | * from an integral value x are as follows: | |
49 | * 1. If x is zero, create an empty BigInteger and stop. | |
50 | * 2. Allocate a one-block number array. | |
51 | * 3. If x is positive (or of an unsigned type), set the | |
52 | * sign of the BigInteger to positive. | |
53 | * 4. If x is of a signed type and is negative, set the | |
54 | * sign of the BigInteger to negative. | |
55 | * 5. If x is of a signed type, convert x (or -x if x < 0) | |
56 | * to the unsigned type of the same length. | |
57 | * 6. Expand x (or the result of step 5) to a Blk, | |
58 | * and store it in the number array. | |
b3fe29df MM |
59 | * |
60 | * See remarks in `BigUnsigned.cc' and `NumberlikeArray.hh' | |
61 | * about new handling of zero-length arrays. | |
e67d6049 MM |
62 | */ |
63 | ||
64 | BigInteger::BigInteger(unsigned long x) { | |
b3fe29df MM |
65 | if (x == 0) |
66 | sign = zero; // NumberlikeArray did the rest | |
67 | else { | |
e67d6049 | 68 | cap = 1; |
a8b42b68 | 69 | blk = new Blk[1]; |
e67d6049 MM |
70 | sign = positive; |
71 | len = 1; | |
2f145f11 | 72 | blk[0] = Blk(x); |
e67d6049 MM |
73 | } |
74 | } | |
75 | ||
76 | BigInteger::BigInteger(long x) { | |
77 | if (x > 0) { | |
78 | cap = 1; | |
a8b42b68 | 79 | blk = new Blk[1]; |
e67d6049 MM |
80 | sign = positive; |
81 | len = 1; | |
2f145f11 | 82 | blk[0] = Blk(x); |
e67d6049 MM |
83 | } else if (x < 0) { |
84 | cap = 1; | |
a8b42b68 | 85 | blk = new Blk[1]; |
e67d6049 MM |
86 | sign = negative; |
87 | len = 1; | |
2f145f11 | 88 | blk[0] = Blk(-x); |
b3fe29df MM |
89 | } else |
90 | sign = zero; | |
e67d6049 MM |
91 | } |
92 | ||
93 | BigInteger::BigInteger(unsigned int x) { | |
b3fe29df | 94 | if (x == 0) |
e67d6049 | 95 | sign = zero; |
b3fe29df | 96 | else { |
e67d6049 | 97 | cap = 1; |
a8b42b68 | 98 | blk = new Blk[1]; |
e67d6049 MM |
99 | sign = positive; |
100 | len = 1; | |
2f145f11 | 101 | blk[0] = Blk(x); |
e67d6049 MM |
102 | } |
103 | } | |
104 | ||
105 | BigInteger::BigInteger(int x) { | |
106 | if (x > 0) { | |
107 | cap = 1; | |
a8b42b68 | 108 | blk = new Blk[1]; |
e67d6049 MM |
109 | sign = positive; |
110 | len = 1; | |
2f145f11 | 111 | blk[0] = Blk(x); |
e67d6049 MM |
112 | } else if (x < 0) { |
113 | cap = 1; | |
a8b42b68 | 114 | blk = new Blk[1]; |
e67d6049 MM |
115 | sign = negative; |
116 | len = 1; | |
2f145f11 | 117 | blk[0] = Blk(-x); |
b3fe29df MM |
118 | } else |
119 | sign = zero; | |
e67d6049 MM |
120 | } |
121 | ||
122 | BigInteger::BigInteger(unsigned short x) { | |
b3fe29df | 123 | if (x == 0) |
e67d6049 | 124 | sign = zero; |
b3fe29df | 125 | else { |
e67d6049 | 126 | cap = 1; |
a8b42b68 | 127 | blk = new Blk[1]; |
e67d6049 MM |
128 | sign = positive; |
129 | len = 1; | |
2f145f11 | 130 | blk[0] = Blk(x); |
e67d6049 MM |
131 | } |
132 | } | |
133 | ||
134 | BigInteger::BigInteger(short x) { | |
135 | if (x > 0) { | |
136 | cap = 1; | |
a8b42b68 | 137 | blk = new Blk[1]; |
e67d6049 MM |
138 | sign = positive; |
139 | len = 1; | |
2f145f11 | 140 | blk[0] = Blk(x); |
e67d6049 MM |
141 | } else if (x < 0) { |
142 | cap = 1; | |
a8b42b68 | 143 | blk = new Blk[1]; |
e67d6049 MM |
144 | sign = negative; |
145 | len = 1; | |
2f145f11 | 146 | blk[0] = Blk(-x); |
b3fe29df MM |
147 | } else |
148 | sign = zero; | |
e67d6049 MM |
149 | } |
150 | ||
151 | // CONVERTERS | |
152 | /* | |
153 | * The steps for conversion of a BigInteger to an | |
154 | * integral type are as follows: | |
155 | * 1. If the BigInteger is zero, return zero. | |
156 | * 2. If the BigInteger is positive: | |
157 | * 3. If it is more than one block long or its lowest | |
158 | * block has bits set out of the range of the target | |
159 | * type, throw an exception. | |
160 | * 4. Otherwise, convert the lowest block to the | |
161 | * target type and return it. | |
162 | * 5. If the BigInteger is negative: | |
163 | * 6. If the target type is unsigned, throw an exception. | |
164 | * 7. If it is more than one block long or its lowest | |
165 | * block has bits set out of the range of the target | |
166 | * type, throw an exception. | |
167 | * 8. Otherwise, convert the lowest block to the | |
168 | * target type, negate it, and return it. | |
169 | */ | |
170 | ||
171 | namespace { | |
172 | // These masks are used to test whether a Blk has bits | |
173 | // set out of the range of a smaller integral type. Note | |
174 | // that this range is not considered to include the sign bit. | |
175 | const BigUnsigned::Blk lMask = ~0 >> 1; | |
176 | const BigUnsigned::Blk uiMask = (unsigned int)(~0); | |
177 | const BigUnsigned::Blk iMask = uiMask >> 1; | |
178 | const BigUnsigned::Blk usMask = (unsigned short)(~0); | |
179 | const BigUnsigned::Blk sMask = usMask >> 1; | |
180 | } | |
181 | ||
182 | BigInteger::operator unsigned long() const { | |
183 | switch (sign) { | |
184 | case zero: | |
185 | return 0; | |
186 | case positive: | |
187 | if (len == 1) | |
2f145f11 | 188 | return blk[0]; |
e67d6049 MM |
189 | else |
190 | throw "BigInteger operator unsigned long() const: Value is too big for an unsigned long"; | |
191 | case negative: | |
192 | throw "BigInteger operator unsigned long() const: Cannot convert a negative integer to an unsigned type"; | |
193 | default: | |
194 | throw "BigInteger: Internal error"; | |
195 | } | |
196 | } | |
197 | ||
198 | BigInteger::operator long() const { | |
199 | switch (sign) { | |
200 | case zero: | |
201 | return 0; | |
202 | case positive: | |
2f145f11 MM |
203 | if (len == 1 && (blk[0] & ~lMask) == 0) |
204 | return long(blk[0]); | |
e67d6049 MM |
205 | else |
206 | throw "BigInteger operator long() const: Value is too big for a long"; | |
207 | case negative: | |
2f145f11 MM |
208 | if (len == 1 && (blk[0] & ~lMask) == 0) |
209 | return -long(blk[0]); | |
e67d6049 MM |
210 | else |
211 | throw "BigInteger operator long() const: Value is too big for a long"; | |
212 | default: | |
213 | throw "BigInteger: Internal error"; | |
214 | } | |
215 | } | |
216 | ||
217 | BigInteger::operator unsigned int() const { | |
218 | switch (sign) { | |
219 | case zero: | |
220 | return 0; | |
221 | case positive: | |
2f145f11 MM |
222 | if (len == 1 && (blk[0] & ~uiMask) == 0) |
223 | return (unsigned int)(blk[0]); | |
e67d6049 MM |
224 | else |
225 | throw "BigInteger operator unsigned int() const: Value is too big for an unsigned int"; | |
226 | case negative: | |
227 | throw "BigInteger operator unsigned int() const: Cannot convert a negative integer to an unsigned type"; | |
228 | default: | |
229 | throw "BigInteger: Internal error"; | |
230 | } | |
231 | } | |
232 | ||
233 | BigInteger::operator int() const { | |
234 | switch (sign) { | |
235 | case zero: | |
236 | return 0; | |
237 | case positive: | |
2f145f11 MM |
238 | if (len == 1 && (blk[0] & ~iMask) == 0) |
239 | return int(blk[0]); | |
e67d6049 MM |
240 | else |
241 | throw "BigInteger operator int() const: Value is too big for an int"; | |
242 | case negative: | |
2f145f11 MM |
243 | if (len == 1 && (blk[0] & ~iMask) == 0) |
244 | return -int(blk[0]); | |
e67d6049 MM |
245 | else |
246 | throw "BigInteger operator int() const: Value is too big for an int"; | |
247 | default: | |
248 | throw "BigInteger: Internal error"; | |
249 | } | |
250 | } | |
251 | ||
252 | BigInteger::operator unsigned short() const { | |
253 | switch (sign) { | |
254 | case zero: | |
255 | return 0; | |
256 | case positive: | |
2f145f11 MM |
257 | if (len == 1 && (blk[0] & ~usMask) == 0) |
258 | return (unsigned short)(blk[0]); | |
e67d6049 MM |
259 | else |
260 | throw "BigInteger operator unsigned short() const: Value is too big for an unsigned short"; | |
261 | case negative: | |
262 | throw "BigInteger operator unsigned short() const: Cannot convert a negative integer to an unsigned type"; | |
263 | default: | |
264 | throw "BigInteger: Internal error"; | |
265 | } | |
266 | } | |
267 | ||
268 | BigInteger::operator short() const { | |
269 | switch (sign) { | |
270 | case zero: | |
271 | return 0; | |
272 | case positive: | |
2f145f11 MM |
273 | if (len == 1 && (blk[0] & ~sMask) == 0) |
274 | return short(blk[0]); | |
e67d6049 MM |
275 | else |
276 | throw "BigInteger operator short() const: Value is too big for a short"; | |
277 | case negative: | |
2f145f11 MM |
278 | if (len == 1 && (blk[0] & ~sMask) == 0) |
279 | return -short(blk[0]); | |
e67d6049 MM |
280 | else |
281 | throw "BigInteger operator short() const: Value is too big for a short"; | |
282 | default: | |
283 | throw "BigInteger: Internal error"; | |
284 | } | |
285 | } | |
286 | ||
287 | // COMPARISON | |
288 | BigInteger::CmpRes BigInteger::compareTo(const BigInteger &x) const { | |
289 | // A greater sign implies a greater number | |
290 | if (sign < x.sign) | |
291 | return less; | |
292 | else if (sign > x.sign) | |
293 | return greater; | |
294 | else switch (sign) { | |
295 | // If the signs are the same... | |
296 | case zero: | |
297 | return equal; // Two zeros are equal | |
298 | case positive: | |
299 | // Compare the magnitudes | |
300 | return BigUnsigned::compareTo(x); | |
301 | case negative: | |
302 | // Compare the magnitudes, but return the opposite result | |
303 | return CmpRes(-BigUnsigned::compareTo(x)); | |
304 | default: | |
305 | throw "BigInteger: Internal error"; | |
306 | } | |
307 | } | |
308 | ||
309 | // PUT-HERE OPERATIONS | |
310 | // These do some messing around to determine the sign of the result, | |
311 | // then call one of BigUnsigned's put-heres. | |
312 | ||
8c16728a MM |
313 | // See remarks about aliased calls in BigUnsigned.cc . |
314 | #define DOTR_ALIASED(cond, op) \ | |
315 | if (cond) { \ | |
316 | BigInteger tmpThis; \ | |
317 | tmpThis.op; \ | |
318 | *this = tmpThis; \ | |
319 | return; \ | |
320 | } | |
321 | ||
e67d6049 MM |
322 | // Addition |
323 | void BigInteger::add(const BigInteger &a, const BigInteger &b) { | |
8c16728a | 324 | DOTR_ALIASED(this == &a || this == &b, add(a, b)); |
e67d6049 MM |
325 | // If one argument is zero, copy the other. |
326 | if (a.sign == zero) | |
327 | operator =(b); | |
328 | else if (b.sign == zero) | |
329 | operator =(a); | |
330 | // If the arguments have the same sign, take the | |
331 | // common sign and add their magnitudes. | |
332 | else if (a.sign == b.sign) { | |
333 | sign = a.sign; | |
334 | BigUnsigned::add(a, b); | |
335 | } else { | |
336 | // Otherwise, their magnitudes must be compared. | |
337 | switch (a.BigUnsigned::compareTo(b)) { | |
338 | // If their magnitudes are the same, copy zero. | |
339 | case equal: | |
340 | len = 0; | |
341 | sign = zero; | |
342 | break; | |
343 | // Otherwise, take the sign of the greater, and subtract | |
344 | // the lesser magnitude from the greater magnitude. | |
345 | case greater: | |
346 | sign = a.sign; | |
347 | BigUnsigned::subtract(a, b); | |
348 | break; | |
349 | case less: | |
350 | sign = b.sign; | |
351 | BigUnsigned::subtract(b, a); | |
352 | break; | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | // Subtraction | |
358 | void BigInteger::subtract(const BigInteger &a, const BigInteger &b) { | |
359 | // Notice that this routine is identical to BigInteger::add, | |
360 | // if one replaces b.sign by its opposite. | |
8c16728a | 361 | DOTR_ALIASED(this == &a || this == &b, subtract(a, b)); |
e67d6049 MM |
362 | // If a is zero, copy b and flip its sign. If b is zero, copy a. |
363 | if (a.sign == zero) { | |
364 | BigUnsigned::operator =(b); | |
f316def7 MM |
365 | // Take the negative of _b_'s, sign, not ours. |
366 | // Bug pointed out by Sam Larkin on 2005.03.30. | |
367 | sign = Sign(-b.sign); | |
e67d6049 | 368 | } else if (b.sign == zero) |
f316def7 | 369 | operator =(a); |
e67d6049 MM |
370 | // If their signs differ, take a.sign and add the magnitudes. |
371 | else if (a.sign != b.sign) { | |
372 | sign = a.sign; | |
373 | BigUnsigned::add(a, b); | |
374 | } else { | |
375 | // Otherwise, their magnitudes must be compared. | |
376 | switch (a.BigUnsigned::compareTo(b)) { | |
377 | // If their magnitudes are the same, copy zero. | |
378 | case equal: | |
379 | len = 0; | |
380 | sign = zero; | |
381 | break; | |
382 | // If a's magnitude is greater, take a.sign and | |
383 | // subtract a from b. | |
384 | case greater: | |
385 | sign = a.sign; | |
386 | BigUnsigned::subtract(a, b); | |
387 | break; | |
388 | // If b's magnitude is greater, take the opposite | |
389 | // of b.sign and subtract b from a. | |
390 | case less: | |
391 | sign = Sign(-b.sign); | |
392 | BigUnsigned::subtract(b, a); | |
393 | break; | |
394 | } | |
395 | } | |
396 | } | |
397 | ||
398 | // Multiplication | |
399 | void BigInteger::multiply(const BigInteger &a, const BigInteger &b) { | |
8c16728a | 400 | DOTR_ALIASED(this == &a || this == &b, multiply(a, b)); |
e67d6049 MM |
401 | // If one object is zero, copy zero and return. |
402 | if (a.sign == zero || b.sign == zero) { | |
403 | sign = zero; | |
404 | len = 0; | |
405 | return; | |
406 | } | |
407 | // If the signs of the arguments are the same, the result | |
408 | // is positive, otherwise it is negative. | |
409 | sign = (a.sign == b.sign) ? positive : negative; | |
410 | // Multiply the magnitudes. | |
411 | BigUnsigned::multiply(a, b); | |
412 | } | |
413 | ||
05780f4b MM |
414 | /* |
415 | * DIVISION WITH REMAINDER | |
416 | * Please read the comments before the definition of | |
417 | * `BigUnsigned::divideWithRemainder' in `BigUnsigned.cc' for lots of | |
418 | * information you should know before reading this function. | |
419 | * | |
420 | * Following Knuth, I decree that x / y is to be | |
421 | * 0 if y==0 and floor(real-number x / y) if y!=0. | |
422 | * Then x % y shall be x - y*(integer x / y). | |
423 | * | |
424 | * Note that x = y * (x / y) + (x % y) always holds. | |
425 | * In addition, (x % y) is from 0 to y - 1 if y > 0, | |
426 | * and from -(|y| - 1) to 0 if y < 0. (x % y) = x if y = 0. | |
427 | * | |
428 | * Examples: (q = a / b, r = a % b) | |
429 | * a b q r | |
430 | * === === === === | |
431 | * 4 3 1 1 | |
432 | * -4 3 -2 2 | |
433 | * 4 -3 -2 -2 | |
434 | * -4 -3 1 -1 | |
435 | */ | |
436 | void BigInteger::divideWithRemainder(const BigInteger &b, BigInteger &q) { | |
8c16728a MM |
437 | // Defend against aliased calls; |
438 | // same idea as in BigUnsigned::divideWithRemainder . | |
439 | if (this == &q) | |
440 | throw "BigInteger::divideWithRemainder: Cannot write quotient and remainder into the same variable"; | |
441 | if (this == &b || &q == &b) { | |
442 | BigInteger tmpB(b); | |
443 | divideWithRemainder(tmpB, q); | |
444 | return; | |
445 | } | |
5ff40cf5 | 446 | |
05780f4b MM |
447 | // Division by zero gives quotient 0 and remainder *this |
448 | if (b.sign == zero) { | |
449 | q.len = 0; | |
450 | q.sign = zero; | |
e67d6049 MM |
451 | return; |
452 | } | |
05780f4b MM |
453 | // 0 / b gives quotient 0 and remainder 0 |
454 | if (sign == zero) { | |
455 | q.len = 0; | |
456 | q.sign = zero; | |
e67d6049 MM |
457 | return; |
458 | } | |
5ff40cf5 | 459 | |
05780f4b | 460 | // Here *this != 0, b != 0. |
5ff40cf5 | 461 | |
05780f4b MM |
462 | // Do the operands have the same sign? |
463 | if (sign == b.sign) { | |
464 | // Yes: easy case. Quotient is zero or positive. | |
465 | q.sign = positive; | |
466 | } else { | |
467 | // No: harder case. Quotient is negative. | |
468 | q.sign = negative; | |
469 | // Decrease the magnitude of the dividend by one. | |
470 | BigUnsigned::operator --(); | |
471 | /* | |
472 | * We tinker with the dividend before and with the | |
473 | * quotient and remainder after so that the result | |
474 | * comes out right. To see why it works, consider the following | |
475 | * list of examples, where A is the magnitude-decreased | |
476 | * a, Q and R are the results of BigUnsigned division | |
477 | * with remainder on A and |b|, and q and r are the | |
478 | * final results we want: | |
479 | * | |
480 | * a A b Q R q r | |
481 | * -3 -2 3 0 2 -1 0 | |
482 | * -4 -3 3 1 0 -2 2 | |
483 | * -5 -4 3 1 1 -2 1 | |
484 | * -6 -5 3 1 2 -2 0 | |
485 | * | |
486 | * It appears that we need a total of 3 corrections: | |
487 | * Decrease the magnitude of a to get A. Increase the | |
488 | * magnitude of Q to get q (and make it negative). | |
489 | * Find r = (b - 1) - R and give it the desired sign. | |
490 | */ | |
491 | } | |
5ff40cf5 | 492 | |
05780f4b MM |
493 | // Divide the magnitudes. |
494 | BigUnsigned::divideWithRemainder(b, q); | |
5ff40cf5 | 495 | |
05780f4b MM |
496 | if (sign != b.sign) { |
497 | // More for the harder case (as described): | |
498 | // Increase the magnitude of the quotient by one. | |
499 | q.BigUnsigned::operator ++(); | |
500 | // Modify the remainder. | |
501 | BigUnsigned temp(*this); | |
502 | BigUnsigned::subtract(b, temp); | |
503 | BigUnsigned::operator --(); | |
504 | } | |
5ff40cf5 | 505 | |
05780f4b MM |
506 | // Sign of the remainder is always the sign of the divisor b. |
507 | sign = b.sign; | |
5ff40cf5 | 508 | |
05780f4b | 509 | // Set signs to zero as necessary. (Thanks David Allen!) |
e67d6049 MM |
510 | if (len == 0) |
511 | sign = zero; | |
05780f4b MM |
512 | if (q.len == 0) |
513 | q.sign = zero; | |
5ff40cf5 | 514 | |
05780f4b | 515 | // WHEW!!! |
e67d6049 MM |
516 | } |
517 | ||
518 | // Negation | |
519 | void BigInteger::negate(const BigInteger &a) { | |
8c16728a | 520 | DOTR_ALIASED(this == &a, negate(a)); |
e67d6049 MM |
521 | // Copy a's magnitude |
522 | BigUnsigned::operator =(a); | |
523 | // Copy the opposite of a.sign | |
524 | sign = Sign(-a.sign); | |
525 | } | |
526 | ||
527 | // INCREMENT/DECREMENT OPERATORS | |
528 | ||
529 | // Prefix increment | |
530 | void BigInteger::operator ++() { | |
531 | switch (sign) { | |
532 | case zero: | |
533 | allocate(1); | |
534 | sign = positive; | |
535 | len = 1; | |
2f145f11 | 536 | blk[0] = 1; |
e67d6049 MM |
537 | break; |
538 | case positive: | |
539 | BigUnsigned::operator ++(); | |
540 | break; | |
541 | case negative: | |
542 | BigUnsigned::operator --(); | |
543 | if (len == 0) | |
544 | sign = zero; | |
545 | break; | |
546 | } | |
547 | } | |
548 | ||
549 | // Postfix increment: same as prefix | |
550 | void BigInteger::operator ++(int) { | |
551 | operator ++(); | |
552 | } | |
553 | ||
554 | // Prefix decrement | |
555 | void BigInteger::operator --() { | |
556 | switch (sign) { | |
557 | case zero: | |
558 | allocate(1); | |
559 | sign = negative; | |
560 | len = 1; | |
2f145f11 | 561 | blk[0] = 1; |
e67d6049 MM |
562 | break; |
563 | case negative: | |
564 | BigUnsigned::operator ++(); | |
565 | break; | |
566 | case positive: | |
567 | BigUnsigned::operator --(); | |
568 | if (len == 0) | |
569 | sign = zero; | |
570 | break; | |
571 | } | |
572 | } | |
573 | ||
574 | // Postfix decrement: same as prefix | |
575 | void BigInteger::operator --(int) { | |
576 | operator --(); | |
577 | } | |
578 |