Skip to content

Commit ca16ac6

Browse files
authored
Merge pull request #18629 from JuliaLang/teh/unsafe_trunc
Support unsafe_trunc(T, ::Integer)
2 parents ac25c58 + bfacc9f commit ca16ac6

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

base/int.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ rem{T<:Integer}(x::T, ::Type{T}) = x
327327
rem(x::Integer, ::Type{Bool}) = ((x&1)!=0)
328328
mod{T<:Integer}(x::Integer, ::Type{T}) = rem(x, T)
329329

330+
unsafe_trunc{T<:Integer}(::Type{T}, x::Integer) = rem(x, T)
331+
330332
convert{Tf<:Union{Float32,Float64}}(T::BitSigned64T, x::Tf) =
331333
box(T,checked_fptosi(T,unbox(Tf,x)))
332334
convert{Tf<:Union{Float32,Float64}}(T::BitUnsigned64T, x::Tf) =

test/int.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,9 @@ end
195195
@test true << 2 === 1 << 2
196196
@test true >> 2 === 1 >> 2
197197
@test true >>> 2 === 1 >>> 2
198+
199+
@test @inferred(unsafe_trunc(Int8, 127)) === Int8(127)
200+
@test unsafe_trunc(Int8, 128) === Int8(-128)
201+
@test unsafe_trunc(Int8, -127) === Int8(-127)
202+
@test unsafe_trunc(Int8, -128) === Int8(-128)
203+
@test unsafe_trunc(Int8, -129) === Int8(127)

0 commit comments

Comments
 (0)