Merge pull request #4845 from t-ikegami/mpz_crash_fix

Fix bit_length() method to work with zero-valued mpz integer.
This commit is contained in:
Scott Shawcroft 2021-06-02 08:28:24 -07:00 committed by GitHub
commit a25872dcb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 0 deletions

View File

@ -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;
}