Skip to content

Commit b6c6bc0

Browse files
float16 cbrt, 50% faster (#39441)
Co-authored-by: Kristoffer Carlsson <kcarlsson89@gmail.com>
1 parent f723667 commit b6c6bc0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

base/special/cbrt.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,20 @@ function cbrt(x::Union{Float32,Float64})
146146
t = _approx_cbrt(x)
147147
return _improve_cbrt(x, t)
148148
end
149+
150+
function cbrt(a::Float16)
151+
if !isfinite(a) || iszero(a)
152+
return a
153+
end
154+
x = Float32(a)
155+
156+
# 5 bit approximation. Simpler than _approx_cbrt since subnormals can not appear
157+
u = highword(x) & 0x7fff_ffff
158+
v = div(u, UInt32(3)) + 0x2a5119f2
159+
t = copysign(fromhighword(Float32, v), x)
160+
161+
# 2 newton iterations
162+
t = 0.33333334f0 * (2f0*t + x/(t*t))
163+
t = 0.33333334f0 * (2f0*t + x/(t*t))
164+
return Float16(t)
165+
end

0 commit comments

Comments
 (0)