| 1 | /**************************************************************** |
| 2 | |
| 3 | The author of this software is David M. Gay. |
| 4 | |
| 5 | Copyright (C) 1998 by Lucent Technologies |
| 6 | All Rights Reserved |
| 7 | |
| 8 | Permission to use, copy, modify, and distribute this software and |
| 9 | its documentation for any purpose and without fee is hereby |
| 10 | granted, provided that the above copyright notice appear in all |
| 11 | copies and that both that the copyright notice and this |
| 12 | permission notice and warranty disclaimer appear in supporting |
| 13 | documentation, and that the name of Lucent or any of its entities |
| 14 | not be used in advertising or publicity pertaining to |
| 15 | distribution of the software without specific, written prior |
| 16 | permission. |
| 17 | |
| 18 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 19 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. |
| 20 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY |
| 21 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 22 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER |
| 23 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 24 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF |
| 25 | THIS SOFTWARE. |
| 26 | |
| 27 | ****************************************************************/ |
| 28 | |
| 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, |
| 30 | * with " at " changed at "@" and " dot " changed to ".").	*/ |
| 31 | |
| 32 | #include "gdtoaimp.h" |
| 33 | |
| 34 | Bigint *sum (Bigint *a, Bigint *b) |
| 35 | { |
| 36 | 	Bigint *c; |
| 37 | 	ULong carry, *xc, *xa, *xb, *xe, y; |
| 38 | #ifdef Pack_32 |
| 39 | 	ULong z; |
| 40 | #endif |
| 41 | |
| 42 | 	if (a->wds < b->wds) { |
| 43 | 		c = b; b = a; a = c; |
| 44 | 	} |
| 45 | 	c = Balloc(a->k); |
| 46 | 	c->wds = a->wds; |
| 47 | 	carry = 0; |
| 48 | 	xa = a->x; |
| 49 | 	xb = b->x; |
| 50 | 	xc = c->x; |
| 51 | 	xe = xc + b->wds; |
| 52 | #ifdef Pack_32 |
| 53 | 	do { |
| 54 | 		y = (*xa & 0xffff) + (*xb & 0xffff) + carry; |
| 55 | 		carry = (y & 0x10000) >> 16; |
| 56 | 		z = (*xa++ >> 16) + (*xb++ >> 16) + carry; |
| 57 | 		carry = (z & 0x10000) >> 16; |
| 58 | 		Storeinc(xc, z, y); |
| 59 | 	} while(xc < xe); |
| 60 | 	xe += a->wds - b->wds; |
| 61 | 	while(xc < xe) { |
| 62 | 		y = (*xa & 0xffff) + carry; |
| 63 | 		carry = (y & 0x10000) >> 16; |
| 64 | 		z = (*xa++ >> 16) + carry; |
| 65 | 		carry = (z & 0x10000) >> 16; |
| 66 | 		Storeinc(xc, z, y); |
| 67 | 	} |
| 68 | #else |
| 69 | 	do { |
| 70 | 		y = *xa++ + *xb++ + carry; |
| 71 | 		carry = (y & 0x10000) >> 16; |
| 72 | 		*xc++ = y & 0xffff; |
| 73 | 	} while(xc < xe); |
| 74 | 	xe += a->wds - b->wds; |
| 75 | 	while(xc < xe) { |
| 76 | 		y = *xa++ + carry; |
| 77 | 		carry = (y & 0x10000) >> 16; |
| 78 | 		*xc++ = y & 0xffff; |
| 79 | 	} |
| 80 | #endif |
| 81 | 	if (carry) { |
| 82 | 		if (c->wds == c->maxwds) { |
| 83 | 			b = Balloc(c->k + 1); |
| 84 | 			Bcopy(b, c); |
| 85 | 			Bfree(c); |
| 86 | 			c = b; |
| 87 | 		} |
| 88 | 		c->x[c->wds++] = 1; |
| 89 | 	} |
| 90 | 	return c; |
| 91 | } |