@@ -358,21 +358,26 @@ module IteratorsMD
358
358
CartesianIndex ()
359
359
end
360
360
@propagate_inbounds function Base. getindex (iter:: CartesianIndices{N,R} , I:: Vararg{Int, N} ) where {N,R}
361
- CartesianIndex (getindex .(iter. indices, I))
361
+ CartesianIndex (_get_cartesianindex .(iter. indices, I))
362
362
end
363
363
364
364
# CartesianIndices act as a multidimensional range, so cartesian indexing of CartesianIndices
365
365
# with compatible dimensions may be seen as indexing into the component ranges.
366
366
# This may use the special indexing behavior implemented for ranges to return another CartesianIndices
367
367
@propagate_inbounds function Base. getindex (iter:: CartesianIndices{N,R} ,
368
368
I:: Vararg{Union{OrdinalRange{<:Integer, <:Integer}, Colon}, N} ) where {N,R}
369
- CartesianIndices (getindex .(iter. indices, I))
369
+ CartesianIndices (_get_cartesianindex .(iter. indices, I))
370
370
end
371
371
@propagate_inbounds function Base. getindex (iter:: CartesianIndices{N} ,
372
372
C:: CartesianIndices{N} ) where {N}
373
- CartesianIndices (getindex .(iter. indices, C. indices))
373
+ CartesianIndices (_get_cartesianindex .(iter. indices, C. indices))
374
374
end
375
375
376
+ # specialize to gives better hint to compiler to enable SIMD (#42115)
377
+ @inline _get_cartesianindex (:: OneTo , i:: Int ) = i
378
+ @inline _get_cartesianindex (:: Base.IdentityUnitRange , i:: Int ) = i
379
+ @inline _get_cartesianindex (r, i) = getindex (r, i)
380
+
376
381
# If dimensions permit, we may index into a CartesianIndices directly instead of constructing a SubArray wrapper
377
382
@propagate_inbounds function Base. view (c:: CartesianIndices{N} , r:: Vararg{Union{OrdinalRange{<:Integer, <:Integer}, Colon},N} ) where {N}
378
383
getindex (c, r... )
0 commit comments