Skip to content

make ReinterpretArray more Offset-safe #58898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ SimdLoop.simd_inner_length(::SCartesianIndices2{K}, ::Any) where K = K
SCartesianIndex2{K}(I1+1, Ilast)
end

_maybe_reshape(::IndexSCartesian2, A::AbstractArray, I...) = _maybe_reshape(IndexCartesian(), A, I...)
_maybe_reshape(::IndexSCartesian2, A::ReshapedReinterpretArray, I...) = A

# fallbacks
Expand All @@ -329,11 +330,25 @@ function _getindex(::IndexSCartesian2, A::AbstractArray{T,N}, ind::SCartesianInd
J = _ind2sub(tail(axes(A)), ind.j)
getindex(A, ind.i, J...)
end

function _getindex(::IndexSCartesian2{2}, A::AbstractArray{T,2}, ind::SCartesianIndex2) where {T}
@_propagate_inbounds_meta
J = first(axes(A, 2)) + ind.j - 1
getindex(A, ind.i, J)
end

function _setindex!(::IndexSCartesian2, A::AbstractArray{T,N}, v, ind::SCartesianIndex2) where {T,N}
@_propagate_inbounds_meta
J = _ind2sub(tail(axes(A)), ind.j)
setindex!(A, v, ind.i, J...)
end

function _setindex!(::IndexSCartesian2{2}, A::AbstractArray{T,2}, v, ind::SCartesianIndex2) where {T}
@_propagate_inbounds_meta
J = first(axes(A, 2)) + ind.j - 1
setindex!(A, v, ind.i, J)
end

eachindex(style::IndexSCartesian2, A::AbstractArray) = eachindex(style, parent(A))

## AbstractArray interface
Expand Down
14 changes: 8 additions & 6 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ tslow(a::AbstractArray) = TSlow(a)
wrapper(a::AbstractArray) = WrapperArray(a)
fcviews(a::AbstractArray) = view(a, ntuple(Returns(:),ndims(a)-1)..., axes(a)[end])
fcviews(a::AbstractArray{<:Any, 0}) = view(a)
offset(a::AbstractArray) = OffsetArray(a)
tslow(t::Tuple) = map(tslow, t)
wrapper(t::Tuple) = map(wrapper, t)
fcviews(t::Tuple) = map(fcviews, t)
offset(t::Tuple) = map(offset, t)

test_many_wrappers(testf, A, wrappers) = foreach(w -> testf(w(A)), wrappers)
test_many_wrappers(testf, A) = test_many_wrappers(testf, A, (identity, tslow, wrapper, fcviews))
test_many_wrappers(testf, A) = test_many_wrappers(testf, A, (identity, tslow, wrapper, fcviews, offset))

A = Int64[1, 2, 3, 4]
Ars = Int64[1 3; 2 4]
Expand All @@ -37,10 +39,6 @@ test_many_wrappers(B, (identity, tslow)) do _B
@test @inferred(size(reinterpret(reshape, Int128, _B))) == (3,)
end

test_many_wrappers(C) do Cr
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just folding this into the same at L163

@test reinterpret(reshape, Tuple{Int8, Int}, Cr) == fill((1,1))
end

@test_throws ArgumentError("cannot reinterpret `Int64` as `Vector{Int64}`, type `Vector{Int64}` is not a bits type") reinterpret(Vector{Int64}, A)
@test_throws ArgumentError("cannot reinterpret `Vector{Int32}` as `Int32`, type `Vector{Int32}` is not a bits type") reinterpret(Int32, Av)
@test_throws ArgumentError("cannot reinterpret a zero-dimensional `Int64` array to `Int32` which is of a different size") reinterpret(Int32, reshape([Int64(0)]))
Expand Down Expand Up @@ -160,8 +158,10 @@ test_many_wrappers(A3) do A3_
@test A3[2,1,2] == 400
end

test_many_wrappers(C) do Cr
test_many_wrappers(C) do Cr_
Cr = deepcopy(Cr_)
r = reinterpret(reshape, Tuple{Int, Int}, Cr)
@test r == fill((1,1))
r[] = (2,2)
@test r[] === (2,2)
r[1] = (3,3)
Expand Down Expand Up @@ -378,6 +378,8 @@ let a = rand(ComplexF32, 5)
r = reinterpret(reshape, Float32, a)
ref = Array(r)

@test all(r .== OffsetArray(r)[:, :, :])

@test r[1, :, 1] == ref[1, :]
@test r[1, :, 1, 1, 1] == ref[1, :]
@test r[1, :, UInt8(1)] == ref[1, :]
Expand Down