• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <tommath.h>
2 #ifdef BN_MP_DIV_2_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4  *
5  * LibTomMath is a library that provides multiple-precision
6  * integer arithmetic as well as number theoretic functionality.
7  *
8  * The library was designed directly after the MPI library by
9  * Michael Fromberger but has been written from scratch with
10  * additional optimizations in place.
11  *
12  * The library is free for all purposes without any express
13  * guarantee it works.
14  *
15  * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
16  */
17 
18 /* b = a/2 */
mp_div_2(mp_int * a,mp_int * b)19 int mp_div_2(mp_int * a, mp_int * b)
20 {
21   int     x, res, oldused;
22 
23   /* copy */
24   if (b->alloc < a->used) {
25     if ((res = mp_grow (b, a->used)) != MP_OKAY) {
26       return res;
27     }
28   }
29 
30   oldused = b->used;
31   b->used = a->used;
32   {
33     register mp_digit r, rr, *tmpa, *tmpb;
34 
35     /* source alias */
36     tmpa = a->dp + b->used - 1;
37 
38     /* dest alias */
39     tmpb = b->dp + b->used - 1;
40 
41     /* carry */
42     r = 0;
43     for (x = b->used - 1; x >= 0; x--) {
44       /* get the carry for the next iteration */
45       rr = *tmpa & 1;
46 
47       /* shift the current digit, add in carry and store */
48       *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
49 
50       /* forward carry to next iteration */
51       r = rr;
52     }
53 
54     /* zero excess digits */
55     tmpb = b->dp + b->used;
56     for (x = b->used; x < oldused; x++) {
57       *tmpb++ = 0;
58     }
59   }
60   b->sign = a->sign;
61   mp_clamp (b);
62   return MP_OKAY;
63 }
64 #endif
65 
66 /* $Source: /cvs/libtom/libtommath/bn_mp_div_2.c,v $ */
67 /* $Revision: 1.3 $ */
68 /* $Date: 2006/03/31 14:18:44 $ */
69