Skip to content

Commit 830212a

Browse files
committed
inline x^literal only for hardware-based number types x (closes #20768)
1 parent 8820d8b commit 830212a

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

NEWS.md

-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ Language changes
7676
* Experimental feature: `x^n` for integer literals `n` (e.g. `x^3`
7777
or `x^-3`) is now lowered to `x^Val{n}`, to enable compile-time
7878
specialization for literal integer exponents ([#20530]).
79-
`x^p` for `x::Number` and a literal `p=0,1,2,3` is now lowered to
80-
`one(x)`, `x`, `x*x`, and `x*x*x`, respectively ([#20648]).
8179
8280
Breaking changes
8381
----------------

base/intfuncs.jl

+10-4
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,18 @@ end
206206
@inline ^(x, p) = internal_pow(x, p)
207207
@inline internal_pow{p}(x, ::Type{Val{p}}) = x^p
208208

209+
# Restrict inlining to hardware-supported arithmetic types, which
210+
# are fast enough to benefit from inlining. This also makes it
211+
# easier to override ^ without having to override the Val method.
212+
const HWReal = Union{Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64,Float32,Float64}
213+
const HWNumber = Union{HWReal, Complex{<:HWReal}, Rational{<:HWReal}}
214+
209215
# inference.jl has complicated logic to inline x^2 and x^3 for
210216
# numeric types. In terms of Val we can do it much more simply:
211-
@inline internal_pow(x::Number, ::Type{Val{0}}) = one(x)
212-
@inline internal_pow(x::Number, ::Type{Val{1}}) = x
213-
@inline internal_pow(x::Number, ::Type{Val{2}}) = x*x
214-
@inline internal_pow(x::Number, ::Type{Val{3}}) = x*x*x
217+
@inline internal_pow(x::HWNumber, ::Type{Val{0}}) = one(x)
218+
@inline internal_pow(x::HWNumber, ::Type{Val{1}}) = x
219+
@inline internal_pow(x::HWNumber, ::Type{Val{2}}) = x*x
220+
@inline internal_pow(x::HWNumber, ::Type{Val{3}}) = x*x*x
215221

216222
# b^p mod m
217223

0 commit comments

Comments
 (0)