Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
sum.c
Go to the documentation of this file.
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 David M. Gay (dmg at acm dot org,
30  * with " at " changed at "@" and " dot " changed to "."). */
31 
32 #include "gdtoaimp.h"
33 
34 Bigint*
35 #ifdef KR_headers
36  sum(a, b) Bigint* a;
37 Bigint* b;
38 #else
39  sum(Bigint* a, Bigint* b)
40 #endif
41 {
42  Bigint* c;
43  ULong carry, *xc, *xa, *xb, *xe, y;
44 #ifdef Pack_32
45  ULong z;
46 #endif
47 
48  if(a->wds < b->wds)
49  {
50  c = b;
51  b = a;
52  a = c;
53  }
54  c = Balloc(a->k);
55  c->wds = a->wds;
56  carry = 0;
57  xa = a->x;
58  xb = b->x;
59  xc = c->x;
60  xe = xc + b->wds;
61 #ifdef Pack_32
62  do
63  {
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  } while(xc < xe);
70  xe += a->wds - b->wds;
71  while(xc < xe)
72  {
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  {
82  y = *xa++ + *xb++ + carry;
83  carry = (y & 0x10000) >> 16;
84  *xc++ = y & 0xffff;
85  } while(xc < xe);
86  xe += a->wds - b->wds;
87  while(xc < xe)
88  {
89  y = *xa++ + carry;
90  carry = (y & 0x10000) >> 16;
91  *xc++ = y & 0xffff;
92  }
93 #endif
94  if(carry)
95  {
96  if(c->wds == c->maxwds)
97  {
98  b = Balloc(c->k + 1);
99  Bcopy(b, c);
100  Bfree(c);
101  c = b;
102  }
103  c->x[c->wds++] = 1;
104  }
105  return c;
106 }
ULong x[1]
Definition: gdtoaimp.h:488
int wds
Definition: gdtoaimp.h:487
#define Bcopy(x, y)
Definition: gdtoaimp.h:501
int maxwds
Definition: gdtoaimp.h:487
#define Storeinc(a, b, c)
Definition: gdtoaimp.h:318
unsigned Long ULong
Definition: gdtoa.h:41
void Bfree(Bigint *v)
Definition: misc.c:92
Bigint * sum(Bigint *a, Bigint *b)
Definition: sum.c:39
Bigint * Balloc(int k)
Definition: misc.c:47
int k
Definition: gdtoaimp.h:487