Skip to content

Commit 6e1d666

Browse files
committed
Reuse mi in simd_outer_range
1 parent bec6c6f commit 6e1d666

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

base/multidimensional.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,21 +610,24 @@ module IteratorsMD
610610
# CartesianPartition.
611611
mi = iter.parent.mi
612612
ci = iter.parent.parent
613-
ax, ax1 = axes(ci), Base.axes1(ci)
614-
subs = Base.ind2sub_rs(ax, mi, first(iter.indices[1]))
615-
vl, fl = Base._sub2ind(tail(ax), tail(subs)...), subs[1]
616-
vr, fr = divrem(last(iter.indices[1]) - 1, mi[end]) .+ (1, first(ax1))
613+
ax1 = Base.axes1(ci)
614+
function splitdim1(i, mi)
615+
d, r = divrem(i - 1, mi)
616+
d + 1, r + first(ax1)
617+
end
618+
vl, fl = splitdim1(first(iter.indices[1]), mi[1])
619+
vr, fr = splitdim1(last(iter.indices[1]), mi[1])
620+
# form the iterator for outer dimensions, equivalent to vec(oci), but mi is reused
617621
oci = CartesianIndices(tail(ci.indices))
618-
# A fake CartesianPartition to reuse the outer iterate fallback
619-
outer = @inbounds view(ReshapedArray(oci, (length(oci),), mi), vl:vr)
620-
init = @inbounds dec(oci[tail(subs)...].I, oci.indices) # real init state
622+
roci = ReshapedArray(oci, (length(oci),), tail(mi))
623+
outer = @inbounds view(roci, vl:vr)
621624
# Use Generator to make inner loop branchless
622625
@inline function skip_len_I(i::Int, I::CartesianIndex)
623626
l = i == 1 ? fl : first(ax1)
624627
r = i == length(outer) ? fr : last(ax1)
625628
l - first(ax1), r - l + 1, I
626629
end
627-
(skip_len_I(i, I) for (i, I) in Iterators.enumerate(Iterators.rest(outer, (init, 0))))
630+
(skip_len_I(i, I) for (i, I) in Iterators.enumerate(outer))
628631
end
629632
@inline function simd_outer_range(iter::CartesianPartition{CartesianIndex{2}})
630633
# But for two-dimensional Partitions the above is just a simple one-dimensional range

0 commit comments

Comments
 (0)