Skip to content

Commit

Permalink
inline x^literal only for hardware-based number types x (closes Julia…
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Feb 24, 2017
1 parent b0ca50b commit e697db5
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,18 @@ end
^(x, p) = internal_pow(x, p)
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:
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
internal_pow(x::HWNumber, ::Type{Val{0}}) = one(x)
internal_pow(x::HWNumber, ::Type{Val{1}}) = x
internal_pow(x::HWNumber, ::Type{Val{2}}) = x*x
internal_pow(x::HWNumber, ::Type{Val{3}}) = x*x*x

# b^p mod m

Expand Down

0 comments on commit e697db5

Please sign in to comment.