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

JuliaLang / julia / #38205

28 Aug 2025 06:55AM UTC coverage: 77.955% (-0.003%) from 77.958%
#38205

push

local

web-flow
PCRE2: New version 10.46 (#59416)

48572 of 62308 relevant lines covered (77.95%)

10154018.81 hits per line

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

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

3
"""
4
Gives a reinterpreted view (of element type T) of the underlying array (of element type S).
5
If the size of `T` differs from the size of `S`, the array will be compressed/expanded in
6
the first dimension. The variant `reinterpret(reshape, T, a)` instead adds or consumes the first dimension
7
depending on the ratio of element sizes.
8
"""
9
struct ReinterpretArray{T,N,S,A<:AbstractArray{S},IsReshaped} <: AbstractArray{T, N}
10
    parent::A
11
    readable::Bool
12
    writable::Bool
13

14
    function throwbits(S::Type, T::Type, U::Type)
×
15
        @noinline
×
16
        throw(ArgumentError(LazyString("cannot reinterpret `", S, "` as `", T, "`, type `", U, "` is not a bits type")))
×
17
    end
18
    function throwsize0(S::Type, T::Type, msg)
5✔
19
        @noinline
5✔
20
        throw(ArgumentError(LazyString("cannot reinterpret a zero-dimensional `", S, "` array to `", T,
5✔
21
            "` which is of a ", msg, " size")))
22
    end
23
    function throwsingleton(S::Type, T::Type)
×
24
        @noinline
×
25
        throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T, "` which is a singleton type")))
×
26
    end
27

28
    global reinterpret
29

30
    @doc """
31
        reinterpret(T::DataType, A::AbstractArray)
32

33
    Construct a view of the array with the same binary data as the given
34
    array, but with `T` as element type.
35

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

39
    ```jldoctest
40
    julia> reinterpret(Float32, UInt32[1 2 3 4 5])
41
    1×5 reinterpret(Float32, ::Matrix{UInt32}):
42
     1.0f-45  3.0f-45  4.0f-45  6.0f-45  7.0f-45
43

44
    julia> reinterpret(Complex{Int}, 1:6)
45
    3-element reinterpret(Complex{$Int}, ::UnitRange{$Int}):
46
     1 + 2im
47
     3 + 4im
48
     5 + 6im
49
    ```
50

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

54
    ```jldoctest
55
    julia> a = reinterpret(Tuple{UInt8, UInt32}, UInt32[1, 2])
56
    1-element reinterpret(Tuple{UInt8, UInt32}, ::Vector{UInt32}):
57
     (0x01, 0x00000002)
58

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

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

64
    julia> b[1]
65
    ERROR: Padding of type UInt32 is not compatible with type Tuple{UInt8, UInt32}.
66
    ```
67
    """
68
    function reinterpret(::Type{T}, a::A) where {T,N,S,A<:AbstractArray{S, N}}
852✔
69
        function thrownonint(S::Type, T::Type, dim)
66,434✔
70
            @noinline
×
71
            throw(ArgumentError(LazyString(
×
72
                "cannot reinterpret an `", S, "` array to `", T, "` whose first dimension has size `", dim,
73
                "`. The resulting array would have a non-integral first dimension.")))
74
        end
75
        function throwaxes1(S::Type, T::Type, ax1)
66,436✔
76
            @noinline
2✔
77
            throw(ArgumentError(LazyString("cannot reinterpret a `", S, "` array to `", T,
2✔
78
                "` when the first axis is ", ax1, ". Try reshaping first.")))
79
        end
80
        isbitstype(T) || throwbits(S, T, T)
66,434✔
81
        isbitstype(S) || throwbits(S, T, S)
66,431✔
82
        (N != 0 || sizeof(T) == sizeof(S)) || throwsize0(S, T, "different")
66,428✔
83
        if N != 0 && sizeof(S) != sizeof(T)
66,424✔
84
            ax1 = axes(a)[1]
67,075✔
85
            dim = length(ax1)
65,972✔
86
            if issingletontype(T)
65,972✔
87
                issingletontype(S) || throwsingleton(S, T)
2✔
88
            else
89
                rem(dim*sizeof(S),sizeof(T)) == 0 || thrownonint(S, T, dim)
67,036✔
90
            end
91
            first(ax1) == 1 || throwaxes1(S, T, ax1)
65,970✔
92
        end
93
        readable = array_subpadding(T, S)
66,418✔
94
        writable = array_subpadding(S, T)
66,418✔
95
        new{T, N, S, A, false}(a, readable, writable)
69,998✔
96
    end
97
    reinterpret(::Type{T}, a::AbstractArray{T}) where {T} = a
155✔
98

99
    # With reshaping
100
    function reinterpret(::typeof(reshape), ::Type{T}, a::A) where {T,S,A<:AbstractArray{S}}
96✔
101
        function throwintmult(S::Type, T::Type)
949✔
102
            @noinline
103
            throw(ArgumentError(LazyString("`reinterpret(reshape, T, a)` requires that one of `sizeof(T)` (got ",
104
                sizeof(T), ") and `sizeof(eltype(a))` (got ", sizeof(S), ") be an integer multiple of the other")))
105
        end
106
        function throwsize1(a::AbstractArray, T::Type)
951✔
107
            @noinline
2✔
108
            throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", eltype(a),
2✔
109
                " requires that `axes(a, 1)` (got ", axes(a, 1), ") be equal to 1:",
110
                sizeof(T) ÷ sizeof(eltype(a)), " (from the ratio of element sizes)")))
111
        end
112
        function throwfromsingleton(S, T)
952✔
113
            @noinline
3✔
114
            throw(ArgumentError(LazyString("`reinterpret(reshape, ", T, ", a)` where `eltype(a)` is ", S,
3✔
115
                " requires that ", T, " be a singleton type, since ", S, " is one")))
116
        end
117
        isbitstype(T) || throwbits(S, T, T)
949✔
118
        isbitstype(S) || throwbits(S, T, S)
947✔
119
        if sizeof(S) == sizeof(T)
946✔
120
            N = ndims(a)
66✔
121
        elseif sizeof(S) > sizeof(T)
880✔
122
            issingletontype(T) && throwsingleton(S, T)
814✔
123
            rem(sizeof(S), sizeof(T)) == 0 || throwintmult(S, T)
811✔
124
            N = ndims(a) + 1
811✔
125
        else
126
            issingletontype(S) && throwfromsingleton(S, T)
66✔
127
            rem(sizeof(T), sizeof(S)) == 0 || throwintmult(S, T)
