Skip to content

Commit 19d0745

Browse files
authored
fix elsize and write for SubArrays of Arrays (#36739)
1 parent 80cdfa1 commit 19d0745

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

base/io.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ function write(s::IO, a::SubArray{T,N,<:Array}) where {T,N}
676676
if !isbitstype(T) || !isa(a, StridedArray)
677677
return invoke(write, Tuple{IO, AbstractArray}, s, a)
678678
end
679-
elsz = sizeof(T)
679+
elsz = elsize(a)
680680
colsz = size(a,1) * elsz
681681
GC.@preserve a if stride(a,1) != 1
682682
for idxs in CartesianIndices(size(a))

base/subarray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ similar(V::SubArray, T::Type, dims::Dims) = similar(V.parent, T, dims)
6767
sizeof(V::SubArray) = length(V) * sizeof(eltype(V))
6868
sizeof(V::SubArray{<:Any,<:Any,<:Array}) = length(V) * elsize(V.parent)
6969

70+
elsize(::Type{<:SubArray{<:Any,<:Any,P}}) where {P<:Array} = elsize(P)
71+
7072
copy(V::SubArray) = V.parent[V.indices...]
7173

7274
parent(V::SubArray) = V.parent

test/subarray.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,16 +585,16 @@ end
585585
@test IndexStyle(B18581) === IndexLinear()
586586
end
587587

588+
primitive type UInt48 48 end
589+
UInt48(x::UInt64) = Core.Intrinsics.trunc_int(UInt48, x)
590+
UInt48(x::UInt32) = Core.Intrinsics.zext_int(UInt48, x)
591+
588592
@testset "sizeof" begin
589593
@test sizeof(view(zeros(UInt8, 10), 1:4)) == 4
590594
@test sizeof(view(zeros(UInt8, 10), 1:3)) == 3
591595
@test sizeof(view(zeros(Float64, 10, 10), 1:3, 2:6)) == 120
592596

593597
# Test non-power of 2 types (Issue #35884)
594-
primitive type UInt48 48 end
595-
UInt48(x::UInt64) = Core.Intrinsics.trunc_int(UInt48, x)
596-
UInt48(x::UInt32) = Core.Intrinsics.zext_int(UInt48, x)
597-
598598
a = UInt48(0x00000001);
599599
b = UInt48(0x00000002);
600600
c = UInt48(0x00000003);
@@ -607,6 +607,15 @@ end
607607
@test sizeof(view(Diagonal(zeros(Float64, 10)), 1:3, 2:6)) == 120
608608
end
609609

610+
@testset "write" begin
611+
io = IOBuffer()
612+
a = UInt48[ UInt48(UInt32(i+j)) for i = 1:5, j = 1:5 ]
613+
@test write(io, view(a, :, 2)) == 40
614+
seekstart(io)
615+
v = Vector{UInt48}(undef, 5)
616+
read!(io, v)
617+
@test v == view(a, :, 2)
618+
end
610619

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

0 commit comments

Comments
 (0)