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