63✔
128
            N = ndims(a) - 1
62✔
129
            N > -1 || throwsize0(S, T, "larger")
62✔
130
            axes(a, 1) == OneTo(sizeof(T) ÷ sizeof(S)) || throwsize1(a, T)
63✔
131
        end
132
        readable = array_subpadding(T, S)
936✔
133
        writable = array_subpadding(S, T)
936✔
134
        new{T, N, S, A, true}(a, readable, writable)
936✔
135
    end
136
    reinterpret(::typeof(reshape), ::Type{T}, a::AbstractArray{T}) where {T} = a
2✔
137
end
138

139
ReshapedReinterpretArray{T,N,S,A<:AbstractArray{S}} = ReinterpretArray{T,N,S,A,true}
140
NonReshapedReinterpretArray{T,N,S,A<:AbstractArray{S, N}} = ReinterpretArray{T,N,S,A,false}
141

142
"""
143
    reinterpret(reshape, T, A::AbstractArray{S}) -> B
144

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

147
If `sizeof(T) = n*sizeof(S)` for `n>1`, `A`'s first dimension must be
148
of size `n` and `B` lacks `A`'s first dimension. Conversely, if `sizeof(S) = n*sizeof(T)` for `n>1`,
149
`B` gets a new first dimension of size `n`. The dimensionality is unchanged if `sizeof(T) == sizeof(S)`.
150

151
!!! compat "Julia 1.6"
152
    This method requires at least Julia 1.6.
153

154
# Examples
155

156
```jldoctest
157
julia> A = [1 2; 3 4]
158
2×2 Matrix{$Int}:
159
 1  2
160
 3  4
161

162
julia> reinterpret(reshape, Complex{Int}, A)    # the result is a vector
163
2-element reinterpret(reshape, Complex{$Int}, ::Matrix{$Int}) with eltype Complex{$Int}:
164
 1 + 3im
165
 2 + 4im
166

167
julia> a = [(1,2,3), (4,5,6)]
168
2-element Vector{Tuple{$Int, $Int, $Int}}:
169
 (1, 2, 3)
170
 (4, 5, 6)
171

172
julia> reinterpret(reshape, Int, a)             # the result is a matrix
173
3×2 reinterpret(reshape, $Int, ::Vector{Tuple{$Int, $Int, $Int}}) with eltype $Int:
174
 1  4
175
 2  5
176
 3  6
177
```
178
"""
179
reinterpret(::typeof(reshape), T::Type, a::AbstractArray)
180

181
reinterpret(::Type{T}, a::NonReshapedReinterpretArray) where {T} = reinterpret(T, a.parent)
154✔
182
reinterpret(::typeof(reshape), ::Type{T}, a::ReshapedReinterpretArray) where {T} = reinterpret(reshape, T, a.parent)
2✔
183

184
# Definition of StridedArray
185
StridedFastContiguousSubArray{T,N,A<:DenseArray} = FastContiguousSubArray{T,N,A}
186
StridedReinterpretArray{T,N,A<:Union{DenseArray,StridedFastContiguousSubArray},IsReshaped} = ReinterpretArray{T,N,S,A,IsReshaped} where S
187
StridedReshapedArray{T,N,A<:Union{DenseArray,StridedFastContiguousSubArray,StridedReinterpretArray}} = ReshapedArray{T,N,A}
188
StridedSubArray{T,N,A<:Union{DenseArray,StridedReshapedArray,StridedReinterpretArray},
189
    I<:Tuple{Vararg{Union{RangeIndex, ReshapedUnitRange, AbstractCartesianIndex}}}} = SubArray{T,N,A,I}
190
StridedArray{T,N} = Union{DenseArray{T,N}, StridedSubArray{T,N}, StridedReshapedArray{T,N}, StridedReinterpretArray{T,N}}
191
StridedVector{T} = StridedArray{T,1}
192
StridedMatrix{T} = StridedArray{T,2}
193
StridedVecOrMat{T} = Union{StridedVector{T}, StridedMatrix{T}}
194

195
strides(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}) = size_to_strides(1, size(a)...)
24,340,070✔
196
stride(A::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}, k::Integer) =
16,196,452✔
197
    k ≤ ndims(A) ? strides(A)[k] : length(A)
198

199
function strides(a::ReinterpretArray{T,<:Any,S,<:AbstractArray{S},IsReshaped}) where {T,S,IsReshaped}
101✔
200
    _checkcontiguous(Bool, a) && return size_to_strides(1, size(a)...)
3,127✔
201
    stp = strides(parent(a))
3,106✔
202
    els, elp = sizeof(T), sizeof(S)
3,106✔
203
    els == elp && return stp # 0dim parent is also handled here.
3,106✔
204
    IsReshaped && els < elp && return (1, _checked_strides(stp, els, elp)...)
1,976✔
205
    stp[1] == 1 || throw(ArgumentError("Parent must be contiguous in the 1st dimension!"))
670✔
206
    st′ = _checked_strides(tail(stp), els, elp)
645✔
207
    return IsReshaped ? st′ : (1, st′...)
607✔
208
end
209

210
@inline function _checked_strides(stp::Tuple, els::Integer, elp::Integer)
211
    if elp > els && rem(elp, els) == 0
1,954✔
212
        N = div(elp, els)
1,797✔
213
        return map(i -> N * i, stp)
4,210✔
214
    end
215
    drs = map(i -> divrem(elp * i, els), stp)
314✔
216
    all(i->iszero(i[2]), drs) ||
393✔
217
        throw(ArgumentError("Parent's strides could not be exactly divided!"))
218
    map(first, drs)
138✔
219
end
220

221
_checkcontiguous(::Type{Bool}, A::ReinterpretArray) = _checkcontiguous(Bool, parent(A))
4,777✔
222

223
similar(a::ReinterpretArray, T::Type, d::Dims) = similar(a.parent, T, d)
65,816✔
224
similar(::Type{TA}, dims::Dims) where {T,N,O,P,TA<:ReinterpretArray{T,N,O,P}} = similar(P, dims)
1✔
225

226
function check_readable(a::ReinterpretArray{T, N, S} where N) where {T,S}
2✔
227
    # See comment in check_writable
228
    if !a.readable && !array_subpadding(T, S)
1,754,508✔
229
        throw(PaddingError(T, S))
2✔
230
    end
231
end
232

233
function check_writable(a::ReinterpretArray{T, N, S} where N) where {T,S}
234
    # `array_subpadding` is relatively expensive (compared to a simple arrayref),
