|
45 | 45 |
|
46 | 46 |
|
47 | 47 | @generated function _map!(f, dest, ::Size{S}, a::StaticArray...) where {S}
|
48 |
| - exprs = Vector{Expr}(undef, prod(S)) |
49 |
| - for i ∈ 1:prod(S) |
50 |
| - tmp = [:(a[$j][$i]) for j ∈ 1:length(a)] |
51 |
| - exprs[i] = :(dest[$i] = f($(tmp...))) |
52 |
| - end |
| 48 | + tmp = [:(a[$j][i]) for j ∈ 1:length(a)] |
53 | 49 | return quote
|
54 | 50 | @_inline_meta
|
55 |
| - @inbounds $(Expr(:block, exprs...)) |
| 51 | + @inbounds @simd for i ∈ 1:$(prod(S)) |
| 52 | + dest[i] = f($(tmp...)) |
| 53 | + end |
| 54 | + return dest |
56 | 55 | end
|
57 | 56 | end
|
58 | 57 |
|
|
66 | 65 |
|
67 | 66 | @generated function _mapreduce(f, op, dims::Colon, nt::NamedTuple{()},
|
68 | 67 | ::Size{S}, a::StaticArray...) where {S}
|
69 |
| - tmp = [:(a[$j][1]) for j ∈ 1:length(a)] |
70 |
| - expr = :(f($(tmp...))) |
71 |
| - for i ∈ 2:prod(S) |
72 |
| - tmp = [:(a[$j][$i]) for j ∈ 1:length(a)] |
73 |
| - expr = :(op($expr, f($(tmp...)))) |
74 |
| - end |
| 68 | + tmp = [:(a[$j][i]) for j ∈ 1:length(a)] |
75 | 69 | return quote
|
76 | 70 | @_inline_meta
|
77 |
| - @inbounds return $expr |
| 71 | + i = 1 |
| 72 | + @inbounds s = f($(tmp...)) |
| 73 | + @inbounds @simd for i = 2:$(prod(S)) |
| 74 | + s = op(s, f($(tmp...))) |
| 75 | + end |
| 76 | + return s |
78 | 77 | end
|
79 | 78 | end
|
80 |
| - |
| 79 | + |
81 | 80 | @generated function _mapreduce(f, op, dims::Colon, nt::NamedTuple{(:init,)},
|
82 |
| - ::Size{S}, a::StaticArray...) where {S} |
83 |
| - expr = :(nt.init) |
84 |
| - for i ∈ 1:prod(S) |
85 |
| - tmp = [:(a[$j][$i]) for j ∈ 1:length(a)] |
86 |
| - expr = :(op($expr, f($(tmp...)))) |
87 |
| - end |
| 81 | + ::Size{S}, a::StaticArray...) where {S} |
| 82 | + tmp = [:(a[$j][i]) for j ∈ 1:length(a)] |
88 | 83 | return quote
|
89 | 84 | @_inline_meta
|
90 |
| - @inbounds return $expr |
| 85 | + @inbounds s = nt.init |
| 86 | + @inbounds @simd for i = 1:$(prod(S)) |
| 87 | + s = op(s, f($(tmp...))) |
| 88 | + end |
| 89 | + return s |
91 | 90 | end
|
92 | 91 | end
|
93 | 92 |
|
|
98 | 97 | @inline _mapreduce(f, op, D::Int, nt::NamedTuple, sz::Size{S}, a::StaticArray) where {S} =
|
99 | 98 | _mapreduce(f, op, Val(D), nt, sz, a)
|
100 | 99 |
|
101 |
| - |
| 100 | + |
102 | 101 | @generated function _mapreduce(f, op, dims::Val{D}, nt::NamedTuple{()},
|
103 | 102 | ::Size{S}, a::StaticArray) where {S,D}
|
104 | 103 | N = length(S)
|
|
0 commit comments