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