From 6278c2bb9f4c7b86fb81f0bbecb26deecc314b0a Mon Sep 17 00:00:00 2001 From: Tsutomu IKEGAMI Date: Wed, 2 Jun 2021 11:57:55 +0900 Subject: [PATCH] Fix bit_length() method to work with zero-valued mpz integer. --- py/mpz.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/py/mpz.h b/py/mpz.h index b273cdcc62..275e0b9a62 100644 --- a/py/mpz.h +++ b/py/mpz.h @@ -142,6 +142,9 @@ static inline size_t mpz_max_num_bits(const mpz_t *z) { return z->len * MPZ_DIG_SIZE; } static inline size_t mpz_num_bits(const mpz_t *z) { + if (mpz_is_zero(z)) { + return 0; + } size_t last_bits = (8 * (sizeof(long) - sizeof(mpz_dig_t))) - __builtin_clzl(z->dig[z->len - 1]); return z->len * MPZ_DIG_SIZE + last_bits; }