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

minimal inlining of x^Val{p} for p=0,1,2,3 and x::Number #20648

Merged
merged 7 commits into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
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
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
Copy link
Contributor

@innerlee innerlee Feb 17, 2017

Choose a reason for hiding this comment

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

p=0,1,2,3,...

edit: when first read, i thought these four numbers are specialized.

Copy link
Contributor

Choose a reason for hiding this comment

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

They are?

Copy link
Member Author

@stevengj stevengj Feb 17, 2017

Choose a reason for hiding this comment

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

Yes, only these four exponents are inlined by this patch. It's a minimal PR to match (and slightly exceed) what inference.jl did before. In the future, we may well want to inline more exponents.

`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)
Copy link
Contributor

Choose a reason for hiding this comment

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

this gives an InexactError for negative powers, such as BigFloat(2) ^ -20 (which was amazingly not tested)

(wrong line beforehand)

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be fixed by #20732


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