Skip to content

Commit 6c1eb93

Browse files
authored
pow: Make n == 0 special case obvious to constprop (#43920)
Should have been part of #43907, but I forgot.
1 parent b34cb17 commit 6c1eb93

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

base/math.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,12 @@ end
10281028
end
10291029

10301030
# compensated power by squaring
1031-
function ^(x::Float64, n::Integer)
1031+
@constprop :aggressive @inline function ^(x::Float64, n::Integer)
10321032
n == 0 && return one(x)
1033+
return pow_body(x, n)
1034+
end
1035+
1036+
@noinline function pow_body(x::Float64, n::Integer)
10331037
y = 1.0
10341038
xnlo = ynlo = 0.0
10351039
if n < 0
@@ -1054,6 +1058,7 @@ function ^(x::Float64, n::Integer)
10541058
!isfinite(x) && return x*y
10551059
return muladd(x, y, muladd(y, xnlo, x*ynlo))
10561060
end
1061+
10571062
function ^(x::Float32, n::Integer)
10581063
n < 0 && return inv(x)^(-n)
10591064
n == 3 && return x*x*x #keep compatibility with literal_pow

test/math.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,3 +1393,9 @@ end
13931393
@test Base.invokelatest((@eval ()->(@force_compile; sqrt(Base.inferencebarrier($x))))) == y
13941394
end
13951395
end
1396+
1397+
# Test inference of x^0.0 (tested here because
1398+
# it requires annotations in the math code. If
1399+
# the compiler ever gets good enough to figure
1400+
# that out by itself, move this to inference).
1401+
@test code_typed(x->Val{x^0.0}(), Tuple{Float64})[1][2] == Val{1.0}

0 commit comments

Comments
 (0)