Skip to content

Conversation

@Pramodh-G
Copy link
Contributor

fixes #43431 .
Please feel free to suggest changes.

cc: @oscardssmith

@inkydragon
Copy link
Member

inkydragon commented Dec 16, 2021

Maybe you can also update trunc and round.

diff --git a/base/float.jl b/base/float.jl
--- base/float.jl
+++ base/float.jl
@@ -305,20 +305,12 @@
 """
 function unsafe_trunc end
 
 for Ti in (Int8, Int16, Int32, Int64)
-    @eval begin
-        unsafe_trunc(::Type{$Ti}, x::Float16) = fptosi($Ti, x)
-        unsafe_trunc(::Type{$Ti}, x::Float32) = fptosi($Ti, x)
-        unsafe_trunc(::Type{$Ti}, x::Float64) = fptosi($Ti, x)
-    end
+    @eval unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptosi($Ti, x)
 end
 for Ti in (UInt8, UInt16, UInt32, UInt64)
-    @eval begin
-        unsafe_trunc(::Type{$Ti}, x::Float16) = fptoui($Ti, x)
-        unsafe_trunc(::Type{$Ti}, x::Float32) = fptoui($Ti, x)
-        unsafe_trunc(::Type{$Ti}, x::Float64) = fptoui($Ti, x)
-    end
+    @eval unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptoui($Ti, x)
 end
 
 function unsafe_trunc(::Type{UInt128}, x::Float64)
     xu = reinterpret(UInt64,x)
@@ -352,35 +344,21 @@
 unsafe_trunc(::Type{Int128}, x::Float16) = unsafe_trunc(Int128, Float32(x))
 
 # matches convert methods
 # also determines floor, ceil, round
-trunc(::Type{Signed}, x::Float16) = trunc(Int,x)
-trunc(::Type{Signed}, x::Float32) = trunc(Int,x)
-trunc(::Type{Signed}, x::Float64) = trunc(Int,x)
-trunc(::Type{Unsigned}, x::Float16) = trunc(UInt,x)
-trunc(::Type{Unsigned}, x::Float32) = trunc(UInt,x)
-trunc(::Type{Unsigned}, x::Float64) = trunc(UInt,x)
-trunc(::Type{Integer}, x::Float16) = trunc(Int,x)
-trunc(::Type{Integer}, x::Float32) = trunc(Int,x)
-trunc(::Type{Integer}, x::Float64) = trunc(Int,x)
+trunc(::Type{Signed}, x::IEEEFloat) = trunc(Int,x)
+trunc(::Type{Unsigned}, x::IEEEFloat) = trunc(UInt,x)
+trunc(::Type{Integer}, x::IEEEFloat) = trunc(Int,x)
 
 # fallbacks
 floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDown))
 ceil(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundUp))
 round(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundNearest))
 
-round(x::Float64, r::RoundingMode{:ToZero})  = trunc_llvm(x)
-round(x::Float32, r::RoundingMode{:ToZero})  = trunc_llvm(x)
-round(x::Float16, r::RoundingMode{:ToZero})  = trunc_llvm(x)
-round(x::Float64, r::RoundingMode{:Down})    = floor_llvm(x)
-round(x::Float32, r::RoundingMode{:Down})    = floor_llvm(x)
-round(x::Float16, r::RoundingMode{:Down})    = floor_llvm(x)
-round(x::Float64, r::RoundingMode{:Up})      = ceil_llvm(x)
-round(x::Float32, r::RoundingMode{:Up})      = ceil_llvm(x)
-round(x::Float16, r::RoundingMode{:Up})      = ceil_llvm(x)
-round(x::Float64, r::RoundingMode{:Nearest}) = rint_llvm(x)
-round(x::Float32, r::RoundingMode{:Nearest}) = rint_llvm(x)
-round(x::Float16, r::RoundingMode{:Nearest}) = rint_llvm(x)
+round(x::IEEEFloat, r::RoundingMode{:ToZero})  = trunc_llvm(x)
+round(x::IEEEFloat, r::RoundingMode{:Down})    = floor_llvm(x)
+round(x::IEEEFloat, r::RoundingMode{:Up})      = ceil_llvm(x)
+round(x::IEEEFloat, r::RoundingMode{:Nearest}) = rint_llvm(x)
 
 ## floating point promotions ##
 promote_rule(::Type{Float32}, ::Type{Float16}) = Float32
 promote_rule(::Type{Float64}, ::Type{Float16}) = Float64

Copy link
Member

@inkydragon inkydragon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format suggestions:

Remove spaces and keep code style consistently.

Copy link
Member

@oscardssmith oscardssmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good assuming tests pass.

@oscardssmith oscardssmith added the merge me PR is reviewed. Merge when all tests are passing label Dec 16, 2021
@DilumAluthge DilumAluthge merged commit 55ad2f0 into JuliaLang:master Dec 16, 2021
@DilumAluthge DilumAluthge removed the merge me PR is reviewed. Merge when all tests are passing label Dec 16, 2021
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repetitive method definitions for Float arithmetic

4 participants