xref: /freebsd/contrib/gdtoa/sum.c (revision 8ab2f5ecc596131f6ca790d6ae35540c06ed7985)
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
30 	David M. Gay
31 	Bell Laboratories, Room 2C-463
32 	600 Mountain Avenue
33 	Murray Hill, NJ 07974-0636
34 	U.S.A.
35 	dmg@bell-labs.com
36  */
37 
38 #include "gdtoaimp.h"
39 
40  Bigint *
41 #ifdef KR_headers
42 sum(a, b) Bigint *a; Bigint *b;
43 #else
44 sum(Bigint *a, Bigint *b)
45 #endif
46 {
47 	Bigint *c;
48 	ULong carry, *xc, *xa, *xb, *xe, y;
49 #ifdef Pack_32
50 	ULong z;
51 #endif
52 
53 	if (a->wds < b->wds) {
54 		c = b; b = a; a = c;
55 		}
56 	c = Balloc(a->k);
57 	c->wds = a->wds;
58 	carry = 0;
59 	xa = a->x;
60 	xb = b->x;
61 	xc = c->x;
62 	xe = xc + b->wds;
63 #ifdef Pack_32
64 	do {
65 		y = (*xa & 0xffff) + (*xb & 0xffff) + carry;
66 		carry = (y & 0x10000) >> 16;
67 		z = (*xa++ >> 16) + (*xb++ >> 16) + carry;
68 		carry = (z & 0x10000) >> 16;
69 		Storeinc(xc, z, y);
70 		}
71 		while(xc < xe);
72 	xe += a->wds - b->wds;
73 	while(xc < xe) {
74 		y = (*xa & 0xffff) + carry;
75 		carry = (y & 0x10000) >> 16;
76 		z = (*xa++ >> 16) + carry;
77 		carry = (z & 0x10000) >> 16;
78 		Storeinc(xc, z, y);
79 		}
80 #else
81 	do {
82 		y = *xa++ + *xb++ + carry;
83 		carry = (y & 0x10000) >> 16;
84 		*xc++ = y & 0xffff;
85 		}
86 		while(xc < xe);
87 	xe += a->wds - b->wds;
88 	while(xc < xe) {
89 		y = *xa++ + carry;
90 		carry = (y & 0x10000) >> 16;
91 		*xc++ = y & 0xffff;
92 		}
93 #endif
94 	if (carry) {
95 		if (c->wds == c->maxwds) {
96 			b = Balloc(c->k + 1);
97 			Bcopy(b, c);
98 			Bfree(c);
99 			c = b;
100 			}
101 		c->x[c->wds++] = 1;
102 		}
103 	return c;
104 	}
105