From 0c5eac2bc5603b82c9ac114bdc8999831cba2f6f Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 23 Feb 2017 14:10:23 -0500 Subject: [PATCH] make sure that the indirection through the `Val{p}` type doesn't stop inlining of `^` --- base/intfuncs.jl | 17 ++++++++++------- base/math.jl | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index d299a8da27944..69c0df7d11c17 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -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 diff --git a/base/math.jl b/base/math.jl index d4db49a661030..944514da5e736 100644 --- a/base/math.jl +++ b/base/math.jl @@ -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