Skip to content

Commit

Permalink
float16 cbrt, 50% faster (JuliaLang#39441)
Browse files Browse the repository at this point in the history
Co-authored-by: Kristoffer Carlsson <kcarlsson89@gmail.com>
  • Loading branch information
2 people authored and ElOceanografo committed May 4, 2021
1 parent 7b3d8a7 commit 87513d8
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions base/special/cbrt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,20 @@ function cbrt(x::Union{Float32,Float64})
t = _approx_cbrt(x)
return _improve_cbrt(x, t)
end

function cbrt(a::Float16)
if !isfinite(a) || iszero(a)
return a
end
x = Float32(a)

# 5 bit approximation. Simpler than _approx_cbrt since subnormals can not appear
u = highword(x) & 0x7fff_ffff
v = div(u, UInt32(3)) + 0x2a5119f2
t = copysign(fromhighword(Float32, v), x)

# 2 newton iterations
t = 0.33333334f0 * (2f0*t + x/(t*t))
t = 0.33333334f0 * (2f0*t + x/(t*t))
return Float16(t)
end

0 comments on commit 87513d8

Please sign in to comment.