aboutsummaryrefslogtreecommitdiff
path: root/py/objint_mpz.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-09-15 16:15:57 +0100
committerDamien George <damien.p.george@gmail.com>2015-09-15 16:15:57 +0100
commit8b4fb4fe140e9cf57fcfa258d0d2d6fe19090fc5 (patch)
treef37cb4277e50faa6d3116a7c9ebcb15395646caa /py/objint_mpz.c
parentb230a86d332376d15b61636111ce47c90a5ab54a (diff)
py/mpz: Fix calculation of max digit storage for mpz; fix sys.maxsize.
When creating constant mpz's, the length of the mpz must be exactly how many digits are used (not allocated) otherwise these numbers are not compatible with dynamically allocated numbers. Addresses issue #1448.
Diffstat (limited to 'py/objint_mpz.c')
-rw-r--r--py/objint_mpz.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 27f1ddbfc..69a81d2c3 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -44,23 +44,34 @@
#if MICROPY_PY_SYS_MAXSIZE
// Export value for sys.maxsize
#define DIG_MASK ((MPZ_LONG_1 << MPZ_DIG_SIZE) - 1)
-STATIC const mpz_dig_t maxsize_dig[MPZ_NUM_DIG_FOR_INT] = {
+STATIC const mpz_dig_t maxsize_dig[] = {
+ #define NUM_DIG 1
(MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) & DIG_MASK,
#if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) > DIG_MASK
- (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) & DIG_MASK,
- #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) > DIG_MASK
- (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) & DIG_MASK,
- (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) & DIG_MASK,
- (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 4) & DIG_MASK,
-// (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 5) & DIG_MASK,
- #endif
+ #undef NUM_DIG
+ #define NUM_DIG 2
+ (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) & DIG_MASK,
+ #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) > DIG_MASK
+ #undef NUM_DIG
+ #define NUM_DIG 3
+ (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) & DIG_MASK,
+ #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) > DIG_MASK
+ #undef NUM_DIG
+ #define NUM_DIG 4
+ (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) & DIG_MASK,
+ #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) > DIG_MASK
+ #error cannot encode MP_SSIZE_MAX as mpz
+ #endif
+ #endif
+ #endif
#endif
};
const mp_obj_int_t mp_maxsize_obj = {
{&mp_type_int},
- {.fixed_dig = 1, .len = MPZ_NUM_DIG_FOR_INT, .alloc = MPZ_NUM_DIG_FOR_INT, .dig = (mpz_dig_t*)maxsize_dig}
+ {.fixed_dig = 1, .len = NUM_DIG, .alloc = NUM_DIG, .dig = (mpz_dig_t*)maxsize_dig}
};
#undef DIG_MASK
+#undef NUM_DIG
#endif
STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {