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
)
15 if ((err
= mp_copy(a
, c
)) != MP_OKAY
) {
19 /* 'a' should not be used after here - it might be the same as d */
21 /* get the remainder */
23 if ((err
= mp_mod_2d(a
, b
, d
)) != MP_OKAY
) {
28 /* shift by as many digits in the bit count */
29 if (b
>= MP_DIGIT_BIT
) {
30 mp_rshd(c
, b
/ MP_DIGIT_BIT
);
33 /* shift any bit count < MP_DIGIT_BIT */
37 mp_digit r
, mask
, shift
;
40 mask
= ((mp_digit
)1 << b
) - 1uL;
43 shift
= (mp_digit
)(MP_DIGIT_BIT
- b
);
47 for (x
= c
->used
; x
--> 0;) {
48 /* get the lower bits of this word in a temp */
49 mp_digit rr
= c
->dp
[x
] & mask
;
51 /* shift the current word and mix in the carry bits from the previous word */
52 c
->dp
[x
] = (c
->dp
[x
] >> b
) | (r
<< shift
);
54 /* set the carry to the carry bits of the current word found above */