Mercurial > dropbear
comparison tomsfastmath/src/addsub/fp_sub.c @ 643:a362b62d38b2 dropbear-tfm
Add tomsfastmath from git rev bfa4582842bc3bab42e4be4aed5703437049502a
with Makefile.in renamed
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 23 Nov 2011 18:10:20 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
642:33fd2f3499d2 | 643:a362b62d38b2 |
---|---|
1 /* TomsFastMath, a fast ISO C bignum library. | |
2 * | |
3 * This project is meant to fill in where LibTomMath | |
4 * falls short. That is speed ;-) | |
5 * | |
6 * This project is public domain and free for all purposes. | |
7 * | |
8 * Tom St Denis, [email protected] | |
9 */ | |
10 #include <tfm.h> | |
11 | |
12 /* c = a - b */ | |
13 void fp_sub(fp_int *a, fp_int *b, fp_int *c) | |
14 { | |
15 int sa, sb; | |
16 | |
17 sa = a->sign; | |
18 sb = b->sign; | |
19 | |
20 if (sa != sb) { | |
21 /* subtract a negative from a positive, OR */ | |
22 /* subtract a positive from a negative. */ | |
23 /* In either case, ADD their magnitudes, */ | |
24 /* and use the sign of the first number. */ | |
25 c->sign = sa; | |
26 s_fp_add (a, b, c); | |
27 } else { | |
28 /* subtract a positive from a positive, OR */ | |
29 /* subtract a negative from a negative. */ | |
30 /* First, take the difference between their */ | |
31 /* magnitudes, then... */ | |
32 if (fp_cmp_mag (a, b) != FP_LT) { | |
33 /* Copy the sign from the first */ | |
34 c->sign = sa; | |
35 /* The first has a larger or equal magnitude */ | |
36 s_fp_sub (a, b, c); | |
37 } else { | |
38 /* The result has the *opposite* sign from */ | |
39 /* the first number. */ | |
40 c->sign = (sa == FP_ZPOS) ? FP_NEG : FP_ZPOS; | |
41 /* The second has a larger magnitude */ | |
42 s_fp_sub (b, a, c); | |
43 } | |
44 } | |
45 } | |
46 | |
47 | |
48 /* $Source$ */ | |
49 /* $Revision$ */ | |
50 /* $Date$ */ |