Skip to content

Commit bee46ee

Browse files
Merge pull request #11629 from JuliaLang/sk/shifts
<<, >>, >>>: use intrinsics for more mixed-type shift operations.
2 parents e8302b2 + c6882a9 commit bee46ee

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

base/int.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ for T in IntTypes
104104
else
105105
@eval >>(x::$T, y::Int) = box($T,ashr_int(unbox($T,x),unbox(Int,y)))
106106
end
107+
for S in IntTypes
108+
(S === Int128 || S === UInt128) && continue
109+
@eval begin
110+
<<(x::$T, y::$S) = box($T, shl_int(unbox($T,x),unbox($S,y)))
111+
>>>(x::$T, y::$S) = box($T,lshr_int(unbox($T,x),unbox($S,y)))
112+
end
113+
if issubtype(T,Unsigned)
114+
@eval >>(x::$T, y::$S) = box($T,lshr_int(unbox($T,x),unbox($S,y)))
115+
else
116+
@eval >>(x::$T, y::$S) = box($T,ashr_int(unbox($T,x),unbox($S,y)))
117+
end
118+
end
107119
end
108120

109121
bswap(x::Int8) = x

base/operators.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ const .≠ = .!=
112112
<<(x,y::Int) = no_op_err("<<", typeof(x))
113113
>>(x,y::Int) = no_op_err(">>", typeof(x))
114114
>>>(x,y::Int) = no_op_err(">>>", typeof(x))
115-
<<(x,y::Integer) = x << convert(Int,y)
116-
>>(x,y::Integer) = x >> convert(Int,y)
117-
>>>(x,y::Integer) = x >>> convert(Int,y)
115+
<<(x,y::Integer) = typemax(Int) < y ? zero(x) : x << (y % Int)
116+
>>(x,y::Integer) = typemax(Int) < y ? zero(x) : x >> (y % Int)
117+
>>>(x,y::Integer) = typemax(Int) < y ? zero(x) : x >>> (y % Int)
118118

119119
# fallback div, fld, and cld implementations
120120
# NOTE: C89 fmod() and x87 FPREM implicitly provide truncating float division,

0 commit comments

Comments
 (0)