ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fp_sub.c Source File

fp_sub.c

00001 /* TomsFastMath, a fast ISO C bignum library.
00002  * 
00003  * This project is meant to fill in where LibTomMath
00004  * falls short.  That is speed ;-)
00005  *
00006  * This project is public domain and free for all purposes.
00007  * 
00008  * Tom St Denis, tomstdenis@gmail.com
00009  */
00010 #include <tfm.h>
00011 
00012 /* c = a - b */
00013 void fp_sub(fp_int *a, fp_int *b, fp_int *c)
00014 {
00015   int     sa, sb;
00016 
00017   sa = a->sign;
00018   sb = b->sign;
00019 
00020   if (sa != sb) {
00021     /* subtract a negative from a positive, OR */
00022     /* subtract a positive from a negative. */
00023     /* In either case, ADD their magnitudes, */
00024     /* and use the sign of the first number. */
00025     c->sign = sa;
00026     s_fp_add (a, b, c);
00027   } else {
00028     /* subtract a positive from a positive, OR */
00029     /* subtract a negative from a negative. */
00030     /* First, take the difference between their */
00031     /* magnitudes, then... */
00032     if (fp_cmp_mag (a, b) != FP_LT) {
00033       /* Copy the sign from the first */
00034       c->sign = sa;
00035       /* The first has a larger or equal magnitude */
00036       s_fp_sub (a, b, c);
00037     } else {
00038       /* The result has the *opposite* sign from */
00039       /* the first number. */
00040       c->sign = (sa == FP_ZPOS) ? FP_NEG : FP_ZPOS;
00041       /* The second has a larger magnitude */
00042       s_fp_sub (b, a, c);
00043     }
00044   }
00045 }
00046 
00047 
00048 /* $Source: /cvs/libtom/tomsfastmath/src/addsub/fp_sub.c,v $ */
00049 /* $Revision: 1.1 $ */
00050 /* $Date: 2006/12/31 21:25:53 $ */