diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /libmp/port/mpadd.c | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'libmp/port/mpadd.c')
| -rw-r--r-- | libmp/port/mpadd.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/libmp/port/mpadd.c b/libmp/port/mpadd.c new file mode 100644 index 00000000..6022a64e --- /dev/null +++ b/libmp/port/mpadd.c @@ -0,0 +1,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; + } +} |
