Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,18 +489,20 @@ julia> powermod(5, 3, 19)
function powermod(x::Integer, p::Integer, m::T) where T<:Integer
p == 0 && return mod(one(m),m)
# When the concrete type of p is signed and has the lowest value,
# `p != 0 && p == -p` is equivalent to `p == typemin(typeof(p))` for 2's complement representation.
# `p < 0 && p == -p` is equivalent to `p == typemin(typeof(p))` for 2's complement representation.
# but will work for integer types like `BigInt` that don't have `typemin` defined
# It needs special handling otherwise will cause overflow problem.
if p == -p
imod = invmod(x, m)
rhalf = powermod(imod, -(p÷2), m)
r::T = mod(widemul(rhalf, rhalf), m)
isodd(p) && (r = mod(widemul(r, imod), m))
#else odd
return r
elseif p < 0
return powermod(invmod(x, m), -p, m)
if p < 0
if p == -p
imod = invmod(x, m)
rhalf = powermod(imod, -(p÷2), m)
r::T = mod(widemul(rhalf, rhalf), m)
isodd(p) && (r = mod(widemul(r, imod), m))
#else odd
return r
else
return powermod(invmod(x, m), -p, m)
end
end
(m == 1 || m == -1) && return zero(m)
b = oftype(m,mod(x,m)) # this also checks for divide by zero
Expand Down
3 changes: 3 additions & 0 deletions test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ end
@test powermod(2, big(3), -5) == -2
@inferred powermod(2, -2, -5)
@inferred powermod(big(2), -2, UInt(5))

@test powermod(-3, 0x80, 7) === 2
@test powermod(0x03, 0x80, 0x07) === 0x02
end

@testset "nextpow/prevpow" begin
Expand Down