Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster digits(::BigInt) #37075

Merged
merged 14 commits into from
Aug 18, 2020
Prev Previous commit
Next Next commit
digits -> digits!
  • Loading branch information
stevengj committed Aug 17, 2020
commit 0fb9919a85d3c103bbc9785276eb5b44fa69f787
18 changes: 11 additions & 7 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -694,15 +694,19 @@ function string(n::BigInt; base::Integer = 10, pad::Integer = 1)
String(sv)
end

function digits(n::BigInt; base::Integer = 10, pad::Integer = 1)
if base ≥ 2
if base ≤ 36 # 0-9, plus a-z for 10-35
return reverse!(map(x -> Int(x>0x39 ? x-0x57 : x-0x30), codeunits(string(n; base, pad))))
elseif base ≤ 62 # 0-9, plus A-Z for 10-35 and a-z for 36..61
return reverse!(map(x -> Int(x>0x39 ? (x>0x60 ? x-0x3d : x-0x37) : x-0x30), codeunits(string(n; base, pad))))
function digits!(a::AbstractVector{T}, n::BigInt; base::Integer = 10) where {T<:Integer}
if 2 ≤ base ≤ 62
s = codeunits(string(n; base))
offset = length(s)+1
for i = 1:min(length(a), length(s))
# base ≤ 36: 0-9, plus a-z for 10-35
# base > 36: 0-9, plus A-Z for 10-35 and a-z for 36..61
x = s[offset-i]
a[i] = base ≤ 36 ? (x>0x39 ? x-0x57 : x-0x30) : (x>0x39 ? (x>0x60 ? x-0x3d : x-0x37) : x-0x30)
end
return a
end
return invoke(digits, Tuple{Integer}, n; base, pad) # slow generic fallback
return invoke(digits!, Tuple{typeof(a), Integer}, a, n; base) # slow generic fallback
end

function ndigits0zpb(x::BigInt, b::Integer)
Expand Down