235
    # so it is cached in the array. However, it is computable at compile time if,
236
    # inference has the types available. By using this form of the check, we can
237
    # get the best of both worlds for the success case. If the types were not
238
    # available to inference, we simply need to check the field (relatively cheap)
239
    # and if they were we should be able to fold this check away entirely.
240
    if !a.writable && !array_subpadding(S, T)
41,289,044✔
241
        throw(PaddingError(T, S))
2✔
242
    end
243
end
244

245
## IndexStyle specializations
246

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

251
IndexStyle(::Type{ReinterpretArray{T,N,S,A,false}}) where {T,N,S,A<:AbstractArray{S,N}} = IndexStyle(A)
134,002✔
252
function IndexStyle(::Type{ReinterpretArray{T,N,S,A,true}}) where {T,N,S,A<:AbstractArray{S}}
253
    if sizeof(T) < sizeof(S)
4,259✔
254
        IndexStyle(A) === IndexLinear() && return IndexSCartesian2{sizeof(S) ÷ sizeof(T)}()
3,335✔
255
        return IndexCartesian()
1,197✔
256
    end
257
    return IndexStyle(A)
924✔
258
end
259
IndexStyle(::IndexSCartesian2{K}, ::IndexSCartesian2{K}) where {K} = IndexSCartesian2{K}()
3✔
260

261
struct SCartesianIndex2{K}   # can't make <:AbstractCartesianIndex without N, and 2 would be a bit misleading
262
    i::Int
8,303✔
263
    j::Int
264
end
265
to_index(i::SCartesianIndex2) = i
84✔
266

267
struct SCartesianIndices2{K,R<:AbstractUnitRange{Int}} <: AbstractMatrix{SCartesianIndex2{K}}
268
    indices2::R
41✔
269
end
270
SCartesianIndices2{K}(indices2::AbstractUnitRange{Int}) where {K} = (@assert K::Int > 1; SCartesianIndices2{K,typeof(indices2)}(indices2))
41✔
271

272
eachindex(::IndexSCartesian2{K}, A::ReshapedReinterpretArray) where {K} = SCartesianIndices2{K}(eachindex(IndexLinear(), parent(A)))
38✔
273
@inline function eachindex(style::IndexSCartesian2{K}, A::AbstractArray, B::AbstractArray...) where {K}
274
    iter = eachindex(style, A)
3✔
275
    itersBs = map(C->eachindex(style, C), B)
6✔
276
    all(==(iter), itersBs) || throw_eachindex_mismatch_indices("axes", axes(A), map(axes, B)...)
3✔
277
    return iter
3✔
278
end
279

280
size(iter::SCartesianIndices2{K}) where K = (K, length(iter.indices2))
4✔
281
axes(iter::SCartesianIndices2{K}) where K = (OneTo(K), iter.indices2)
9✔
282

283
first(iter::SCartesianIndices2{K}) where {K} = SCartesianIndex2{K}(1, first(iter.indices2))
5✔
284
last(iter::SCartesianIndices2{K}) where {K}  = SCartesianIndex2{K}(K, last(iter.indices2))
9✔
285

286
@inline function getindex(iter::SCartesianIndices2{K}, i::Int, j::Int) where {K}
×
287
    @boundscheck checkbounds(iter, i, j)
×
288
    return SCartesianIndex2{K}(i, iter.indices2[j])
×
289
end
290

291
function iterate(iter::SCartesianIndices2{K}) where {K}
292
    ret = iterate(iter.indices2)
36✔
293
    ret === nothing && return nothing
35✔
294
    item2, state2 = ret
35✔
295
    return SCartesianIndex2{K}(1, item2), (1, item2, state2)
35✔
296
end
297

298
function iterate(iter::SCartesianIndices2{K}, (state1, item2, state2)) where {K}
299
    if state1 < K
253✔
300
        item1 = state1 + 1
157✔
301
        return SCartesianIndex2{K}(item1, item2), (item1, item2, state2)
157✔
302
    end
303
    ret = iterate(iter.indices2, state2)
169✔
304
    ret === nothing && return nothing
96✔
305
    item2, state2 = ret
73✔
306
    return SCartesianIndex2{K}(1, item2), (1, item2, state2)
73✔
307
end
308

309
SimdLoop.simd_outer_range(iter::SCartesianIndices2) = iter.indices2
×
310
SimdLoop.simd_inner_length(::SCartesianIndices2{K}, ::Any) where K = K
×
311
@inline function SimdLoop.simd_index(::SCartesianIndices2{K}, Ilast::Int, I1::Int) where {K}
×
312
    SCartesianIndex2{K}(I1+1, Ilast)
×
313
end
314

315
_maybe_reshape(::IndexSCartesian2, A::AbstractArray, I...) = _maybe_reshape(IndexCartesian(), A, I...)
1✔
316
_maybe_reshape(::IndexSCartesian2, A::ReshapedReinterpretArray, I...) = A
4✔
317

318
# fallbacks
319
function _getindex(::IndexSCartesian2, A::AbstractArray, I::Vararg{Int, N}) where {N}
320
    @_propagate_inbounds_meta
66✔
321
    _getindex(IndexCartesian(), A, I...)
66✔
322
end
323
function _setindex!(::IndexSCartesian2, A::AbstractArray, v, I::Vararg{Int, N}) where {N}
×
324
    @_propagate_inbounds_meta
×
325
    _setindex!(IndexCartesian(), A, v, I...)
×
326
end
327
# fallbacks for array types that use "pass-through" indexing (e.g., `IndexStyle(A) = IndexStyle(parent(A))`)
328
# but which don't handle SCartesianIndex2
329
function _getindex(::IndexSCartesian2, A::AbstractArray{T,N}, ind::SCartesianIndex2) where {T,N}
×
330
    @_propagate_inbounds_meta
×
331
    J = _ind2sub(tail(axes(A)), ind.j)
×
332
    getindex(A, ind.i, J...)
×
333
end
334

335
function _getindex(::IndexSCartesian2{2}, A::AbstractArray{T,2}, ind::SCartesianIndex2) where {T}
336
    @_propagate_inbounds_meta
30✔
337
    J = first(axes(A, 2)) + ind.j - 1
30✔
338
    getindex(A, ind.i, J)
30✔
339
end
340

341
function _setindex!(::IndexSCartesian2, A::AbstractArray{T,N}, v, ind::SCartesianIndex2) where {T,N}
342
    @_propagate_inbounds_meta
24✔
343
    J = _ind2sub(tail(axes(A)), ind.j)
