Skip to content

Commit 1474566

Browse files
authored
faster Float32 and Float16 pow (#40236)
Approximately .5 ULP, relatively fast. Update float^integer as well
1 parent ac7974a commit 1474566

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

base/math.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,29 +911,35 @@ end
911911
z
912912
end
913913
@inline function ^(x::Float32, y::Float32)
914-
z = ccall("llvm.pow.f32", llvmcall, Float32, (Float32, Float32), x, y)
914+
z = Float32(exp2_fast(log2(Float64(x))*y))
915+
if isnan(z) & !isnan(x+y)
916+
throw_exp_domainerror(x)
917+
end
918+
z
919+
end
920+
@inline function ^(x::Float16, y::Float16)
921+
z = Float16(exp2_fast(log2(Float32(x))*y))
915922
if isnan(z) & !isnan(x+y)
916923
throw_exp_domainerror(x)
917924
end
918925
z
919926
end
920-
@inline ^(x::Float16, y::Float16) = Float16(Float32(x)^Float32(y)) # TODO: optimize
921927

922928
@inline function ^(x::Float64, y::Integer)
923929
y == -1 && return inv(x)
924930
y == 0 && return one(x)
925931
y == 1 && return x
926932
y == 2 && return x*x
927933
y == 3 && return x*x*x
928-
ccall("llvm.pow.f64", llvmcall, Float64, (Float64, Float64), x, Float64(y))
934+
return x^Float64(y)
929935
end
930936
@inline function ^(x::Float32, y::Integer)
931937
y == -1 && return inv(x)
932938
y == 0 && return one(x)
933939
y == 1 && return x
934940
y == 2 && return x*x
935941
y == 3 && return x*x*x
936-
ccall("llvm.pow.f32", llvmcall, Float32, (Float32, Float32), x, Float32(y))
942+
x^Float32(y)
937943
end
938944
@inline ^(x::Float16, y::Integer) = Float16(Float32(x) ^ y)
939945
@inline literal_pow(::typeof(^), x::Float16, ::Val{p}) where {p} = Float16(literal_pow(^,Float32(x),Val(p)))

0 commit comments

Comments
 (0)