Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float16 cbrt, 50% faster #39441

Merged
merged 4 commits into from
Apr 17, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions base/special/cbrt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,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)
KristofferC marked this conversation as resolved.
Show resolved Hide resolved

# 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