24✔
344
    setindex!(A, v, ind.i, J...)
24✔
345
end
346

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

353
eachindex(style::IndexSCartesian2, A::AbstractArray) = eachindex(style, parent(A))
18✔
354

355
## AbstractArray interface
356

357
parent(a::ReinterpretArray) = a.parent
1,986,923✔
358
dataids(a::ReinterpretArray) = dataids(a.parent)
6,296✔
359
unaliascopy(a::NonReshapedReinterpretArray{T}) where {T} = reinterpret(T, unaliascopy(a.parent))
2✔
360
unaliascopy(a::ReshapedReinterpretArray{T}) where {T} = reinterpret(reshape, T, unaliascopy(a.parent))
1✔
361

362
function size(a::NonReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
6✔
363
    psize = size(a.parent)
14,818✔
364
    size1 = issingletontype(T) ? psize[1] : div(psize[1]*sizeof(S), sizeof(T))
14,789✔
365
    tuple(size1, tail(psize)...)
14,789✔
366
end
367
function size(a::ReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
7✔
368
    psize = size(a.parent)
4,317✔
369
    sizeof(S) > sizeof(T) && return (div(sizeof(S), sizeof(T)), psize...)
4,283✔
370
    sizeof(S) < sizeof(T) && return tail(psize)
508✔
371
    return psize
200✔
372
end
373
size(a::NonReshapedReinterpretArray{T,0}) where {T} = ()
53✔
374

375
function axes(a::NonReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
482✔
376
    paxs = axes(a.parent)
1,707,032✔
377
    f, l = first(paxs[1]), length(paxs[1])
1,652,364✔
378
    size1 = issingletontype(T) ? l : div(l*sizeof(S), sizeof(T))
1,704,220✔
379
    tuple(oftype(paxs[1], f:f+size1-1), tail(paxs)...)
1,704,259✔
380
end
381
function axes(a::ReshapedReinterpretArray{T,N,S} where {N}) where {T,S}
1,410✔
382
    paxs = axes(a.parent)
22,430✔
383
    sizeof(S) > sizeof(T) && return (OneTo(div(sizeof(S), sizeof(T))), paxs...)
16,110✔
384
    sizeof(S) < sizeof(T) && return tail(paxs)
2,293✔
385
    return paxs
1,819✔
386
end
387
axes(a::NonReshapedReinterpretArray{T,0}) where {T} = ()
6✔
388

389
has_offset_axes(a::ReinterpretArray) = has_offset_axes(a.parent)
88✔
390

391
elsize(::Type{<:ReinterpretArray{T}}) where {T} = sizeof(T)
1,983,295✔
392
cconvert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = cconvert(Ptr{S}, a.parent)
1,472,771✔
393
unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = Ptr{T}(unsafe_convert(Ptr{S},a.parent))
2✔
394

395
@propagate_inbounds function getindex(a::NonReshapedReinterpretArray{T,0,S}) where {T,S}
1✔
396
    if isprimitivetype(T) && isprimitivetype(S)
15✔
397
        reinterpret(T, a.parent[])
6✔
398
    else
399
        a[firstindex(a)]
9✔
400
    end
401
end
402

403
check_ptr_indexable(a::ReinterpretArray, sz = elsize(a)) = check_ptr_indexable(parent(a), sz)
3,957,945✔
404
check_ptr_indexable(a::ReshapedArray, sz) = check_ptr_indexable(parent(a), sz)
494✔
405
check_ptr_indexable(a::FastContiguousSubArray, sz) = check_ptr_indexable(parent(a), sz)
309,292✔
406
check_ptr_indexable(a::Array, sz) = sizeof(eltype(a)) !== sz
1,656,069✔
407
check_ptr_indexable(a::Memory, sz) = true
×
408
check_ptr_indexable(a::AbstractArray, sz) = false
×
409

410
@propagate_inbounds getindex(a::ReshapedReinterpretArray{T,0}) where {T} = a[firstindex(a)]
40✔
411

412
@propagate_inbounds isassigned(a::ReinterpretArray, inds::Integer...) = checkbounds(Bool, a, inds...) && (check_ptr_indexable(a) || _isassigned_ra(a, inds...))
12✔
413
@propagate_inbounds isassigned(a::ReinterpretArray, inds::SCartesianIndex2) = isassigned(a.parent, inds.j)
×
414
@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
×
415

416
@propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, inds::Vararg{Int, N}) where {T,N,S}
92✔
417
    check_readable(a)
4,039,809✔
418
    check_ptr_indexable(a) && return _getindex_ptr(a, inds...)
4,091,663✔
419
    _getindex_ra(a, inds[1], tail(inds))
40,461,288✔
420
end
421

422
@propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, i::Int) where {T,N,S}
36✔
423
    check_readable(a)
9,735✔
424
    check_ptr_indexable(a) && return _getindex_ptr(a, i)
9,735✔
425
    if isa(IndexStyle(a), IndexLinear)
3,443✔
426
        return _getindex_ra(a, i, ())
673✔
427
    end
428
    # Convert to full indices here, to avoid needing multiple conversions in
429
    # the loop in _getindex_ra
430
    inds = _to_subscript_indices(a, i)
4,692✔
431
    isempty(inds) ? _getindex_ra(a, firstindex(a), ()) : _getindex_ra(a, inds[1], tail(inds))
2,884✔
432
end
433

434
@propagate_inbounds function getindex(a::ReshapedReinterpretArray{T,N,S}, ind::SCartesianIndex2) where {T,N,S}
435
    check_readable(a)
8,068✔
436
    s = Ref{S}(a.parent[ind.j])
8,068✔
437
    tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
8,068✔
438
    GC.@preserve s return unsafe_load(tptr, ind.i)
8,068✔
439
end
440

441
@inline function _getindex_ptr(a::ReinterpretArray{T}, inds...) where {T}
442
    @boundscheck checkbounds(a, inds...)
1,467,518✔
443
    li = _to_linear_index(a, inds...)
1,415,650✔
444
    ap = cconvert(Ptr{T}, a)
1,467,506✔
445
    p = unsafe_convert(Ptr{T}, ap) + sizeof(T) * (li - 1)
1,467,506✔
446
    GC.@preserve ap return unsafe_load(p)
1,467,506✔
447
end
448

449
@propagate_inbounds function _getindex_ra(a::NonReshapedReinterpretArray{T,N,S}, i1::Int, tailinds::TT) where {T,N,S,TT}
450
    # Make sure to match the scalar reinterpret if that is applicable
451
    if sizeof(T) == sizeof(S) && (fieldcount(T) + fieldcount(S)) == 0
2,631,873✔
452
        if issingletontype(T) # singleton types
2,630,252✔
453
            @boundscheck checkbounds(a, i1, tailinds...)
93✔
454
            return T.instance
87✔
455
        end
456
        return reinterpret(T, a.parent[i1, tailinds...])
40,460,850✔
457
    else
458
        @boundscheck checkbounds(a, i1, tailinds...)
1,639✔
459
        ind_start, sidx = divrem((i1-1)*sizeof(T), sizeof(S))
1,603✔
460
        # Optimizations that avoid branches
461
        if sizeof(T) % sizeof(S) == 0
1,603✔
462
            # T is bigger than S and contains an integer number of them
463
            n = sizeof(T) ÷ sizeof(S)
83✔
464
            t = Ref{T}()
83✔
465
            GC.@preserve t begin
83✔
466
                sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
83✔
467
                for i = 1:n
83✔
468
                     s = a.parent[ind_start + i, tailinds...]
140✔
469
                     unsafe_store!(sptr, s, i)
132✔
470
                end
181✔
471
            end
472
            return t[]
83✔
473
        elseif sizeof(S) % sizeof(T) == 0
1,520✔
474
            # S is bigger than T and contains an integer number of them
475
            s = Ref{S}(a.parent[ind_start + 1, tailinds...])
1,508✔
476
            GC.@preserve s begin
1,484✔
477
                tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
1,484✔
478
                return unsafe_load(tptr + sidx)
1,484✔
479
            end
480
        else
481
            i = 1
36✔
482
            nbytes_copied = 0
36✔
483
            # This is a bit complicated to deal with partial elements
484
            # at both the start and the end. LLVM will fold as appropriate,
485
            # once it knows the data layout
486
            s = Ref{S}()
36✔
487
            t = Ref{T}()
36✔
488
            GC.@preserve s t begin
36✔
489
                sptr = Ptr{S}(unsafe_convert(Ref{S}, s))
36✔
490
                tptr = Ptr{T}(unsafe_convert(Ref{T}, t))
36✔
491
                while nbytes_copied < sizeof(T)
116✔
492
                    s[] = a.parent[ind_start + i, tailinds...]
84✔
493
                    nb = min(sizeof(S) - sidx, sizeof(T)-nbytes_copied)
80✔
494
                    memcpy(tptr + nbytes_copied, sptr + sidx, nb)
80✔
495
                    nbytes_copied += nb
80✔
496
                    sidx = 0
80✔
497
                    i += 1
80✔
498
                end
116✔
499
            end
500
            return t[]
36✔
501
        end
502
    end
503
end
504

505
@propagate_inbounds function _getindex_ra(a::ReshapedReinterpretArray{T,N,S}, i1::Int, tailinds::TT) where {T,N,S,TT}
506
    # Make sure to match the scalar reinterpret if that is applicable
507
    if sizeof(T) == sizeof(S) && (fieldcount(T) + fieldcount(S)) == 0
2,013✔
508
        if issingletontype(T) # singleton types
689✔
509
            @boundscheck checkbounds(a, i1, tailinds...)
64✔
510
            return T.instance
62✔
511
        end
512
        return reinterpret(T, a.parent[i1, tailinds...])
626✔
513
    end
514
    @boundscheck checkbounds(a, i1, tailinds...)
1,324✔
515
    if sizeof(T) >= sizeof(S)
1,324✔
516
        t = Ref{T}()
122✔
517
        GC.@preserve t begin
122✔
518
            sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
122✔
519
            if sizeof(T) > sizeof(S)
122✔
520
                # Extra dimension in the parent array
521
                n = sizeof(T) ÷ sizeof(S)
114✔
522
                if isempty(tailinds) && IndexStyle(a.parent) === IndexLinear()
114✔
523
                    offset = n * (i1 - firstindex(a))
32✔
524
                    for i = 1:n
32✔
525
                        s = a.parent[i + offset]
156✔
526
                        unsafe_store!(sptr, s, i)
156✔
527
                    end
280✔
528
                else
529
                    for i = 1:n
82✔
530
                        s = a.parent[i, i1, tailinds...]
184✔
531
                        unsafe_store!(sptr, s, i)
164✔
532
                    end
246✔
533
                end
534
            else
535
                # No extra dimension
536
                s = a.parent[i1, tailinds...]
8✔
537
                unsafe_store!(sptr, s)
122✔
538
            end
539
        end
540
        return t[]
122✔
541
    end
542
    # S is bigger than T and contains an integer number of them
543
    # n = sizeof(S) ÷ sizeof(T)
544
    s = Ref{S}()
1,202✔
545
    GC.@preserve s begin
1,202✔
546
        tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
1,202✔
547
        s[] = a.parent[tailinds...]
1,214✔
548
        return unsafe_load(tptr, i1)
1,202✔
549
    end
550
end
551

552
@propagate_inbounds function setindex!(a::NonReshapedReinterpretArray{T,0,S}, v) where {T,S}
2✔
553
    if isprimitivetype(S) && isprimitivetype(T)
13✔
554
        a.parent[] = reinterpret(S, convert(T, v)::T)
5✔
555
        return a
3✔
556
    end
557
    setindex!(a, v, firstindex(a))
8✔
558
end
559

560
@propagate_inbounds setindex!(a::ReshapedReinterpretArray{T,0}, v) where {T} = setindex!(a, v, firstindex(a))
28✔
561

562
@propagate_inbounds function setindex!(a::ReinterpretArray{T,N,S}, v, inds::Vararg{Int, N}) where {T,N,S}
57✔
563
    check_writable(a)
41,417,675✔
564
    check_ptr_indexable(a) && return _setindex_ptr!(a, v, inds...)
41,417,673✔
565
    _setindex_ra!(a, v, inds[1], tail(inds))
41,433,359✔
566
end
567

568
@propagate_inbounds function setindex!(a::ReinterpretArray{T,N,S}, v, i::Int) where {T,N,S}
34✔
569
    check_writable(a)
180✔
570
    check_ptr_indexable(a) && return _setindex_ptr!(a, v, i)
180✔
571
    if isa(IndexStyle(a), IndexLinear)
137✔
572
        return _setindex_ra!(a, v, i, ())
107✔
573
    end
574
    inds = _to_subscript_indices(a, i)
52✔
575
    isempty(inds) ? _setindex_ra!(a, v, firstindex(a), ()) : _setindex_ra!(a, v, inds[1], tail(inds))
56✔
576
end
577

578
@propagate_inbounds function setindex!(a::ReshapedReinterpretArray{T,N,S}, v, ind::SCartesianIndex2) where {T,N,S}
579
    check_writable(a)
5✔
580
    v = convert(T, v)::T
5✔
581
    s = Ref{S}(a.parent[ind.j])
5✔
582
    GC.@preserve s begin
5✔
583
        tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
5✔
584
        unsafe_store!(tptr, v, ind.i)
5✔
585
    end
586
    a.parent[ind.j] = s[]
5✔
587
    return a
5✔
588
end
589

590
@inline function _setindex_ptr!(a::ReinterpretArray{T}, v, inds...) where {T}
591
    @boundscheck checkbounds(a, inds...)
826✔
592
    li = _to_linear_index(a, inds...)
814✔
593
    ap = cconvert(Ptr{T}, a)
814✔
594
    p = unsafe_convert(Ptr{T}, ap) + sizeof(T) * (li - 1)
814✔
595
    GC.@preserve ap unsafe_store!(p, v)
814✔
596
    return a
814✔
597
end
598

599
@propagate_inbounds function _setindex_ra!(a::NonReshapedReinterpretArray{T,N,S}, v, i1::Int, tailinds::TT) where {T,N,S,TT}
600
    v = convert(T, v)::T
41,416,958✔
601
    # Make sure to match the scalar reinterpret if that is applicable
602
    if sizeof(T) == sizeof(S) && (fieldcount(T) + fieldcount(S)) == 0
41,416,924✔
603
        if issingletontype(T) # singleton types
41,416,800✔
604
            @boundscheck checkbounds(a, i1, tailinds...)
6✔
605
            # setindex! is a noop except for the index check
606
        else
607
            setindex!(a.parent, reinterpret(S, v), i1, tailinds...)
41,433,260✔
608
        end
609
    else
610
        @boundscheck checkbounds(a, i1, tailinds...)
142✔
611
        ind_start, sidx = divrem((i1-1)*sizeof(T), sizeof(S))
106✔
612
        # Optimizations that avoid branches
613
        if sizeof(T) % sizeof(S) == 0
106✔
614
            # T is bigger than S and contains an integer number of them
615
            t = Ref{T}(v)
19✔
616
            GC.@preserve t begin
19✔
617
                sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
19✔
618
                n = sizeof(T) ÷ sizeof(S)
19✔
619
                for i = 1:n
19✔
620
                    s = unsafe_load(sptr, i)
25✔
621
                    a.parent[ind_start + i, tailinds...] = s
25✔
622
                end
31✔
623
            end
624
        elseif sizeof(S) % sizeof(T) == 0
87✔
625
            # S is bigger than T and contains an integer number of them
626
            s = Ref{S}(a.parent[ind_start + 1, tailinds...])
79✔
627
            GC.@preserve s begin
79✔
628
                tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
79✔
629
                unsafe_store!(tptr + sidx, v)
79✔
630
                a.parent[ind_start + 1, tailinds...] = s[]
79✔
631
            end
632
        else
633
            t = Ref{T}(v)
8✔
634
            s = Ref{S}()
8✔
635
            GC.@preserve t s begin
8✔
636
                tptr = Ptr{UInt8}(unsafe_convert(Ref{T}, t))
8✔
637
                sptr = Ptr{UInt8}(unsafe_convert(Ref{S}, s))
8✔
638
                nbytes_copied = 0
8✔
639
                i = 1
8✔
640
                # Deal with any partial elements at the start. We'll have to copy in the
641
                # element from the original array and overwrite the relevant parts
642
                if sidx != 0
8✔
643
                    s[] = a.parent[ind_start + i, tailinds...]
5✔
644
                    nb = min((sizeof(S) - sidx) % UInt, sizeof(T) % UInt)
4✔
645
                    memcpy(sptr + sidx, tptr, nb)
4✔
646
                    nbytes_copied += nb
4✔
647
                    a.parent[ind_start + i, tailinds...] = s[]
4✔
648
                    i += 1
4✔
649
                    sidx = 0
4✔
650
                end
651
                # Deal with the main body of elements
652
                while nbytes_copied < sizeof(T) && (sizeof(T) - nbytes_copied) > sizeof(S)
12✔
653
                    nb = min(sizeof(S), sizeof(T) - nbytes_copied)
4✔
654
                    memcpy(sptr, tptr + nbytes_copied, nb)
4✔
655
                    nbytes_copied += nb
4✔
656
                    a.parent[ind_start + i, tailinds...] = s[]
4✔
657
                    i += 1
4✔
658
                end
4✔
659
                # Deal with trailing partial elements
660
                if nbytes_copied < sizeof(T)
8✔
661
                    s[] = a.parent[ind_start + i, tailinds...]
10✔
662
                    nb = min(sizeof(S), sizeof(T) - nbytes_copied)
8✔
663
                    memcpy(sptr, tptr + nbytes_copied, nb)
8✔
664
                    a.parent[ind_start + i, tailinds...] = s[]
8✔
665
                end
666
            end
667
        end
668
    end
669
    return a
41,416,914✔
670
end
671

672
@propagate_inbounds function _setindex_ra!(a::ReshapedReinterpretArray{T,N,S}, v, i1::Int, tailinds::TT) where {T,N,S,TT}
673
    v = convert(T, v)::T
89✔
674
    # Make sure to match the scalar reinterpret if that is applicable
675
    if sizeof(T) == sizeof(S) && (fieldcount(T) + fieldcount(S)) == 0
88✔
676
        if issingletontype(T) # singleton types
16✔
677
            @boundscheck checkbounds(a, i1, tailinds...)
3✔
678
            # setindex! is a noop except for the index check
679
        else
680
            setindex!(a.parent, reinterpret(S, v), i1, tailinds...)
14✔
681
        end
682
    end
683
    @boundscheck checkbounds(a, i1, tailinds...)
87✔
684
    if sizeof(T) >= sizeof(S)
87✔
685
        t = Ref{T}(v)
47✔
686
        GC.@preserve t begin
47✔
687
            sptr = Ptr{S}(unsafe_convert(Ref{T}, t))
47✔
688
            if sizeof(T) > sizeof(S)
47✔
689
                # Extra dimension in the parent array
690
                n = sizeof(T) ÷ sizeof(S)
27✔
691
                if isempty(tailinds) && IndexStyle(a.parent) === IndexLinear()
27✔
692
                    offset = n * (i1 - firstindex(a))
4✔
693
                    for i = 1:n
4✔
694
                        s = unsafe_load(sptr, i)
8✔
695
                        a.parent[i + offset] = s
8✔
696
                    end
12✔
697
                else
698
                    for i = 1:n
23✔
699
                        s = unsafe_load(sptr, i)
46✔
700
                        a.parent[i, i1, tailinds...] = s
46✔
701
                    end
69✔
702
                end
703
            else # sizeof(T) == sizeof(S)
704
                # No extra dimension
705
                s = unsafe_load(sptr)
20✔
706
                a.parent[i1, tailinds...] = s
47✔
707
            end
708
        end
709
    else
710
        # S is bigger than T and contains an integer number of them
711
        s = Ref{S}()
40✔
712
        GC.@preserve s begin
40✔
713
            tptr = Ptr{T}(unsafe_convert(Ref{S}, s))
40✔
714
            s[] = a.parent[tailinds...]
48✔
715
            unsafe_store!(tptr, v, i1)
40✔
716
            a.parent[tailinds...] = s[]
40✔
717
        end
718
    end
719
    return a
87✔
720
end
721

722
# Padding
723
struct Padding
724
    offset::Int # 0-indexed offset of the next valid byte; sizeof(T) indicates trailing padding
1✔
725
    size::Int   # bytes of padding before a valid byte
726
end
727
function intersect(p1::Padding, p2::Padding)
×
728
    start = max(p1.offset, p2.offset)
×
729
    stop = min(p1.offset + p1.size, p2.offset + p2.size)
×
730
    Padding(start, max(0, stop-start))
×
731
end
732

733
struct PaddingError <: Exception
734
    S::Type
4✔
735
    T::Type
736
end
737

738
function showerror(io::IO, p::PaddingError)
×
739
    print(io, "Padding of type $(p.S) is not compatible with type $(p.T).")
×
740
end
741

742
"""
743
    CyclePadding(padding, total_size)
744

745
Cycles an iterator of `Padding` structs, restarting the padding at `total_size`.
746
E.g. if `padding` is all the padding in a struct and `total_size` is the total
747
aligned size of that array, `CyclePadding` will correspond to the padding in an
748
infinite vector of such structs.
749
"""
750
struct CyclePadding{P}
751
    padding::P
7,758✔
752
    total_size::Int
753
end
754
eltype(::Type{<:CyclePadding}) = Padding
×
755
IteratorSize(::Type{<:CyclePadding}) = IsInfinite()
×
756
isempty(cp::CyclePadding) = isempty(cp.padding)
×
757
function iterate(cp::CyclePadding)
24✔
758
    y = iterate(cp.padding)
3,879✔
759
    y === nothing && return nothing
3,879✔
760
    y[1], (0, y[2])
×
761
end
762
function iterate(cp::CyclePadding, state::Tuple)
×
763
    y = iterate(cp.padding, tail(state)...)
×
764
    y === nothing && return iterate(cp, (state[1]+cp.total_size,))
×
765
    Padding(y[1].offset+state[1], y[1].size), (state[1], tail(y)...)
×
766
end
767

768
"""
769
    Compute the location of padding in an isbits datatype. Recursive over the fields of that type.
770
"""
771
@assume_effects :foldable function padding(T::DataType, baseoffset::Int = 0)
10,579✔
772
    pads = Padding[]
18,556✔
773
    last_end::Int = baseoffset
10,594✔
774
    for i = 1:fieldcount(T)
10,708✔
775
        offset = baseoffset + Int(fieldoffset(T, i))
685✔
776
        fT = fieldtype(T, i)
685✔
777
        append!(pads, padding(fT, offset))
685✔
778
        if offset != last_end
685✔
779
            push!(pads, Padding(offset, offset-last_end))
×
780
        end
781
        last_end = offset + sizeof(fT)
685✔
782
    end
1,255✔
783
    if 0 < last_end - baseoffset < sizeof(T)
10,594✔
784
        push!(pads, Padding(baseoffset + sizeof(T), sizeof(T) - last_end + baseoffset))
1✔
785
    end
786
    return Core.svec(pads...)
10,596✔
787
end
788

789
function CyclePadding(T::DataType)
7,758✔
790
    a, s = datatype_alignment(T), sizeof(T)
7,758✔
791
    as = s + (a - (s % a)) % a
7,758✔
792
    pad = padding(T)
7,758✔
793
    if s != as
7,758✔
794
        pad = Core.svec(pad..., Padding(s, as - s))
×
795
    end
796
    CyclePadding(pad, as)
7,758✔
797
end
798

799
@assume_effects :total function array_subpadding(S, T)
3,879✔
800
    lcm_size = lcm(sizeof(S), sizeof(T))
3,879✔
801
    s, t = CyclePadding(S), CyclePadding(T)
3,879✔
802
    checked_size = 0
3,879✔
803
    # use of Stateful harms inference and makes this vulnerable to invalidation
804
    (pad, tstate) = let
805
        it = iterate(t)
3,879✔
806
        it === nothing && return true
3,879✔
807
        it
×
808
    end
809
    (ps, sstate) = let
810
        it = iterate(s)
×
811
        it === nothing && return false
×
812
        it
×
813
    end
814
    while checked_size < lcm_size
×
815
        while true
×
816
            # See if there's corresponding padding in S
817
            ps.offset > pad.offset && return false
×
818
            intersect(ps, pad) == pad && break
×
819
            ps, sstate = iterate(s, sstate)
×
820
        end
×
821
        checked_size = pad.offset + pad.size
×
822
        pad, tstate = iterate(t, tstate)
×
823
    end
×
824
    return true
×
825
end
826

827
@assume_effects :foldable function struct_subpadding(::Type{Out}, ::Type{In}) where {Out, In}
47✔
828
    padding(Out) == padding(In)
47✔
829
end
830

831
@assume_effects :foldable function packedsize(::Type{T}) where T
79✔
832
    pads = padding(T)
79✔
833
    return sizeof(T) - sum((p.size for p ∈ pads), init = 0)
79✔
834
end
835

836
@assume_effects :foldable ispacked(::Type{T}) where T = isempty(padding(T))
16✔
837

838
function _copytopacked!(ptr_out::Ptr{Out}, ptr_in::Ptr{In}) where {Out, In}
3✔
839
    writeoffset = 0
4✔
840
    for i ∈ 1:fieldcount(In)
4✔
841
        readoffset = fieldoffset(In, i)
13✔
842
        fT = fieldtype(In, i)
13✔
843
        if ispacked(fT)
15✔
844
            readsize = sizeof(fT)
14✔
845
            memcpy(ptr_out + writeoffset, ptr_in + readoffset, readsize)
12✔
846
            writeoffset += readsize
12✔
847
        else # nested padded type
848
            _copytopacked!(ptr_out + writeoffset, Ptr{fT}(ptr_in + readoffset))
2✔
849
            writeoffset += packedsize(fT)
1✔
850
        end
851
    end
14✔
852
end
853

854
function _copyfrompacked!(ptr_out::Ptr{Out}, ptr_in::Ptr{In}) where {Out, In}
3✔
855
    readoffset = 0
4✔
856
    for i ∈ 1:fieldcount(Out)
4✔
857
        writeoffset = fieldoffset(Out, i)
13✔
858
        fT = fieldtype(Out, i)
13✔
859
        if ispacked(fT)
15✔
860
            writesize = sizeof(fT)
14✔
861
            memcpy(ptr_out + writeoffset, ptr_in + readoffset, writesize)
12✔
862
            readoffset += writesize
12✔
863
        else # nested padded type
864
            _copyfrompacked!(Ptr{fT}(ptr_out + writeoffset), ptr_in + readoffset)
2✔
865
            readoffset += packedsize(fT)
1✔
866
        end
867
    end
14✔
868
end
869

870
@inline function _reinterpret(::Type{Out}, x::In) where {Out, In}
871
    # handle non-primitive types
872
    isbitstype(Out) || throw(ArgumentError("Target type for `reinterpret` must be isbits"))
85✔
873
    isbitstype(In) || throw(ArgumentError("Source type for `reinterpret` must be isbits"))
84✔
874
    inpackedsize = packedsize(In)
83✔
875
    outpackedsize = packedsize(Out)
83✔
876
    inpackedsize == outpackedsize ||
83✔
877
        throw(ArgumentError(LazyString("Packed sizes of types ", Out, " and ", In,
878
            " do not match; got ", outpackedsize, " and ", inpackedsize, ", respectively.")))
879
    in = Ref{In}(x)
2,511✔
880
    out = Ref{Out}()
2,511✔
881
    if struct_subpadding(Out, In)
79✔
882
        # if packed the same, just copy
883
        GC.@preserve in out begin
2,508✔
884
            ptr_in = unsafe_convert(Ptr{In}, in)
2,508✔
885
            ptr_out = unsafe_convert(Ptr{Out}, out)
2,508✔
886
            memcpy(ptr_out, ptr_in, sizeof(Out))
2,508✔
887
        end
888
        return out[]
2,508✔
889
    else
890
        # mismatched padding
891
        return _reinterpret_padding(Out, x)
3✔
892
    end
893
end
894

895
# If the code reaches this part, it needs to handle padding and is unlikely
896
# to compile to a noop. Therefore, we don't forcibly inline it.
897
function _reinterpret_padding(::Type{Out}, x::In) where {Out, In}
898
    inpackedsize = packedsize(In)
3✔
899
    in = Ref{In}(x)
3✔
900
    out = Ref{Out}()
3✔
901
    GC.@preserve in out begin
3✔
902
        ptr_in = unsafe_convert(Ptr{In}, in)
3✔
903
        ptr_out = unsafe_convert(Ptr{Out}, out)
3✔
904

905
        if fieldcount(In) > 0 && ispacked(Out)
3✔
906
            _copytopacked!(ptr_out, ptr_in)
×
907
        elseif fieldcount(Out) > 0 && ispacked(In)
3✔
908
            _copyfrompacked!(ptr_out, ptr_in)
×
909
        else
910
            packed = Ref{NTuple{inpackedsize, UInt8}}()
3✔
911
            GC.@preserve packed begin
3✔
912
                ptr_packed = unsafe_convert(Ptr{NTuple{inpackedsize, UInt8}}, packed)
3✔
913
                _copytopacked!(ptr_packed, ptr_in)
3✔
914
                _copyfrompacked!(ptr_out, ptr_packed)
3✔
915
            end
916
        end
917
    end
918
    return out[]
3✔
919
end
920

921

922
# Reductions with IndexSCartesian2
923

924
function _mapreduce(f::F, op::OP, style::IndexSCartesian2{K}, A::AbstractArrayOrBroadcasted) where {F,OP,K}
925
    inds = eachindex(style, A)
4✔
926
    n = size(inds)[2]
4✔
927
    if n == 0
4✔
928
        return mapreduce_empty_iter(f, op, A, IteratorEltype(A))
×
929
    else
930
        return mapreduce_impl(f, op, A, first(inds), last(inds))
4✔
931
    end
932
end
933

934
@noinline function mapreduce_impl(f::F, op::OP, A::AbstractArrayOrBroadcasted,
8✔
935
                                  ifirst::SCI, ilast::SCI, blksize::Int) where {F,OP,SCI<:SCartesianIndex2{K}} where K
936
    if ilast.j - ifirst.j < blksize
8✔
937
        # sequential portion
938
        @inbounds a1 = A[ifirst]
6✔
939
        @inbounds a2 = A[SCI(2,ifirst.j)]
6✔
940
        v = op(f(a1), f(a2))
6✔
941
        @simd for i = ifirst.i + 2 : K
6✔
942
            @inbounds ai = A[SCI(i,ifirst.j)]
6✔
943
            v = op(v, f(ai))
6✔
944
        end
945
        # Remaining columns
946
        for j = ifirst.j+1 : ilast.j
6✔
947
            @simd for i = 1:K
2,666✔
948
                @inbounds ai = A[SCI(i,j)]
7,998✔
949
                v = op(v, f(ai))
7,998✔
950
            end
951
        end
5,326✔
952
        return v
6✔
953
    else
954
        # pairwise portion
955
        jmid = ifirst.j + (ilast.j - ifirst.j) >> 1
2✔
956
        v1 = mapreduce_impl(f, op, A, ifirst, SCI(K,jmid), blksize)
2✔
957
        v2 = mapreduce_impl(f, op, A, SCI(1,jmid+1), ilast, blksize)
2✔
958
        return op(v1, v2)
2✔
959
    end
960
end
961

962
mapreduce_impl(f::F, op::OP, A::AbstractArrayOrBroadcasted, ifirst::SCartesianIndex2, ilast::SCartesianIndex2) where {F,OP} =
4✔
963
    mapreduce_impl(f, op, A, ifirst, ilast, pairwise_blocksize(f, op))
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc