From c6882a99f849cb047acace19bbadad58f2fc73ae Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Tue, 9 Jun 2015 05:19:04 -0400 Subject: [PATCH] <<, >>, >>>: use intrinsics for more mixed-type shift operations. LLVM isn't able to eliminate the additional checks done by fallbacks for the mixed-type bitshift operations, so this change just calls the intrinsics directly, which minimizes the amount of work done. The fallback now avoids conversion checking as well and just returns zero if the second argument is too large to fit in an Int. --- base/int.jl | 12 ++++++++++++ base/operators.jl | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/base/int.jl b/base/int.jl index efb376098703b..c11c7b3c5b0a7 100644 --- a/base/int.jl +++ b/base/int.jl @@ -104,6 +104,18 @@ for T in IntTypes else @eval >>(x::$T, y::Int) = box($T,ashr_int(unbox($T,x),unbox(Int,y))) end + for S in IntTypes + (S === Int128 || S === UInt128) && continue + @eval begin + <<(x::$T, y::$S) = box($T, shl_int(unbox($T,x),unbox($S,y))) + >>>(x::$T, y::$S) = box($T,lshr_int(unbox($T,x),unbox($S,y))) + end + if issubtype(T,Unsigned) + @eval >>(x::$T, y::$S) = box($T,lshr_int(unbox($T,x),unbox($S,y))) + else + @eval >>(x::$T, y::$S) = box($T,ashr_int(unbox($T,x),unbox($S,y))) + end + end end bswap(x::Int8) = x diff --git a/base/operators.jl b/base/operators.jl index e9404778333c2..541a5c231bfe6 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -112,9 +112,9 @@ const .≠ = .!= <<(x,y::Int) = no_op_err("<<", typeof(x)) >>(x,y::Int) = no_op_err(">>", typeof(x)) >>>(x,y::Int) = no_op_err(">>>", typeof(x)) -<<(x,y::Integer) = x << convert(Int,y) ->>(x,y::Integer) = x >> convert(Int,y) ->>>(x,y::Integer) = x >>> convert(Int,y) +<<(x,y::Integer) = typemax(Int) < y ? zero(x) : x << (y % Int) +>>(x,y::Integer) = typemax(Int) < y ? zero(x) : x >> (y % Int) +>>>(x,y::Integer) = typemax(Int) < y ? zero(x) : x >>> (y % Int) # fallback div, fld, and cld implementations # NOTE: C89 fmod() and x87 FPREM implicitly provide truncating float division,