1112158Sdas/****************************************************************
2112158Sdas
3112158SdasThe author of this software is David M. Gay.
4112158Sdas
5112158SdasCopyright (C) 1998 by Lucent Technologies
6112158SdasAll Rights Reserved
7112158Sdas
8112158SdasPermission to use, copy, modify, and distribute this software and
9112158Sdasits documentation for any purpose and without fee is hereby
10112158Sdasgranted, provided that the above copyright notice appear in all
11112158Sdascopies and that both that the copyright notice and this
12112158Sdaspermission notice and warranty disclaimer appear in supporting
13112158Sdasdocumentation, and that the name of Lucent or any of its entities
14112158Sdasnot be used in advertising or publicity pertaining to
15112158Sdasdistribution of the software without specific, written prior
16112158Sdaspermission.
17112158Sdas
18112158SdasLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19112158SdasINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20112158SdasIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21112158SdasSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22112158SdasWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23112158SdasIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24112158SdasARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25112158SdasTHIS SOFTWARE.
26112158Sdas
27112158Sdas****************************************************************/
28112158Sdas
29165743Sdas/* Please send bug reports to David M. Gay (dmg at acm dot org,
30165743Sdas * with " at " changed at "@" and " dot " changed to ".").	*/
31112158Sdas
32112158Sdas#include "gdtoaimp.h"
33112158Sdas
34112158Sdas Bigint *
35112158Sdas#ifdef KR_headers
36112158Sdassum(a, b) Bigint *a; Bigint *b;
37112158Sdas#else
38112158Sdassum(Bigint *a, Bigint *b)
39112158Sdas#endif
40112158Sdas{
41112158Sdas	Bigint *c;
42112158Sdas	ULong carry, *xc, *xa, *xb, *xe, y;
43112158Sdas#ifdef Pack_32
44112158Sdas	ULong z;
45112158Sdas#endif
46112158Sdas
47112158Sdas	if (a->wds < b->wds) {
48112158Sdas		c = b; b = a; a = c;
49112158Sdas		}
50112158Sdas	c = Balloc(a->k);
51112158Sdas	c->wds = a->wds;
52112158Sdas	carry = 0;
53112158Sdas	xa = a->x;
54112158Sdas	xb = b->x;
55112158Sdas	xc = c->x;
56112158Sdas	xe = xc + b->wds;
57112158Sdas#ifdef Pack_32
58112158Sdas	do {
59112158Sdas		y = (*xa & 0xffff) + (*xb & 0xffff) + carry;
60112158Sdas		carry = (y & 0x10000) >> 16;
61112158Sdas		z = (*xa++ >> 16) + (*xb++ >> 16) + carry;
62112158Sdas		carry = (z & 0x10000) >> 16;
63112158Sdas		Storeinc(xc, z, y);
64112158Sdas		}
65112158Sdas		while(xc < xe);
66112158Sdas	xe += a->wds - b->wds;
67112158Sdas	while(xc < xe) {
68112158Sdas		y = (*xa & 0xffff) + carry;
69112158Sdas		carry = (y & 0x10000) >> 16;
70112158Sdas		z = (*xa++ >> 16) + carry;
71112158Sdas		carry = (z & 0x10000) >> 16;
72112158Sdas		Storeinc(xc, z, y);
73112158Sdas		}
74112158Sdas#else
75112158Sdas	do {
76112158Sdas		y = *xa++ + *xb++ + carry;
77112158Sdas		carry = (y & 0x10000) >> 16;
78112158Sdas		*xc++ = y & 0xffff;
79112158Sdas		}
80112158Sdas		while(xc < xe);
81112158Sdas	xe += a->wds - b->wds;
82112158Sdas	while(xc < xe) {
83112158Sdas		y = *xa++ + carry;
84112158Sdas		carry = (y & 0x10000) >> 16;
85112158Sdas		*xc++ = y & 0xffff;
86112158Sdas		}
87112158Sdas#endif
88112158Sdas	if (carry) {
89112158Sdas		if (c->wds == c->maxwds) {
90112158Sdas			b = Balloc(c->k + 1);
91112158Sdas			Bcopy(b, c);
92112158Sdas			Bfree(c);
93112158Sdas			c = b;
94112158Sdas			}
95112158Sdas		c->x[c->wds++] = 1;
96112158Sdas		}
97112158Sdas	return c;
98112158Sdas	}
99