1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include "os.h"
#include <mp.h>
#include "dat.h"
/* sum = abs(b1) + abs(b2), i.e., add the magnitudes */
void
mpmagadd(mpint *b1, mpint *b2, mpint *sum)
{
int m, n;
mpint *t;
/* get the sizes right */
if(b2->top > b1->top){
t = b1;
b1 = b2;
b2 = t;
}
n = b1->top;
m = b2->top;
if(n == 0){
mpassign(mpzero, sum);
return;
}
if(m == 0){
mpassign(b1, sum);
return;
}
mpbits(sum, (n+1)*Dbits);
sum->top = n+1;
mpvecadd(b1->p, n, b2->p, m, sum->p);
sum->sign = 1;
mpnorm(sum);
}
/* sum = b1 + b2 */
void
mpadd(mpint *b1, mpint *b2, mpint *sum)
{
int sign;
if(b1->sign != b2->sign){
if(b1->sign < 0)
mpmagsub(b2, b1, sum);
else
mpmagsub(b1, b2, sum);
} else {
sign = b1->sign;
mpmagadd(b1, b2, sum);
if(sum->top != 0)
sum->sign = sign;
}
}
|