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

Improve effect analysis of bitshifts by non Int and UInt Integers #47567

Merged
merged 7 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 6 additions & 3 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,16 @@ trailing_ones(x::Integer) = trailing_zeros(~x)
>>>(x::BitInteger, y::BitUnsigned) = lshr_int(x, y)
# signed shift counts can shift in either direction
# note: this early during bootstrap, `>=` is not yet available
# note: we only define Int shift counts here; the generic case is handled later
>>(x::BitInteger, y::Int) =
ifelse(0 <= y, x >> unsigned(y), x << unsigned(-y))
<<(x::BitInteger, y::Int) =
ifelse(0 <= y, x << unsigned(y), x >> unsigned(-y))
>>>(x::BitInteger, y::Int) =
ifelse(0 <= y, x >>> unsigned(y), x << unsigned(-y))
>>(x::BitInteger, y::BitSigned) =
ifelse(0 <= y, x >> unsigned(y), x << unsigned(-y))
<<(x::BitInteger, y::BitSigned) =
ifelse(0 <= y, x << unsigned(y), x >> unsigned(-y))
>>>(x::BitInteger, y::BitSigned) =
ifelse(0 <= y, x >>> unsigned(y), x << unsigned(-y))

for to in BitInteger_types, from in (BitInteger_types..., Bool)
if !(to === from)
Expand Down
17 changes: 9 additions & 8 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,10 @@ See also [`>>`](@ref), [`>>>`](@ref), [`exp2`](@ref), [`ldexp`](@ref).
"""
function <<(x::Integer, c::Integer)
@inline
typemin(Int) <= c <= typemax(Int) && return x << (c % Int)
(x >= 0 || c >= 0) && return zero(x) << 0 # for type stability
oftype(x, -1)
0 <= c <= typemax(UInt) && return x << (c % UInt)
-c <= typemax(UInt) && return x >> (-c % UInt)
(x >= 0 || c >= 0) && return zero(x) << UInt(0) # for type stability
return oftype(x, -1) << UInt(0)
end
function <<(x::Integer, c::Unsigned)
@inline
Expand All @@ -652,7 +653,6 @@ function <<(x::Integer, c::Unsigned)
end
c <= typemax(UInt) ? x << (c % UInt) : zero(x) << UInt(0)
end
<<(x::Integer, c::Int) = c >= 0 ? x << unsigned(c) : x >> unsigned(-c)

"""
>>(x, n)
Expand Down Expand Up @@ -689,11 +689,11 @@ function >>(x::Integer, c::Integer)
if c isa UInt
throw(MethodError(>>, (x, c)))
end
typemin(Int) <= c <= typemax(Int) && return x >> (c % Int)
0 <= c <= typemax(UInt) && return x >> (c % UInt)
gbaraldi marked this conversation as resolved.
Show resolved Hide resolved
-c <= typemax(UInt) && return x << (-c % UInt)
(x >= 0 || c < 0) && return zero(x) >> 0
oftype(x, -1)
end
>>(x::Integer, c::Int) = c >= 0 ? x >> unsigned(c) : x << unsigned(-c)

"""
>>>(x, n)
Expand Down Expand Up @@ -724,7 +724,9 @@ See also [`>>`](@ref), [`<<`](@ref).
"""
function >>>(x::Integer, c::Integer)
@inline
typemin(Int) <= c <= typemax(Int) ? x >>> (c % Int) : zero(x) >>> 0
0 <= c <= typemax(UInt) && return x >>> (c % UInt)
-c <= typemax(UInt) && return x << (-c % UInt)
zero(x) >>> 0
end
function >>>(x::Integer, c::Unsigned)
@inline
Expand All @@ -733,7 +735,6 @@ function >>>(x::Integer, c::Unsigned)
end
c <= typemax(UInt) ? x >>> (c % UInt) : zero(x) >>> 0
end
>>>(x::Integer, c::Int) = c >= 0 ? x >>> unsigned(c) : x << unsigned(-c)

# operator alias

Expand Down
5 changes: 5 additions & 0 deletions test/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ end
@test val >> -scount === val << ucount
end
end
for T2 in Base.BitInteger_types
for op in (>>, <<, >>>)
Core.Compiler.is_total(Base.infer_effects(op, (T1, T2)))
oscardssmith marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end

Expand Down