@@ -23,7 +23,7 @@ import .Base: log, exp, sin, cos, tan, sinh, cosh, tanh, asin,
23
23
using . Base: sign_mask, exponent_mask, exponent_one,
24
24
exponent_half, uinttype, significand_mask,
25
25
significand_bits, exponent_bits, exponent_bias,
26
- exponent_max, exponent_raw_max, clamp, clamp!
26
+ exponent_max, exponent_raw_max, clamp, clamp!, reduce_empty_iter
27
27
28
28
using Core. Intrinsics: sqrt_llvm
29
29
@@ -103,17 +103,20 @@ function evalpoly(x, p::Tuple)
103
103
_evalpoly (x, p)
104
104
end
105
105
end
106
+ evalpoly (x, :: Tuple{} ) = zero (one (x)) # dimensionless zero, i.e. 0 * x^0
106
107
107
108
evalpoly (x, p:: AbstractVector ) = _evalpoly (x, p)
108
109
109
110
function _evalpoly (x, p)
110
111
Base. require_one_based_indexing (p)
111
112
N = length (p)
112
- ex = p[end ]
113
+ @inbounds p0 = N == 0 ? reduce_empty_iter (+ , p) : p[N]
114
+ s = oftype (one (x) * p0, p0)
115
+ N <= 1 && return s
113
116
for i in N- 1 : - 1 : 1
114
- ex = muladd (x, ex , p[i])
117
+ @inbounds s = muladd (x, s , p[i])
115
118
end
116
- ex
119
+ return s
117
120
end
118
121
119
122
function evalpoly (z:: Complex , p:: Tuple )
@@ -142,16 +145,17 @@ function evalpoly(z::Complex, p::Tuple)
142
145
end
143
146
end
144
147
evalpoly (z:: Complex , p:: Tuple{<:Any} ) = p[1 ]
145
-
148
+ evalpoly (z :: Complex , :: Tuple{} ) = zero ( one (z)) # dimensionless zero, i.e. 0 * z^0
146
149
147
150
evalpoly (z:: Complex , p:: AbstractVector ) = _evalpoly (z, p)
148
151
149
152
function _evalpoly (z:: Complex , p)
150
153
Base. require_one_based_indexing (p)
151
- length (p) == 1 && return p[1 ]
152
154
N = length (p)
153
- a = p[end ]
154
- b = p[end - 1 ]
155
+ @inbounds p0 = N == 0 ? reduce_empty_iter (+ , p) : p[N]
156
+ N <= 1 && return oftype (one (z) * p0, p0)
157
+ a = p0
158
+ @inbounds b = p[N- 1 ]
155
159
156
160
x = real (z)
157
161
y = imag (z)
@@ -160,7 +164,7 @@ function _evalpoly(z::Complex, p)
160
164
for i in N- 2 : - 1 : 1
161
165
ai = a
162
166
a = muladd (r, ai, b)
163
- b = muladd (- s, ai, p[i])
167
+ @inbounds b = muladd (- s, ai, p[i])
164
168
end
165
169
ai = a
166
170
muladd (ai, z, b)
0 commit comments