Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / dropbear / libtommath / bn_mp_div_d.c
blob771aa6aac1f062a1d76b08e2426a82d56d5f78a0
1 #include <tommath.h>
2 #ifdef BN_MP_DIV_D_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 static int s_is_power_of_two(mp_digit b, int *p)
20 int x;
22 for (x = 1; x < DIGIT_BIT; x++) {
23 if (b == (((mp_digit)1)<<x)) {
24 *p = x;
25 return 1;
28 return 0;
31 /* single digit division (based on routine from MPI) */
32 int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
34 mp_int q;
35 mp_word w;
36 mp_digit t;
37 int res, ix;
39 /* cannot divide by zero */
40 if (b == 0) {
41 return MP_VAL;
44 /* quick outs */
45 if (b == 1 || mp_iszero(a) == 1) {
46 if (d != NULL) {
47 *d = 0;
49 if (c != NULL) {
50 return mp_copy(a, c);
52 return MP_OKAY;
55 /* power of two ? */
56 if (s_is_power_of_two(b, &ix) == 1) {
57 if (d != NULL) {
58 *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
60 if (c != NULL) {
61 return mp_div_2d(a, ix, c, NULL);
63 return MP_OKAY;
66 #ifdef BN_MP_DIV_3_C
67 /* three? */
68 if (b == 3) {
69 return mp_div_3(a, c, d);
71 #endif
73 /* no easy answer [c'est la vie]. Just division */
74 if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
75 return res;
78 q.used = a->used;
79 q.sign = a->sign;
80 w = 0;
81 for (ix = a->used - 1; ix >= 0; ix--) {
82 w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
84 if (w >= b) {
85 t = (mp_digit)(w / b);
86 w -= ((mp_word)t) * ((mp_word)b);
87 } else {
88 t = 0;
90 q.dp[ix] = (mp_digit)t;
93 if (d != NULL) {
94 *d = (mp_digit)w;
97 if (c != NULL) {
98 mp_clamp(&q);
99 mp_exch(&q, c);
101 mp_clear(&q);
103 return res;
106 #endif
108 /* $Source: /cvs/libtom/libtommath/bn_mp_div_d.c,v $ */
109 /* $Revision: 1.3 $ */
110 /* $Date: 2006/03/31 14:18:44 $ */