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