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