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

inline x^literal only for hardware-based number types x #20782

Merged
merged 1 commit into from
Feb 25, 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: 0 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ 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
14 changes: 10 additions & 4 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,18 @@ end
@inline ^(x, p) = internal_pow(x, p)
@inline internal_pow{p}(x, ::Type{Val{p}}) = x^p

# Restrict inlining to hardware-supported arithmetic types, which
# are fast enough to benefit from inlining. This also makes it
# easier to override ^ without having to override the Val method.
const HWReal = Union{Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64,Float32,Float64}
const HWNumber = Union{HWReal, Complex{<:HWReal}, Rational{<:HWReal}}

# 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:
@inline internal_pow(x::Number, ::Type{Val{0}}) = one(x)
@inline internal_pow(x::Number, ::Type{Val{1}}) = x
@inline internal_pow(x::Number, ::Type{Val{2}}) = x*x
@inline internal_pow(x::Number, ::Type{Val{3}}) = x*x*x
@inline internal_pow(x::HWNumber, ::Type{Val{0}}) = one(x)
@inline internal_pow(x::HWNumber, ::Type{Val{1}}) = x
@inline internal_pow(x::HWNumber, ::Type{Val{2}}) = x*x
@inline internal_pow(x::HWNumber, ::Type{Val{3}}) = x*x*x

# b^p mod m

Expand Down