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
15
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18
static int s_is_power_of_two(mp_digit b, int *p)
22
/* fast return if no power of two */
23
if ((b==0) || (b & (b-1))) {
27
for (x = 0; x < DIGIT_BIT; x++) {
28
if (b == (((mp_digit)1)<<x)) {
36
/* single digit division (based on routine from MPI) */
37
int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
44
/* cannot divide by zero */
50
if (b == 1 || mp_iszero(a) == 1) {
61
if (s_is_power_of_two(b, &ix) == 1) {
63
*d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
66
return mp_div_2d(a, ix, c, NULL);
74
return mp_div_3(a, c, d);
78
/* no easy answer [c'est la vie]. Just division */
79
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
86
for (ix = a->used - 1; ix >= 0; ix--) {
87
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
90
t = (mp_digit)(w / b);
91
w -= ((mp_word)t) * ((mp_word)b);
95
q.dp[ix] = (mp_digit)t;
113
/* $Source: /cvs/libtom/libtommath/bn_mp_div_d.c,v $ */
114
/* $Revision: 1.5 $ */
115
/* $Date: 2007/01/09 04:44:32 $ */