Skip to content

Commit

Permalink
inline x^literal only for hardware-based number types x (closes #20768)…
Browse files Browse the repository at this point in the history
… (#20782)
  • Loading branch information
stevengj authored Feb 25, 2017
1 parent 581a264 commit 6ac7e24
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
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

0 comments on commit 6ac7e24

Please sign in to comment.