13#ifndef WPIUTIL_WPI_ENDIAN_H
14#define WPIUTIL_WPI_ENDIAN_H
34template<
class T,
int alignment>
36 enum {
value = alignment == 0 ?
alignof(T) : alignment };
43template <
typename value_type>
51template <
typename value_type, endianness endian>
52[[nodiscard]]
inline value_type
byte_swap(value_type value) {
59template <
typename value_type, std::
size_t alignment = unaligned>
70template <
typename value_type, endianness endian, std::
size_t alignment>
71[[nodiscard]]
inline value_type
read(
const void *
memory) {
77template <
typename value_type, std::size_t alignment =
unaligned,
82 memory +=
sizeof(value_type);
86template <
typename value_type,
endianness endian,
87 std::size_t alignment =
unaligned,
typename CharT>
93template <
typename value_type, std::
size_t alignment = unaligned>
98 &value,
sizeof(value_type));
101template<
typename value_type,
103 std::size_t alignment>
110template <
typename value_type, std::size_t alignment =
unaligned,
114 memory +=
sizeof(value_type);
117template <
typename value_type,
endianness endian,
118 std::size_t alignment =
unaligned,
typename CharT>
123template <
typename value_type>
128template <
typename value_type, endianness endian, std::
size_t alignment>
131 assert(startBit < 8);
140 sizeof(value_type) * 2);
148 (
sizeof(value_type) * 8) - startBit;
155 upperVal <<= numBitsFirstVal;
157 return lowerVal | upperVal;
163template <
typename value_type, endianness endian, std::
size_t alignment>
166 assert(startBit < 8);
175 sizeof(value_type) * 2);
183 (
sizeof(value_type) * 8) - startBit;
191 lowerVal <<= startBit;
209 &val[0],
sizeof(value_type) * 2);
217template <
typename ValueType,
endianness Endian, std::size_t Alignment,
230 (
const void*)Value.buffer);
235 (
void*)Value.buffer, newValue);
239 *
this = *
this + newValue;
244 *
this = *
this - newValue;
249 *
this = *
this | newValue;
254 *
this = *
this & newValue;
265 explicit ref(
void *Ptr) : Ptr(Ptr) {}
400template <
typename T, endianness E> [[nodiscard]]
inline T
read(
const void *P) {
414template <endianness E> [[nodiscard]]
inline uint16_t
read16(
const void *P) {
417template <endianness E> [[nodiscard]]
inline uint32_t
read32(
const void *P) {
420template <endianness E> [[nodiscard]]
inline uint64_t
read64(
const void *P) {
424[[nodiscard]]
inline uint16_t
read16le(
const void *P) {
427[[nodiscard]]
inline uint32_t
read32le(
const void *P) {
430[[nodiscard]]
inline uint64_t
read64le(
const void *P) {
433[[nodiscard]]
inline uint16_t
read16be(
const void *P) {
436[[nodiscard]]
inline uint32_t
read32be(
const void *P) {
439[[nodiscard]]
inline uint64_t
read64be(
const void *P) {
443template <
typename T, endianness E>
inline void write(
void *P, T V) {
457template <endianness E>
inline void write16(
void *P, uint16_t V) {
460template <endianness E>
inline void write32(
void *P, uint32_t V) {
463template <endianness E>
inline void write64(
void *P, uint64_t V) {
#define LLVM_ASSUME_ALIGNED(p, a)
\macro LLVM_ASSUME_ALIGNED Returns a pointer with an assumed alignment.
Definition Compiler.h:435
This file implements the C++20 <bit> header.
detail namespace with internal helper functions
Definition input_adapters.h:32
Memory namespace.
Definition heap_allocator.hpp:20
uint64_t read64(const void *P, endianness E)
Definition Endian.h:410
std::make_unsigned_t< value_type > make_unsigned_t
Definition Endian.h:124
void writeAtBitAlignment(void *memory, value_type value, uint64_t startBit)
Write a value to memory with a particular endianness, for a location that starts at the given bit off...
Definition Endian.h:164
uint32_t read32le(const void *P)
Definition Endian.h:427
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition Endian.h:60
uint64_t read64be(const void *P)
Definition Endian.h:439
void write32be(void *P, uint32_t V)
Definition Endian.h:479
void write64be(void *P, uint64_t V)
Definition Endian.h:482
uint32_t read32be(const void *P)
Definition Endian.h:436
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:94
value_type byte_swap(value_type value, endianness endian)
Definition Endian.h:44
uint64_t read64le(const void *P)
Definition Endian.h:430
void write32le(void *P, uint32_t V)
Definition Endian.h:470
uint16_t read16le(const void *P)
Definition Endian.h:424
void write64le(void *P, uint64_t V)
Definition Endian.h:473
value_type readNext(const CharT *&memory, endianness endian)
Read a value of a particular endianness from a buffer, and increment the buffer past that value.
Definition Endian.h:79
void writeNext(CharT *&memory, value_type value, endianness endian)
Write a value of a particular endianness, and increment the buffer past that value.
Definition Endian.h:112
void write32(void *P, uint32_t V, endianness E)
Definition Endian.h:450
void write16(void *P, uint16_t V, endianness E)
Definition Endian.h:447
uint32_t read32(const void *P, endianness E)
Definition Endian.h:407
void write64(void *P, uint64_t V, endianness E)
Definition Endian.h:453
void write16be(void *P, uint16_t V)
Definition Endian.h:476
uint16_t read16be(const void *P)
Definition Endian.h:433
uint16_t read16(const void *P, endianness E)
Definition Endian.h:404
void write16le(void *P, uint16_t V)
Definition Endian.h:467
value_type readAtBitAlignment(const void *memory, uint64_t startBit)
Read a value of a particular endianness from memory, for a location that starts at the given bit offs...
Definition Endian.h:129
@ unaligned
Definition Endian.h:29
@ aligned
Definition Endian.h:29
void swapByteOrder(T &Value)
Definition SwapByteOrder.h:61
Foonathan namespace.
Definition ntcore_cpp.h:26
endianness
Definition bit.h:32
value is either alignment, or alignof(T) if alignment is 0.
Definition Endian.h:35
@ value
Definition Endian.h:36
void operator=(value_type NewValue)
Definition Endian.h:271
ref(void *Ptr)
Definition Endian.h:265
char buffer[sizeof(value_type)]
Definition Endian.h:260
packed_endian_specific_integral & operator|=(value_type newValue)
Definition Endian.h:248
static constexpr std::size_t alignment
Definition Endian.h:222
packed_endian_specific_integral(value_type val)
Definition Endian.h:226
packed_endian_specific_integral & operator&=(value_type newValue)
Definition Endian.h:253
static constexpr endianness endian
Definition Endian.h:221
packed_endian_specific_integral & operator-=(value_type newValue)
Definition Endian.h:243
packed_endian_specific_integral & operator+=(value_type newValue)
Definition Endian.h:238
void operator=(value_type newValue)
Definition Endian.h:233
ValueType value_type
Definition Endian.h:220
packed_endian_specific_integral()=default