• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / 1662

19 Nov 2026 09:09PM UTC coverage: 93.225% (+0.007%) from 93.218%
1662

push

buildkite

web-flow
codegen: fix lexical conflict between #64695 and #64927 (#65093)

100542 of 107849 relevant lines covered (93.22%)

397.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.14
/base/reinterpretarray.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
    ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T,N}
5

6
Array type typically returned by [`reinterpret(T, a)`](@ref reinterpret) for an array `a`: a
7
reinterpreted view (of element type `T`) of the parent array `a` (of type `A` with element
8
type `S`).
9
If the size of `T` differs from the size of `S`, the array will be compressed/expanded in
10
the first dimension. The variant `reinterpret(reshape, T, a)` (with `IsReshaped == true`)
11
instead adds or consumes the first dimension depending on the ratio of element sizes.
12
"""
13
struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T, N}
14
    parent::A
15
    readable::Bool
16
    writable::Bool
17

18
    function throwbits(S::Type, T::Type, U::Type)
6 ✔
19
        @noinline
6 ✔
20
        throw(ArgumentError(LazyString("cannot reinterpret `", S, "` as `", T, "`, type `", U, "` is not a bits type")))
6 ✔
21
    end
22
    function throwsize0(S::Type, T::Type, msg)
3 ✔
23
        @noinline
3 ✔
24
        throw(ArgumentError(LazyString("cannot reinterpret a zero-dimensional `", S, "` array to `", T,
3 ✔
25
            "` which is of a ", msg, " size")))
26
    end
27
    function throwsingleton(S::Type, T::Type)
3 ✔
28
        @noinline
3 ✔
29
        throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T, "` which is a singleton type")))
3 ✔
30
    end
31
    function throwbitpadding(S::Type, T::Type, U::Type)
3 ✔
32
        @noinline
3 ✔
33
        throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T,
3 ✔
34
            "` because type `", U, "` contains non-byte-aligned primitive fields")))
35
    end
36

37
    global reinterpret
38

39
    @doc """
40
        reinterpret(T::DataType, A::AbstractArray)
41

42
    Construct a view of the array with the same binary data as the given
43
    array, but with `T` as element type.
44

45
    This function also works on "lazy" array whose elements are not computed until they are explicitly retrieved.
46
    For instance, `reinterpret` on the range `1:6` works similarly as on the dense vector `collect(1:6)`:
47

48
    ```jldoctest
49
    julia> reinterpret(Float32, UInt32[1 2 3 4 5])
50
    1×5 reinterpret(Float32, ::Matrix{UInt32}):
51
     1.0f-45  3.0f-45  4.0f-45  6.0f-45  7.0f-45
52

53
    julia> reinterpret(Complex{Int}, 1:6)
54
    3-element reinterpret(Complex{$Int}, ::UnitRange{$Int}):
55
     1 + 2im
56
     3 + 4im
57
     5 + 6im
58
    ```
59

60
    If the location of padding bits does not line up between `T` and `eltype(A)`, the resulting array will be
61
    read-only or write-only, to prevent invalid bits from being written to or read from, respectively.
62

63
    ```jldoctest
64
    julia> a = reinterpret(Tuple{UInt8, UInt32}, UInt32[1, 2])
65
    1-element reinterpret(Tuple{UInt8, UInt32}, ::Vector{UInt32}):
66
     (0x01, 0x00000002)
67

68
    julia> a[1] = 3
69
    ERROR: Padding of type Tuple{UInt8, UInt32} is not compatible with type UInt32.
70

71
    julia> b = reinterpret(UInt32, Tuple{UInt8, UInt32}[(0x01, 0x00000002)]); # showing will error
72

73
    julia> b[1]
74
    ERROR: Padding of type UInt32 is not compatible with type Tuple{UInt8, UInt32}.
75
    ```
76
    """
77
    function reinterpret(::Type{T}, a::A) where {T,N,S,A<:AbstractArray{S, N}}
232 ✔
78
        function thrownonint(S::Type, T::Type, dim)
232 ✔
79
            @noinline
6 ✔
80
            throw(ArgumentError(LazyString(
6 ✔
81
                "cannot reinterpret an `", S, "` array to `", T, "` whose first dimension has size `", dim,
82
                "`. The resulting array would have a non-integral first dimension.")))
83
        end
84
        function throwaxes1(S::Type, T::Type, ax1)
232 ✔
85
            @noinline
3 ✔
86
            throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T,
3 ✔
87
                "` when the first axis is ", ax1, ". Try reshaping first.")))
88
        end
89
        isbitstype(T) || throwbits(S, T, T)
232 ✔
90
        isbitstype(S) || throwbits(S, T, S)
232 ✔
91
        has_bit_padding(T) && throwbitpadding(S, T, T)
232 ✔
92
        has_bit_padding(S) && throwbitpadding(S, T, S)
232 ✔
93
        (N != 0 || aligned_sizeof(T) == aligned_sizeof(S)) || throwsize0(S, T, "different")
232 ✔
94
        if N != 0 && aligned_sizeof(S) != aligned_sizeof(T)
232 ✔
95
            ax1 = axes(a)[1]
232 ✔
96
            dim = length(ax1)
232 ✔
97
            if issingletontype(T)
232 ✔
98
                issingletontype(S) || throwsingleton(S, T)
3 ✔
99
            else
100
                rem(dim*aligned_sizeof(S),aligned_sizeof(T)) == 0 || thrownonint(S, T, dim)
232 ✔
101
            end
102
            first(ax1) == 1 || throwaxes1(S, T, ax1)
232 ✔
103
        end
104
        readable = array_subpadding(T, S)
232 ✔
105
        writable = array_subpadding(S, T)
232 ✔
106
        new{T, N, S, A, false}(a, readable, writable)
232 ✔
107
    end
108
    reinterpret(::Type{T}, a::AbstractArray{T}) where {T} = a
17 ✔
109

110
    # With reshaping
111
    function ReinterpretArray{T}(::typeof(reshape), a::A) where {T,S,A<:AbstractArray{S}}
35 ✔
112
        function throwintmult(S::Type, T::Type)
35 ✔
113
            @noinline
3 ✔
114
            throw(ArgumentError(LazyString("`reinterpret(reshape, T, a)` requires that one of `Base.elsize(Array{T})` (got ",
3 ✔
115
                aligned_sizeof(T), ") and `Base.elsize(Array{eltype(a)})` (got ", aligned_sizeof(S), ") be an integer multiple of the other")))
116
        end
117
        function throwsize1(a::AbstractArray, T::Type)
35 ✔
118
            @noinline
3 ✔
119
            throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", eltype(a),
3 ✔
120
                " requires that `axes(a, 1)` (got ", axes(a, 1), ") be equal to 1:",
121
                aligned_sizeof(T) ÷ aligned_sizeof(eltype(a)), " (from the ratio of element sizes)")))
122
        end
123
        function throwfromsingleton(S, T)
35 ✔
124
            @noinline
3 ✔
125
            throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", S,
3 ✔
126
                " requires that ", T, " be a singleton type, since ", S, " is one")))
127
        end
128
        isbitstype(T) || throwbits(S, T, T)
35 ✔
129
        isbitstype(S) || throwbits(S, T, S)
35 ✔
130
        has_bit_padding(T) && throwbitpadding(S, T, T)
35 ✔
131
        has_bit_padding(S) && throwbitpadding(S, T, S)
35 ✔
132
        if aligned_sizeof(S) == aligned_sizeof(T)
35 ✔
133
            N = ndims(a)
6 ✔
134
        elseif aligned_sizeof(S) > aligned_sizeof(T)
35 ✔
135
            issingletontype(T) && throwsingleton(S, T)
26 ✔
136
            rem(aligned_sizeof(S), aligned_sizeof(T)) == 0 || throwintmult(S, T)
26 ✔
137
            N = ndims(a) + 1
26 ✔
138
        else
139
            issingletontype(S) && throwfromsingleton(S, T)
15 ✔
140
            rem(aligned_sizeof(T), aligned_sizeof(S)) == 0 || throwintmult(S, T)
15 ✔
141
            N = ndims(a) - 1
15 ✔
142
            N > -1 || throwsize0(S, T, "larger")
15 ✔
143
            axes(a, 1) == OneTo(aligned_sizeof(T) ÷ aligned_sizeof(S)) || throwsize1(a, T)
15 ✔
144
        end
145
        readable = array_subpadding(T, S)
35 ✔
146
        writable = array_subpadding(S, T)
35 ✔
147
        new{T, N, S, A, true}(a, readable, writable)
35 ✔
148
    end
149
    reinterpret(::typeof(reshape), ::Type{T}, a::AbstractArray{T}) where {T} = a
3 ✔
150
end
151

152
ReshapedReinterpretArray{T,N,S,A<:AbstractArray{S}} = ReinterpretArray{T,N,S,A,true}
153
NonReshapedReinterpretArray{T,N,S,A<:AbstractArray{S, N}} = ReinterpretArray{T,N,S,A,false}
154

155
"""
156
    reinterpret(reshape, T, A::AbstractArray{S}) -> B
157

158
Change the type-interpretation of `A` while consuming or adding a "channel dimension."
159

160
If `Base.elsize(Array{T}) = n*Base.elsize(Array{S})` for `n>1`, `A`'s first dimension must be
161
of size `n` and `B` lacks `A`'s first dimension. Conversely, if `Base.elsize(Array{S}) = n*Base.elsize(Array{T})` for `n>1`,
162
`B` gets a new first dimension of size `n`. The dimensionality is unchanged if `Base.elsize(Array{T}) == Base.elsize(Array{S})`.
163

164
!!! compat "Julia 1.6"
165
    This method requires at least Julia 1.6.
166

167
# Examples
168

169
```jldoctest
170
julia> A = [1 2; 3 4]
171
2×2 Matrix{$Int}:
172
 1  2
173
 3  4
174

175
julia> reinterpret(reshape, Complex{Int}, A)    # the result is a vector
176
2-element reinterpret(reshape, Complex{$Int}, ::Matrix{$Int}) with eltype Complex{$Int}:
177
 1 + 3im
178
 2 + 4im
179

180
julia> a = [(1,2,3), (4,5,6)]
181
2-element Vector{Tuple{$Int, $Int, $Int}}:
182
 (1, 2, 3)
183
 (4, 5, 6)
184

185
julia> reinterpret(reshape, Int, a)             # the result is a matrix
186
3×2 reinterpret(reshape, $Int, ::Vector{Tuple{$Int, $Int, $Int}}) with eltype $Int:
187
 1  4
188
 2  5
189
 3  6
190
```
191
"""
192
reinterpret(::typeof(reshape), ::Type{T}, a::AbstractArray) where {T} = ReinterpretArray{T}(reshape, a)
35 ✔
193

194
reinterpret(::Type{T}, a::NonReshapedReinterpretArray) where {T} = reinterpret(T, a.parent)
9 ✔
195
function reinterpret(::typeof(reshape), ::Type{T}, a::ReshapedReinterpretArray{S,N,P}) where {T,S,N,P}
3 ✔
196
    t, s, p = aligned_sizeof(T), aligned_sizeof(S), aligned_sizeof(P)
3 ✔
197
    return t == s || t == p || s == p ? reinterpret(reshape, T, a.parent) : ReinterpretArray{T}(reshape, a)
3 ✔
198
end
199

200
# Definition of StridedArray
201
StridedFastContiguousSubArray{T,N,A<:DenseArray} = FastContiguousSubArray{T,N,A}
202
StridedReinterpretArray{T,N,A<:Union{DenseArray,StridedFastContiguousSubArray},IsReshaped} = ReinterpretArray{T,N,S,A,IsReshaped} where S
203
StridedReshapedArray{T,N,A<:Union{DenseArray,StridedFastContiguousSubArray,StridedReinterpretArray}} = ReshapedArray{T,N,A}
204
StridedSubArray{T,N,A<:Union{DenseArray,StridedReshapedArray,StridedReinterpretArray},
205
    I<:Tuple{Vararg{Union{RangeIndex, ReshapedUnitRange, AbstractCartesianIndex}}}} = SubArray{T,N,A,I}
206
StridedArray{T,N} = Union{DenseArray{T,N}, StridedSubArray{T,N}, StridedReshapedArray{T,N}, StridedReinterpretArray{T,N}}
207
StridedVector{T} = StridedArray{T,1}
208
StridedMatrix{T} = StridedArray{T,2}
209
StridedVecOrMat{T} = Union{StridedVector{T}, StridedMatrix{T}}
210

211
strides(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}) = size_to_strides(1, size(a)...)
109 ✔
212
stride(A::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}, k::Integer) =
87 ✔
213
    k ≤ ndims(A) ? strides(A)[k] : length(A)
214

215
function strides(a::ReinterpretArray{T,<:Any,S,<:AbstractArray{S},IsReshaped}) where {T,S,IsReshaped}
6 ✔
216
    _checkcontiguous(Bool, a) && return size_to_strides(1, size(a)...)
6 ✔
217
    stp = strides(parent(a))
6 ✔
218
    els, elp = aligned_sizeof(T), aligned_sizeof(S)
6 ✔
219
    els == elp && return stp # 0dim parent is also handled here.
6 ✔
220
    # Note: ep typically equals elp, but a custom strided parent may define elsize differently
221
    ep = elsize(parent(a))
6 ✔
222
    byte_strides = map(i -> i * ep, stp)
6 ✔
223
    IsReshaped && els < elp && return (1, _checked_strides(byte_strides, els)...)
6 ✔
224
    first(byte_strides) == elp || throw(ArgumentError("Parent must be contiguous in the 1st dimension!"))
3 ✔
225
    st′ = _checked_strides(tail(byte_strides), els)
3 ✔
226
    return IsReshaped ? st′ : (1, st′...)
3 ✔
227
end
228

229
@inline function _checked_strides(byte_strides::Tuple, els::Integer)
6 ✔
230
    drs = map(i -> divrem(i, els), byte_strides)
6 ✔
231
    all(i->iszero(i[2]), drs) ||
6 ✔
232
        throw(ArgumentError("Parent's strides could not be exactly divided!"))
233
    map(first, drs)
6 ✔
234
end
235

236
_checkcontiguous(::Type{Bool}, A::ReinterpretArray) = _checkcontiguous(Bool, parent(A))
23 ✔
237

238
similar(a::ReinterpretArray, T::Type, d::Dims) = similar(a.parent, T, d)
232 ✔
239
similar(::Type{TA}, dims::Dims) where {T,N,O,P,TA<:ReinterpretArray{T,N,O,P}} = similar(P, dims)
3 ✔
240

241
function check_readable(a::ReinterpretArray{T, N, S} where N) where {T,S}
232 ✔
242
    # See comment in check_writable
243
    if !a.readable && !array_subpadding(T, S)
232 ✔
244
        throw(PaddingError(T, S))
6 ✔
245
    end
246
end
247

248
function check_writable(a::ReinterpretArray{T, N, S} where N) where {T,S}
34 ✔
249
    # `array_subpadding` is relatively expensive (compared to a simple arrayref),
250
    # so it is cached in the array. However, it is computable at compile time if,
251
    # inference has the types available. By using this form of the check, we can
252
    # get the best of both worlds for the success case. If the types were not
253
    # available to inference, we simply need to check the field (relatively cheap)
254
    # and if they were we should be able to fold this check away entirely.
255
    if !a.writable && !array_subpadding(S, T)
34 ✔
256
        throw(PaddingError(T, S))
3 ✔
257
    end
258
end
259

260
## IndexStyle specializations
261

262
# For `reinterpret(reshape, T, a)` where we're adding a channel dimension and with
263
# `IndexStyle(a) == IndexLinear()`, it's advantageous to retain pseudo-linear indexing.
264
struct IndexSCartesian2{K} <: IndexStyle end   # K = aligned_sizeof(S) ÷ aligned_sizeof(T), a static-sized 2d cartesian iterator
23 ✔
265

266
# only `IndexLinear` is known to be valid when the axes change (#63305)
267
IndexStyle(::Type{ReinterpretArray{T,N,S,A,false}}) where {T,N,S,A<:AbstractArray{S,N}} =
232 ✔
268
    IndexStyle(A) === IndexLinear() ? IndexLinear() : IndexCartesian()
269

270
function IndexStyle(::Type{ReinterpretArray{T,N,S,A,true}}) where {T,N,S,A<:AbstractArray{S}}
32 ✔
271
    if aligned_sizeof(T) < aligned_sizeof(S)
32 ✔
272
        IndexStyle(A) === IndexLinear() && return IndexSCartesian2{aligned_sizeof(S) ÷ aligned_sizeof(T)}()
26 ✔
273
        return IndexCartesian()
6 ✔
274
    end
275
    return IndexStyle(A) === IndexLinear() ? IndexLinear() : IndexCartesian()
12 ✔
276
end
277
IndexStyle(::IndexSCartesian2{K}, ::IndexSCartesian2{K}) where {K} = IndexSCartesian2{K}()
3 ✔
278

279
struct SCartesianIndex2{K}   # can't make <:AbstractCartesianIndex without N, and 2 would be a bit misleading
280
    i::Int
6 ✔
281
    j::Int
282
end
283
to_index(i::SCartesianIndex2) = i
3 ✔
284
nextind(::AbstractArray, i::SCartesianIndex2{K}) where {K} =
3 ✔
285
    i.i < K ? SCartesianIndex2{K}(i.i + 1, i.j) : SCartesianIndex2{K}(1, i.j + 1)
286
prevind(::AbstractArray, i::SCartesianIndex2{K}) where {K} =
3 ✔
287
    i.i > 1 ? SCartesianIndex2{K}(i.i - 1, i.j) : SCartesianIndex2{K}(K, i.j - 1)
288

289
struct SCartesianIndices2{K,R<:AbstractUnitRange{Int}} <: AbstractMatrix{SCartesianIndex2{K}}
290
    indices2::R
6 ✔
291
end
292
SCartesianIndices2{K}(indices2::AbstractUnitRange{Int}) where {K} = (@assert K::Int > 1 "invalid index"; SCartesianIndices2{K,typeof(indices2)}(indices2))
6 ✔
293

294
eachindex(::IndexSCartesian2{K}, A::ReshapedReinterpretArray) where {K} = SCartesianIndices2{K}(eachindex(IndexLinear(), parent(A)))
3 ✔
295
@inline function eachindex(style::IndexSCartesian2{K}, A::AbstractArray, B::AbstractArray...) where {K}
3 ✔
296
    iter = eachindex(style, A)
3 ✔
297
    itersBs = map(C->eachindex(style, C), B)
3 ✔
298
    all(==(iter), itersBs) || throw_eachindex_mismatch_indices("axes", axes(A), map(axes, B)...)
3 ✔
299
    return iter
3 ✔
300
end
301

302
size(iter::SCartesianIndices2{K}) where K = (K, length(iter.indices2))
3 ✔
303
axes(iter::SCartesianIndices2{K}) where K = (OneTo(K), iter.indices2)
3 ✔
304

305
first(iter::SCartesianIndices2{K}) where {K} = SCartesianIndex2{K}(1, first(iter.indices2))
6 ✔
306
last(iter::SCartesianIndices2{K}) where {K}  = SCartesianIndex2{K}(K, last(iter.indices2))
3 ✔
307

308
@inline function getindex(iter::SCartesianIndices2{K}, i::Int, j::Int) where {K}
×
309
    @boundscheck checkbounds(iter, i, j)
×
310
    return SCartesianIndex2{K}(i, iter.indices2[j])
×
311
end
312

313
function iterate(iter::SCartesianIndices2{K}) where {K}
6 ✔
314
    ret = iterate(iter.indices2)
6 ✔
315
    ret === nothing && return nothing
6 ✔
316
    item2, state2 = ret
6 ✔
317
    return SCartesianIndex2{K}(1, item2), (1, item2, state2)
6 ✔
318
end
319

320
function iterate(iter::SCartesianIndices2{K}, (state1, item2, state2)) where {K}
6 ✔
321
    if state1 < K
6 ✔
322
        item1 = state1 + 1
6 ✔
323
        return SCartesianIndex2{K}(item1, item2), (item1, item2, state2)
6 ✔
324
    end
325
    ret = iterate(iter.indices2, state2)
6 ✔
326
    ret === nothing && return nothing
6 ✔
327
    item2, state2 = ret
6 ✔
328
    return SCartesianIndex2{K}(1, item2), (1, item2, state2)
6 ✔
329
end
330

331
SimdLoop.simd_outer_range(iter::SCartesianIndices2) = iter.indices2
×
332
SimdLoop.simd_inner_length(::SCartesianIndices2{K}, ::Any) where K = K
×
333
@inline function SimdLoop.simd_index(::SCartesianIndices2{K}, Ilast::Int, I1::Int) where {K}
×
334
    SCartesianIndex2{K}(I1+1, Ilast)
×
335
end
336

337
_maybe_reshape(::IndexSCartesian2, A::AbstractArray, I...) = _maybe_reshape(IndexCartesian(), A, I...)
3 ✔
338
_maybe_reshape(::IndexSCartesian2, A::ReshapedReinterpretArray, I...) = A
3 ✔
339

340
# fallbacks
341
function _getindex(::IndexSCartesian2, A::AbstractArray, I::Vararg{Int, N}) where {N}
3 ✔
342
    @_propagate_inbounds_meta
3 ✔
343
    _getindex(IndexCartesian(), A, I...)
3 ✔
344
end
345
function _setindex!(::IndexSCartesian2, A::AbstractArray, v, I::Vararg{Int, N}) where {N}
3 ✔
346
    @_propagate_inbounds_meta
3 ✔
347
    _setindex!(IndexCartesian(), A, v, I...)
3 ✔
348
end
349
# fallbacks for array types that use "pass-through" indexing (e.g., `IndexStyle(A) = IndexStyle(parent(A))`)
350
# but which don't handle SCartesianIndex2
351
function _getindex(style::IndexSCartesian2, A::AbstractArray, ind::SCartesianIndex2)
3 ✔
352
    @_propagate_inbounds_meta
3 ✔
353
    getindex(A, first(axes(A, 1)) + ind.i - 1, _scartesian2_trailing(style, A, ind)...)
3 ✔
354
end
355

356
function _setindex!(style::IndexSCartesian2, A::AbstractArray, v, ind::SCartesianIndex2)
3 ✔
357
    @_propagate_inbounds_meta
3 ✔
358
    setindex!(A, v, first(axes(A, 1)) + ind.i - 1, _scartesian2_trailing(style, A, ind)...)
3 ✔
359
end
360

361
# `ind.j` is a linear index of the innermost reinterpreted parent, which may not start at 1.
362
# Convert it to the matching indices in the trailing axes of `A`.
363
@propagate_inbounds function _scartesian2_trailing(style::IndexSCartesian2, A::AbstractArray, ind::SCartesianIndex2)
3 ✔
364
    k = ind.j - first(eachindex(style, A).indices2)
3 ✔
365
    ax = tail(axes(A))
3 ✔
366
    return length(ax) == 1 ? (first(ax[1]) + k,) : Tuple(CartesianIndices(ax)[k + 1])
3 ✔
367
end
368

369
eachindex(style::IndexSCartesian2, A::AbstractArray) = eachindex(style, parent(A))
3 ✔
370

371
## AbstractArray interface
372

373
parent(a::ReinterpretArray) = a.parent
232 ✔
374
dataids(a::ReinterpretArray) = dataids(a.parent)
42 ✔
375
unaliascopy(a::NonReshapedReinterpretArray{T}) where {T} = reinterpret(T, unaliascopy(a.parent))
3 ✔
376
unaliascopy(a::ReshapedReinterpretArray{T}) where {T} = reinterpret(reshape, T, unaliascopy(a.parent))
3 ✔
377

378
function size(a::NonReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
77 ✔
379
    psize = size(a.parent)
77 ✔
380
    size1 = issingletontype(T) ? psize[1] : div(psize[1]*aligned_sizeof(S), aligned_sizeof(T))
77 ✔
381
    tuple(size1, tail(psize)...)
77 ✔
382
end
383
function size(a::ReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
23 ✔
384
    psize = size(a.parent)
23 ✔
385
    aligned_sizeof(S) > aligned_sizeof(T) && return (div(aligned_sizeof(S), aligned_sizeof(T)), psize...)
23 ✔
386
    aligned_sizeof(S) < aligned_sizeof(T) && return tail(psize)
6 ✔
387
    return psize
6 ✔
388
end
389
size(a::NonReshapedReinterpretArray{T,0}) where {T} = ()
6 ✔
390

391
function axes(a::NonReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
232 ✔
392
    paxs = axes(a.parent)
232 ✔
393
    f, l = first(paxs[1]), length(paxs[1])
232 ✔
394
    size1 = issingletontype(T) ? l : div(l*aligned_sizeof(S), aligned_sizeof(T))
232 ✔
395
    tuple(oftype(paxs[1], f:f+size1-1), tail(paxs)...)
232 ✔
396
end
397
function axes(a::ReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
35 ✔
398
    paxs = axes(a.parent)
35 ✔
399
    aligned_sizeof(S) > aligned_sizeof(T) && return (OneTo(div(aligned_sizeof(S), aligned_sizeof(T))), paxs...)
35 ✔
400
    aligned_sizeof(S) < aligned_sizeof(T) && return tail(paxs)
15 ✔
401
    return paxs
3 ✔
402
end
403
axes(a::NonReshapedReinterpretArray{T,0}) where {T} = ()
3 ✔
404

405
has_offset_axes(a::ReinterpretArray) = has_offset_axes(a.parent)
24 ✔
406

407
function elsize(::Type{<:ReinterpretArray{T,<:Any,S,A}}) where {T,S,A}
232 ✔
408
    if aligned_sizeof(T) == aligned_sizeof(S)
232 ✔
409
        elsize(A)
6 ✔
410
    else
411
        aligned_sizeof(T)
232 ✔
412
    end
413
end
414
cconvert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = cconvert(Ptr{S}, a.parent)
55 ✔
415
unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = Ptr{T}(unsafe_convert(Ptr{S},a.parent))
2 ✔
416

417
@propagate_inbounds function getindex(a::NonReshapedReinterpretArray{T,0,S}) where {T,S}
3 ✔
418
    if _is_scalar_reinterpret_applicable(T, S)
3 ✔
419
        reinterpret(T, a.parent[])
3 ✔
420
    else
421
        a[firstindex(a)]
3 ✔
422
    end
423
end
424

425
function check_ptr_indexable(a::ReinterpretArray{T,<:Any,S}) where {T,S}
232 ✔
426
    aligned_sizeof(T) === aligned_sizeof(S) && return false
232 ✔
427
    return check_ptr_indexable(parent(a))
232 ✔
428
end
429
check_ptr_indexable(a::ReshapedArray) = check_ptr_indexable(parent(a))
9 ✔
430
check_ptr_indexable(a::FastContiguousSubArray) = check_ptr_indexable(parent(a))
9 ✔
431
check_ptr_indexable(a::Union{Array{S},Memory{S}}) where {S} = aligned_sizeof(S) > 0
232 ✔
432
check_ptr_indexable(a::AbstractArray) = false
21 ✔
433

434
_memoryref_and_byteoffset(a::Array) = (a.ref, 0)
232 ✔
435
_memoryref_and_byteoffset(a::Memory) = (memoryref(a), 0)
1,046 ✔
436
_memoryref_and_byteoffset(a::Union{ReinterpretArray,ReshapedArray}) = _memoryref_and_byteoffset(parent(a))
232 ✔
437
@inline function _memoryref_and_byteoffset(a::FastContiguousSubArray)
11 ✔
438
    ref, offset = _memoryref_and_byteoffset(parent(a))
11 ✔
439
    offset += (Int(first_index(a)) - Int(firstindex(parent(a)))) * elsize(parent(a))
11 ✔
440
    return ref, offset
11 ✔
441
end
442

443
@propagate_inbounds getindex(a::ReshapedReinterpretArray{T,0}) where {T} = a[firstindex(a)]
3 ✔
444

445
@propagate_inbounds isassigned(a::ReinterpretArray, inds::Integer...) = checkbounds(Bool, a, inds...) && (check_ptr_indexable(a) || _isassigned_ra(a, inds...))
3 ✔
446
@propagate_inbounds isassigned(a::ReinterpretArray, inds::SCartesianIndex2) = isassigned(a.parent, inds.j)
×
447
@propagate_inbounds _isassigned_ra(a::ReinterpretArray, inds...) = true # that is not entirely true, but computing exactly which indexes will be accessed in the parent requires a lot of duplication from the _getindex_ra code
×
448

449
@propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, inds::Vararg{Int, N}) where {T,N,S}
232 ✔
450
    check_readable(a)
232 ✔
451
    check_ptr_indexable(a) && return _getindex_ptr(a, inds...)
232 ✔
452
    _getindex_ra(a, inds[1], tail(inds))
59 ✔
453
end
454

455
@propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, i::Int) where {T,N,S}
21 ✔
456
    check_readable(a)
21 ✔
457
    check_ptr_indexable(a) && return _getindex_ptr(a, i)
21 ✔
458
    if isa(IndexStyle(a), IndexLinear)
9 ✔
459
        return _getindex_ra(a, i, ())
12 ✔
460
    end
461
    # Convert to full indices here, to avoid needing multiple conversions in
462
    # the loop in _getindex_ra
463
    inds = _to_subscript_indices(a, i)
3 ✔
464
    isempty(inds) ? _getindex_ra(a, firstindex(a), ()) : _getindex_ra(a, inds[1], tail(inds))
3 ✔
465
end
466

467
@propagate_inbounds function getindex(a::ReshapedReinterpretArray{T,N,S}, ind::SCartesianIndex2) where {T,N,S}
3 ✔
468
    check_readable(a)
3 ✔
469
    s = Ref{S}(a.parent[ind.j])
3 ✔
470
    tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
3 ✔
471
    GC.@preserve s return unsafe_load(tptr, ind.i)
3 ✔
472
end
473

474
@inline function _getindex_ptr(a::ReinterpretArray{T}, inds...) where {T}
232 ✔
475
    @boundscheck checkbounds(a, inds...)
232 ✔
476
    li = _to_linear_index(a, inds...)
232 ✔
477
    ref, offset = _memoryref_and_byteoffset(a)
232 ✔
478
    return Core.unsafe_memoryrefload(ref, T, offset + elsize(a) * (Int(li) - Int(firstindex(a))), false)
232 ✔
479
end
480

481
function _is_scalar_reinterpret_applicable(T, S)
61 ✔
482
    isprimitivetype(S) && isprimitivetype(T) && Core.bitsizeof(T) === Core.bitsizeof(S)
61 ✔
483
end
484

485
@propagate_inbounds function _getindex_ra(a::NonReshapedReinterpretArray{T,N,S}, i1::Int, tailinds::TT) where {T,N,S,TT}
62 ✔
486
    if issingletontype(T) # singleton types
60 ✔
487
        @boundscheck checkbounds(a, i1, tailinds...)
3 ✔
488
        return T.instance
3 ✔
489
    elseif _is_scalar_reinterpret_applicable(T, S)
60 ✔
490
        return reinterpret(T, a.parent[i1, tailinds...])
55 ✔
491
    else
492
        @boundscheck checkbounds(a, i1, tailinds...)
18 ✔
493
        ind_start, sidx = divrem((i1-1)*aligned_sizeof(T), aligned_sizeof(S))
18 ✔
494
        # Optimizations that avoid branches
495
        if aligned_sizeof(T) % aligned_sizeof(S) == 0
18 ✔
496
            # T is bigger than S and contains an integer number of them
497
            n = aligned_sizeof(T) ÷ aligned_sizeof(S)
9 ✔
498
            t = Ref{T}()
9 ✔
499
            GC.@preserve t begin
9 ✔
500
                sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
9 ✔
501
                for i = 1:n
9 ✔
502
                     s = a.parent[ind_start + i, tailinds...]
9 ✔
503
                     unsafe_store!(sptr, s, i)
9 ✔
504
                end
9 ✔
505
            end
506
            return t[]
9 ✔
507
        elseif aligned_sizeof(S) % aligned_sizeof(T) == 0
12 ✔
508
            # S is bigger than T and contains an integer number of them
509
            s = Ref{S}(a.parent[ind_start + 1, tailinds...])
12 ✔
510
            GC.@preserve s begin
12 ✔
511
                tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
12 ✔
512
                return unsafe_load(tptr + sidx)
12 ✔
513
            end
514
        else
515
            i = 1
3 ✔
516
            nbytes_copied = 0
3 ✔
517
            # This is a bit complicated to deal with partial elements
518
            # at both the start and the end. LLVM will fold as appropriate,
519
            # once it knows the data layout
520
            s = Ref{S}()
3 ✔
521
            t = Ref{T}()
3 ✔
522
            GC.@preserve s t begin
3 ✔
523
                sptr = Ptr{S}(unsafe_convert(Ref{S}, s))
3 ✔
524
                tptr = Ptr{T}(unsafe_convert(Ref{T}, t))
3 ✔
525
                while nbytes_copied < aligned_sizeof(T)
3 ✔
526
                    s[] = a.parent[ind_start + i, tailinds...]
3 ✔
527
                    nb = min(aligned_sizeof(S) - sidx, aligned_sizeof(T)-nbytes_copied)
3 ✔
528
                    memcpy(tptr + nbytes_copied, sptr + sidx, nb)
3 ✔
529
                    nbytes_copied += nb
3 ✔
530
                    sidx = 0
3 ✔
531
                    i += 1
3 ✔
532
                end
3 ✔
533
            end
534
            return t[]
3 ✔
535
        end
536
    end
537
end
538

539
@propagate_inbounds function _getindex_ra(a::ReshapedReinterpretArray{T,N,S}, i1::Int, tailinds::TT) where {T,N,S,TT}
12 ✔
540
    if issingletontype(T) # singleton types
9 ✔
541
        @boundscheck checkbounds(a, i1, tailinds...)
3 ✔
542
        return T.instance
3 ✔
543
    elseif _is_scalar_reinterpret_applicable(T, S)
9 ✔
544
        return reinterpret(T, a.parent[i1, tailinds...])
6 ✔
545
    end
546
    @boundscheck checkbounds(a, i1, tailinds...)
9 ✔
547
    if aligned_sizeof(T) >= aligned_sizeof(S)
9 ✔
548
        t = Ref{T}()
9 ✔
549
        GC.@preserve t begin
9 ✔
550
            sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
9 ✔
551
            if aligned_sizeof(T) > aligned_sizeof(S)
9 ✔
552
                # Extra dimension in the parent array
553
                n = aligned_sizeof(T) ÷ aligned_sizeof(S)
9 ✔
554
                if isempty(tailinds) && IndexStyle(a.parent) === IndexLinear()
9 ✔
555
                    offset = n * (i1 - firstindex(a))
9 ✔
556
                    for i = 1:n
9 ✔
557
                        s = a.parent[i + offset]
9 ✔
558
                        unsafe_store!(sptr, s, i)
9 ✔
559
                    end
9 ✔
560
                else
561
                    for i = 1:n
3 ✔
562
                        s = a.parent[i, i1, tailinds...]
3 ✔
563
                        unsafe_store!(sptr, s, i)
3 ✔
564
                    end
3 ✔
565
                end
566
            else
567
                # No extra dimension
568
                s = a.parent[i1, tailinds...]
3 ✔
569
                unsafe_store!(sptr, s)
9 ✔
570
            end
571
        end
572
        return t[]
9 ✔
573
    end
574
    # S is bigger than T and contains an integer number of them
575
    # n = aligned_sizeof(S) ÷ aligned_sizeof(T)
576
    s = Ref{S}()
3 ✔
577
    GC.@preserve s begin
3 ✔
578
        tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
3 ✔
579
        s[] = a.parent[tailinds...]
3 ✔
580
        return unsafe_load(tptr, i1)
3 ✔
581
    end
582
end
583

584
@propagate_inbounds function setindex!(a::NonReshapedReinterpretArray{T,0,S}, v) where {T,S}
3 ✔
585
    if _is_scalar_reinterpret_applicable(T, S)
3 ✔
586
        a.parent[] = reinterpret(S, convert(T, v)::T)
3 ✔
587
        return a
3 ✔
588
    end
589
    setindex!(a, v, firstindex(a))
3 ✔
590
end
591

592
@propagate_inbounds setindex!(a::ReshapedReinterpretArray{T,0}, v) where {T} = setindex!(a, v, firstindex(a))
3 ✔
593

594
@propagate_inbounds function setindex!(a::ReinterpretArray{T,N,S}, v, inds::Vararg{Int, N}) where {T,N,S}
34 ✔
595
    check_writable(a)
34 ✔
596
    check_ptr_indexable(a) && return _setindex_ptr!(a, v, inds...)
34 ✔
597
    _setindex_ra!(a, v, inds[1], tail(inds))
31 ✔
598
end
599

600
@propagate_inbounds function setindex!(a::ReinterpretArray{T,N,S}, v, i::Int) where {T,N,S}
6 ✔
601
    check_writable(a)
6 ✔
602
    check_ptr_indexable(a) && return _setindex_ptr!(a, v, i)
6 ✔
603
    if isa(IndexStyle(a), IndexLinear)
3 ✔
604
        return _setindex_ra!(a, v, i, ())
3 ✔
605
    end
606
    inds = _to_subscript_indices(a, i)
3 ✔
607
    isempty(inds) ? _setindex_ra!(a, v, firstindex(a), ()) : _setindex_ra!(a, v, inds[1], tail(inds))
3 ✔
608
end
609

610
@propagate_inbounds function setindex!(a::ReshapedReinterpretArray{T,N,S}, v, ind::SCartesianIndex2) where {T,N,S}
3 ✔
611
    check_writable(a)
3 ✔
612
    v = convert(T, v)::T
3 ✔
613
    s = Ref{S}(a.parent[ind.j])
3 ✔
614
    GC.@preserve s begin
3 ✔
615
        tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
3 ✔
616
        unsafe_store!(tptr, v, ind.i)
3 ✔
617
    end
618
    a.parent[ind.j] = s[]
3 ✔
619
    return a
3 ✔
620
end
621

622
@inline function _setindex_ptr!(a::ReinterpretArray{T}, v, inds...) where {T}
12 ✔
623
    v = convert(T, v)::T
12 ✔
624
    @boundscheck checkbounds(a, inds...)
12 ✔
625
    li = _to_linear_index(a, inds...)
12 ✔
626
    ref, offset = _memoryref_and_byteoffset(a)
12 ✔
627
    Core.unsafe_memoryrefstore!(ref, v, offset + elsize(a) * (Int(li) - Int(firstindex(a))), false)
12 ✔
628
    return a
12 ✔
629
end
630

631
@propagate_inbounds function _setindex_ra!(a::NonReshapedReinterpretArray{T,N,S}, v, i1::Int, tailinds::TT) where {T,N,S,TT}
31 ✔
632
    v = convert(T, v)::T
31 ✔
633
    if issingletontype(T) # singleton types
31 ✔
634
        @boundscheck checkbounds(a, i1, tailinds...)
3 ✔
635
        # setindex! is a noop except for the index check
636
    elseif _is_scalar_reinterpret_applicable(T, S)
31 ✔
637
        setindex!(a.parent, reinterpret(S, v), i1, tailinds...)
31 ✔
638
    else
639
        @boundscheck checkbounds(a, i1, tailinds...)
3 ✔
640
        ind_start, sidx = divrem((i1-1)*aligned_sizeof(T), aligned_sizeof(S))
3 ✔
641
        # Optimizations that avoid branches
642
        if aligned_sizeof(T) % aligned_sizeof(S) == 0
3 ✔
643
            # T is bigger than S and contains an integer number of them
644
            t = Ref{T}(v)
3 ✔
645
            GC.@preserve t begin
3 ✔
646
                sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
3 ✔
647
                n = aligned_sizeof(T) ÷ aligned_sizeof(S)
3 ✔
648
                for i = 1:n
3 ✔
649
                    s = unsafe_load(sptr, i)
3 ✔
650
                    a.parent[ind_start + i, tailinds...] = s
3 ✔
651
                end
3 ✔
652
            end
653
        elseif aligned_sizeof(S) % aligned_sizeof(T) == 0
3 ✔
654
            # S is bigger than T and contains an integer number of them
655
            s = Ref{S}(a.parent[ind_start + 1, tailinds...])
3 ✔
656
            GC.@preserve s begin
3 ✔
657
                tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
3 ✔
658
                unsafe_store!(tptr + sidx, v)
3 ✔
659
                a.parent[ind_start + 1, tailinds...] = s[]
3 ✔
660
            end
661
        else
662
            t = Ref{T}(v)
3 ✔
663
            s = Ref{S}()
3 ✔
664
            GC.@preserve t s begin
3 ✔
665
                tptr = Ptr{UInt8}(unsafe_convert(Ref{T}, t))
3 ✔
666
                sptr = Ptr{UInt8}(unsafe_convert(Ref{S}, s))
3 ✔
667
                nbytes_copied = 0
3 ✔
668
                i = 1
3 ✔
669
                # Deal with any partial elements at the start. We'll have to copy in the
670
                # element from the original array and overwrite the relevant parts
671
                if sidx != 0
3 ✔
672
                    s[] = a.parent[ind_start + i, tailinds...]
3 ✔
673
                    nb = min((aligned_sizeof(S) - sidx) % UInt, aligned_sizeof(T) % UInt)
3 ✔
674
                    memcpy(sptr + sidx, tptr, nb)
3 ✔
675
                    nbytes_copied += nb
3 ✔
676
                    a.parent[ind_start + i, tailinds...] = s[]
3 ✔
677
                    i += 1
3 ✔
678
                end
679
                # Deal with the main body of elements
680
                while nbytes_copied < aligned_sizeof(T) && (aligned_sizeof(T) - nbytes_copied) > aligned_sizeof(S)
3 ✔
681
                    nb = min(aligned_sizeof(S), aligned_sizeof(T) - nbytes_copied)
3 ✔
682
                    memcpy(sptr, tptr + nbytes_copied, nb)
3 ✔
683
                    nbytes_copied += nb
3 ✔
684
                    a.parent[ind_start + i, tailinds...] = s[]
3 ✔
685
                    i += 1
3 ✔
686
                end
3 ✔
687
                # Deal with trailing partial elements
688
                if nbytes_copied < aligned_sizeof(T)
3 ✔
689
                    s[] = a.parent[ind_start + i, tailinds...]
3 ✔
690
                    nb = min(aligned_sizeof(S), aligned_sizeof(T) - nbytes_copied)
3 ✔
691
                    memcpy(sptr, tptr + nbytes_copied, nb)
3 ✔
692
                    a.parent[ind_start + i, tailinds...] = s[]
3 ✔
693
                end
694
            end
695
        end
696
    end
697
    return a
31 ✔
698
end
699

700
@propagate_inbounds function _setindex_ra!(a::ReshapedReinterpretArray{T,N,S}, v, i1::Int, tailinds::TT) where {T,N,S,TT}
3 ✔
701
    v = convert(T, v)::T
3 ✔
702
    if issingletontype(T) # singleton types
3 ✔
703
        @boundscheck checkbounds(a, i1, tailinds...)
3 ✔
704
        # setindex! is a noop except for the index check
705
    elseif _is_scalar_reinterpret_applicable(T, S)
3 ✔
706
        setindex!(a.parent, reinterpret(S, v), i1, tailinds...)
3 ✔
707
    else
708
        @boundscheck checkbounds(a, i1, tailinds...)
3 ✔
709
        if aligned_sizeof(T) >= aligned_sizeof(S)
3 ✔
710
            t = Ref{T}(v)
3 ✔
711
            GC.@preserve t begin
3 ✔
712
                sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
3 ✔
713
                if aligned_sizeof(T) > aligned_sizeof(S)
3 ✔
714
                    # Extra dimension in the parent array
715
                    n = aligned_sizeof(T) ÷ aligned_sizeof(S)
3 ✔
716
                    if isempty(tailinds) && IndexStyle(a.parent) === IndexLinear()
3 ✔
717
                        offset = n * (i1 - firstindex(a))
3 ✔
718
                        for i = 1:n
3 ✔
719
                            s = unsafe_load(sptr, i)
3 ✔
720
                            a.parent[i + offset] = s
3 ✔
721
                        end
3 ✔
722
                    else
723
                        for i = 1:n
3 ✔
724
                            s = unsafe_load(sptr, i)
3 ✔
725
                            a.parent[i, i1, tailinds...] = s
3 ✔
726
                        end
3 ✔
727
                    end
728
                else # aligned_sizeof(T) == aligned_sizeof(S)
729
                    # No extra dimension
730
                    s = unsafe_load(sptr)
3 ✔
731
                    a.parent[i1, tailinds...] = s
3 ✔
732
                end
733
            end
734
        else
735
            # S is bigger than T and contains an integer number of them
736
            s = Ref{S}()
3 ✔
737
            GC.@preserve s begin
3 ✔
738
                tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
3 ✔
739
                s[] = a.parent[tailinds...]
3 ✔
740
                unsafe_store!(tptr, v, i1)
3 ✔
741
                a.parent[tailinds...] = s[]
3 ✔
742
            end
743
        end
744
    end
745
    return a
3 ✔
746
end
747

748
struct PaddingError <: Exception
749
    S::Type
6 ✔
750
    T::Type
751
end
752

753
function showerror(io::IO, p::PaddingError)
×
754
    print(io, "Padding of type $(p.S) is not compatible with type $(p.T).")
×
755
end
756

757
# Return a byte index to Bool map for each byte of an aligned `T`
758
# if false, that byte is undefined padding that should not be observed.
759
# Preconditions, already checked by the `reinterpret` constructors and `_reinterpret`:
760
# `isbitstype(T)` and `!has_bit_padding(T)`. A byte holding both value and padding
761
# bits is neither, so the latter is checked here as well.
762
function non_padding_bytes(T::DataType)::Memory{Bool}
6 ✔
763
    # @assert isbitstype(T)
764
    has_bit_padding(T) && throw(ArgumentError(LazyString("type `", T,
6 ✔
765
        "` contains non-byte-aligned primitive fields, so its padding bytes are not defined")))
766
    used = Memory{Bool}(undef, aligned_sizeof(T))
6 ✔
767
    fill!(used, false)
6 ✔
768
    fill_nonpadding_bytes!(T, 0, used)
6 ✔
769
    used
6 ✔
770
end
771
function fill_nonpadding_bytes!(T::DataType, offset::Int, used::Memory{Bool})
6 ✔
772
    if isprimitivetype(T)
6 ✔
773
        # sizeof rounds the value bytes up to a multiple of the alignment, so the
774
        # bytes past them are padding
775
        for i in 1:cld(Core.bitsizeof(T), 8)
6 ✔
776
            used[i + offset] = true
6 ✔
777
        end
6 ✔
778
    else
779
        for i in 1:fieldcount(T)
6 ✔
780
            fill_nonpadding_bytes!(fieldtype(T, i), offset + Int(fieldoffset(T, i)), used)
6 ✔
781
        end
6 ✔
782
    end
783
end
784

785
# Preconditions, already checked by the `reinterpret` constructors:
786
# `isbitstype(T/S)` and `!has_bit_padding(T/S)`
787
@assume_effects :foldable function array_subpadding(S, T)
3 ✔
788
    # Fast path: if every byte of `T` is a non-padding byte, any byte can be
789
    # read. This also covers zero-size `T`.
790
    if ispacked(T)
3 ✔
791
        return true
×
792
    end
793
    # `T` has at least one padding byte here. If `S` has none, the byte cycle
794
    # below visits every byte of `T`, so some readable byte of `S` must land on
795
    # padding in `T`. This also covers zero-size `S`.
796
    if ispacked(S)
3 ✔
797
        return false
×
798
    end
799
    s_used, t_used = non_padding_bytes(S), non_padding_bytes(T)
3 ✔
800
    # zero-size T and S are unreachable here (handled by the fast paths above);
801
    # these guards only keep the loop below well-defined
802
    isempty(t_used) && return true
3 ✔
803
    isempty(s_used) && return false
3 ✔
804
    s_n, t_n = length(s_used), length(t_used)
3 ✔
805
    s_i, t_i = 1, 1
3 ✔
806
    while true
3 ✔
807
        # If a byte can be read in S, but is padding in T, return false
808
        if s_used[s_i] && !t_used[t_i]
3 ✔
809
            return false
3 ✔
810
        end
811
        # Advance the indexes with wrapping around
812
        s_i += 1
3 ✔
813
        if s_i > s_n
3 ✔
814
            s_i = 1
3 ✔
815
        end
816
        t_i += 1
3 ✔
817
        if t_i > t_n
3 ✔
818
            t_i = 1
3 ✔
819
        end
820
        # We made it back to the start
821
        if isone(s_i) && isone(t_i)
3 ✔
822
            return true
3 ✔
823
        end
824
    end
3 ✔
825
end
826

827
@assume_effects :foldable function struct_subpadding(Out, In)
6 ✔
828
    non_padding_bytes(Out) == non_padding_bytes(In)
6 ✔
829
end
830

831
@assume_effects :foldable function packedsize(T)
6 ✔
832
    if !datatype_haspadding(T)
6 ✔
833
        sizeof(T)
×
834
    else
835
        count(non_padding_bytes(T))
6 ✔
836
    end
837
end
838

839
@assume_effects :foldable function ispacked(T)
6 ✔
840
    !datatype_haspadding(T)
6 ✔
841
end
842

843
@assume_effects :foldable function has_bit_padding(::Type{T}) where T
98 ✔
844
    if isprimitivetype(T)
98 ✔
845
        return Core.bitsizeof(T) % 8 != 0
×
846
    end
847
    T isa Union && return any(has_bit_padding, uniontypes(T))
98 ✔
848
    for i in 1:fieldcount(T)
98 ✔
849
        has_bit_padding(fieldtype(T, i)) && return true
98 ✔
850
    end
98 ✔
851
    return false
98 ✔
852
end
853

854
# The copies that move each non-padding byte of `In` to the corresponding one of `Out`, as
855
# `(source offset, destination offset, size)` triples. The plan is returned as a type
856
# parameter: as a value, a plan of more than a few copies would exceed the size of constants
857
# the optimizer embeds in code, and the plan would be recomputed on every call.
858
# Precondition, already checked by `_reinterpret`: `packedsize(Out) == packedsize(In)`.
859
@assume_effects :foldable function packed_copies(Out, In)
6 ✔
860
    src, dst = non_padding_bytes(In), non_padding_bytes(Out)
6 ✔
861
    copies = Tuple{Int,Int,Int}[]
6 ✔
862
    s = t = 1
6 ✔
863
    while true
6 ✔
864
        s = findnext(src, s)
6 ✔
865
        s === nothing && break
6 ✔
866
        t = findnext(dst, t)::Int
6 ✔
867
        # extend the copy while both sides stay contiguous
868
        n = 1
6 ✔
869
        while s + n <= length(src) && src[s + n] && dst[t + n]
6 ✔
870
            n += 1
6 ✔
871
        end
6 ✔
872
        push!(copies, (s - 1, t - 1, n))
6 ✔
873
        s += n
6 ✔
874
        t += n
6 ✔
875
    end
6 ✔
876
    return Val((copies...,))
6 ✔
877
end
878

879
@inline function _reinterpret(::Type{Out}, x::In) where {Out, In}
228 ✔
880
    # handle non-primitive types
881
    isbitstype(Out) || throw(ArgumentError("Target type for `reinterpret` must be isbits"))
228 ✔
882
    isbitstype(In) || throw(ArgumentError("Source type for `reinterpret` must be isbits"))
228 ✔
883
    has_bit_padding(Out) && throw(ArgumentError(LazyString(
228 ✔
884
        "cannot `reinterpret` type ", Out, " containing non-byte-aligned primitive fields")))
885
    has_bit_padding(In) && throw(ArgumentError(LazyString(
228 ✔
886
        "cannot `reinterpret` type ", In, " containing non-byte-aligned primitive fields")))
887
    inpackedsize = packedsize(In)
228 ✔
888
    outpackedsize = packedsize(Out)
228 ✔
889
    inpackedsize == outpackedsize ||
228 ✔
890
        throw(ArgumentError(LazyString("Packed sizes of types ", Out, " and ", In,
891
            " do not match; got ", outpackedsize, " and ", inpackedsize, ", respectively.")))
892
    in = Ref{In}(x)
228 ✔
893
    out = Ref{Out}()
228 ✔
894
    if (ispacked(In) && ispacked(Out)) || struct_subpadding(Out, In)
228 ✔
895
        # if packed the same, just copy
896
        GC.@preserve in out begin
228 ✔
897
            ptr_in = unsafe_convert(Ptr{In}, in)
228 ✔
898
            ptr_out = unsafe_convert(Ptr{Out}, out)
228 ✔
899
            memcpy(ptr_out, ptr_in, sizeof(Out))
228 ✔
900
        end
901
        return out[]
228 ✔
902
    else
903
        # mismatched padding
904
        return _reinterpret_padding(Out, x)
6 ✔
905
    end
906
end
907

908
# If the code reaches this part, it needs to handle padding and is unlikely
909
# to compile to a noop. Therefore, we don't forcibly inline it.
910
function _reinterpret_padding(::Type{Out}, x::In) where {Out, In}
6 ✔
911
    in = Ref{In}(x)
6 ✔
912
    out = Ref{Out}()
6 ✔
913
    GC.@preserve in out begin
6 ✔
914
        ptr_in = Ptr{UInt8}(unsafe_convert(Ptr{In}, in))
6 ✔
915
        ptr_out = Ptr{UInt8}(unsafe_convert(Ptr{Out}, out))
6 ✔
916
        _copy_packed!(ptr_out, ptr_in, packed_copies(Out, In))
6 ✔
917
    end
918
    return out[]
6 ✔
919
end
920

921
@inline function _copy_packed!(ptr_out::Ptr{UInt8}, ptr_in::Ptr{UInt8}, ::Val{copies}) where {copies}
6 ✔
922
    for (src, dst, n) in copies
6 ✔
923
        memcpy(ptr_out + dst, ptr_in + src, n)
6 ✔
924
    end
6 ✔
925
end
926

927
function String(v::ReinterpretArray{UInt8,1,S,<:Union{Vector{S},Memory{S}},IsReshaped}) where {S,IsReshaped}
3 ✔
928
    len = length(v)
3 ✔
929
    len == 0 && return ""
3 ✔
930
    check_readable(v) # stringifying empty arrays is always allowed
3 ✔
931
    return ccall(:jl_pchar_to_string, Ref{String}, (Ptr{UInt8}, Int), v, len)
3 ✔
932
end
933

934
# Reductions with IndexSCartesian2
935

936
function _mapreduce(f::F, op::OP, style::IndexSCartesian2{K}, A::AbstractArrayOrBroadcasted) where {F,OP,K}
3 ✔
937
    inds = eachindex(style, A)
3 ✔
938
    n = size(inds)[2]
3 ✔
939
    if n == 0
3 ✔
940
        return mapreduce_empty_iter(f, op, A, IteratorEltype(A))
×
941
    else
942
        return mapreduce_impl(f, op, A, first(inds), last(inds))
3 ✔
943
    end
944
end
945

946
@noinline function mapreduce_impl(f::F, op::OP, A::AbstractArrayOrBroadcasted,
3 ✔
947
                                  ifirst::SCI, ilast::SCI, blksize::Int) where {F,OP,SCI<:SCartesianIndex2{K}} where K
948
    if ilast.j - ifirst.j < blksize
3 ✔
949
        # sequential portion
950
        @inbounds a1 = A[ifirst]
3 ✔
951
        @inbounds a2 = A[SCI(2,ifirst.j)]
3 ✔
952
        v = op(f(a1), f(a2))
3 ✔
953
        @simd for i = ifirst.i + 2 : K
3 ✔
954
            @inbounds ai = A[SCI(i,ifirst.j)]
3 ✔
955
            v = op(v, f(ai))
3 ✔
956
        end
957
        # Remaining columns
958
        for j = ifirst.j+1 : ilast.j
3 ✔
959
            @simd for i = 1:K
3 ✔
960
                @inbounds ai = A[SCI(i,j)]
3 ✔
961
                v = op(v, f(ai))
3 ✔
962
            end
963
        end
3 ✔
964
        return v
3 ✔
965
    else
966
        # pairwise portion
967
        jmid = ifirst.j + (ilast.j - ifirst.j) >> 1
3 ✔
968
        v1 = mapreduce_impl(f, op, A, ifirst, SCI(K,jmid), blksize)
3 ✔
969
        v2 = mapreduce_impl(f, op, A, SCI(1,jmid+1), ilast, blksize)
3 ✔
970
        return op(v1, v2)
3 ✔
971
    end
972
end
973

974
mapreduce_impl(f::F, op::OP, A::AbstractArrayOrBroadcasted, ifirst::SCartesianIndex2, ilast::SCartesianIndex2) where {F,OP} =
3 ✔
975
    mapreduce_impl(f, op, A, ifirst, ilast, pairwise_blocksize(f, op))
976

977
# Fast path for `NTuple{N,E}(::Union{Array,Memory})` with `N >= 32`: when the
978
# eltype matches and is isbits, the storage is layout-identical to the tuple,
979
# so a single `reinterpret` load suffices (O(1) in N, unlike the All32 fallback).
980
function _totuple(T::Type{All32{E,N}}, itr::Union{Array,Memory}) where {E,N}
9 ✔
981
    len = N + 32
9 ✔
982
    length(itr) >= len || _totuple_err(T)
9 ✔
983
    if isbitstype(E) && eltype(itr) === E
9 ✔
984
        v = length(itr) == len ? vec(itr) : view(itr, 1:len)
9 ✔
985
        return @inbounds reinterpret(T, v)[1]
9 ✔
986
    end
987
    elts = collect(E, Iterators.take(itr, len))
3 ✔
988
    return (elts...,)
3 ✔
989
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc