libtommath/mp_div.c

43 lines
984 B
C
Raw Normal View History

#include "tommath_private.h"
#ifdef MP_DIV_C
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2004-10-29 22:07:18 +00:00
mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
2004-10-29 22:07:18 +00:00
{
2019-05-19 15:16:13 +00:00
mp_err err;
2004-10-29 22:07:18 +00:00
2017-08-30 18:23:46 +00:00
/* is divisor zero ? */
2019-10-24 15:43:31 +00:00
if (mp_iszero(b)) {
2017-08-30 18:23:46 +00:00
return MP_VAL;
}
/* if a < b then q = 0, r = a */
2017-08-30 18:23:46 +00:00
if (mp_cmp_mag(a, b) == MP_LT) {
if (d != NULL) {
2019-10-29 17:41:25 +00:00
if ((err = mp_copy(a, d)) != MP_OKAY) {
return err;
}
2017-08-30 18:23:46 +00:00
}
if (c != NULL) {
mp_zero(c);
}
2019-10-29 17:41:25 +00:00
return MP_OKAY;
2017-08-30 18:23:46 +00:00
}
if (MP_HAS(S_MP_DIV_RECURSIVE)
&& (b->used > (2 * MP_MUL_KARATSUBA_CUTOFF))
2019-10-24 16:26:25 +00:00
&& (b->used <= ((a->used/3)*2))) {
err = s_mp_div_recursive(a, b, c, d);
} else if (MP_HAS(S_MP_DIV_SCHOOL)) {
err = s_mp_div_school(a, b, c, d);
2019-10-29 17:41:25 +00:00
} else if (MP_HAS(S_MP_DIV_SMALL)) {
err = s_mp_div_small(a, b, c, d);
2019-10-29 17:41:25 +00:00
} else {
err = MP_VAL;
2017-08-30 18:23:46 +00:00
}
2003-05-17 12:33:54 +00:00
2019-05-19 15:16:13 +00:00
return err;
2003-02-28 16:08:34 +00:00
}
2004-10-29 22:07:18 +00:00
#endif