From 6a40cf88e852c3059754ab3a7587f1aca15553fd Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Mon, 19 Apr 2021 04:36:04 -0500 Subject: [PATCH] faster Float16 sinh (#39432) --- base/special/hyperbolic.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/base/special/hyperbolic.jl b/base/special/hyperbolic.jl index f6ff43d900b1b..d84cadcb2b6f2 100644 --- a/base/special/hyperbolic.jl +++ b/base/special/hyperbolic.jl @@ -14,6 +14,7 @@ # is preserved. # ==================================================== + # Hyperbolic functions # sinh methods H_SMALL_X(::Type{Float64}) = 2.0^-28 @@ -49,6 +50,11 @@ function sinh_kernel(x::Float32) return Float32(res*x) end +@inline function sinh16_kernel(x::Float32) + res = evalpoly(x*x, (1.0f0, 0.16666667f0, 0.008333337f0, 0.00019841001f0, + 2.7555539f-6, 2.514339f-8, 1.6260095f-10)) + return Float16(res*x) +end function sinh(x::T) where T<:Union{Float32,Float64} # Method @@ -74,6 +80,14 @@ function sinh(x::T) where T<:Union{Float32,Float64} return copysign(T(.5)*(E - 1/E),x) end +function Base.sinh(a::Float16) + x = Float32(a) + absx = abs(x) + absx <= SINH_SMALL_X(Float32) && return sinh16_kernel(x) + E = exp(absx) + return Float16(copysign(.5f0*(E - 1/E),x)) +end + COSH_SMALL_X(::Type{T}) where T= one(T) function cosh_kernel(x2::Float32)