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_div_2d.c Source File

fp_div_2d.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 / 2**b */
00013 void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d)
00014 {
00015   fp_digit D, r, rr;
00016   int      x;
00017   fp_int   t;
00018 
00019   /* if the shift count is <= 0 then we do no work */
00020   if (b <= 0) {
00021     fp_copy (a, c);
00022     if (d != NULL) {
00023       fp_zero (d);
00024     }
00025     return;
00026   }
00027 
00028   fp_init(&t);
00029 
00030   /* get the remainder */
00031   if (d != NULL) {
00032     fp_mod_2d (a, b, &t);
00033   }
00034 
00035   /* copy */
00036   fp_copy(a, c);
00037 
00038   /* shift by as many digits in the bit count */
00039   if (b >= (int)DIGIT_BIT) {
00040     fp_rshd (c, b / DIGIT_BIT);
00041   }
00042 
00043   /* shift any bit count < DIGIT_BIT */
00044   D = (fp_digit) (b % DIGIT_BIT);
00045   if (D != 0) {
00046     register fp_digit *tmpc, mask, shift;
00047 
00048     /* mask */
00049     mask = (((fp_digit)1) << D) - 1;
00050 
00051     /* shift for lsb */
00052     shift = DIGIT_BIT - D;
00053 
00054     /* alias */
00055     tmpc = c->dp + (c->used - 1);
00056 
00057     /* carry */
00058     r = 0;
00059     for (x = c->used - 1; x >= 0; x--) {
00060       /* get the lower  bits of this word in a temp */
00061       rr = *tmpc & mask;
00062 
00063       /* shift the current word and mix in the carry bits from the previous word */
00064       *tmpc = (*tmpc >> D) | (r << shift);
00065       --tmpc;
00066 
00067       /* set the carry to the carry bits of the current word found above */
00068       r = rr;
00069     }
00070   }
00071   fp_clamp (c);
00072   if (d != NULL) {
00073     fp_copy (&t, d);
00074   }
00075 }
00076 
00077 /* $Source: /cvs/libtom/tomsfastmath/src/bit/fp_div_2d.c,v $ */
00078 /* $Revision: 1.1 $ */
00079 /* $Date: 2006/12/31 21:25:53 $ */