2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/mpz.h"
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
#define DIG_SIZE (MPZ_DIG_SIZE)
|
2014-10-30 13:39:22 +00:00
|
|
|
#define DIG_MASK ((MPZ_LONG_1 << DIG_SIZE) - 1)
|
|
|
|
#define DIG_MSB (MPZ_LONG_1 << (DIG_SIZE - 1))
|
|
|
|
#define DIG_BASE (MPZ_LONG_1 << DIG_SIZE)
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
/*
|
2014-03-01 19:50:50 +00:00
|
|
|
mpz is an arbitrary precision integer type with a public API.
|
|
|
|
|
|
|
|
mpn functions act on non-negative integers represented by an array of generalised
|
|
|
|
digits (eg a word per digit). You also need to specify separately the length of the
|
|
|
|
array. There is no public API for mpn. Rather, the functions are used by mpz to
|
|
|
|
implement its features.
|
|
|
|
|
|
|
|
Integer values are stored little endian (first digit is first in memory).
|
|
|
|
|
|
|
|
Definition of normalise: ?
|
2014-02-22 19:25:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* compares i with j
|
|
|
|
returns sign(i - j)
|
|
|
|
assumes i, j are normalised
|
|
|
|
*/
|
2014-10-03 18:44:14 +01:00
|
|
|
STATIC int mpn_cmp(const mpz_dig_t *idig, mp_uint_t ilen, const mpz_dig_t *jdig, mp_uint_t jlen) {
|
2014-02-22 19:25:23 +00:00
|
|
|
if (ilen < jlen) { return -1; }
|
|
|
|
if (ilen > jlen) { return 1; }
|
|
|
|
|
|
|
|
for (idig += ilen, jdig += ilen; ilen > 0; --ilen) {
|
2014-09-06 17:15:34 +01:00
|
|
|
mpz_dbl_dig_signed_t cmp = (mpz_dbl_dig_t)*(--idig) - (mpz_dbl_dig_t)*(--jdig);
|
2014-02-22 19:25:23 +00:00
|
|
|
if (cmp < 0) { return -1; }
|
|
|
|
if (cmp > 0) { return 1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-26 16:56:30 +00:00
|
|
|
/* computes i = j << n
|
|
|
|
returns number of digits in i
|
2014-03-01 19:50:50 +00:00
|
|
|
assumes enough memory in i; assumes normalised j; assumes n > 0
|
2014-02-26 16:56:30 +00:00
|
|
|
can have i, j pointing to same memory
|
|
|
|
*/
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC mp_uint_t mpn_shl(mpz_dig_t *idig, mpz_dig_t *jdig, mp_uint_t jlen, mp_uint_t n) {
|
|
|
|
mp_uint_t n_whole = (n + DIG_SIZE - 1) / DIG_SIZE;
|
|
|
|
mp_uint_t n_part = n % DIG_SIZE;
|
2014-03-12 15:36:06 +00:00
|
|
|
if (n_part == 0) {
|
|
|
|
n_part = DIG_SIZE;
|
|
|
|
}
|
2014-02-26 16:56:30 +00:00
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
// start from the high end of the digit arrays
|
|
|
|
idig += jlen + n_whole - 1;
|
|
|
|
jdig += jlen - 1;
|
2014-02-26 16:56:30 +00:00
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
// shift the digits
|
|
|
|
mpz_dbl_dig_t d = 0;
|
2014-09-05 20:37:06 +01:00
|
|
|
for (mp_uint_t i = jlen; i > 0; i--, idig--, jdig--) {
|
2014-03-01 19:50:50 +00:00
|
|
|
d |= *jdig;
|
2014-08-07 15:27:48 +01:00
|
|
|
*idig = (d >> (DIG_SIZE - n_part)) & DIG_MASK;
|
2014-03-01 19:50:50 +00:00
|
|
|
d <<= DIG_SIZE;
|
2014-02-26 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
// store remaining bits
|
2014-08-07 15:27:48 +01:00
|
|
|
*idig = (d >> (DIG_SIZE - n_part)) & DIG_MASK;
|
2014-03-01 19:50:50 +00:00
|
|
|
idig -= n_whole - 1;
|
2014-03-12 15:36:06 +00:00
|
|
|
memset(idig, 0, (n_whole - 1) * sizeof(mpz_dig_t));
|
2014-03-01 19:50:50 +00:00
|
|
|
|
|
|
|
// work out length of result
|
|
|
|
jlen += n_whole;
|
2015-04-22 23:16:49 +01:00
|
|
|
while (jlen != 0 && idig[jlen - 1] == 0) {
|
2014-03-01 19:50:50 +00:00
|
|
|
jlen--;
|
2014-02-26 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
// return length of result
|
2014-02-26 16:56:30 +00:00
|
|
|
return jlen;
|
|
|
|
}
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
/* computes i = j >> n
|
|
|
|
returns number of digits in i
|
2014-03-01 19:50:50 +00:00
|
|
|
assumes enough memory in i; assumes normalised j; assumes n > 0
|
2014-02-22 19:25:23 +00:00
|
|
|
can have i, j pointing to same memory
|
|
|
|
*/
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC mp_uint_t mpn_shr(mpz_dig_t *idig, mpz_dig_t *jdig, mp_uint_t jlen, mp_uint_t n) {
|
|
|
|
mp_uint_t n_whole = n / DIG_SIZE;
|
|
|
|
mp_uint_t n_part = n % DIG_SIZE;
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
if (n_whole >= jlen) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
jdig += n_whole;
|
|
|
|
jlen -= n_whole;
|
|
|
|
|
2014-09-05 20:37:06 +01:00
|
|
|
for (mp_uint_t i = jlen; i > 0; i--, idig++, jdig++) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dbl_dig_t d = *jdig;
|
2014-02-26 16:56:30 +00:00
|
|
|
if (i > 1) {
|
2014-09-06 17:15:34 +01:00
|
|
|
d |= (mpz_dbl_dig_t)jdig[1] << DIG_SIZE;
|
2014-02-26 16:56:30 +00:00
|
|
|
}
|
2014-02-22 19:25:23 +00:00
|
|
|
d >>= n_part;
|
|
|
|
*idig = d & DIG_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idig[-1] == 0) {
|
2014-03-01 19:50:50 +00:00
|
|
|
jlen--;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return jlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes i = j + k
|
|
|
|
returns number of digits in i
|
|
|
|
assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
|
|
|
|
can have i, j, k pointing to same memory
|
|
|
|
*/
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC mp_uint_t mpn_add(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jlen, const mpz_dig_t *kdig, mp_uint_t klen) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
mpz_dbl_dig_t carry = 0;
|
|
|
|
|
|
|
|
jlen -= klen;
|
|
|
|
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
2014-09-06 17:15:34 +01:00
|
|
|
carry += (mpz_dbl_dig_t)*jdig + (mpz_dbl_dig_t)*kdig;
|
2014-02-22 19:25:23 +00:00
|
|
|
*idig = carry & DIG_MASK;
|
|
|
|
carry >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; jlen > 0; --jlen, ++idig, ++jdig) {
|
|
|
|
carry += *jdig;
|
|
|
|
*idig = carry & DIG_MASK;
|
|
|
|
carry >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (carry != 0) {
|
|
|
|
*idig++ = carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return idig - oidig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes i = j - k
|
|
|
|
returns number of digits in i
|
|
|
|
assumes enough memory in i; assumes normalised j, k; assumes j >= k
|
|
|
|
can have i, j, k pointing to same memory
|
|
|
|
*/
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC mp_uint_t mpn_sub(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jlen, const mpz_dig_t *kdig, mp_uint_t klen) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
mpz_dbl_dig_signed_t borrow = 0;
|
|
|
|
|
|
|
|
jlen -= klen;
|
|
|
|
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
2014-09-06 17:15:34 +01:00
|
|
|
borrow += (mpz_dbl_dig_t)*jdig - (mpz_dbl_dig_t)*kdig;
|
2014-02-22 19:25:23 +00:00
|
|
|
*idig = borrow & DIG_MASK;
|
|
|
|
borrow >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
|
2014-02-24 21:32:52 +00:00
|
|
|
for (; jlen > 0; --jlen, ++idig, ++jdig) {
|
2014-02-22 19:25:23 +00:00
|
|
|
borrow += *jdig;
|
|
|
|
*idig = borrow & DIG_MASK;
|
|
|
|
borrow >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (--idig; idig >= oidig && *idig == 0; --idig) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return idig + 1 - oidig;
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
STATIC mp_uint_t mpn_remove_trailing_zeros(mpz_dig_t *oidig, mpz_dig_t *idig) {
|
|
|
|
for (--idig; idig >= oidig && *idig == 0; --idig) {
|
|
|
|
}
|
|
|
|
return idig + 1 - oidig;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
|
2014-03-22 23:52:36 +00:00
|
|
|
/* computes i = j & k
|
|
|
|
returns number of digits in i
|
2015-01-20 12:47:20 +00:00
|
|
|
assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen (jlen argument not needed)
|
2014-03-22 23:52:36 +00:00
|
|
|
can have i, j, k pointing to same memory
|
|
|
|
*/
|
2015-01-20 12:47:20 +00:00
|
|
|
STATIC mp_uint_t mpn_and(mpz_dig_t *idig, const mpz_dig_t *jdig, const mpz_dig_t *kdig, mp_uint_t klen) {
|
2014-03-22 23:52:36 +00:00
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
|
|
|
*idig = *jdig & *kdig;
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
2014-03-22 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* i = -((-j) & (-k)) = ~((~j + 1) & (~k + 1)) + 1
|
|
|
|
i = (j & (-k)) = (j & (~k + 1)) = ( j & (~k + 1))
|
|
|
|
i = ((-j) & k) = ((~j + 1) & k) = ((~j + 1) & k )
|
|
|
|
computes general form:
|
|
|
|
i = (im ^ (((j ^ jm) + jc) & ((k ^ km) + kc))) + ic where Xm = Xc == 0 ? 0 : DIG_MASK
|
2014-05-29 15:01:49 +01:00
|
|
|
returns number of digits in i
|
2016-01-31 03:35:58 +00:00
|
|
|
assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
|
2014-05-29 15:01:49 +01:00
|
|
|
can have i, j, k pointing to same memory
|
|
|
|
*/
|
2016-01-31 03:35:58 +00:00
|
|
|
STATIC mp_uint_t mpn_and_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jlen, const mpz_dig_t *kdig, mp_uint_t klen,
|
|
|
|
mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
|
2014-05-29 15:01:49 +01:00
|
|
|
mpz_dig_t *oidig = idig;
|
2016-01-31 03:35:58 +00:00
|
|
|
mpz_dig_t imask = (0 == carryi) ? 0 : DIG_MASK;
|
|
|
|
mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
|
|
|
|
mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
|
2014-05-29 15:01:49 +01:00
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
for (; jlen > 0; ++idig, ++jdig) {
|
|
|
|
carryj += *jdig ^ jmask;
|
|
|
|
carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
|
|
|
|
carryi += ((carryj & carryk) ^ imask) & DIG_MASK;
|
|
|
|
*idig = carryi & DIG_MASK;
|
|
|
|
carryk >>= DIG_SIZE;
|
|
|
|
carryj >>= DIG_SIZE;
|
|
|
|
carryi >>= DIG_SIZE;
|
2014-05-29 15:01:49 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
if (0 != carryi) {
|
|
|
|
*idig++ = carryi;
|
2014-05-29 15:01:49 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
2014-05-29 15:01:49 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
|
2014-03-22 23:52:36 +00:00
|
|
|
/* computes i = j | k
|
|
|
|
returns number of digits in i
|
|
|
|
assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
|
|
|
|
can have i, j, k pointing to same memory
|
|
|
|
*/
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC mp_uint_t mpn_or(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jlen, const mpz_dig_t *kdig, mp_uint_t klen) {
|
2014-03-22 23:52:36 +00:00
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
|
|
|
|
jlen -= klen;
|
|
|
|
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
|
|
|
*idig = *jdig | *kdig;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; jlen > 0; --jlen, ++idig, ++jdig) {
|
|
|
|
*idig = *jdig;
|
|
|
|
}
|
|
|
|
|
|
|
|
return idig - oidig;
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* i = -((-j) | (-k)) = ~((~j + 1) | (~k + 1)) + 1
|
|
|
|
i = -(j | (-k)) = -(j | (~k + 1)) = ~( j | (~k + 1)) + 1
|
|
|
|
i = -((-j) | k) = -((~j + 1) | k) = ~((~j + 1) | k ) + 1
|
|
|
|
computes general form:
|
|
|
|
i = ~(((j ^ jm) + jc) | ((k ^ km) + kc)) + 1 where Xm = Xc == 0 ? 0 : DIG_MASK
|
|
|
|
returns number of digits in i
|
|
|
|
assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
|
|
|
|
can have i, j, k pointing to same memory
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
|
|
|
|
STATIC mp_uint_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jlen, const mpz_dig_t *kdig, mp_uint_t klen,
|
|
|
|
mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
|
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
mpz_dbl_dig_t carryi = 1;
|
|
|
|
mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
|
|
|
|
mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
|
|
|
|
|
|
|
|
for (; jlen > 0; ++idig, ++jdig) {
|
|
|
|
carryj += *jdig ^ jmask;
|
|
|
|
carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
|
|
|
|
carryi += ((carryj | carryk) ^ DIG_MASK) & DIG_MASK;
|
|
|
|
*idig = carryi & DIG_MASK;
|
|
|
|
carryk >>= DIG_SIZE;
|
|
|
|
carryj >>= DIG_SIZE;
|
|
|
|
carryi >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
|
2016-12-13 23:39:41 +00:00
|
|
|
// At least one of j,k must be negative so the above for-loop runs at least
|
|
|
|
// once. For carryi to be non-zero here it must be equal to 1 at the end of
|
|
|
|
// each iteration of the loop. So the accumulation of carryi must overflow
|
|
|
|
// each time, ie carryi += 0xff..ff. So carryj|carryk must be 0 in the
|
|
|
|
// DIG_MASK bits on each iteration. But considering all cases of signs of
|
|
|
|
// j,k one sees that this is not possible.
|
|
|
|
assert(carryi == 0);
|
2016-01-31 03:35:58 +00:00
|
|
|
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
STATIC mp_uint_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jlen, const mpz_dig_t *kdig, mp_uint_t klen,
|
|
|
|
mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
|
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
mpz_dig_t imask = (0 == carryi) ? 0 : DIG_MASK;
|
|
|
|
mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
|
|
|
|
mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
|
|
|
|
|
|
|
|
for (; jlen > 0; ++idig, ++jdig) {
|
|
|
|
carryj += *jdig ^ jmask;
|
|
|
|
carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
|
|
|
|
carryi += ((carryj | carryk) ^ imask) & DIG_MASK;
|
|
|
|
*idig = carryi & DIG_MASK;
|
|
|
|
carryk >>= DIG_SIZE;
|
|
|
|
carryj >>= DIG_SIZE;
|
|
|
|
carryi >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
|
2016-12-13 23:39:41 +00:00
|
|
|
// See comment in above mpn_or_neg for why carryi must be 0.
|
|
|
|
assert(carryi == 0);
|
2016-01-31 03:35:58 +00:00
|
|
|
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
|
2014-03-22 23:52:36 +00:00
|
|
|
/* computes i = j ^ k
|
|
|
|
returns number of digits in i
|
|
|
|
assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
|
|
|
|
can have i, j, k pointing to same memory
|
|
|
|
*/
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC mp_uint_t mpn_xor(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jlen, const mpz_dig_t *kdig, mp_uint_t klen) {
|
2014-03-22 23:52:36 +00:00
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
|
|
|
|
jlen -= klen;
|
|
|
|
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
|
|
|
*idig = *jdig ^ *kdig;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; jlen > 0; --jlen, ++idig, ++jdig) {
|
|
|
|
*idig = *jdig;
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* i = (-j) ^ (-k) = ~(j - 1) ^ ~(k - 1) = (j - 1) ^ (k - 1)
|
|
|
|
i = -(j ^ (-k)) = -(j ^ ~(k - 1)) = ~(j ^ ~(k - 1)) + 1 = (j ^ (k - 1)) + 1
|
|
|
|
i = -((-j) ^ k) = -(~(j - 1) ^ k) = ~(~(j - 1) ^ k) + 1 = ((j - 1) ^ k) + 1
|
|
|
|
computes general form:
|
|
|
|
i = ((j - 1 + jc) ^ (k - 1 + kc)) + ic
|
|
|
|
returns number of digits in i
|
|
|
|
assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
|
|
|
|
can have i, j, k pointing to same memory
|
|
|
|
*/
|
|
|
|
STATIC mp_uint_t mpn_xor_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, mp_uint_t jlen, const mpz_dig_t *kdig, mp_uint_t klen,
|
|
|
|
mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
|
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
|
|
|
|
for (; jlen > 0; ++idig, ++jdig) {
|
|
|
|
carryj += *jdig + DIG_MASK;
|
|
|
|
carryk += (--klen <= --jlen) ? (*kdig++ + DIG_MASK) : DIG_MASK;
|
|
|
|
carryi += (carryj ^ carryk) & DIG_MASK;
|
|
|
|
*idig = carryi & DIG_MASK;
|
|
|
|
carryk >>= DIG_SIZE;
|
|
|
|
carryj >>= DIG_SIZE;
|
|
|
|
carryi >>= DIG_SIZE;
|
2015-11-22 20:03:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
if (0 != carryi) {
|
|
|
|
*idig++ = carryi;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
2014-03-22 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
/* computes i = i * d1 + d2
|
|
|
|
returns number of digits in i
|
|
|
|
assumes enough memory in i; assumes normalised i; assumes dmul != 0
|
|
|
|
*/
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC mp_uint_t mpn_mul_dig_add_dig(mpz_dig_t *idig, mp_uint_t ilen, mpz_dig_t dmul, mpz_dig_t dadd) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
mpz_dbl_dig_t carry = dadd;
|
|
|
|
|
|
|
|
for (; ilen > 0; --ilen, ++idig) {
|
2014-09-06 17:15:34 +01:00
|
|
|
carry += (mpz_dbl_dig_t)*idig * (mpz_dbl_dig_t)dmul; // will never overflow so long as DIG_SIZE <= 8*sizeof(mpz_dbl_dig_t)/2
|
2014-02-22 19:25:23 +00:00
|
|
|
*idig = carry & DIG_MASK;
|
|
|
|
carry >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (carry != 0) {
|
|
|
|
*idig++ = carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return idig - oidig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes i = j * k
|
|
|
|
returns number of digits in i
|
|
|
|
assumes enough memory in i; assumes i is zeroed; assumes normalised j, k
|
|
|
|
can have j, k point to same memory
|
|
|
|
*/
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC mp_uint_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, mp_uint_t jlen, mpz_dig_t *kdig, mp_uint_t klen) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dig_t *oidig = idig;
|
2014-09-05 20:37:06 +01:00
|
|
|
mp_uint_t ilen = 0;
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
for (; klen > 0; --klen, ++idig, ++kdig) {
|
|
|
|
mpz_dig_t *id = idig;
|
|
|
|
mpz_dbl_dig_t carry = 0;
|
|
|
|
|
2014-09-05 20:37:06 +01:00
|
|
|
mp_uint_t jl = jlen;
|
2014-02-22 19:25:23 +00:00
|
|
|
for (mpz_dig_t *jd = jdig; jl > 0; --jl, ++jd, ++id) {
|
2014-09-06 17:15:34 +01:00
|
|
|
carry += (mpz_dbl_dig_t)*id + (mpz_dbl_dig_t)*jd * (mpz_dbl_dig_t)*kdig; // will never overflow so long as DIG_SIZE <= 8*sizeof(mpz_dbl_dig_t)/2
|
2014-02-22 19:25:23 +00:00
|
|
|
*id = carry & DIG_MASK;
|
|
|
|
carry >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (carry != 0) {
|
|
|
|
*id++ = carry;
|
|
|
|
}
|
|
|
|
|
|
|
|
ilen = id - oidig;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ilen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* natural_div - quo * den + new_num = old_num (ie num is replaced with rem)
|
|
|
|
assumes den != 0
|
|
|
|
assumes num_dig has enough memory to be extended by 1 digit
|
|
|
|
assumes quo_dig has enough memory (as many digits as num)
|
|
|
|
assumes quo_dig is filled with zeros
|
|
|
|
*/
|
2016-05-09 17:21:42 +01:00
|
|
|
STATIC void mpn_div(mpz_dig_t *num_dig, mp_uint_t *num_len, const mpz_dig_t *den_dig, mp_uint_t den_len, mpz_dig_t *quo_dig, mp_uint_t *quo_len) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dig_t *orig_num_dig = num_dig;
|
|
|
|
mpz_dig_t *orig_quo_dig = quo_dig;
|
|
|
|
mpz_dig_t norm_shift = 0;
|
|
|
|
mpz_dbl_dig_t lead_den_digit;
|
|
|
|
|
|
|
|
// handle simple cases
|
|
|
|
{
|
2014-10-03 18:44:14 +01:00
|
|
|
int cmp = mpn_cmp(num_dig, *num_len, den_dig, den_len);
|
2014-02-22 19:25:23 +00:00
|
|
|
if (cmp == 0) {
|
|
|
|
*num_len = 0;
|
|
|
|
quo_dig[0] = 1;
|
|
|
|
*quo_len = 1;
|
|
|
|
return;
|
|
|
|
} else if (cmp < 0) {
|
|
|
|
// numerator remains the same
|
|
|
|
*quo_len = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:21:42 +01:00
|
|
|
// We need to normalise the denominator (leading bit of leading digit is 1)
|
|
|
|
// so that the division routine works. Since the denominator memory is
|
|
|
|
// read-only we do the normalisation on the fly, each time a digit of the
|
|
|
|
// denominator is needed. We need to know is how many bits to shift by.
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
// count number of leading zeros in leading digit of denominator
|
|
|
|
{
|
|
|
|
mpz_dig_t d = den_dig[den_len - 1];
|
2014-09-06 17:15:34 +01:00
|
|
|
while ((d & DIG_MSB) == 0) {
|
2014-02-22 19:25:23 +00:00
|
|
|
d <<= 1;
|
|
|
|
++norm_shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now need to shift numerator by same amount as denominator
|
|
|
|
// first, increase length of numerator in case we need more room to shift
|
|
|
|
num_dig[*num_len] = 0;
|
|
|
|
++(*num_len);
|
|
|
|
for (mpz_dig_t *num = num_dig, carry = 0; num < num_dig + *num_len; ++num) {
|
|
|
|
mpz_dig_t n = *num;
|
|
|
|
*num = ((n << norm_shift) | carry) & DIG_MASK;
|
2016-05-08 21:38:43 +01:00
|
|
|
carry = (mpz_dbl_dig_t)n >> (DIG_SIZE - norm_shift);
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cache the leading digit of the denominator
|
2016-05-09 17:21:42 +01:00
|
|
|
lead_den_digit = (mpz_dbl_dig_t)den_dig[den_len - 1] << norm_shift;
|
|
|
|
if (den_len >= 2) {
|
|
|
|
lead_den_digit |= (mpz_dbl_dig_t)den_dig[den_len - 2] >> (DIG_SIZE - norm_shift);
|
|
|
|
}
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
// point num_dig to last digit in numerator
|
|
|
|
num_dig += *num_len - 1;
|
|
|
|
|
|
|
|
// calculate number of digits in quotient
|
|
|
|
*quo_len = *num_len - den_len;
|
|
|
|
|
|
|
|
// point to last digit to store for quotient
|
|
|
|
quo_dig += *quo_len - 1;
|
|
|
|
|
|
|
|
// keep going while we have enough digits to divide
|
|
|
|
while (*num_len > den_len) {
|
2014-09-06 17:15:34 +01:00
|
|
|
mpz_dbl_dig_t quo = ((mpz_dbl_dig_t)*num_dig << DIG_SIZE) | num_dig[-1];
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
// get approximate quotient
|
|
|
|
quo /= lead_den_digit;
|
|
|
|
|
2014-09-06 17:15:34 +01:00
|
|
|
// Multiply quo by den and subtract from num to get remainder.
|
|
|
|
// We have different code here to handle different compile-time
|
|
|
|
// configurations of mpz:
|
|
|
|
//
|
|
|
|
// 1. DIG_SIZE is stricly less than half the number of bits
|
|
|
|
// available in mpz_dbl_dig_t. In this case we can use a
|
|
|
|
// slightly more optimal (in time and space) routine that
|
|
|
|
// uses the extra bits in mpz_dbl_dig_signed_t to store a
|
|
|
|
// sign bit.
|
|
|
|
//
|
|
|
|
// 2. DIG_SIZE is exactly half the number of bits available in
|
|
|
|
// mpz_dbl_dig_t. In this (common) case we need to be careful
|
|
|
|
// not to overflow the borrow variable. And the shifting of
|
|
|
|
// borrow needs some special logic (it's a shift right with
|
|
|
|
// round up).
|
|
|
|
|
|
|
|
if (DIG_SIZE < 8 * sizeof(mpz_dbl_dig_t) / 2) {
|
2016-05-09 17:21:42 +01:00
|
|
|
const mpz_dig_t *d = den_dig;
|
|
|
|
mpz_dbl_dig_t d_norm = 0;
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dbl_dig_signed_t borrow = 0;
|
|
|
|
|
2016-05-09 17:21:42 +01:00
|
|
|
for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
|
|
|
|
d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
|
|
|
|
borrow += (mpz_dbl_dig_t)*n - (mpz_dbl_dig_t)quo * (d_norm & DIG_MASK); // will overflow if DIG_SIZE >= 8*sizeof(mpz_dbl_dig_t)/2
|
2014-02-22 19:25:23 +00:00
|
|
|
*n = borrow & DIG_MASK;
|
|
|
|
borrow >>= DIG_SIZE;
|
|
|
|
}
|
2014-09-06 17:15:34 +01:00
|
|
|
borrow += *num_dig; // will overflow if DIG_SIZE >= 8*sizeof(mpz_dbl_dig_t)/2
|
2014-02-22 19:25:23 +00:00
|
|
|
*num_dig = borrow & DIG_MASK;
|
|
|
|
borrow >>= DIG_SIZE;
|
|
|
|
|
|
|
|
// adjust quotient if it is too big
|
|
|
|
for (; borrow != 0; --quo) {
|
2016-05-09 17:21:42 +01:00
|
|
|
d = den_dig;
|
|
|
|
d_norm = 0;
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dbl_dig_t carry = 0;
|
2016-05-09 17:21:42 +01:00
|
|
|
for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
|
|
|
|
d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
|
|
|
|
carry += (mpz_dbl_dig_t)*n + (d_norm & DIG_MASK);
|
2014-02-22 19:25:23 +00:00
|
|
|
*n = carry & DIG_MASK;
|
|
|
|
carry >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
carry += *num_dig;
|
|
|
|
*num_dig = carry & DIG_MASK;
|
|
|
|
carry >>= DIG_SIZE;
|
|
|
|
|
|
|
|
borrow += carry;
|
|
|
|
}
|
2014-09-06 17:15:34 +01:00
|
|
|
} else { // DIG_SIZE == 8 * sizeof(mpz_dbl_dig_t) / 2
|
2016-05-09 17:21:42 +01:00
|
|
|
const mpz_dig_t *d = den_dig;
|
|
|
|
mpz_dbl_dig_t d_norm = 0;
|
2014-09-06 17:15:34 +01:00
|
|
|
mpz_dbl_dig_t borrow = 0;
|
|
|
|
|
2016-05-09 17:21:42 +01:00
|
|
|
for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
|
|
|
|
d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
|
|
|
|
mpz_dbl_dig_t x = (mpz_dbl_dig_t)quo * (d_norm & DIG_MASK);
|
2014-09-06 17:15:34 +01:00
|
|
|
if (x >= *n || *n - x <= borrow) {
|
|
|
|
borrow += (mpz_dbl_dig_t)x - (mpz_dbl_dig_t)*n;
|
|
|
|
*n = (-borrow) & DIG_MASK;
|
|
|
|
borrow = (borrow >> DIG_SIZE) + ((borrow & DIG_MASK) == 0 ? 0 : 1); // shift-right with round-up
|
|
|
|
} else {
|
|
|
|
*n = ((mpz_dbl_dig_t)*n - (mpz_dbl_dig_t)x - (mpz_dbl_dig_t)borrow) & DIG_MASK;
|
|
|
|
borrow = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (borrow >= *num_dig) {
|
|
|
|
borrow -= (mpz_dbl_dig_t)*num_dig;
|
|
|
|
*num_dig = (-borrow) & DIG_MASK;
|
|
|
|
borrow = (borrow >> DIG_SIZE) + ((borrow & DIG_MASK) == 0 ? 0 : 1); // shift-right with round-up
|
|
|
|
} else {
|
|
|
|
*num_dig = (*num_dig - borrow) & DIG_MASK;
|
|
|
|
borrow = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust quotient if it is too big
|
|
|
|
for (; borrow != 0; --quo) {
|
2016-05-09 17:21:42 +01:00
|
|
|
d = den_dig;
|
|
|
|
d_norm = 0;
|
2014-09-06 17:15:34 +01:00
|
|
|
mpz_dbl_dig_t carry = 0;
|
2016-05-09 17:21:42 +01:00
|
|
|
for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
|
|
|
|
d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
|
|
|
|
carry += (mpz_dbl_dig_t)*n + (d_norm & DIG_MASK);
|
2014-09-06 17:15:34 +01:00
|
|
|
*n = carry & DIG_MASK;
|
|
|
|
carry >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
carry += (mpz_dbl_dig_t)*num_dig;
|
|
|
|
*num_dig = carry & DIG_MASK;
|
|
|
|
carry >>= DIG_SIZE;
|
|
|
|
|
|
|
|
//assert(borrow >= carry); // enable this to check the logic
|
|
|
|
borrow -= carry;
|
|
|
|
}
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// store this digit of the quotient
|
|
|
|
*quo_dig = quo & DIG_MASK;
|
|
|
|
--quo_dig;
|
|
|
|
|
|
|
|
// move down to next digit of numerator
|
|
|
|
--num_dig;
|
|
|
|
--(*num_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// unnormalise numerator (remainder now)
|
|
|
|
for (mpz_dig_t *num = orig_num_dig + *num_len - 1, carry = 0; num >= orig_num_dig; --num) {
|
|
|
|
mpz_dig_t n = *num;
|
|
|
|
*num = ((n >> norm_shift) | carry) & DIG_MASK;
|
2016-05-08 21:38:43 +01:00
|
|
|
carry = (mpz_dbl_dig_t)n << (DIG_SIZE - norm_shift);
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// strip trailing zeros
|
|
|
|
|
|
|
|
while (*quo_len > 0 && orig_quo_dig[*quo_len - 1] == 0) {
|
|
|
|
--(*quo_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*num_len > 0 && orig_num_dig[*num_len - 1] == 0) {
|
|
|
|
--(*num_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
#define MIN_ALLOC (2)
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
void mpz_init_zero(mpz_t *z) {
|
|
|
|
z->neg = 0;
|
2014-03-01 19:50:50 +00:00
|
|
|
z->fixed_dig = 0;
|
|
|
|
z->alloc = 0;
|
2014-02-22 19:25:23 +00:00
|
|
|
z->len = 0;
|
|
|
|
z->dig = NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
void mpz_init_from_int(mpz_t *z, mp_int_t val) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_init_zero(z);
|
|
|
|
mpz_set_from_int(z, val);
|
|
|
|
}
|
|
|
|
|
2014-09-05 20:37:06 +01:00
|
|
|
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, mp_uint_t alloc, mp_int_t val) {
|
2014-03-01 19:50:50 +00:00
|
|
|
z->neg = 0;
|
|
|
|
z->fixed_dig = 1;
|
|
|
|
z->alloc = alloc;
|
|
|
|
z->len = 0;
|
|
|
|
z->dig = dig;
|
|
|
|
mpz_set_from_int(z, val);
|
|
|
|
}
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
void mpz_deinit(mpz_t *z) {
|
2014-03-01 19:50:50 +00:00
|
|
|
if (z != NULL && !z->fixed_dig) {
|
2014-02-22 19:25:23 +00:00
|
|
|
m_del(mpz_dig_t, z->dig, z->alloc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 15:59:40 +00:00
|
|
|
#if 0
|
|
|
|
these functions are unused
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_t *mpz_zero(void) {
|
|
|
|
mpz_t *z = m_new_obj(mpz_t);
|
|
|
|
mpz_init_zero(z);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mpz_t *mpz_from_int(mp_int_t val) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_t *z = mpz_zero();
|
|
|
|
mpz_set_from_int(z, val);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2014-09-10 22:10:33 +01:00
|
|
|
mpz_t *mpz_from_ll(long long val, bool is_signed) {
|
2014-03-12 15:36:06 +00:00
|
|
|
mpz_t *z = mpz_zero();
|
2014-09-10 22:10:33 +01:00
|
|
|
mpz_set_from_ll(z, val, is_signed);
|
2014-03-12 15:36:06 +00:00
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2015-01-02 12:39:22 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
mpz_t *mpz_from_float(mp_float_t val) {
|
|
|
|
mpz_t *z = mpz_zero();
|
|
|
|
mpz_set_from_float(z, val);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-05 20:37:06 +01:00
|
|
|
mpz_t *mpz_from_str(const char *str, mp_uint_t len, bool neg, mp_uint_t base) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_t *z = mpz_zero();
|
|
|
|
mpz_set_from_str(z, str, len, neg, base);
|
|
|
|
return z;
|
|
|
|
}
|
2015-03-12 15:59:40 +00:00
|
|
|
#endif
|
2014-02-22 19:25:23 +00:00
|
|
|
|
2015-03-12 15:59:40 +00:00
|
|
|
STATIC void mpz_free(mpz_t *z) {
|
2014-02-22 19:25:23 +00:00
|
|
|
if (z != NULL) {
|
|
|
|
m_del(mpz_dig_t, z->dig, z->alloc);
|
|
|
|
m_del_obj(mpz_t, z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 20:37:06 +01:00
|
|
|
STATIC void mpz_need_dig(mpz_t *z, mp_uint_t need) {
|
2014-02-22 19:25:23 +00:00
|
|
|
if (need < MIN_ALLOC) {
|
2014-03-01 19:50:50 +00:00
|
|
|
need = MIN_ALLOC;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
if (z->dig == NULL || z->alloc < need) {
|
2016-10-11 03:00:56 +01:00
|
|
|
// if z has fixed digit buffer there's not much we can do as the caller will
|
|
|
|
// be expecting a buffer with at least "need" bytes (but it shouldn't happen)
|
|
|
|
assert(!z->fixed_dig);
|
2014-03-01 19:50:50 +00:00
|
|
|
z->dig = m_renew(mpz_dig_t, z->dig, z->alloc, need);
|
|
|
|
z->alloc = need;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 15:59:40 +00:00
|
|
|
STATIC mpz_t *mpz_clone(const mpz_t *src) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_t *z = m_new_obj(mpz_t);
|
|
|
|
z->neg = src->neg;
|
2014-03-01 19:50:50 +00:00
|
|
|
z->fixed_dig = 0;
|
|
|
|
z->alloc = src->alloc;
|
2014-02-22 19:25:23 +00:00
|
|
|
z->len = src->len;
|
|
|
|
if (src->dig == NULL) {
|
|
|
|
z->dig = NULL;
|
|
|
|
} else {
|
|
|
|
z->dig = m_new(mpz_dig_t, z->alloc);
|
|
|
|
memcpy(z->dig, src->dig, src->alloc * sizeof(mpz_dig_t));
|
|
|
|
}
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
/* sets dest = src
|
|
|
|
can have dest, src the same
|
|
|
|
*/
|
2014-02-22 19:25:23 +00:00
|
|
|
void mpz_set(mpz_t *dest, const mpz_t *src) {
|
|
|
|
mpz_need_dig(dest, src->len);
|
|
|
|
dest->neg = src->neg;
|
|
|
|
dest->len = src->len;
|
|
|
|
memcpy(dest->dig, src->dig, src->len * sizeof(mpz_dig_t));
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
void mpz_set_from_int(mpz_t *z, mp_int_t val) {
|
2015-01-09 20:58:58 +00:00
|
|
|
if (val == 0) {
|
|
|
|
z->len = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
mpz_need_dig(z, MPZ_NUM_DIG_FOR_INT);
|
2014-02-22 19:25:23 +00:00
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_uint_t uval;
|
2014-03-12 15:36:06 +00:00
|
|
|
if (val < 0) {
|
|
|
|
z->neg = 1;
|
|
|
|
uval = -val;
|
|
|
|
} else {
|
|
|
|
z->neg = 0;
|
|
|
|
uval = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
z->len = 0;
|
|
|
|
while (uval > 0) {
|
|
|
|
z->dig[z->len++] = uval & DIG_MASK;
|
|
|
|
uval >>= DIG_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 22:10:33 +01:00
|
|
|
void mpz_set_from_ll(mpz_t *z, long long val, bool is_signed) {
|
2014-03-12 15:36:06 +00:00
|
|
|
mpz_need_dig(z, MPZ_NUM_DIG_FOR_LL);
|
|
|
|
|
|
|
|
unsigned long long uval;
|
2014-09-10 22:10:33 +01:00
|
|
|
if (is_signed && val < 0) {
|
2014-02-22 19:25:23 +00:00
|
|
|
z->neg = 1;
|
2014-03-12 15:36:06 +00:00
|
|
|
uval = -val;
|
2014-02-22 19:25:23 +00:00
|
|
|
} else {
|
|
|
|
z->neg = 0;
|
2014-03-12 15:36:06 +00:00
|
|
|
uval = val;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
z->len = 0;
|
2014-03-12 15:36:06 +00:00
|
|
|
while (uval > 0) {
|
|
|
|
z->dig[z->len++] = uval & DIG_MASK;
|
|
|
|
uval >>= DIG_SIZE;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 12:39:22 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
void mpz_set_from_float(mpz_t *z, mp_float_t src) {
|
|
|
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
|
|
|
|
typedef uint64_t mp_float_int_t;
|
2015-01-13 15:19:37 +00:00
|
|
|
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
2015-01-02 12:39:22 +00:00
|
|
|
typedef uint32_t mp_float_int_t;
|
|
|
|
#endif
|
|
|
|
union {
|
|
|
|
mp_float_t f;
|
2016-01-08 17:56:58 +00:00
|
|
|
#if MP_ENDIANNESS_LITTLE
|
2015-01-13 15:19:37 +00:00
|
|
|
struct { mp_float_int_t frc:MP_FLOAT_FRAC_BITS, exp:MP_FLOAT_EXP_BITS, sgn:1; } p;
|
2016-01-08 17:56:58 +00:00
|
|
|
#else
|
|
|
|
struct { mp_float_int_t sgn:1, exp:MP_FLOAT_EXP_BITS, frc:MP_FLOAT_FRAC_BITS; } p;
|
|
|
|
#endif
|
2015-01-02 12:39:22 +00:00
|
|
|
} u = {src};
|
|
|
|
|
|
|
|
z->neg = u.p.sgn;
|
|
|
|
if (u.p.exp == 0) {
|
|
|
|
// value == 0 || value < 1
|
2015-01-09 20:58:58 +00:00
|
|
|
mpz_set_from_int(z, 0);
|
2015-01-13 15:19:37 +00:00
|
|
|
} else if (u.p.exp == ((1 << MP_FLOAT_EXP_BITS) - 1)) {
|
2015-01-02 23:04:09 +00:00
|
|
|
// u.p.frc == 0 indicates inf, else NaN
|
|
|
|
// should be handled by caller
|
2015-01-09 20:58:58 +00:00
|
|
|
mpz_set_from_int(z, 0);
|
2015-01-02 12:39:22 +00:00
|
|
|
} else {
|
2015-01-13 15:19:37 +00:00
|
|
|
const int adj_exp = (int)u.p.exp - MP_FLOAT_EXP_BIAS;
|
2015-01-02 12:39:22 +00:00
|
|
|
if (adj_exp < 0) {
|
|
|
|
// value < 1 , truncates to 0
|
2015-01-09 20:58:58 +00:00
|
|
|
mpz_set_from_int(z, 0);
|
2015-01-02 12:39:22 +00:00
|
|
|
} else if (adj_exp == 0) {
|
|
|
|
// 1 <= value < 2 , so truncates to 1
|
2015-01-09 20:58:58 +00:00
|
|
|
mpz_set_from_int(z, 1);
|
2015-01-02 12:39:22 +00:00
|
|
|
} else {
|
|
|
|
// 2 <= value
|
|
|
|
const int dig_cnt = (adj_exp + 1 + (DIG_SIZE - 1)) / DIG_SIZE;
|
|
|
|
const unsigned int rem = adj_exp % DIG_SIZE;
|
|
|
|
int dig_ind, shft;
|
2015-01-13 15:19:37 +00:00
|
|
|
mp_float_int_t frc = u.p.frc | ((mp_float_int_t)1 << MP_FLOAT_FRAC_BITS);
|
2015-01-02 12:39:22 +00:00
|
|
|
|
2015-01-13 15:19:37 +00:00
|
|
|
if (adj_exp < MP_FLOAT_FRAC_BITS) {
|
2015-01-02 12:39:22 +00:00
|
|
|
shft = 0;
|
|
|
|
dig_ind = 0;
|
2015-01-13 15:19:37 +00:00
|
|
|
frc >>= MP_FLOAT_FRAC_BITS - adj_exp;
|
2015-01-02 12:39:22 +00:00
|
|
|
} else {
|
2015-01-13 15:19:37 +00:00
|
|
|
shft = (rem - MP_FLOAT_FRAC_BITS) % DIG_SIZE;
|
|
|
|
dig_ind = (adj_exp - MP_FLOAT_FRAC_BITS) / DIG_SIZE;
|
2015-01-02 12:39:22 +00:00
|
|
|
}
|
|
|
|
mpz_need_dig(z, dig_cnt);
|
|
|
|
z->len = dig_cnt;
|
|
|
|
if (dig_ind != 0) {
|
|
|
|
memset(z->dig, 0, dig_ind * sizeof(mpz_dig_t));
|
|
|
|
}
|
|
|
|
if (shft != 0) {
|
|
|
|
z->dig[dig_ind++] = (frc << shft) & DIG_MASK;
|
|
|
|
frc >>= DIG_SIZE - shft;
|
|
|
|
}
|
2015-01-13 15:20:32 +00:00
|
|
|
#if DIG_SIZE < (MP_FLOAT_FRAC_BITS + 1)
|
2015-01-02 12:39:22 +00:00
|
|
|
while (dig_ind != dig_cnt) {
|
|
|
|
z->dig[dig_ind++] = frc & DIG_MASK;
|
|
|
|
frc >>= DIG_SIZE;
|
|
|
|
}
|
2015-01-13 15:20:32 +00:00
|
|
|
#else
|
|
|
|
if (dig_ind != dig_cnt) {
|
|
|
|
z->dig[dig_ind] = frc;
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-02 12:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
// returns number of bytes from str that were processed
|
2014-09-05 20:37:06 +01:00
|
|
|
mp_uint_t mpz_set_from_str(mpz_t *z, const char *str, mp_uint_t len, bool neg, mp_uint_t base) {
|
2016-12-28 01:04:19 +00:00
|
|
|
assert(base <= 36);
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
const char *cur = str;
|
|
|
|
const char *top = str + len;
|
|
|
|
|
|
|
|
mpz_need_dig(z, len * 8 / DIG_SIZE + 1);
|
|
|
|
|
|
|
|
if (neg) {
|
|
|
|
z->neg = 1;
|
|
|
|
} else {
|
|
|
|
z->neg = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
z->len = 0;
|
|
|
|
for (; cur < top; ++cur) { // XXX UTF8 next char
|
2014-09-05 20:37:06 +01:00
|
|
|
//mp_uint_t v = char_to_numeric(cur#); // XXX UTF8 get char
|
|
|
|
mp_uint_t v = *cur;
|
2014-02-22 19:25:23 +00:00
|
|
|
if ('0' <= v && v <= '9') {
|
|
|
|
v -= '0';
|
|
|
|
} else if ('A' <= v && v <= 'Z') {
|
|
|
|
v -= 'A' - 10;
|
|
|
|
} else if ('a' <= v && v <= 'z') {
|
|
|
|
v -= 'a' - 10;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (v >= base) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
z->len = mpn_mul_dig_add_dig(z->dig, z->len, base, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cur - str;
|
|
|
|
}
|
|
|
|
|
2017-01-21 17:07:50 +00:00
|
|
|
void mpz_set_from_bytes(mpz_t *z, bool big_endian, mp_uint_t len, const byte *buf) {
|
|
|
|
int delta = 1;
|
|
|
|
if (big_endian) {
|
|
|
|
buf += len - 1;
|
|
|
|
delta = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpz_need_dig(z, (len * 8 + DIG_SIZE - 1) / DIG_SIZE);
|
|
|
|
|
|
|
|
mpz_dig_t d = 0;
|
|
|
|
int num_bits = 0;
|
|
|
|
z->neg = 0;
|
|
|
|
z->len = 0;
|
|
|
|
while (len) {
|
|
|
|
while (len && num_bits < DIG_SIZE) {
|
|
|
|
d |= *buf << num_bits;
|
|
|
|
num_bits += 8;
|
|
|
|
buf += delta;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
z->dig[z->len++] = d & DIG_MASK;
|
|
|
|
// Need this #if because it's C undefined behavior to do: uint32_t >> 32
|
|
|
|
#if DIG_SIZE != 8 && DIG_SIZE != 16 && DIG_SIZE != 32
|
|
|
|
d >>= DIG_SIZE;
|
|
|
|
#else
|
|
|
|
d = 0;
|
|
|
|
#endif
|
|
|
|
num_bits -= DIG_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
bool mpz_is_zero(const mpz_t *z) {
|
|
|
|
return z->len == 0;
|
|
|
|
}
|
|
|
|
|
2015-03-02 12:58:06 +00:00
|
|
|
#if 0
|
|
|
|
these functions are unused
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
bool mpz_is_pos(const mpz_t *z) {
|
|
|
|
return z->len > 0 && z->neg == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpz_is_neg(const mpz_t *z) {
|
|
|
|
return z->len > 0 && z->neg != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpz_is_odd(const mpz_t *z) {
|
|
|
|
return z->len > 0 && (z->dig[0] & 1) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpz_is_even(const mpz_t *z) {
|
|
|
|
return z->len == 0 || (z->dig[0] & 1) == 0;
|
|
|
|
}
|
2015-03-02 12:58:06 +00:00
|
|
|
#endif
|
2014-02-22 19:25:23 +00:00
|
|
|
|
2014-10-03 18:44:14 +01:00
|
|
|
int mpz_cmp(const mpz_t *z1, const mpz_t *z2) {
|
2015-01-27 17:47:38 +00:00
|
|
|
// to catch comparison of -0 with +0
|
|
|
|
if (z1->len == 0 && z2->len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-03 18:44:14 +01:00
|
|
|
int cmp = (int)z2->neg - (int)z1->neg;
|
2014-02-22 19:25:23 +00:00
|
|
|
if (cmp != 0) {
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
cmp = mpn_cmp(z1->dig, z1->len, z2->dig, z2->len);
|
|
|
|
if (z1->neg != 0) {
|
|
|
|
cmp = -cmp;
|
|
|
|
}
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
#if 0
|
|
|
|
// obsolete
|
|
|
|
// compares mpz with an integer that fits within DIG_SIZE bits
|
2014-09-05 20:37:06 +01:00
|
|
|
mp_int_t mpz_cmp_sml_int(const mpz_t *z, mp_int_t sml_int) {
|
|
|
|
mp_int_t cmp;
|
2014-02-22 19:25:23 +00:00
|
|
|
if (z->neg == 0) {
|
|
|
|
if (sml_int < 0) return 1;
|
|
|
|
if (sml_int == 0) {
|
|
|
|
if (z->len == 0) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (z->len == 0) return -1;
|
|
|
|
assert(sml_int < (1 << DIG_SIZE));
|
|
|
|
if (z->len != 1) return 1;
|
|
|
|
cmp = z->dig[0] - sml_int;
|
|
|
|
} else {
|
|
|
|
if (sml_int > 0) return -1;
|
|
|
|
if (sml_int == 0) {
|
|
|
|
if (z->len == 0) return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (z->len == 0) return 1;
|
|
|
|
assert(sml_int > -(1 << DIG_SIZE));
|
|
|
|
if (z->len != 1) return -1;
|
|
|
|
cmp = -z->dig[0] - sml_int;
|
|
|
|
}
|
|
|
|
if (cmp < 0) return -1;
|
|
|
|
if (cmp > 0) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-01 19:50:50 +00:00
|
|
|
#endif
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
these functions are unused
|
|
|
|
|
|
|
|
/* returns abs(z)
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_abs(const mpz_t *z) {
|
|
|
|
mpz_t *z2 = mpz_clone(z);
|
|
|
|
z2->neg = 0;
|
|
|
|
return z2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns -z
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_neg(const mpz_t *z) {
|
|
|
|
mpz_t *z2 = mpz_clone(z);
|
|
|
|
z2->neg = 1 - z2->neg;
|
|
|
|
return z2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns lhs + rhs
|
|
|
|
can have lhs, rhs the same
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_add(const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
mpz_t *z = mpz_zero();
|
|
|
|
mpz_add_inpl(z, lhs, rhs);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns lhs - rhs
|
|
|
|
can have lhs, rhs the same
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_sub(const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
mpz_t *z = mpz_zero();
|
|
|
|
mpz_sub_inpl(z, lhs, rhs);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns lhs * rhs
|
|
|
|
can have lhs, rhs the same
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_mul(const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
mpz_t *z = mpz_zero();
|
|
|
|
mpz_mul_inpl(z, lhs, rhs);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns lhs ** rhs
|
|
|
|
can have lhs, rhs the same
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_pow(const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
mpz_t *z = mpz_zero();
|
|
|
|
mpz_pow_inpl(z, lhs, rhs);
|
|
|
|
return z;
|
|
|
|
}
|
2015-03-02 12:58:06 +00:00
|
|
|
|
|
|
|
/* computes new integers in quo and rem such that:
|
|
|
|
quo * rhs + rem = lhs
|
|
|
|
0 <= rem < rhs
|
|
|
|
can have lhs, rhs the same
|
|
|
|
*/
|
|
|
|
void mpz_divmod(const mpz_t *lhs, const mpz_t *rhs, mpz_t **quo, mpz_t **rem) {
|
|
|
|
*quo = mpz_zero();
|
|
|
|
*rem = mpz_zero();
|
|
|
|
mpz_divmod_inpl(*quo, *rem, lhs, rhs);
|
|
|
|
}
|
2014-02-22 19:25:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* computes dest = abs(z)
|
|
|
|
can have dest, z the same
|
|
|
|
*/
|
|
|
|
void mpz_abs_inpl(mpz_t *dest, const mpz_t *z) {
|
|
|
|
if (dest != z) {
|
|
|
|
mpz_set(dest, z);
|
|
|
|
}
|
|
|
|
dest->neg = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes dest = -z
|
|
|
|
can have dest, z the same
|
|
|
|
*/
|
|
|
|
void mpz_neg_inpl(mpz_t *dest, const mpz_t *z) {
|
|
|
|
if (dest != z) {
|
|
|
|
mpz_set(dest, z);
|
|
|
|
}
|
|
|
|
dest->neg = 1 - dest->neg;
|
|
|
|
}
|
|
|
|
|
2014-03-01 19:50:50 +00:00
|
|
|
/* computes dest = ~z (= -z - 1)
|
|
|
|
can have dest, z the same
|
|
|
|
*/
|
|
|
|
void mpz_not_inpl(mpz_t *dest, const mpz_t *z) {
|
|
|
|
if (dest != z) {
|
|
|
|
mpz_set(dest, z);
|
|
|
|
}
|
2014-12-31 19:35:01 +00:00
|
|
|
if (dest->len == 0) {
|
|
|
|
mpz_need_dig(dest, 1);
|
|
|
|
dest->dig[0] = 1;
|
|
|
|
dest->len = 1;
|
|
|
|
dest->neg = 1;
|
|
|
|
} else if (dest->neg) {
|
2014-03-01 19:50:50 +00:00
|
|
|
dest->neg = 0;
|
|
|
|
mpz_dig_t k = 1;
|
|
|
|
dest->len = mpn_sub(dest->dig, dest->dig, dest->len, &k, 1);
|
|
|
|
} else {
|
2014-12-31 19:35:01 +00:00
|
|
|
mpz_need_dig(dest, dest->len + 1);
|
2014-03-01 19:50:50 +00:00
|
|
|
mpz_dig_t k = 1;
|
|
|
|
dest->len = mpn_add(dest->dig, dest->dig, dest->len, &k, 1);
|
|
|
|
dest->neg = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 16:56:30 +00:00
|
|
|
/* computes dest = lhs << rhs
|
|
|
|
can have dest, lhs the same
|
|
|
|
*/
|
2015-10-01 18:01:37 +01:00
|
|
|
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs) {
|
2014-03-01 19:50:50 +00:00
|
|
|
if (lhs->len == 0 || rhs == 0) {
|
2014-02-26 16:56:30 +00:00
|
|
|
mpz_set(dest, lhs);
|
|
|
|
} else {
|
2014-03-01 19:50:50 +00:00
|
|
|
mpz_need_dig(dest, lhs->len + (rhs + DIG_SIZE - 1) / DIG_SIZE);
|
|
|
|
dest->len = mpn_shl(dest->dig, lhs->dig, lhs->len, rhs);
|
|
|
|
dest->neg = lhs->neg;
|
2014-02-26 16:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes dest = lhs >> rhs
|
|
|
|
can have dest, lhs the same
|
|
|
|
*/
|
2015-10-01 18:01:37 +01:00
|
|
|
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs) {
|
2014-03-01 19:50:50 +00:00
|
|
|
if (lhs->len == 0 || rhs == 0) {
|
2014-02-26 16:56:30 +00:00
|
|
|
mpz_set(dest, lhs);
|
|
|
|
} else {
|
2014-03-01 19:50:50 +00:00
|
|
|
mpz_need_dig(dest, lhs->len);
|
|
|
|
dest->len = mpn_shr(dest->dig, lhs->dig, lhs->len, rhs);
|
|
|
|
dest->neg = lhs->neg;
|
|
|
|
if (dest->neg) {
|
|
|
|
// arithmetic shift right, rounding to negative infinity
|
2014-09-05 20:37:06 +01:00
|
|
|
mp_uint_t n_whole = rhs / DIG_SIZE;
|
|
|
|
mp_uint_t n_part = rhs % DIG_SIZE;
|
2014-03-01 19:50:50 +00:00
|
|
|
mpz_dig_t round_up = 0;
|
2014-09-05 20:37:06 +01:00
|
|
|
for (mp_uint_t i = 0; i < lhs->len && i < n_whole; i++) {
|
2014-03-01 19:50:50 +00:00
|
|
|
if (lhs->dig[i] != 0) {
|
|
|
|
round_up = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n_whole < lhs->len && (lhs->dig[n_whole] & ((1 << n_part) - 1)) != 0) {
|
|
|
|
round_up = 1;
|
|
|
|
}
|
|
|
|
if (round_up) {
|
2014-12-31 19:35:01 +00:00
|
|
|
if (dest->len == 0) {
|
|
|
|
// dest == 0, so need to add 1 by hand (answer will be -1)
|
|
|
|
dest->dig[0] = 1;
|
|
|
|
dest->len = 1;
|
|
|
|
} else {
|
|
|
|
// dest > 0, so can use mpn_add to add 1
|
|
|
|
dest->len = mpn_add(dest->dig, dest->dig, dest->len, &round_up, 1);
|
|
|
|
}
|
2014-03-01 19:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 16:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
/* computes dest = lhs + rhs
|
|
|
|
can have dest, lhs, rhs the same
|
|
|
|
*/
|
|
|
|
void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
if (mpn_cmp(lhs->dig, lhs->len, rhs->dig, rhs->len) < 0) {
|
|
|
|
const mpz_t *temp = lhs;
|
|
|
|
lhs = rhs;
|
|
|
|
rhs = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs->neg == rhs->neg) {
|
|
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
|
|
dest->len = mpn_add(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
|
|
} else {
|
|
|
|
mpz_need_dig(dest, lhs->len);
|
|
|
|
dest->len = mpn_sub(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->neg = lhs->neg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes dest = lhs - rhs
|
|
|
|
can have dest, lhs, rhs the same
|
|
|
|
*/
|
|
|
|
void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
bool neg = false;
|
|
|
|
|
|
|
|
if (mpn_cmp(lhs->dig, lhs->len, rhs->dig, rhs->len) < 0) {
|
|
|
|
const mpz_t *temp = lhs;
|
|
|
|
lhs = rhs;
|
|
|
|
rhs = temp;
|
|
|
|
neg = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs->neg != rhs->neg) {
|
|
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
|
|
dest->len = mpn_add(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
|
|
} else {
|
|
|
|
mpz_need_dig(dest, lhs->len);
|
|
|
|
dest->len = mpn_sub(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (neg) {
|
|
|
|
dest->neg = 1 - lhs->neg;
|
|
|
|
} else {
|
|
|
|
dest->neg = lhs->neg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 23:52:36 +00:00
|
|
|
/* computes dest = lhs & rhs
|
|
|
|
can have dest, lhs, rhs the same
|
|
|
|
*/
|
|
|
|
void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
2016-01-31 03:35:58 +00:00
|
|
|
// make sure lhs has the most digits
|
|
|
|
if (lhs->len < rhs->len) {
|
|
|
|
const mpz_t *temp = lhs;
|
|
|
|
lhs = rhs;
|
|
|
|
rhs = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
|
|
|
|
if ((0 == lhs->neg) && (0 == rhs->neg)) {
|
|
|
|
mpz_need_dig(dest, lhs->len);
|
|
|
|
dest->len = mpn_and(dest->dig, lhs->dig, rhs->dig, rhs->len);
|
|
|
|
dest->neg = 0;
|
2014-03-22 23:52:36 +00:00
|
|
|
} else {
|
2014-05-29 15:01:49 +01:00
|
|
|
mpz_need_dig(dest, lhs->len + 1);
|
2016-01-31 03:35:58 +00:00
|
|
|
dest->len = mpn_and_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
|
|
lhs->neg == rhs->neg, 0 != lhs->neg, 0 != rhs->neg);
|
|
|
|
dest->neg = lhs->neg & rhs->neg;
|
2014-03-22 23:52:36 +00:00
|
|
|
}
|
2016-01-31 03:35:58 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
|
|
|
|
dest->len = mpn_and_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
|
|
(lhs->neg == rhs->neg) ? lhs->neg : 0, lhs->neg, rhs->neg);
|
|
|
|
dest->neg = lhs->neg & rhs->neg;
|
|
|
|
|
|
|
|
#endif
|
2014-03-22 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* computes dest = lhs | rhs
|
|
|
|
can have dest, lhs, rhs the same
|
|
|
|
*/
|
|
|
|
void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
2016-01-31 03:35:58 +00:00
|
|
|
// make sure lhs has the most digits
|
|
|
|
if (lhs->len < rhs->len) {
|
2014-03-22 23:52:36 +00:00
|
|
|
const mpz_t *temp = lhs;
|
|
|
|
lhs = rhs;
|
|
|
|
rhs = temp;
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
|
|
|
|
if ((0 == lhs->neg) && (0 == rhs->neg)) {
|
2014-03-22 23:52:36 +00:00
|
|
|
mpz_need_dig(dest, lhs->len);
|
|
|
|
dest->len = mpn_or(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
2016-01-31 03:35:58 +00:00
|
|
|
dest->neg = 0;
|
2014-03-22 23:52:36 +00:00
|
|
|
} else {
|
2016-01-31 03:35:58 +00:00
|
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
|
|
dest->len = mpn_or_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
|
|
0 != lhs->neg, 0 != rhs->neg);
|
|
|
|
dest->neg = 1;
|
2014-03-22 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
|
|
|
|
dest->len = mpn_or_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
|
|
(lhs->neg || rhs->neg), lhs->neg, rhs->neg);
|
|
|
|
dest->neg = lhs->neg | rhs->neg;
|
|
|
|
|
|
|
|
#endif
|
2014-03-22 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* computes dest = lhs ^ rhs
|
|
|
|
can have dest, lhs, rhs the same
|
|
|
|
*/
|
|
|
|
void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
2016-01-31 03:35:58 +00:00
|
|
|
// make sure lhs has the most digits
|
|
|
|
if (lhs->len < rhs->len) {
|
2014-03-22 23:52:36 +00:00
|
|
|
const mpz_t *temp = lhs;
|
|
|
|
lhs = rhs;
|
|
|
|
rhs = temp;
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
|
2014-03-22 23:52:36 +00:00
|
|
|
if (lhs->neg == rhs->neg) {
|
|
|
|
mpz_need_dig(dest, lhs->len);
|
2016-01-31 03:35:58 +00:00
|
|
|
if (lhs->neg == 0) {
|
|
|
|
dest->len = mpn_xor(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
|
|
} else {
|
|
|
|
dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len, 0, 0, 0);
|
|
|
|
}
|
|
|
|
dest->neg = 0;
|
2014-03-22 23:52:36 +00:00
|
|
|
} else {
|
2016-01-31 03:35:58 +00:00
|
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
|
|
dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len, 1,
|
|
|
|
0 == lhs->neg, 0 == rhs->neg);
|
|
|
|
dest->neg = 1;
|
2014-03-22 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:35:58 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
|
|
|
|
dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
|
|
(lhs->neg != rhs->neg), 0 == lhs->neg, 0 == rhs->neg);
|
|
|
|
dest->neg = lhs->neg ^ rhs->neg;
|
|
|
|
|
|
|
|
#endif
|
2014-03-22 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
/* computes dest = lhs * rhs
|
|
|
|
can have dest, lhs, rhs the same
|
|
|
|
*/
|
2015-04-09 16:29:54 +01:00
|
|
|
void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
2014-02-22 19:25:23 +00:00
|
|
|
if (lhs->len == 0 || rhs->len == 0) {
|
2014-02-26 16:56:30 +00:00
|
|
|
mpz_set_from_int(dest, 0);
|
|
|
|
return;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mpz_t *temp = NULL;
|
|
|
|
if (lhs == dest) {
|
|
|
|
lhs = temp = mpz_clone(lhs);
|
|
|
|
if (rhs == dest) {
|
|
|
|
rhs = lhs;
|
|
|
|
}
|
|
|
|
} else if (rhs == dest) {
|
|
|
|
rhs = temp = mpz_clone(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
mpz_need_dig(dest, lhs->len + rhs->len); // min mem l+r-1, max mem l+r
|
|
|
|
memset(dest->dig, 0, dest->alloc * sizeof(mpz_dig_t));
|
|
|
|
dest->len = mpn_mul(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
|
|
|
|
|
|
if (lhs->neg == rhs->neg) {
|
|
|
|
dest->neg = 0;
|
|
|
|
} else {
|
|
|
|
dest->neg = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpz_free(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes dest = lhs ** rhs
|
|
|
|
can have dest, lhs, rhs the same
|
|
|
|
*/
|
|
|
|
void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
if (lhs->len == 0 || rhs->neg != 0) {
|
2014-02-26 16:56:30 +00:00
|
|
|
mpz_set_from_int(dest, 0);
|
|
|
|
return;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rhs->len == 0) {
|
2014-02-26 16:56:30 +00:00
|
|
|
mpz_set_from_int(dest, 1);
|
|
|
|
return;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mpz_t *x = mpz_clone(lhs);
|
|
|
|
mpz_t *n = mpz_clone(rhs);
|
|
|
|
|
|
|
|
mpz_set_from_int(dest, 1);
|
|
|
|
|
|
|
|
while (n->len > 0) {
|
2015-03-02 12:58:06 +00:00
|
|
|
if ((n->dig[0] & 1) != 0) {
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_mul_inpl(dest, dest, x);
|
|
|
|
}
|
|
|
|
n->len = mpn_shr(n->dig, n->dig, n->len, 1);
|
2014-04-04 00:16:32 +01:00
|
|
|
if (n->len == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mpz_mul_inpl(x, x, x);
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mpz_free(x);
|
|
|
|
mpz_free(n);
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:30:49 +00:00
|
|
|
/* computes dest = (lhs ** rhs) % mod
|
|
|
|
can have dest, lhs, rhs the same; mod can't be the same as dest
|
|
|
|
*/
|
|
|
|
void mpz_pow3_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs, const mpz_t *mod) {
|
|
|
|
if (lhs->len == 0 || rhs->neg != 0) {
|
|
|
|
mpz_set_from_int(dest, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rhs->len == 0) {
|
|
|
|
mpz_set_from_int(dest, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpz_t *x = mpz_clone(lhs);
|
|
|
|
mpz_t *n = mpz_clone(rhs);
|
|
|
|
mpz_t quo; mpz_init_zero(&quo);
|
|
|
|
|
|
|
|
mpz_set_from_int(dest, 1);
|
|
|
|
|
|
|
|
while (n->len > 0) {
|
|
|
|
if ((n->dig[0] & 1) != 0) {
|
|
|
|
mpz_mul_inpl(dest, dest, x);
|
|
|
|
mpz_divmod_inpl(&quo, dest, dest, mod);
|
|
|
|
}
|
|
|
|
n->len = mpn_shr(n->dig, n->dig, n->len, 1);
|
|
|
|
if (n->len == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mpz_mul_inpl(x, x, x);
|
|
|
|
mpz_divmod_inpl(&quo, x, x, mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
mpz_deinit(&quo);
|
|
|
|
mpz_free(x);
|
|
|
|
mpz_free(n);
|
|
|
|
}
|
|
|
|
|
2017-02-01 23:41:22 +00:00
|
|
|
#if 0
|
|
|
|
these functions are unused
|
|
|
|
|
2014-02-22 19:25:23 +00:00
|
|
|
/* computes gcd(z1, z2)
|
|
|
|
based on Knuth's modified gcd algorithm (I think?)
|
|
|
|
gcd(z1, z2) >= 0
|
|
|
|
gcd(0, 0) = 0
|
|
|
|
gcd(z, 0) = abs(z)
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2) {
|
|
|
|
if (z1->len == 0) {
|
|
|
|
mpz_t *a = mpz_clone(z2);
|
|
|
|
a->neg = 0;
|
|
|
|
return a;
|
|
|
|
} else if (z2->len == 0) {
|
|
|
|
mpz_t *a = mpz_clone(z1);
|
|
|
|
a->neg = 0;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpz_t *a = mpz_clone(z1);
|
|
|
|
mpz_t *b = mpz_clone(z2);
|
|
|
|
mpz_t c; mpz_init_zero(&c);
|
|
|
|
a->neg = 0;
|
|
|
|
b->neg = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (mpz_cmp(a, b) < 0) {
|
|
|
|
if (a->len == 0) {
|
|
|
|
mpz_free(a);
|
|
|
|
mpz_deinit(&c);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
mpz_t *t = a; a = b; b = t;
|
|
|
|
}
|
|
|
|
if (!(b->len >= 2 || (b->len == 1 && b->dig[0] > 1))) { // compute b > 0; could be mpz_cmp_small_int(b, 1) > 0
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mpz_set(&c, b);
|
|
|
|
do {
|
|
|
|
mpz_add_inpl(&c, &c, &c);
|
|
|
|
} while (mpz_cmp(&c, a) <= 0);
|
|
|
|
c.len = mpn_shr(c.dig, c.dig, c.len, 1);
|
|
|
|
mpz_sub_inpl(a, a, &c);
|
|
|
|
}
|
|
|
|
|
|
|
|
mpz_deinit(&c);
|
|
|
|
|
|
|
|
if (b->len == 1 && b->dig[0] == 1) { // compute b == 1; could be mpz_cmp_small_int(b, 1) == 0
|
|
|
|
mpz_free(a);
|
|
|
|
return b;
|
|
|
|
} else {
|
|
|
|
mpz_free(b);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes lcm(z1, z2)
|
|
|
|
= abs(z1) / gcd(z1, z2) * abs(z2)
|
|
|
|
lcm(z1, z1) >= 0
|
|
|
|
lcm(0, 0) = 0
|
|
|
|
lcm(z, 0) = 0
|
|
|
|
*/
|
2014-08-07 15:27:48 +01:00
|
|
|
mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2) {
|
|
|
|
if (z1->len == 0 || z2->len == 0) {
|
|
|
|
return mpz_zero();
|
2014-05-05 11:18:27 +01:00
|
|
|
}
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
mpz_t *gcd = mpz_gcd(z1, z2);
|
|
|
|
mpz_t *quo = mpz_zero();
|
|
|
|
mpz_t *rem = mpz_zero();
|
|
|
|
mpz_divmod_inpl(quo, rem, z1, gcd);
|
|
|
|
mpz_mul_inpl(rem, quo, z2);
|
|
|
|
mpz_free(gcd);
|
|
|
|
mpz_free(quo);
|
|
|
|
rem->neg = 0;
|
|
|
|
return rem;
|
|
|
|
}
|
2015-03-02 12:58:06 +00:00
|
|
|
#endif
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
/* computes new integers in quo and rem such that:
|
|
|
|
quo * rhs + rem = lhs
|
|
|
|
0 <= rem < rhs
|
|
|
|
can have lhs, rhs the same
|
2016-10-11 03:00:01 +01:00
|
|
|
assumes rhs != 0 (undefined behaviour if it is)
|
2014-02-22 19:25:23 +00:00
|
|
|
*/
|
|
|
|
void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs) {
|
2016-10-11 03:00:01 +01:00
|
|
|
assert(!mpz_is_zero(rhs));
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
mpz_need_dig(dest_quo, lhs->len + 1); // +1 necessary?
|
|
|
|
memset(dest_quo->dig, 0, (lhs->len + 1) * sizeof(mpz_dig_t));
|
|
|
|
dest_quo->len = 0;
|
|
|
|
mpz_need_dig(dest_rem, lhs->len + 1); // +1 necessary?
|
|
|
|
mpz_set(dest_rem, lhs);
|
|
|
|
mpn_div(dest_rem->dig, &dest_rem->len, rhs->dig, rhs->len, dest_quo->dig, &dest_quo->len);
|
|
|
|
|
2016-05-08 22:21:21 +01:00
|
|
|
// check signs and do Python style modulo
|
2014-02-22 19:25:23 +00:00
|
|
|
if (lhs->neg != rhs->neg) {
|
|
|
|
dest_quo->neg = 1;
|
2016-05-08 22:21:21 +01:00
|
|
|
if (!mpz_is_zero(dest_rem)) {
|
|
|
|
mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
|
|
|
|
mpz_add_inpl(dest_quo, dest_quo, &mpzone);
|
|
|
|
mpz_add_inpl(dest_rem, dest_rem, rhs);
|
|
|
|
}
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
these functions are unused
|
|
|
|
|
|
|
|
/* computes floor(lhs / rhs)
|
|
|
|
can have lhs, rhs the same
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_div(const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
mpz_t *quo = mpz_zero();
|
|
|
|
mpz_t rem; mpz_init_zero(&rem);
|
|
|
|
mpz_divmod_inpl(quo, &rem, lhs, rhs);
|
|
|
|
mpz_deinit(&rem);
|
|
|
|
return quo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computes lhs % rhs ( >= 0)
|
|
|
|
can have lhs, rhs the same
|
|
|
|
*/
|
|
|
|
mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) {
|
|
|
|
mpz_t quo; mpz_init_zero(&quo);
|
|
|
|
mpz_t *rem = mpz_zero();
|
|
|
|
mpz_divmod_inpl(&quo, rem, lhs, rhs);
|
|
|
|
mpz_deinit(&quo);
|
|
|
|
return rem;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-24 14:21:37 +01:00
|
|
|
// must return actual int value if it fits in mp_int_t
|
|
|
|
mp_int_t mpz_hash(const mpz_t *z) {
|
|
|
|
mp_int_t val = 0;
|
|
|
|
mpz_dig_t *d = z->dig + z->len;
|
|
|
|
|
2015-01-09 20:58:58 +00:00
|
|
|
while (d-- > z->dig) {
|
2014-07-24 14:21:37 +01:00
|
|
|
val = (val << DIG_SIZE) | *d;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (z->neg != 0) {
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:41:43 +01:00
|
|
|
bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value) {
|
2015-01-16 17:47:07 +00:00
|
|
|
mp_uint_t val = 0;
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dig_t *d = i->dig + i->len;
|
|
|
|
|
2015-01-09 20:58:58 +00:00
|
|
|
while (d-- > i->dig) {
|
2014-07-31 14:41:43 +01:00
|
|
|
if (val > (~(WORD_MSBIT_HIGH) >> DIG_SIZE)) {
|
|
|
|
// will overflow
|
|
|
|
return false;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
2014-07-31 14:41:43 +01:00
|
|
|
val = (val << DIG_SIZE) | *d;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i->neg != 0) {
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:41:43 +01:00
|
|
|
*value = val;
|
|
|
|
return true;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:41:43 +01:00
|
|
|
bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value) {
|
|
|
|
if (i->neg != 0) {
|
|
|
|
// can't represent signed values
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_uint_t val = 0;
|
2014-04-03 12:00:54 +01:00
|
|
|
mpz_dig_t *d = i->dig + i->len;
|
|
|
|
|
2015-01-09 20:58:58 +00:00
|
|
|
while (d-- > i->dig) {
|
2014-09-06 17:15:34 +01:00
|
|
|
if (val > (~(WORD_MSBIT_HIGH) >> (DIG_SIZE - 1))) {
|
2014-07-31 14:41:43 +01:00
|
|
|
// will overflow
|
2014-04-03 12:00:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-31 14:41:43 +01:00
|
|
|
val = (val << DIG_SIZE) | *d;
|
2014-04-03 12:00:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*value = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-25 23:16:39 +01:00
|
|
|
// writes at most len bytes to buf (so buf should be zeroed before calling)
|
|
|
|
void mpz_as_bytes(const mpz_t *z, bool big_endian, mp_uint_t len, byte *buf) {
|
|
|
|
byte *b = buf;
|
|
|
|
if (big_endian) {
|
|
|
|
b += len;
|
|
|
|
}
|
|
|
|
mpz_dig_t *zdig = z->dig;
|
|
|
|
int bits = 0;
|
|
|
|
mpz_dbl_dig_t d = 0;
|
|
|
|
mpz_dbl_dig_t carry = 1;
|
|
|
|
for (mp_uint_t zlen = z->len; zlen > 0; --zlen) {
|
|
|
|
bits += DIG_SIZE;
|
|
|
|
d = (d << DIG_SIZE) | *zdig++;
|
|
|
|
for (; bits >= 8; bits -= 8, d >>= 8) {
|
|
|
|
mpz_dig_t val = d;
|
|
|
|
if (z->neg) {
|
2015-04-25 23:51:14 +01:00
|
|
|
val = (~val & 0xff) + carry;
|
|
|
|
carry = val >> 8;
|
2015-04-25 23:16:39 +01:00
|
|
|
}
|
|
|
|
if (big_endian) {
|
|
|
|
*--b = val;
|
|
|
|
if (b == buf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*b++ = val;
|
|
|
|
if (b == buf + len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-08 15:04:54 +00:00
|
|
|
mp_float_t mpz_as_float(const mpz_t *i) {
|
|
|
|
mp_float_t val = 0;
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dig_t *d = i->dig + i->len;
|
|
|
|
|
2015-01-09 20:58:58 +00:00
|
|
|
while (d-- > i->dig) {
|
2014-09-06 17:15:34 +01:00
|
|
|
val = val * DIG_BASE + *d;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i->neg != 0) {
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2014-03-08 15:04:54 +00:00
|
|
|
#endif
|
2014-02-22 19:25:23 +00:00
|
|
|
|
2014-09-05 20:37:06 +01:00
|
|
|
#if 0
|
|
|
|
this function is unused
|
|
|
|
char *mpz_as_str(const mpz_t *i, mp_uint_t base) {
|
|
|
|
char *s = m_new(char, mpz_as_str_size(i, base, NULL, '\0'));
|
|
|
|
mpz_as_str_inpl(i, base, NULL, 'a', '\0', s);
|
2014-02-22 19:25:23 +00:00
|
|
|
return s;
|
|
|
|
}
|
2014-09-05 20:37:06 +01:00
|
|
|
#endif
|
2014-02-22 19:25:23 +00:00
|
|
|
|
2016-10-11 03:11:32 +01:00
|
|
|
// assumes enough space as calculated by mp_int_format_size
|
2014-02-22 19:25:23 +00:00
|
|
|
// returns length of string, not including null byte
|
2014-09-05 20:37:06 +01:00
|
|
|
mp_uint_t mpz_as_str_inpl(const mpz_t *i, mp_uint_t base, const char *prefix, char base_char, char comma, char *str) {
|
2016-10-25 10:03:39 +01:00
|
|
|
if (str == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (base < 2 || base > 32) {
|
2014-02-22 19:25:23 +00:00
|
|
|
str[0] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-05 20:37:06 +01:00
|
|
|
mp_uint_t ilen = i->len;
|
2014-02-22 19:25:23 +00:00
|
|
|
|
2014-04-07 19:19:51 +01:00
|
|
|
char *s = str;
|
2014-02-22 19:25:23 +00:00
|
|
|
if (ilen == 0) {
|
2014-04-07 19:19:51 +01:00
|
|
|
if (prefix) {
|
|
|
|
while (*prefix)
|
|
|
|
*s++ = *prefix++;
|
|
|
|
}
|
|
|
|
*s++ = '0';
|
|
|
|
*s = '\0';
|
|
|
|
return s - str;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 23:11:00 +01:00
|
|
|
// make a copy of mpz digits, so we can do the div/mod calculation
|
2014-02-22 19:25:23 +00:00
|
|
|
mpz_dig_t *dig = m_new(mpz_dig_t, ilen);
|
|
|
|
memcpy(dig, i->dig, ilen * sizeof(mpz_dig_t));
|
|
|
|
|
|
|
|
// convert
|
2014-04-07 19:19:51 +01:00
|
|
|
char *last_comma = str;
|
2014-02-22 19:25:23 +00:00
|
|
|
bool done;
|
|
|
|
do {
|
|
|
|
mpz_dig_t *d = dig + ilen;
|
|
|
|
mpz_dbl_dig_t a = 0;
|
|
|
|
|
|
|
|
// compute next remainder
|
|
|
|
while (--d >= dig) {
|
|
|
|
a = (a << DIG_SIZE) | *d;
|
|
|
|
*d = a / base;
|
|
|
|
a %= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert to character
|
|
|
|
a += '0';
|
|
|
|
if (a > '9') {
|
2014-04-07 19:19:51 +01:00
|
|
|
a += base_char - '9' - 1;
|
2014-02-22 19:25:23 +00:00
|
|
|
}
|
|
|
|
*s++ = a;
|
|
|
|
|
|
|
|
// check if number is zero
|
|
|
|
done = true;
|
|
|
|
for (d = dig; d < dig + ilen; ++d) {
|
|
|
|
if (*d != 0) {
|
|
|
|
done = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-07 19:19:51 +01:00
|
|
|
if (comma && (s - last_comma) == 3) {
|
|
|
|
*s++ = comma;
|
|
|
|
last_comma = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!done);
|
2014-02-22 19:25:23 +00:00
|
|
|
|
2014-04-08 23:11:00 +01:00
|
|
|
// free the copy of the digits array
|
|
|
|
m_del(mpz_dig_t, dig, ilen);
|
|
|
|
|
2014-04-07 19:19:51 +01:00
|
|
|
if (prefix) {
|
|
|
|
const char *p = &prefix[strlen(prefix)];
|
|
|
|
while (p > prefix) {
|
|
|
|
*s++ = *--p;
|
|
|
|
}
|
|
|
|
}
|
2014-02-22 19:25:23 +00:00
|
|
|
if (i->neg != 0) {
|
|
|
|
*s++ = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse string
|
|
|
|
for (char *u = str, *v = s - 1; u < v; ++u, --v) {
|
|
|
|
char temp = *u;
|
|
|
|
*u = *v;
|
|
|
|
*v = temp;
|
|
|
|
}
|
|
|
|
|
2014-04-07 19:19:51 +01:00
|
|
|
*s = '\0'; // null termination
|
2014-02-22 19:25:23 +00:00
|
|
|
|
|
|
|
return s - str;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
|