13#ifndef WPIUTIL_WPI_ENDIAN_H
14#define WPIUTIL_WPI_ENDIAN_H
35template<
class T,
int alignment>
37 enum {
value = alignment == 0 ?
alignof(T) : alignment };
48template <
typename value_type>
56template<
typename value_type, endianness endian>
64template <
typename value_type, std::
size_t alignment>
72 return byte_swap<value_type>(ret, endian);
75template<
typename value_type,
77 std::size_t alignment>
78inline value_type
read(
const void *memory) {
79 return read<value_type, alignment>(memory, endian);
84template <
typename value_type, std::
size_t alignment,
typename CharT>
86 value_type ret = read<value_type, alignment>(memory, endian);
87 memory +=
sizeof(value_type);
91template<
typename value_type,
endianness endian, std::size_t alignment,
93inline value_type
readNext(
const CharT *&memory) {
94 return readNext<value_type, alignment, CharT>(memory, endian);
98template <
typename value_type, std::
size_t alignment>
100 value = byte_swap<value_type>(value, endian);
103 &value,
sizeof(value_type));
106template<
typename value_type,
108 std::size_t alignment>
109inline void write(
void *memory, value_type value) {
110 write<value_type, alignment>(memory, value, endian);
113template <
typename value_type>
118template <
typename value_type, endianness endian, std::
size_t alignment>
120 assert(startBit < 8);
122 return read<value_type, endian, alignment>(memory);
129 sizeof(value_type) * 2);
130 val[0] = byte_swap<value_type, endian>(val[0]);
131 val[1] = byte_swap<value_type, endian>(val[1]);
137 (
sizeof(value_type) * 8) - startBit;
144 upperVal <<= numBitsFirstVal;
146 return lowerVal | upperVal;
152template <
typename value_type, endianness endian, std::
size_t alignment>
155 assert(startBit < 8);
157 write<value_type, endian, alignment>(memory, value);
164 sizeof(value_type) * 2);
165 val[0] = byte_swap<value_type, endian>(val[0]);
166 val[1] = byte_swap<value_type, endian>(val[1]);
172 (
sizeof(value_type) * 8) - startBit;
180 lowerVal <<= startBit;
194 val[0] = byte_swap<value_type, endian>(val[0]);
195 val[1] = byte_swap<value_type, endian>(val[1]);
198 &val[0],
sizeof(value_type) * 2);
206template <
typename ValueType,
endianness Endian, std::size_t Alignment,
218 return endian::read<value_type, endian, alignment>(
219 (
const void*)Value.buffer);
223 endian::write<value_type, endian, alignment>(
224 (
void*)Value.buffer, newValue);
228 *
this = *
this + newValue;
233 *
this = *
this - newValue;
238 *
this = *
this | newValue;
243 *
this = *
this & newValue;
254 explicit ref(
void *Ptr) : Ptr(Ptr) {}
257 return endian::read<value_type, endian, alignment>(Ptr);
261 endian::write<value_type, endian, alignment>(Ptr, NewValue);
355 return read<T, unaligned>(P, E);
358template <
typename T, endianness E>
inline T
read(
const void *P) {
363 return read<uint16_t>(P, E);
366 return read<uint32_t>(P, E);
369 return read<uint64_t>(P, E);
372template <endianness E>
inline uint16_t
read16(
const void *P) {
373 return read<uint16_t, E>(P);
375template <endianness E>
inline uint32_t
read32(
const void *P) {
376 return read<uint32_t, E>(P);
378template <endianness E>
inline uint64_t
read64(
const void *P) {
379 return read<uint64_t, E>(P);
382inline uint16_t
read16le(
const void *P) {
return read16<little>(P); }
383inline uint32_t
read32le(
const void *P) {
return read32<little>(P); }
384inline uint64_t
read64le(
const void *P) {
return read64<little>(P); }
385inline uint16_t
read16be(
const void *P) {
return read16<big>(P); }
386inline uint32_t
read32be(
const void *P) {
return read32<big>(P); }
387inline uint64_t
read64be(
const void *P) {
return read64<big>(P); }
390 write<T, unaligned>(P, V, E);
393template <
typename T, endianness E>
inline void write(
void *P, T V) {
398 write<uint16_t>(P, V, E);
401 write<uint32_t>(P, V, E);
404 write<uint64_t>(P, V, E);
407template <endianness E>
inline void write16(
void *P, uint16_t V) {
408 write<uint16_t, E>(P, V);
410template <endianness E>
inline void write32(
void *P, uint32_t V) {
411 write<uint32_t, E>(P, V);
413template <endianness E>
inline void write64(
void *P, uint64_t V) {
414 write<uint64_t, E>(P, V);
417inline void write16le(
void *P, uint16_t V) { write16<little>(P, V); }
418inline void write32le(
void *P, uint32_t V) { write32<little>(P, V); }
419inline void write64le(
void *P, uint64_t V) { write64<little>(P, V); }
420inline void write16be(
void *P, uint16_t V) { write16<big>(P, V); }
421inline void write32be(
void *P, uint32_t V) { write32<big>(P, V); }
422inline void write64be(
void *P, uint64_t V) { write64<big>(P, V); }
#define LLVM_ASSUME_ALIGNED(p, a)
\macro LLVM_ASSUME_ALIGNED Returns a pointer with an assumed alignment.
Definition: Compiler.h:402
detail namespace with internal helper functions
Definition: xchar.h:20
uint64_t read64(const void *P, endianness E)
Definition: Endian.h:368
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:153
uint32_t read32le(const void *P)
Definition: Endian.h:383
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:65
uint64_t read64be(const void *P)
Definition: Endian.h:387
void write32be(void *P, uint32_t V)
Definition: Endian.h:421
void write64be(void *P, uint64_t V)
Definition: Endian.h:422
uint32_t read32be(const void *P)
Definition: Endian.h:386
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:99
value_type byte_swap(value_type value, endianness endian)
Definition: Endian.h:49
uint64_t read64le(const void *P)
Definition: Endian.h:384
void write32le(void *P, uint32_t V)
Definition: Endian.h:418
uint16_t read16le(const void *P)
Definition: Endian.h:382
void write64le(void *P, uint64_t V)
Definition: Endian.h:419
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:85
void write32(void *P, uint32_t V, endianness E)
Definition: Endian.h:400
constexpr endianness system_endianness()
Definition: Endian.h:44
void write16(void *P, uint16_t V, endianness E)
Definition: Endian.h:397
std::make_unsigned_t< value_type > make_unsigned_t
Definition: Endian.h:114
uint32_t read32(const void *P, endianness E)
Definition: Endian.h:365
void write64(void *P, uint64_t V, endianness E)
Definition: Endian.h:403
void write16be(void *P, uint16_t V)
Definition: Endian.h:420
uint16_t read16be(const void *P)
Definition: Endian.h:385
uint16_t read16(const void *P, endianness E)
Definition: Endian.h:362
void write16le(void *P, uint16_t V)
Definition: Endian.h:417
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:119
endianness
Definition: Endian.h:27
@ big
Definition: Endian.h:27
@ native
Definition: Endian.h:27
@ little
Definition: Endian.h:27
@ unaligned
Definition: Endian.h:30
@ aligned
Definition: Endian.h:30
void swapByteOrder(T &Value)
Definition: SwapByteOrder.h:102
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:54
Definition: ntcore_cpp.h:26
::value is either alignment, or alignof(T) if alignment is 0.
Definition: Endian.h:36
@ value
Definition: Endian.h:37
void operator=(value_type NewValue)
Definition: Endian.h:260
ref(void *Ptr)
Definition: Endian.h:254
char buffer[sizeof(value_type)]
Definition: Endian.h:249
packed_endian_specific_integral & operator|=(value_type newValue)
Definition: Endian.h:237
static constexpr std::size_t alignment
Definition: Endian.h:211
packed_endian_specific_integral(value_type val)
Definition: Endian.h:215
packed_endian_specific_integral & operator&=(value_type newValue)
Definition: Endian.h:242
static constexpr endianness endian
Definition: Endian.h:210
packed_endian_specific_integral & operator-=(value_type newValue)
Definition: Endian.h:232
packed_endian_specific_integral & operator+=(value_type newValue)
Definition: Endian.h:227
void operator=(value_type newValue)
Definition: Endian.h:222
ValueType value_type
Definition: Endian.h:209
packed_endian_specific_integral()=default