Skip to content

Commit

Permalink
minimal inlining of x^Val{p} for p=0,1,2,3 and x::Number (#20648)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj authored and StefanKarpinski committed Feb 18, 2017
1 parent c081b28 commit 6c6bbba
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Language changes
* Experimental feature: `x^n` for integer literals `n` (e.g. `x^3`
or `x^-3`) is now lowered to `x^Val{n}`, to enable compile-time
specialization for literal integer exponents ([#20530]).
`x^p` for `x::Number` and a literal `p=0,1,2,3` is now lowered to
`one(x)`, `x`, `x*x`, and `x*x*x`, respectively ([#20648]).

Breaking changes
----------------
Expand Down
3 changes: 3 additions & 0 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ end
^(x::Integer, y::BigInt ) = bigint_pow(BigInt(x), y)
^(x::Bool , y::BigInt ) = Base.power_by_squaring(x, y)

# override default inlining of x^2 and x^3 etc.
^{p}(x::BigInt, ::Type{Val{p}}) = x^Culong(p)

function powermod(x::BigInt, p::BigInt, m::BigInt)
r = BigInt()
ccall((:__gmpz_powm, :libgmp), Void,
Expand Down
14 changes: 12 additions & 2 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,18 @@ end

# x^p for any literal integer p is lowered to x^Val{p},
# to enable compile-time optimizations specialized to p.
# However, we still need a fallback that calls the general ^:
^{p}(x, ::Type{Val{p}}) = x^p
# However, we still need a fallback that calls the general ^.
# To avoid ambiguities for methods that dispatch on the
# first argument, we dispatch the fallback via internal_pow:
^(x, p) = internal_pow(x, p)
internal_pow{p}(x, ::Type{Val{p}}) = x^p

# inference.jl has complicated logic to inline x^2 and x^3 for
# numeric types. In terms of Val we can do it much more simply:
internal_pow(x::Number, ::Type{Val{0}}) = one(x)
internal_pow(x::Number, ::Type{Val{1}}) = x
internal_pow(x::Number, ::Type{Val{2}}) = x*x
internal_pow(x::Number, ::Type{Val{3}}) = x*x*x

# b^p mod m

Expand Down
1 change: 1 addition & 0 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ catalan
for T in (Irrational, Rational, Integer, Number)
^(::Irrational{:e}, x::T) = exp(x)
end
^{p}(::Irrational{:e}, ::Type{Val{p}}) = exp(p)

log(::Irrational{:e}) = 1 # use 1 to correctly promote expressions like log(x)/log(e)
log(::Irrational{:e}, x::Number) = log(x)
Expand Down
1 change: 1 addition & 0 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ end
^(x::Float32, y::Integer) = x^Int32(y)
^(x::Float32, y::Int32) = powi_llvm(x, y)
^(x::Float16, y::Integer) = Float16(Float32(x)^y)
^{p}(x::Float16, ::Type{Val{p}}) = Float16(Float32(x)^Val{p})

function angle_restrict_symm(theta)
const P1 = 4 * 7.8539812564849853515625e-01
Expand Down
3 changes: 3 additions & 0 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ end
^(x::BigFloat, y::Integer) = typemin(Clong) <= y <= typemax(Clong) ? x^Clong(y) : x^BigInt(y)
^(x::BigFloat, y::Unsigned) = typemin(Culong) <= y <= typemax(Culong) ? x^Culong(y) : x^BigInt(y)

# override default inlining of x^2 etc.
^{p}(x::BigFloat, ::Type{Val{p}}) = x^Culong(p)

for f in (:exp, :exp2, :exp10, :expm1, :cosh, :sinh, :tanh, :sech, :csch, :coth, :cbrt)
@eval function $f(x::BigFloat)
z = BigFloat()
Expand Down

2 comments on commit 6c6bbba

@pabloferz
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to make sure that all the regressions from 3222749 where mitigated

@nanosoldier runbenchmarks(ALL, vs = "@d8ac581f474d5d0f0c032594b46fd1ed84b042f3")

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @jrevels

Please sign in to comment.