Skip to content

Commit 539c818

Browse files
Merge pull request #11620 from JuliaLang/sk/shifts
<<, >>, >>>: make core bit shift operators take Int as second arg.
2 parents 057f573 + 444a058 commit 539c818

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

base/bitarray.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,28 +1177,23 @@ end
11771177

11781178
reverse(v::BitVector) = reverse!(copy(v))
11791179

1180-
function (<<)(B::BitVector, i::Int64)
1180+
function (<<)(B::BitVector, i::Int)
11811181
n = length(B)
11821182
i == 0 && return copy(B)
11831183
A = falses(n)
11841184
i < n && copy_chunks!(A.chunks, 1, B.chunks, i+1, n-i)
11851185
return A
11861186
end
1187-
(<<)(B::BitVector, i::Int32) = B << Int64(i)
1188-
(<<)(B::BitVector, i::Integer) = B << Int64(i)
11891187

1190-
function (>>>)(B::BitVector, i::Int64)
1188+
function (>>>)(B::BitVector, i::Int)
11911189
n = length(B)
11921190
i == 0 && return copy(B)
11931191
A = falses(n)
11941192
i < n && copy_chunks!(A.chunks, i+1, B.chunks, 1, n-i)
11951193
return A
11961194
end
1197-
(>>>)(B::BitVector, i::Int32) = B >>> Int64(i)
1198-
(>>>)(B::BitVector, i::Integer) = B >>> Int64(i)
11991195

1200-
(>>)(B::BitVector, i::Int32) = B >>> i
1201-
(>>)(B::BitVector, i::Integer) = B >>> i
1196+
(>>)(B::BitVector, i::Int) = B >>> i
12021197

12031198
function rol!(dest::BitVector, src::BitVector, i::Integer)
12041199
length(dest) == length(src) || throw(ArgumentError("destination and source should be of same size"))

base/gmp.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,23 +334,23 @@ for (fJ, fC) in ((:-, :neg), (:~, :com))
334334
end
335335
end
336336

337-
function <<(x::BigInt, c::Int32)
337+
function <<(x::BigInt, c::Int)
338338
c < 0 && throw(DomainError())
339339
c == 0 && return x
340340
z = BigInt()
341341
ccall((:__gmpz_mul_2exp, :libgmp), Void, (Ptr{BigInt}, Ptr{BigInt}, Culong), &z, &x, c)
342342
return z
343343
end
344344

345-
function >>(x::BigInt, c::Int32)
345+
function >>(x::BigInt, c::Int)
346346
c < 0 && throw(DomainError())
347347
c == 0 && return x
348348
z = BigInt()
349349
ccall((:__gmpz_fdiv_q_2exp, :libgmp), Void, (Ptr{BigInt}, Ptr{BigInt}, Culong), &z, &x, c)
350350
return z
351351
end
352352

353-
>>>(x::BigInt, c::Int32) = x >> c
353+
>>>(x::BigInt, c::Int) = x >> c
354354

355355
trailing_zeros(x::BigInt) = Int(ccall((:__gmpz_scan1, :libgmp), Culong, (Ptr{BigInt}, Culong), &x, 0))
356356
trailing_ones(x::BigInt) = Int(ccall((:__gmpz_scan0, :libgmp), Culong, (Ptr{BigInt}, Culong), &x, 0))

base/int.jl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ for T in IntTypes
9696
(|)(x::$T, y::$T) = box($T, or_int(unbox($T,x),unbox($T,y)))
9797
($)(x::$T, y::$T) = box($T,xor_int(unbox($T,x),unbox($T,y)))
9898

99-
<<(x::$T, y::Int32) = box($T, shl_int(unbox($T,x),unbox(Int32,y)))
100-
>>>(x::$T, y::Int32) = box($T,lshr_int(unbox($T,x),unbox(Int32,y)))
99+
<<(x::$T, y::Int) = box($T, shl_int(unbox($T,x),unbox(Int,y)))
100+
>>>(x::$T, y::Int) = box($T,lshr_int(unbox($T,x),unbox(Int,y)))
101101
end
102102
if issubtype(T,Unsigned)
103-
@eval >>(x::$T, y::Int32) = box($T,lshr_int(unbox($T,x),unbox(Int32,y)))
103+
@eval >>(x::$T, y::Int) = box($T,lshr_int(unbox($T,x),unbox(Int,y)))
104104
else
105-
@eval >>(x::$T, y::Int32) = box($T,ashr_int(unbox($T,x),unbox(Int32,y)))
105+
@eval >>(x::$T, y::Int) = box($T,ashr_int(unbox($T,x),unbox(Int,y)))
106106
end
107107
end
108108

@@ -348,8 +348,8 @@ typemin(::Type{UInt64}) = UInt64(0)
348348
typemax(::Type{UInt64}) = 0xffffffffffffffff
349349
@eval typemin(::Type{UInt128}) = $(UInt128(0))
350350
@eval typemax(::Type{UInt128}) = $(box(UInt128,unbox(Int128,convert(Int128,-1))))
351-
@eval typemin(::Type{Int128} ) = $(convert(Int128,1)<<Int32(127))
352-
@eval typemax(::Type{Int128} ) = $(box(Int128,unbox(UInt128,typemax(UInt128)>>Int32(1))))
351+
@eval typemin(::Type{Int128} ) = $(convert(Int128,1)<<127)
352+
@eval typemax(::Type{Int128} ) = $(box(Int128,unbox(UInt128,typemax(UInt128)>>1)))
353353

