]> pd.if.org Git - zpackage/blob - tomsfastmath/src/addsub/fp_sub.c
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / addsub / fp_sub.c
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, tomstdenis@gmail.com
9  */
10 #include <tfm_private.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$ */