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@iahu.ca, http://math.libtomcrypt.org
18 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
19 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
26 /* if the shift count is <= 0 then we do no work */
35 if ((res = mp_init (&t)) != MP_OKAY) {
39 /* get the remainder */
41 if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) {
48 if ((res = mp_copy (a, c)) != MP_OKAY) {
53 /* shift by as many digits in the bit count */
54 if (b >= (int)DIGIT_BIT) {
55 mp_rshd (c, b / DIGIT_BIT);
58 /* shift any bit count < DIGIT_BIT */
59 D = (mp_digit) (b % DIGIT_BIT);
61 register mp_digit *tmpc, mask, shift;
64 mask = (((mp_digit)1) << D) - 1;
67 shift = DIGIT_BIT - D;
70 tmpc = c->dp + (c->used - 1);
74 for (x = c->used - 1; x >= 0; x--) {
75 /* get the lower bits of this word in a temp */
78 /* shift the current word and mix in the carry bits from the previous word */
79 *tmpc = (*tmpc >> D) | (r << shift);
82 /* set the carry to the carry bits of the current word found above */