Skip to content

Commit

Permalink
Merge pull request #26 from JuliaSIMD/tanh_fastFloat32
Browse files Browse the repository at this point in the history
tanh_fast `Float32` specialization
  • Loading branch information
chriselrod authored Aug 28, 2022
2 parents 8049a38 + de9f592 commit 7d24825
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SLEEFPirates"
uuid = "476501e8-09a2-5ece-8869-fb82de89a1fa"
authors = ["chriselrod <elrodc@gmail.com>"]
version = "0.6.33"
version = "0.6.34"

[deps]
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
Expand Down
8 changes: 8 additions & 0 deletions src/SLEEFPirates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ ldexp(x::Float16, q::Int) = Float16(ldexpk(Float32(x), q))
max_tanh(::Type{Float64}) = 19.06154746539849599509609553228539867418786340504817671278462587964799037885145
max_tanh(::Type{Float32}) = 9.010913339828708369989037671244720498805572920317272822795576296065428827978905f0

@inline function tanh_fast(x::AbstractSIMD{W,Float32}) where {W}
# stolen from https://github.com/FluxML/NNlib.jl/pull/345
# https://github.com/FluxML/NNlib.jl/blob/5dd04df4e95f9f9b70d6232fac546f3e98899fc2/src/activations.jl#L766-L773
x2 = abs2(x)
n = evalpoly(x2, (1.0f0, 0.1346604f0, 0.0035974074f0, 2.2332108f-5, 1.587199f-8))
d = evalpoly(x2, (1.0f0, 0.4679937f0, 0.026262015f0, 0.0003453992f0, 8.7767893f-7))
ifelse(x2 < 66f0, @fastmath(x * (n / d)), sign(x))
end
@inline function tanh_fast(x)
exp2xm1 = expm1_fast(Base.FastMath.add_fast(x, x))
# Division is faster than approximate inversion in
Expand Down

2 comments on commit 7d24825

@chriselrod
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/67223

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.34 -m "<description of version>" 7d24825d2b459e8693e3424fe5e24536757aad39
git push origin v0.6.34

Please sign in to comment.