|
|
1.1 root 1: #include "mprec.h"
2: #include <assert.h>
3:
4:
5: /*
6: * Madd adds the mints pointed to by "a" and "b" and puts the result
7: * into the mint pointed to by "c". Note that it assumes that the
8: * contents of the mint pointed to by "c" are valid. Also note that
9: * there is no assumption made on the distinctness of "a", "b" and "c".
10: */
11: void
12: madd(a, b, c)
13: mint *a, *b, *c;
14: {
15: register char *rp, *ap, *bp;
16: char *limit;
17: char asign, bsign;
18: mint *temp;
19: mint res;
20:
21: /* make a the bigger of the two */
22: if (a->len < b->len) {
23: temp = a;
24: a = b;
25: b = temp;
26: }
27:
28: /* allocate new value */
29: rp = (char *)mpalc(a->len + 1);
30: res.val = rp;
31: res.len = a->len + 1;
32:
33: ap = a->val;
34: bp = b->val;
35:
36: /* add a and b over range of both */
37: limit = bp + b->len;
38: while (bp < limit)
39: *rp++ = *ap++ + *bp++;
40:
41: /* add sign extended b to rest of a */
42: bsign = (ispos(b) ? 00 : NEFL);
43: limit = a->val + a->len;
44: while (ap < limit)
45: *rp++ = *ap++ + bsign;
46:
47: /* add sign of a and sign of b and put sum in lead digit of res */
48: asign = (ispos(a) ? 00 : NEFL);
49: *rp = asign + bsign;
50: assert(rp == res.val + res.len - 1);
51:
52: norm(&res);
53: mpfree(c->val);
54: *c = res;
55: }
56:
57:
58: /*
59: * Msub subtracts the mint pointed to by "b" from that pointed at
60: * by "a" and stores the result in that pointed to by "c". Note
61: * that there is no assumption made on the distinctness of "a", "b"
62: * and "c".
63: */
64:
65: void
66: msub(a, b, c)
67: mint *a, *b, *c;
68: {
69: mint temp;
70:
71: minit(&temp);
72: mneg(b, &temp);
73: madd(a, &temp, c);
74: mpfree(temp.val);
75: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.