Skip to content

Commit bb8df83

Browse files
committed
Use contiguous for static ArrayInterface.stride.
1 parent 4372580 commit bb8df83

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.13.7"
3+
version = "2.13.8"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/stridelayout.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ julia> ArrayInterface.strides(A)
260260
```
261261
"""
262262
strides(A) = Base.strides(A)
263+
263264
"""
264265
offsets(A)
265266
@@ -270,7 +271,24 @@ For example, if `A isa Base.Matrix`, `offsets(A) === (StaticInt(1), StaticInt(1)
270271
offsets(::Any) = (StaticInt{1}(),) # Assume arbitrary Julia data structures use 1-based indexing by default.
271272
@inline strides(A::Vector{<:Any}) = (StaticInt(1),)
272273
@inline strides(A::Array{<:Any,N}) where {N} = (StaticInt(1), Base.tail(Base.strides(A))...)
273-
@inline strides(A::AbstractArray{<:Any,N}) where {N} = Base.strides(A)
274+
@inline strides(A::AbstractArray) = _strides(A, Base.strides(A), contiguous_axis(A))
275+
@generated function _strides(A::AbstractArray{T,N}, s::NTuple{N}, ::Contiguous{C}) where {T,N,C}
276+
if C 0 || C > N
277+
return Expr(:block, Expr(:meta,:inline), :s)
278+
end
279+
stup = Expr(:tuple)
280+
for n 1:N
281+
if n == C
282+
push!(stup.args, :(StaticInt{$(sizeof(T))}()))
283+
else
284+
push!(stup.args, Expr(:ref, :s, n))
285+
end
286+
end
287+
quote
288+
$(Expr(:meta,:inline))
289+
@inbounds $stup
290+
end
291+
end
274292

275293
@inline function offsets(x, i)
276294
inds = indices(x, i)

0 commit comments

Comments
 (0)