32 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ 34 #define DIG_SIZE (MPZ_DIG_SIZE) 35 #define DIG_MASK ((MPZ_LONG_1 << DIG_SIZE) - 1) 36 #define DIG_MSB (MPZ_LONG_1 << (DIG_SIZE - 1)) 37 #define DIG_BASE (MPZ_LONG_1 << DIG_SIZE) 53 for (--idig; idig >= oidig && *idig == 0; --idig) {
55 return idig + 1 - oidig;
63 if (ilen < jlen) {
return -1; }
64 if (ilen > jlen) {
return 1; }
66 for (idig += ilen, jdig += ilen; ilen > 0; --ilen) {
68 if (cmp < 0) {
return -1; }
69 if (cmp > 0) {
return 1; }
88 idig += jlen + n_whole - 1;
93 for (
size_t i = jlen; i > 0; i--, idig--, jdig--) {
106 while (jlen != 0 && idig[jlen - 1] == 0) {
123 if (n_whole >= jlen) {
130 for (
size_t i = jlen; i > 0; i--, idig++, jdig++) {
157 for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
163 for (; jlen > 0; --jlen, ++idig, ++jdig) {
187 for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
193 for (; jlen > 0; --jlen, ++idig, ++jdig) {
202 #if MICROPY_OPT_MPZ_BITWISE 212 for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
213 *idig = *jdig & *kdig;
237 for (; jlen > 0; ++idig, ++jdig) {
238 carryj += *jdig ^ jmask;
239 carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
240 carryi += ((carryj & carryk) ^ imask) &
DIG_MASK;
254 #if MICROPY_OPT_MPZ_BITWISE 266 for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
267 *idig = *jdig | *kdig;
270 for (; jlen > 0; --jlen, ++idig, ++jdig) {
289 #if MICROPY_OPT_MPZ_BITWISE 298 for (; jlen > 0; ++idig, ++jdig) {
299 carryj += *jdig ^ jmask;
300 carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
328 for (; jlen > 0; ++idig, ++jdig) {
329 carryj += *jdig ^ jmask;
330 carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
331 carryi += ((carryj | carryk) ^ imask) &
DIG_MASK;
346 #if MICROPY_OPT_MPZ_BITWISE 358 for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
359 *idig = *jdig ^ *kdig;
362 for (; jlen > 0; --jlen, ++idig, ++jdig) {
384 for (; jlen > 0; ++idig, ++jdig) {
387 carryi += (carryj ^ carryk) &
DIG_MASK;
409 for (; ilen > 0; --ilen, ++idig) {
431 for (; klen > 0; --klen, ++idig, ++kdig) {
436 for (
mpz_dig_t *jd = jdig; jl > 0; --jl, ++jd, ++id) {
466 int cmp =
mpn_cmp(num_dig, *num_len, den_dig, den_len);
472 }
else if (cmp < 0) {
495 num_dig[*num_len] = 0;
497 for (
mpz_dig_t *num = num_dig, carry = 0; num < num_dig + *num_len; ++num) {
499 *num = ((n << norm_shift) | carry) &
DIG_MASK;
504 lead_den_digit = (
mpz_dbl_dig_t)den_dig[den_len - 1] << norm_shift;
510 num_dig += *num_len - 1;
513 *quo_len = *num_len - den_len;
516 quo_dig += *quo_len - 1;
519 while (*num_len > den_len) {
523 quo /= lead_den_digit;
546 for (
mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
557 for (; borrow != 0; --quo) {
561 for (
mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
578 for (
mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
581 if (x >= *n || *n - x <= borrow) {
590 if (borrow >= *num_dig) {
595 *num_dig = (*num_dig - borrow) &
DIG_MASK;
600 for (; borrow != 0; --quo) {
604 for (
mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
629 for (
mpz_dig_t *num = orig_num_dig + *num_len - 1, carry = 0; num >= orig_num_dig; --num) {
631 *num = ((n >> norm_shift) | carry) &
DIG_MASK;
637 while (*quo_len > 0 && orig_quo_dig[*quo_len - 1] == 0) {
641 while (*num_len > 0 && orig_num_dig[*num_len - 1] == 0) {
646 #define MIN_ALLOC (2) 677 these functions are unused
679 mpz_t *mpz_zero(
void) {
686 mpz_t *z = mpz_zero();
692 mpz_t *z = mpz_zero();
697 #if MICROPY_PY_BUILTINS_FLOAT 698 mpz_t *mpz_from_float(mp_float_t val) {
699 mpz_t *z = mpz_zero();
700 mpz_set_from_float(z, val);
705 mpz_t *mpz_from_str(
const char *str,
size_t len,
bool neg,
unsigned int base) {
706 mpz_t *z = mpz_zero();
785 unsigned long long uval;
801 #if MICROPY_PY_BUILTINS_FLOAT 802 void mpz_set_from_float(
mpz_t *z, mp_float_t src) {
803 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 805 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 810 #if MP_ENDIANNESS_LITTLE 811 struct { mp_float_int_t frc:MP_FLOAT_FRAC_BITS,
exp:MP_FLOAT_EXP_BITS, sgn:1; } p;
813 struct { mp_float_int_t sgn:1,
exp:MP_FLOAT_EXP_BITS, frc:MP_FLOAT_FRAC_BITS; } p;
821 }
else if (u.p.exp == ((1 << MP_FLOAT_EXP_BITS) - 1)) {
826 const int adj_exp = (int)u.p.exp - MP_FLOAT_EXP_BIAS;
830 }
else if (adj_exp == 0) {
836 const unsigned int rem = adj_exp %
DIG_SIZE;
838 mp_float_int_t frc = u.p.frc | ((mp_float_int_t)1 << MP_FLOAT_FRAC_BITS);
840 if (adj_exp < MP_FLOAT_FRAC_BITS) {
843 frc >>= MP_FLOAT_FRAC_BITS - adj_exp;
845 shft = (rem - MP_FLOAT_FRAC_BITS) %
DIG_SIZE;
846 dig_ind = (adj_exp - MP_FLOAT_FRAC_BITS) /
DIG_SIZE;
857 #if DIG_SIZE < (MP_FLOAT_FRAC_BITS + 1) 858 while (dig_ind != dig_cnt) {
863 if (dig_ind != dig_cnt) {
864 z->
dig[dig_ind] = frc;
876 const char *cur = str;
877 const char *top = str + len;
888 for (; cur < top; ++cur) {
891 if (
'0' <= v && v <=
'9') {
893 }
else if (
'A' <= v && v <=
'Z') {
895 }
else if (
'a' <= v && v <=
'z') {
923 while (len && num_bits <
DIG_SIZE) {
924 d |= *buf << num_bits;
931 #if DIG_SIZE != 8 && DIG_SIZE != 16 && DIG_SIZE != 32 943 these functions are unused
945 bool mpz_is_pos(
const mpz_t *z) {
946 return z->
len > 0 && z->
neg == 0;
949 bool mpz_is_odd(
const mpz_t *z) {
950 return z->
len > 0 && (z->
dig[0] & 1) != 0;
953 bool mpz_is_even(
const mpz_t *z) {
954 return z->
len == 0 || (z->
dig[0] & 1) == 0;
960 if (z1->
len == 0 && z2->
len == 0) {
963 int cmp = (int)z2->
neg - (
int)z1->
neg;
980 if (sml_int < 0)
return 1;
982 if (z->
len == 0)
return 0;
985 if (z->
len == 0)
return -1;
987 if (z->
len != 1)
return 1;
988 cmp = z->
dig[0] - sml_int;
990 if (sml_int > 0)
return -1;
992 if (z->
len == 0)
return 0;
995 if (z->
len == 0)
return 1;
997 if (z->
len != 1)
return -1;
998 cmp = -z->
dig[0] - sml_int;
1000 if (cmp < 0)
return -1;
1001 if (cmp > 0)
return 1;
1007 these functions are unused
1029 mpz_t *z = mpz_zero();
1038 mpz_t *z = mpz_zero();
1047 mpz_t *z = mpz_zero();
1056 mpz_t *z = mpz_zero();
1090 dest->
neg = 1 - dest->
neg;
1100 if (dest->
len == 0) {
1105 }
else if (dest->
neg) {
1121 if (lhs->
len == 0 || rhs == 0) {
1134 if (lhs->
len == 0 || rhs == 0) {
1145 for (
size_t i = 0; i < lhs->
len && i < n_whole; i++) {
1146 if (lhs->
dig[i] != 0) {
1151 if (n_whole < lhs->len && (lhs->
dig[n_whole] & ((1 << n_part) - 1)) != 0) {
1155 if (dest->
len == 0) {
1173 const mpz_t *temp = lhs;
1178 if (lhs->
neg == rhs->
neg) {
1196 const mpz_t *temp = lhs;
1202 if (lhs->
neg != rhs->
neg) {
1211 dest->
neg = 1 - lhs->
neg;
1222 if (lhs->
len < rhs->
len) {
1223 const mpz_t *temp = lhs;
1228 #if MICROPY_OPT_MPZ_BITWISE 1230 if ((0 == lhs->
neg) && (0 == rhs->
neg)) {
1256 if (lhs->
len < rhs->
len) {
1257 const mpz_t *temp = lhs;
1262 #if MICROPY_OPT_MPZ_BITWISE 1264 if ((0 == lhs->
neg) && (0 == rhs->
neg)) {
1271 0 != lhs->
neg, 0 != rhs->
neg);
1290 if (lhs->
len < rhs->
len) {
1291 const mpz_t *temp = lhs;
1296 #if MICROPY_OPT_MPZ_BITWISE 1298 if (lhs->
neg == rhs->
neg) {
1300 if (lhs->
neg == 0) {
1309 0 == lhs->
neg, 0 == rhs->
neg);
1327 if (lhs->
len == 0 || rhs->
len == 0) {
1338 }
else if (rhs == dest) {
1346 if (lhs->
neg == rhs->
neg) {
1359 if (lhs->
len == 0 || rhs->
neg != 0) {
1364 if (rhs->
len == 0) {
1374 while (n->
len > 0) {
1375 if ((n->
dig[0] & 1) != 0) {
1393 if (lhs->
len == 0 || rhs->
neg != 0) {
1398 if (rhs->
len == 0) {
1409 while (n->
len > 0) {
1410 if ((n->
dig[0] & 1) != 0) {
1428 these functions are unused
1441 }
else if (z2->
len == 0) {
1460 mpz_t *t = a; a = b; b = t;
1462 if (!(b->
len >= 2 || (b->
len == 1 && b->
dig[0] > 1))) {
1475 if (b->
len == 1 && b->
dig[0] == 1) {
1491 if (z1->
len == 0 || z2->
len == 0) {
1495 mpz_t *gcd = mpz_gcd(z1, z2);
1496 mpz_t *quo = mpz_zero();
1497 mpz_t *rem = mpz_zero();
1514 assert(!mpz_is_zero(rhs));
1524 if (lhs->
neg != rhs->
neg) {
1526 if (!mpz_is_zero(dest_rem)) {
1535 these functions are unused
1541 mpz_t *quo = mpz_zero();
1553 mpz_t *rem = mpz_zero();
1565 while (d-- > z->
dig) {
1580 while (d-- > i->
dig) {
1605 while (d-- > i->
dig) {
1627 for (
size_t zlen = z->
len; zlen > 0; --zlen) {
1630 for (; bits >= 8; bits -= 8, d >>= 8) {
1633 val = (~val & 0xff) + carry;
1643 if (b == buf + len) {
1651 #if MICROPY_PY_BUILTINS_FLOAT 1652 mp_float_t mpz_as_float(
const mpz_t *i) {
1656 while (d-- > i->
dig) {
1669 this function is unused
1670 char *mpz_as_str(
const mpz_t *i,
unsigned int base) {
1679 size_t mpz_as_str_inpl(
const mpz_t *i,
unsigned int base,
const char *prefix,
char base_char,
char comma,
char *str) {
1683 if (base < 2 || base > 32) {
1688 size_t ilen = i->
len;
1706 char *last_comma = str;
1713 while (--d >= dig) {
1722 a += base_char -
'9' - 1;
1728 for (d = dig; d < dig + ilen; ++d) {
1734 if (comma && (s - last_comma) == 3) {
1745 const char *p = &prefix[
strlen(prefix)];
1746 while (p > prefix) {
1755 for (
char *u = str, *v = s - 1; u < v; ++u, --v) {
1766 #endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
void mpz_deinit(mpz_t *z)
void mpz_init_zero(mpz_t *z)
void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
void * memset(void *b, int c, size_t len)
STATIC size_t mpn_shr(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mp_uint_t n)
void mpz_set_from_ll(mpz_t *z, long long val, bool is_signed)
#define m_del(type, ptr, num)
void mpz_set_from_bytes(mpz_t *z, bool big_endian, size_t len, const byte *buf)
void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
STATIC size_t mpn_and_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen, mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk)
STATIC size_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen, mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk)
size_t mpz_set_from_str(mpz_t *z, const char *str, size_t len, bool neg, unsigned int base)
void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
STATIC size_t mpn_sub(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen)
void mpz_pow3_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs, const mpz_t *mod)
void mpz_set_from_int(mpz_t *z, mp_int_t val)
size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma)
mp_int_t mpz_hash(const mpz_t *z)
void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf)
void mpz_init_from_int(mpz_t *z, mp_int_t val)
STATIC size_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mpz_dig_t *kdig, size_t klen)
STATIC size_t mpn_remove_trailing_zeros(mpz_dig_t *oidig, mpz_dig_t *idig)
#define m_del_obj(type, ptr)
size_t strlen(const char *s)
#define MPZ_NUM_DIG_FOR_LL
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs)
void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
void mpz_neg_inpl(mpz_t *dest, const mpz_t *z)
unsigned long long uint64_t
STATIC size_t mpn_xor_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen, mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk)
int32_t mpz_dbl_dig_signed_t
void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
STATIC size_t mpn_mul_dig_add_dig(mpz_dig_t *idig, size_t ilen, mpz_dig_t dmul, mpz_dig_t dadd)
void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
STATIC void mpz_need_dig(mpz_t *z, size_t need)
#define is_signed(typecode)
void mpz_abs_inpl(mpz_t *dest, const mpz_t *z)
void mpz_set(mpz_t *dest, const mpz_t *src)
int mpz_cmp(const mpz_t *z1, const mpz_t *z2)
#define m_renew(type, ptr, old_num, new_num)
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs)
STATIC int mpn_cmp(const mpz_dig_t *idig, size_t ilen, const mpz_dig_t *jdig, size_t jlen)
void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
STATIC size_t mpn_add(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen)
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, size_t alloc, mp_int_t val)
bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value)
STATIC size_t mpn_shl(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mp_uint_t n)
#define MPZ_NUM_DIG_FOR_INT
STATIC void mpz_free(mpz_t *z)
STATIC mpz_t * mpz_clone(const mpz_t *src)
STATIC void mpn_div(mpz_dig_t *num_dig, size_t *num_len, const mpz_dig_t *den_dig, size_t den_len, mpz_dig_t *quo_dig, size_t *quo_len)
bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value)
void * memcpy(void *dst, const void *src, size_t n)
size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, char base_char, char comma, char *str)
void mpz_not_inpl(mpz_t *dest, const mpz_t *z)
void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs)