Skip to content

Commit 407c65f

Browse files
authored
Merge pull request #603 from tkoolen/tk/dot-simdloop
Use `@simd` in `_vecdot`
2 parents ec26f0a + 90ffc02 commit 407c65f

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/linalg.jl

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -214,38 +214,23 @@ end
214214
@inbounds return similar_type(a, typeof(Signed(a[2]*b[3])-Signed(a[3]*b[2])))(((Signed(a[2]*b[3])-Signed(a[3]*b[2]), Signed(a[3]*b[1])-Signed(a[1]*b[3]), Signed(a[1]*b[2])-Signed(a[2]*b[1]))))
215215
end
216216

217-
@inline dot(a::StaticVector, b::StaticVector) = _vecdot(same_size(a, b), a, b)
218-
@generated function _vecdot(::Size{S}, a::StaticArray, b::StaticArray) where {S}
219-
if prod(S) == 0
220-
return :(zero(promote_op(*, eltype(a), eltype(b))))
221-
end
222-
223-
expr = :(adjoint(a[1]) * b[1])
224-
for j = 2:prod(S)
225-
expr = :($expr + adjoint(a[$j]) * b[$j])
226-
end
217+
@inline dot(a::StaticVector, b::StaticVector) = _vecdot(same_size(a, b), a, b, dot)
218+
@inline bilinear_vecdot(a::StaticArray, b::StaticArray) = _vecdot(same_size(a, b), a, b, *)
227219

228-
return quote
229-
@_inline_meta
230-
@inbounds return $expr
231-
end
232-
end
233-
234-
@inline bilinear_vecdot(a::StaticArray, b::StaticArray) = _bilinear_vecdot(same_size(a, b), a, b)
235-
@generated function _bilinear_vecdot(::Size{S}, a::StaticArray, b::StaticArray) where {S}
220+
@inline function _vecdot(::Size{S}, a::StaticArray, b::StaticArray, product) where {S}
236221
if prod(S) == 0
237-
return :(zero(promote_op(*, eltype(a), eltype(b))))
238-
end
239-
240-
expr = :(a[1] * b[1])
241-
for j = 2:prod(S)
242-
expr = :($expr + a[$j] * b[$j])
222+
za, zb = zero(eltype(a)), zero(eltype(b))
223+
else
224+
# Use an actual element if there is one, to support e.g. Vector{<:Number}
225+
# element types for which runtime size information is required to construct
226+
# a zero element.
227+
za, zb = zero(a[1]), zero(b[1])
243228
end
244-
245-
return quote
246-
@_inline_meta
247-
@inbounds return $expr
229+
ret = product(za, zb) + product(za, zb)
230+
@inbounds @simd for j = 1 : prod(S)
231+
ret += product(a[j], b[j])
248232
end
233+
return ret
249234
end
250235

251236
@inline LinearAlgebra.norm_sqr(v::StaticVector) = mapreduce(abs2, +, v; init=zero(real(eltype(v))))

0 commit comments

Comments
 (0)