Skip to content

Commit

Permalink
make sure that the indirection through the Val{p} type doesn't stop…
Browse files Browse the repository at this point in the history
… inlining of `^`
  • Loading branch information
vtjnash committed Feb 23, 2017
1 parent cc7990b commit 0c5eac2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,19 @@ end
# to enable compile-time optimizations specialized to 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
# first argument, we dispatch the fallback via internal_pow.
# We mark these @inline since if the target is marked @inline,
# we want to make sure that gets propagated,
# even if it is over the inlining threshold.
@inline ^(x, p) = internal_pow(x, p)
@inline 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
@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

# b^p mod m

Expand Down
2 changes: 1 addition & 1 deletion base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ end
@inline ^(x::Float64, y::Integer) = x ^ Float64(y)
@inline ^(x::Float32, y::Integer) = x ^ Float32(y)
@inline ^(x::Float16, y::Integer) = Float16(Float32(x) ^ Float32(y))
^{p}(x::Float16, ::Type{Val{p}}) = Float16(Float32(x)^Val{p})
@inline ^{p}(x::Float16, ::Type{Val{p}}) = Float16(Float32(x) ^ Val{p})

function angle_restrict_symm(theta)
const P1 = 4 * 7.8539812564849853515625e-01
Expand Down

0 comments on commit 0c5eac2

Please sign in to comment.