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

faster Float32 and Float16 pow #40236

Merged
merged 2 commits into from
Apr 24, 2021
Merged
Changes from all 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
14 changes: 10 additions & 4 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -867,29 +867,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))
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

why don't we want to trust llvm (aka libm) here anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

1.6 introduced about a 3x regression on this due to a switch in which libm got loaded.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Should we fix openlibm?

Copy link
Member Author

@oscardssmith oscardssmith Apr 21, 2021

Choose a reason for hiding this comment

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

probably. That said, I generally think that we shouldn't be relying on external libraries, so I'm not that sad about replacing this anyway.

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)
vtjnash marked this conversation as resolved.
Show resolved Hide resolved
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