Skip to content

Commit 88db576

Browse files
authored
Merge pull request #21312 from JuliaLang/teh/unsafe_trunc_float16
Support `unsafe_trunc(<:Integer, x::Float16)`
2 parents e5e365c + bfad817 commit 88db576

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

base/float.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,14 @@ float{T<:Number}(::Type{T}) = typeof(float(zero(T)))
275275

276276
for Ti in (Int8, Int16, Int32, Int64)
277277
@eval begin
278+
unsafe_trunc(::Type{$Ti}, x::Float16) = unsafe_trunc($Ti, Float32(x))
278279
unsafe_trunc(::Type{$Ti}, x::Float32) = fptosi($Ti, x)
279280
unsafe_trunc(::Type{$Ti}, x::Float64) = fptosi($Ti, x)
280281
end
281282
end
282283
for Ti in (UInt8, UInt16, UInt32, UInt64)
283284
@eval begin
285+
unsafe_trunc(::Type{$Ti}, x::Float16) = unsafe_trunc($Ti, Float32(x))
284286
unsafe_trunc(::Type{$Ti}, x::Float32) = fptoui($Ti, x)
285287
unsafe_trunc(::Type{$Ti}, x::Float64) = fptoui($Ti, x)
286288
end
@@ -314,6 +316,8 @@ function unsafe_trunc(::Type{Int128}, x::Float32)
314316
copysign(unsafe_trunc(UInt128,x) % Int128, x)
315317
end
316318

319+
unsafe_trunc(::Type{UInt128}, x::Float16) = unsafe_trunc(UInt128, Float32(x))
320+
unsafe_trunc(::Type{Int128}, x::Float16) = unsafe_trunc(Int128, Float32(x))
317321

318322
# matches convert methods
319323
# also determines floor, ceil, round

test/float16.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ g = Float16(1.)
4949
@test floor(Float16(1)) === Float16(1)
5050
@test ceil(Float16(0.1)) == ceil(0.1)
5151
@test ceil(Float16(0.9)) == ceil(0.9)
52+
@test unsafe_trunc(UInt8, Float16(3)) === 0x03
53+
@test unsafe_trunc(Int16, Float16(3)) === Int16(3)
54+
@test unsafe_trunc(UInt128, Float16(3)) === UInt128(3)
55+
@test unsafe_trunc(Int128, Float16(3)) === Int128(3)
56+
@test unsafe_trunc(Int16, NaN16) === Int16(0) #18771
5257
@test fma(Float16(0.1),Float16(0.9),Float16(0.5)) fma(0.1,0.9,0.5)
5358
@test muladd(Float16(0.1),Float16(0.9),Float16(0.5)) muladd(0.1,0.9,0.5)
5459
@test convert(Int128,Float16(-1.0)) == Int128(-1)

0 commit comments

Comments
 (0)