Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
sum.c File Reference
#include "gdtoaimp.h"
Include dependency graph for sum.c:

Go to the source code of this file.

Functions

Bigintsum (Bigint *a, Bigint *b)
 

Function Documentation

◆ sum()

Bigint* sum ( Bigint a,
Bigint b 
)

Definition at line 39 of file sum.c.

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 * Balloc(int k)
Definition: misc.c:47
int k
Definition: gdtoaimp.h:487

References Balloc(), Bcopy, Bfree(), Bigint::k, Bigint::maxwds, Storeinc, Bigint::wds, and Bigint::x.

Referenced by g_ddfmt(), and strtodg().