Browse Source

mpz: Fix 64bit msvc build

msvc does not treat 1L a 64bit integer hence all occurences of shifting it left or right
result in undefined behaviour since the maximum allowed shift count for 32bit ints is 31.
Forcing the correct type explicitely, stored in MPZ_LONG_1, solves this.
pull/947/merge
stijn 10 years ago
committed by Damien George
parent
commit
0e557facb9
  1. 6
      py/mpz.c
  2. 8
      py/mpz.h
  3. 2
      py/objint_mpz.c

6
py/mpz.c

@ -37,9 +37,9 @@
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
#define DIG_SIZE (MPZ_DIG_SIZE)
#define DIG_MASK ((1L << DIG_SIZE) - 1)
#define DIG_MSB (1L << (DIG_SIZE - 1))
#define DIG_BASE (1L << DIG_SIZE)
#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)
/*
mpz is an arbitrary precision integer type with a public API.

8
py/mpz.h

@ -36,7 +36,7 @@
// depending on the machine, but it (and MPZ_DIG_SIZE) can be freely changed so
// long as the constraints mentioned above are met.
#if defined(__x86_64__)
#if defined(__x86_64__) || defined(_WIN64)
// 64-bit machine, using 32-bit storage for digits
typedef uint32_t mpz_dig_t;
typedef uint64_t mpz_dbl_dig_t;
@ -50,6 +50,12 @@ typedef int32_t mpz_dbl_dig_signed_t;
#define MPZ_DIG_SIZE (16)
#endif
#ifdef _WIN64
#define MPZ_LONG_1 1i64
#else
#define MPZ_LONG_1 1L
#endif
#define MPZ_NUM_DIG_FOR_INT (sizeof(mp_int_t) * 8 / MPZ_DIG_SIZE + 1)
#define MPZ_NUM_DIG_FOR_LL (sizeof(long long) * 8 / MPZ_DIG_SIZE + 1)

2
py/objint_mpz.c

@ -45,7 +45,7 @@
#if MICROPY_PY_SYS_MAXSIZE
// Export value for sys.maxsize
#define DIG_MASK ((1L << MPZ_DIG_SIZE) - 1)
#define DIG_MASK ((MPZ_LONG_1 << MPZ_DIG_SIZE) - 1)
STATIC const mpz_dig_t maxsize_dig[MPZ_NUM_DIG_FOR_INT] = {
(MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) & DIG_MASK,
#if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) > DIG_MASK

Loading…
Cancel
Save