1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
7 mp_err mp_div_2(const mp_int *a, mp_int *b)
10 mp_digit r, rr, *tmpa, *tmpb;
14 if (b->alloc < a->used) {
15 if ((err = mp_grow(b, a->used)) != MP_OKAY) {
24 tmpa = a->dp + b->used - 1;
27 tmpb = b->dp + b->used - 1;
31 for (x = b->used - 1; x >= 0; x--) {
32 /* get the carry for the next iteration */
35 /* shift the current digit, add in carry and store */
36 *tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
38 /* forward carry to next iteration */
42 /* zero excess digits */
43 MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);