354354
widen(::Type{Int8}) = Int
355355
widen(::Type{Int16}) = Int
@@ -373,7 +373,7 @@ widemul(x::Number,y::Bool) = x*y
373373

374374
## wide multiplication, Int128 multiply and divide ##
375375

376-
if WORD_SIZE==32
376+
if WORD_SIZE == 32
377377
function widemul(u::Int64, v::Int64)
378378
local u0::UInt64, v0::UInt64, w0::UInt64
379379
local u1::Int64, v1::Int64, w1::UInt64, w2::Int64, t::UInt64
@@ -434,12 +434,12 @@ if WORD_SIZE==32
434434

435435
mod(x::Int128, y::Int128) = Int128(mod(BigInt(x),BigInt(y)))
436436

437-
<< (x::Int128, y::Int32) = y == 0 ? x : box(Int128,shl_int(unbox(Int128,x),unbox(Int32,y)))
438-
<< (x::UInt128, y::Int32) = y == 0 ? x : box(UInt128,shl_int(unbox(UInt128,x),unbox(Int32,y)))
439-
>> (x::Int128, y::Int32) = y == 0 ? x : box(Int128,ashr_int(unbox(Int128,x),unbox(Int32,y)))
440-
>> (x::UInt128, y::Int32) = y == 0 ? x : box(UInt128,lshr_int(unbox(UInt128,x),unbox(Int32,y)))
441-
>>>(x::Int128, y::Int32) = y == 0 ? x : box(Int128,lshr_int(unbox(Int128,x),unbox(Int32,y)))
442-
>>>(x::UInt128, y::Int32) = y == 0 ? x : box(UInt128,lshr_int(unbox(UInt128,x),unbox(Int32,y)))
437+
<< (x::Int128, y::Int) = y == 0 ? x : box(Int128,shl_int(unbox(Int128,x),unbox(Int,y)))
438+
<< (x::UInt128, y::Int) = y == 0 ? x : box(UInt128,shl_int(unbox(UInt128,x),unbox(Int,y)))
439+
>> (x::Int128, y::Int) = y == 0 ? x : box(Int128,ashr_int(unbox(Int128,x),unbox(Int,y)))
440+
>> (x::UInt128, y::Int) = y == 0 ? x : box(UInt128,lshr_int(unbox(UInt128,x),unbox(Int,y)))
441+
>>>(x::Int128, y::Int) = y == 0 ? x : box(Int128,lshr_int(unbox(Int128,x),unbox(Int,y)))
442+
>>>(x::UInt128, y::Int) = y == 0 ? x : box(UInt128,lshr_int(unbox(UInt128,x),unbox(Int,y)))
443443
else
444444
*(x::Int128, y::Int128) = box(Int128,mul_int(unbox(Int128,x),unbox(Int128,y)))
445445
*(x::UInt128, y::UInt128) = box(UInt128,mul_int(unbox(UInt128,x),unbox(UInt128,y)))
@@ -491,13 +491,13 @@ for T in (Int8,UInt8)
491491
end
492492

493493
if WORD_SIZE == 32
494-
for T in (Int64,UInt64)
495-
@eval function checked_mul(x::$T, y::$T)
496-
xy = Int128(x)*Int128(y)
497-
(typemin($T) <= xy <= typemax($T)) || throw(OverflowError())
498-
return xy % $T
494+
for T in (Int64,UInt64)
495+
@eval function checked_mul(x::$T, y::$T)
496+
xy = Int128(x)*Int128(y)
497+
(typemin($T) <= xy <= typemax($T)) || throw(OverflowError())
498+
return xy % $T
499+
end
499500
end
500-
end
501501
else
502502
checked_mul(x::Int64, y::Int64) = box(Int64,checked_smul(unbox(Int64,x),unbox(Int64,y)))
503503
checked_mul(x::UInt64, y::UInt64) = box(UInt64,checked_umul(unbox(UInt64,x),unbox(UInt64,y)))

base/operators.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ end
108108
const .≤ = .<=
109109
const .≠ = .!=
110110

111-
# core << >> and >>> takes Int32 as second arg
112-
<<(x,y::Int32) = no_op_err("<<", typeof(x))
113-
>>(x,y::Int32) = no_op_err(">>", typeof(x))
114-
>>>(x,y::Int32) = no_op_err(">>>", typeof(x))
115-
<<(x,y::Integer) = x << convert(Int32,y)
116-
>>(x,y::Integer) = x >> convert(Int32,y)
117-
>>>(x,y::Integer) = x >>> convert(Int32,y)
111+
# core << >> and >>> takes Int as second arg
112+
<<(x,y::Int) = no_op_err("<<", typeof(x))
113+
>>(x,y::Int) = no_op_err(">>", typeof(x))
114+
>>>(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)
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)