1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
7 mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
13 /* if the shift count is <= 0 then we do no work */
23 if ((err = mp_copy(a, c)) != MP_OKAY) {
26 /* 'a' should not be used after here - it might be the same as d */
28 /* get the remainder */
30 if ((err = mp_mod_2d(a, b, d)) != MP_OKAY) {
35 /* shift by as many digits in the bit count */
36 if (b >= MP_DIGIT_BIT) {
37 mp_rshd(c, b / MP_DIGIT_BIT);
40 /* shift any bit count < MP_DIGIT_BIT */
41 D = (mp_digit)(b % MP_DIGIT_BIT);
43 mp_digit *tmpc, mask, shift;
46 mask = ((mp_digit)1 << D) - 1uL;
49 shift = (mp_digit)MP_DIGIT_BIT - D;
52 tmpc = c->dp + (c->used - 1);
56 for (x = c->used - 1; x >= 0; x--) {
57 /* get the lower bits of this word in a temp */
60 /* shift the current word and mix in the carry bits from the previous word */
61 *tmpc = (*tmpc >> D) | (r << shift);
64 /* set the carry to the carry bits of the current word found above */