]> pd.if.org Git - zpackage/blob - tomsfastmath/src/bit/fp_div_2.c
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / bit / fp_div_2.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 /* b = a/2 */
13 void fp_div_2(fp_int * a, fp_int * b)
14 {
15   int     x, oldused;
16
17   oldused = b->used;
18   b->used = a->used;
19   {
20     register fp_digit r, rr, *tmpa, *tmpb;
21
22     /* source alias */
23     tmpa = a->dp + b->used - 1;
24
25     /* dest alias */
26     tmpb = b->dp + b->used - 1;
27
28     /* carry */
29     r = 0;
30     for (x = b->used - 1; x >= 0; x--) {
31       /* get the carry for the next iteration */
32       rr = *tmpa & 1;
33
34       /* shift the current digit, add in carry and store */
35       *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
36
37       /* forward carry to next iteration */
38       r = rr;
39     }
40
41     /* zero excess digits */
42     tmpb = b->dp + b->used;
43     for (x = b->used; x < oldused; x++) {
44       *tmpb++ = 0;
45     }
46   }
47   b->sign = a->sign;
48   fp_clamp (b);
49 }
50
51 /* $Source$ */
52 /* $Revision$ */
53 /* $Date$ */