Skip to content

Commit

Permalink
fix elsize and write for SubArrays of Arrays (#36739)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Jul 22, 2020
1 parent 80cdfa1 commit 19d0745
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ function write(s::IO, a::SubArray{T,N,<:Array}) where {T,N}
if !isbitstype(T) || !isa(a, StridedArray)
return invoke(write, Tuple{IO, AbstractArray}, s, a)
end
elsz = sizeof(T)
elsz = elsize(a)
colsz = size(a,1) * elsz
GC.@preserve a if stride(a,1) != 1
for idxs in CartesianIndices(size(a))
Expand Down
2 changes: 2 additions & 0 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ similar(V::SubArray, T::Type, dims::Dims) = similar(V.parent, T, dims)
sizeof(V::SubArray) = length(V) * sizeof(eltype(V))
sizeof(V::SubArray{<:Any,<:Any,<:Array}) = length(V) * elsize(V.parent)

elsize(::Type{<:SubArray{<:Any,<:Any,P}}) where {P<:Array} = elsize(P)

copy(V::SubArray) = V.parent[V.indices...]

parent(V::SubArray) = V.parent
Expand Down
17 changes: 13 additions & 4 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -585,16 +585,16 @@ end
@test IndexStyle(B18581) === IndexLinear()
end

primitive type UInt48 48 end
UInt48(x::UInt64) = Core.Intrinsics.trunc_int(UInt48, x)
UInt48(x::UInt32) = Core.Intrinsics.zext_int(UInt48, x)

@testset "sizeof" begin
@test sizeof(view(zeros(UInt8, 10), 1:4)) == 4
@test sizeof(view(zeros(UInt8, 10), 1:3)) == 3
@test sizeof(view(zeros(Float64, 10, 10), 1:3, 2:6)) == 120

# Test non-power of 2 types (Issue #35884)
primitive type UInt48 48 end
UInt48(x::UInt64) = Core.Intrinsics.trunc_int(UInt48, x)
UInt48(x::UInt32) = Core.Intrinsics.zext_int(UInt48, x)

a = UInt48(0x00000001);
b = UInt48(0x00000002);
c = UInt48(0x00000003);
Expand All @@ -607,6 +607,15 @@ end
@test sizeof(view(Diagonal(zeros(Float64, 10)), 1:3, 2:6)) == 120
end

@testset "write" begin
io = IOBuffer()
a = UInt48[ UInt48(UInt32(i+j)) for i = 1:5, j = 1:5 ]
@test write(io, view(a, :, 2)) == 40
seekstart(io)
v = Vector{UInt48}(undef, 5)
read!(io, v)
@test v == view(a, :, 2)
end

@testset "unaliascopy trimming; Issue #26263" begin
A = rand(5,5,5,5)
Expand Down

0 comments on commit 19d0745

Please sign in to comment.