Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / dropbear / libtommath / bn_mp_div_2.c
blob0035e56589577710fdd059144546eec296e1c4fa
1 #include <tommath.h>
2 #ifdef BN_MP_DIV_2_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
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.
12 * The library is free for all purposes without any express
13 * guarantee it works.
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* b = a/2 */
19 int mp_div_2(mp_int * a, mp_int * b)
21 int x, res, oldused;
23 /* copy */
24 if (b->alloc < a->used) {
25 if ((res = mp_grow (b, a->used)) != MP_OKAY) {
26 return res;
30 oldused = b->used;
31 b->used = a->used;
33 register mp_digit r, rr, *tmpa, *tmpb;
35 /* source alias */
36 tmpa = a->dp + b->used - 1;
38 /* dest alias */
39 tmpb = b->dp + b->used - 1;
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;
47 /* shift the current digit, add in carry and store */
48 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
50 /* forward carry to next iteration */
51 r = rr;
54 /* zero excess digits */
55 tmpb = b->dp + b->used;
56 for (x = b->used; x < oldused; x++) {
57 *tmpb++ = 0;
60 b->sign = a->sign;
61 mp_clamp (b);
62 return MP_OKAY;
64 #endif
66 /* $Source: /cvs/libtom/libtommath/bn_mp_div_2.c,v $ */
67 /* $Revision: 1.3 $ */
68 /* $Date: 2006/03/31 14:18:44 $ */