Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Merge new reinterpret with essentials.jl reinterpret #50367

Merged
merged 5 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,21 +543,41 @@ unsafe_convert(::Type{T}, x::T) where {T<:Ptr} = x # to resolve ambiguity with
unsafe_convert(::Type{P}, x::Ptr) where {P<:Ptr} = convert(P, x)

"""
reinterpret(type, x)
reinterpret(::Type{Out}, x::In)

Change the type-interpretation of the binary data in the primitive value `x`
to that of the primitive type `type`.
The size of `type` has to be the same as that of the type of `x`.
Change the type-interpretation of the binary data in the isbits value `x`
to that of the isbits type `type`.
BioTurboNick marked this conversation as resolved.
Show resolved Hide resolved
The size (ignoring padding) of `type` has to be the same as that of the type of `x`.
For example, `reinterpret(Float32, UInt32(7))` interprets the 4 bytes corresponding to `UInt32(7)` as a
[`Float32`](@ref).

# Examples
```jldoctest
julia> reinterpret(Float32, UInt32(7))
1.0f-44

julia> reinterpret(NTuple{2, UInt8}, 0x1234)
(0x34, 0x12)

julia> reinterpret(UInt16, (0x34, 0x12))
0x1234

julia> reinterpret(Tuple{UInt16, UInt8}, (0x01, 0x0203))
(0x0301, 0x02)
```

!!! warning

Use caution if some combinations of bits in `Out` are not considered valid and would
otherwise be prevented by the type's constructors and methods. Unexpected behavior
may result without additional validation.
"""
reinterpret(::Type{T}, x) where {T} = bitcast(T, x)
function reinterpret(Out::Type, x::In) where {In}
if isprimitivetype(Out) && isprimitivetype(In)
return bitcast(Out, x)
end
# only available when Base is fully loaded.
return _reinterpret(Out, x)
end

"""
sizeof(T::DataType)
Expand Down
36 changes: 2 additions & 34 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -795,42 +795,10 @@ function _copyfrompacked!(ptr_out::Ptr{Out}, ptr_in::Ptr{In}) where {Out, In}
end
end

"""
reinterpret(::Type{Out}, x::In)

Reinterpret the valid non-padding bytes of an isbits value `x` as isbits type `Out`.

Both types must have the same amount of non-padding bytes. This operation is guaranteed
to be reversible.

```jldoctest
julia> reinterpret(NTuple{2, UInt8}, 0x1234)
(0x34, 0x12)

julia> reinterpret(UInt16, (0x34, 0x12))
0x1234

julia> reinterpret(Tuple{UInt16, UInt8}, (0x01, 0x0203))
(0x0301, 0x02)
```

!!! warning

Use caution if some combinations of bits in `Out` are not considered valid and would
otherwise be prevented by the type's constructors and methods. Unexpected behavior
may result without additional validation.
"""
@inline function reinterpret(::Type{Out}, x::In) where {Out, In}
@inline function _reinterpret(::Type{Out}, x::In) where {Out, In}
# handle non-primitive types
isbitstype(Out) || throw(ArgumentError("Target type for `reinterpret` must be isbits"))
isbitstype(In) || throw(ArgumentError("Source type for `reinterpret` must be isbits"))
if isprimitivetype(Out) && isprimitivetype(In)
outsize = sizeof(Out)
insize = sizeof(In)
outsize == insize ||
throw(ArgumentError("Sizes of types $Out and $In do not match; got $outsize \
and $insize, respectively."))
return bitcast(Out, x)
end
inpackedsize = packedsize(In)
outpackedsize = packedsize(Out)
inpackedsize == outpackedsize ||
Expand Down