Skip to content

Commit

Permalink
Support unsafe_trunc(<:Integer, x::Float16)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Apr 7, 2017
1 parent 5cacdd6 commit 80dd40b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,14 @@ float{T<:Number}(::Type{T}) = typeof(float(zero(T)))

for Ti in (Int8, Int16, Int32, Int64)
@eval begin
unsafe_trunc(::Type{$Ti}, x::Float16) = unsafe_trunc($Ti, Float32(x))
unsafe_trunc(::Type{$Ti}, x::Float32) = fptosi($Ti, x)
unsafe_trunc(::Type{$Ti}, x::Float64) = fptosi($Ti, x)
end
end
for Ti in (UInt8, UInt16, UInt32, UInt64)
@eval begin
unsafe_trunc(::Type{$Ti}, x::Float16) = unsafe_trunc($Ti, Float32(x))
unsafe_trunc(::Type{$Ti}, x::Float32) = fptoui($Ti, x)
unsafe_trunc(::Type{$Ti}, x::Float64) = fptoui($Ti, x)
end
Expand Down Expand Up @@ -314,6 +316,8 @@ function unsafe_trunc(::Type{Int128}, x::Float32)
copysign(unsafe_trunc(UInt128,x) % Int128, x)
end

unsafe_trunc(::Type{UInt128}, x::Float16) = unsafe_trunc(UInt128, Float32(x))
unsafe_trunc(::Type{Int128}, x::Float16) = unsafe_trunc(Int128, Float32(x))

# matches convert methods
# also determines floor, ceil, round
Expand Down
2 changes: 2 additions & 0 deletions test/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ g = Float16(1.)
@test floor(Float16(1)) === Float16(1)
@test ceil(Float16(0.1)) == ceil(0.1)
@test ceil(Float16(0.9)) == ceil(0.9)
@test unsafe_trunc(UInt8, Float16(3)) === 0x03
@test unsafe_trunc(Int16, Float16(3)) === Int16(3)
@test fma(Float16(0.1),Float16(0.9),Float16(0.5)) fma(0.1,0.9,0.5)
@test muladd(Float16(0.1),Float16(0.9),Float16(0.5)) muladd(0.1,0.9,0.5)
@test convert(Int128,Float16(-1.0)) == Int128(-1)
Expand Down

0 comments on commit 80dd40b

Please sign in to comment.