Skip to content

Commit

Permalink
faster Float32 and Float16 pow (#40236)
Browse files Browse the repository at this point in the history
Approximately .5 ULP, relatively fast.
Update float^integer as well
  • Loading branch information
oscardssmith authored Apr 24, 2021
1 parent ac7974a commit 1474566
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -911,29 +911,35 @@ end
z
end
@inline function ^(x::Float32, y::Float32)
z = ccall("llvm.pow.f32", llvmcall, Float32, (Float32, Float32), x, y)
z = Float32(exp2_fast(log2(Float64(x))*y))
if isnan(z) & !isnan(x+y)
throw_exp_domainerror(x)
end
z
end
@inline function ^(x::Float16, y::Float16)
z = Float16(exp2_fast(log2(Float32(x))*y))
if isnan(z) & !isnan(x+y)
throw_exp_domainerror(x)
end
z
end
@inline ^(x::Float16, y::Float16) = Float16(Float32(x)^Float32(y)) # TODO: optimize

@inline function ^(x::Float64, y::Integer)
y == -1 && return inv(x)
y == 0 && return one(x)
y == 1 && return x
y == 2 && return x*x
y == 3 && return x*x*x
ccall("llvm.pow.f64", llvmcall, Float64, (Float64, Float64), x, Float64(y))
return x^Float64(y)
end
@inline function ^(x::Float32, y::Integer)
y == -1 && return inv(x)
y == 0 && return one(x)
y == 1 && return x
y == 2 && return x*x
y == 3 && return x*x*x
ccall("llvm.pow.f32", llvmcall, Float32, (Float32, Float32), x, Float32(y))
x^Float32(y)
end
@inline ^(x::Float16, y::Integer) = Float16(Float32(x) ^ y)
@inline literal_pow(::typeof(^), x::Float16, ::Val{p}) where {p} = Float16(literal_pow(^,Float32(x),Val(p)))
Expand Down

0 comments on commit 1474566

Please sign in to comment.