97
97
scalar_getindex (x) = x
98
98
scalar_getindex (x:: Ref ) = x[]
99
99
100
- @generated function _broadcast (f, :: Size{newsize} , s:: Tuple{Vararg{Size}} , a... ) where newsize
101
- first_staticarray = a[findfirst (ai -> ai <: Union{StaticArray, Transpose{<:Any, <:StaticArray}, Adjoint{<:Any, <:StaticArray}, Diagonal{<:Any, <:StaticArray}} , a)]
100
+ isstatic (:: StaticArray ) = true
101
+ isstatic (:: Transpose{<:Any, <:StaticArray} ) = true
102
+ isstatic (:: Adjoint{<:Any, <:StaticArray} ) = true
103
+ isstatic (:: Diagonal{<:Any, <:StaticArray} ) = true
104
+ isstatic (_) = false
102
105
106
+ @inline first_statictype (x, y... ) = isstatic (x) ? typeof (x) : first_statictype (y... )
107
+ first_statictype () = error (" unresolved dest type" )
108
+
109
+ @inline function _broadcast (f, sz:: Size{newsize} , s:: Tuple{Vararg{Size}} , a... ) where newsize
110
+ first_staticarray = first_statictype (a... )
103
111
if prod (newsize) == 0
104
112
# Use inference to get eltype in empty case (see also comments in _map)
105
- eltys = [:(eltype (a[$ i])) for i ∈ 1 : length (a)]
106
- return quote
107
- @_inline_meta
108
- T = Core. Compiler. return_type (f, Tuple{$ (eltys... )})
109
- @inbounds return similar_type ($ first_staticarray, T, Size (newsize))()
110
- end
113
+ eltys = Tuple{map (eltype, a)... }
114
+ T = Core. Compiler. return_type (f, eltys)
115
+ @inbounds return similar_type (first_staticarray, T, Size (newsize))()
111
116
end
117
+ elements = __broadcast (f, sz, s, a... )
118
+ @inbounds return similar_type (first_staticarray, eltype (elements), Size (newsize))(elements)
119
+ end
112
120
121
+ @generated function __broadcast (f, :: Size{newsize} , s:: Tuple{Vararg{Size}} , a... ) where newsize
113
122
sizes = [sz. parameters[1 ] for sz ∈ s. parameters]
114
123
indices = CartesianIndices (newsize)
115
124
exprs = similar (indices, Expr)
@@ -123,8 +132,7 @@ scalar_getindex(x::Ref) = x[]
123
132
124
133
return quote
125
134
@_inline_meta
126
- @inbounds elements = tuple ($ (exprs... ))
127
- @inbounds return similar_type ($ first_staticarray, eltype (elements), Size (newsize))(elements)
135
+ @inbounds return elements = tuple ($ (exprs... ))
128
136
end
129
137
end
130
138
0 commit comments