Skip to content

Commit

Permalink
ndigits0z(n::BigInt, b): avoid expensive off-by-one check for b=2^k.
Browse files Browse the repository at this point in the history
Fixes #8727, but it's kind of a bandaid since for non-power-of-two
bases ndigits0z is still slow due to GMP's inexcusable laziness. At
least for power-of-two bases, however, BigInt hashing is now about
3x faster and allocates no memory.

(cherry picked from commit 19eb2fa)
  • Loading branch information
StefanKarpinski authored and ivarne committed Oct 22, 2014
1 parent 62d83b8 commit b1fc473
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ end
function ndigits0z(x::BigInt, b::Integer=10)
# mpz_sizeinbase might return an answer 1 too big
n = int(ccall((:__gmpz_sizeinbase,:libgmp), Culong, (Ptr{BigInt}, Int32), &x, b))
abs(x) < big(b)^(n-1) ? n-1 : n
ispow2(b) || abs(x) >= big(b)^(n-1) ? n : n-1
end
ndigits(x::BigInt, b::Integer=10) = x.size == 0 ? 1 : ndigits0z(x,b)

Expand Down

0 comments on commit b1fc473

Please sign in to comment.