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