WPILibC++ 2024.3.2
Endian.h
Go to the documentation of this file.
1//===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares generic functions to read and write endian specific data.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef WPIUTIL_WPI_ENDIAN_H
14#define WPIUTIL_WPI_ENDIAN_H
15
16#include "wpi/Compiler.h"
17#include "wpi/SwapByteOrder.h"
18#include <cassert>
19#include <cstddef>
20#include <cstdint>
21#include <cstring>
22#include <type_traits>
23
24namespace wpi {
25namespace support {
26
28
29// These are named values for common alignments.
30enum {aligned = 0, unaligned = 1};
31
32namespace detail {
33
34/// ::value is either alignment, or alignof(T) if alignment is 0.
35template<class T, int alignment>
37 enum { value = alignment == 0 ? alignof(T) : alignment };
38};
39
40} // end namespace detail
41
42namespace endian {
43
46}
47
48template <typename value_type>
49inline value_type byte_swap(value_type value, endianness endian) {
50 if ((endian != native) && (endian != system_endianness()))
51 sys::swapByteOrder(value);
52 return value;
53}
54
55/// Swap the bytes of value to match the given endianness.
56template<typename value_type, endianness endian>
57inline value_type byte_swap(value_type value) {
58 if constexpr ((endian != native) && (endian != system_endianness()))
59 sys::swapByteOrder(value);
60 return value;
61}
62
63/// Read a value of a particular endianness from memory.
64template <typename value_type, std::size_t alignment>
65inline value_type read(const void *memory, endianness endian) {
66 value_type ret;
67
68 memcpy(&ret,
71 sizeof(value_type));
72 return byte_swap<value_type>(ret, endian);
73}
74
75template<typename value_type,
76 endianness endian,
77 std::size_t alignment>
78inline value_type read(const void *memory) {
79 return read<value_type, alignment>(memory, endian);
80}
81
82/// Read a value of a particular endianness from a buffer, and increment the
83/// buffer past that value.
84template <typename value_type, std::size_t alignment, typename CharT>
85inline value_type readNext(const CharT *&memory, endianness endian) {
86 value_type ret = read<value_type, alignment>(memory, endian);
87 memory += sizeof(value_type);
88 return ret;
89}
90
91template<typename value_type, endianness endian, std::size_t alignment,
92 typename CharT>
93inline value_type readNext(const CharT *&memory) {
94 return readNext<value_type, alignment, CharT>(memory, endian);
95}
96
97/// Write a value to memory with a particular endianness.
98template <typename value_type, std::size_t alignment>
99inline void write(void *memory, value_type value, endianness endian) {
100 value = byte_swap<value_type>(value, endian);
101 memcpy(LLVM_ASSUME_ALIGNED(
103 &value, sizeof(value_type));
104}
105
106template<typename value_type,
107 endianness endian,
108 std::size_t alignment>
109inline void write(void *memory, value_type value) {
110 write<value_type, alignment>(memory, value, endian);
111}
112
113template <typename value_type>
114using make_unsigned_t = std::make_unsigned_t<value_type>;
115
116/// Read a value of a particular endianness from memory, for a location
117/// that starts at the given bit offset within the first byte.
118template <typename value_type, endianness endian, std::size_t alignment>
119inline value_type readAtBitAlignment(const void *memory, uint64_t startBit) {
120 assert(startBit < 8);
121 if (startBit == 0)
122 return read<value_type, endian, alignment>(memory);
123 else {
124 // Read two values and compose the result from them.
125 value_type val[2];
126 memcpy(&val[0],
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]);
132
133 // Shift bits from the lower value into place.
134 make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
135 // Mask off upper bits after right shift in case of signed type.
136 make_unsigned_t<value_type> numBitsFirstVal =
137 (sizeof(value_type) * 8) - startBit;
138 lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
139
140 // Get the bits from the upper value.
142 val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
143 // Shift them in to place.
144 upperVal <<= numBitsFirstVal;
145
146 return lowerVal | upperVal;
147 }
148}
149
150/// Write a value to memory with a particular endianness, for a location
151/// that starts at the given bit offset within the first byte.
152template <typename value_type, endianness endian, std::size_t alignment>
153inline void writeAtBitAlignment(void *memory, value_type value,
154 uint64_t startBit) {
155 assert(startBit < 8);
156 if (startBit == 0)
157 write<value_type, endian, alignment>(memory, value);
158 else {
159 // Read two values and shift the result into them.
160 value_type val[2];
161 memcpy(&val[0],
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]);
167
168 // Mask off any existing bits in the upper part of the lower value that
169 // we want to replace.
170 val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
171 make_unsigned_t<value_type> numBitsFirstVal =
172 (sizeof(value_type) * 8) - startBit;
173 make_unsigned_t<value_type> lowerVal = value;
174 if (startBit > 0) {
175 // Mask off the upper bits in the new value that are not going to go into
176 // the lower value. This avoids a left shift of a negative value, which
177 // is undefined behavior.
178 lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
179 // Now shift the new bits into place
180 lowerVal <<= startBit;
181 }
182 val[0] |= lowerVal;
183
184 // Mask off any existing bits in the lower part of the upper value that
185 // we want to replace.
186 val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
187 // Next shift the bits that go into the upper value into position.
188 make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
189 // Mask off upper bits after right shift in case of signed type.
190 upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
191 val[1] |= upperVal;
192
193 // Finally, rewrite values.
194 val[0] = byte_swap<value_type, endian>(val[0]);
195 val[1] = byte_swap<value_type, endian>(val[1]);
196 memcpy(LLVM_ASSUME_ALIGNED(
198 &val[0], sizeof(value_type) * 2);
199 }
200}
201
202} // end namespace endian
203
204namespace detail {
205
206template <typename ValueType, endianness Endian, std::size_t Alignment,
209 using value_type = ValueType;
210 static constexpr endianness endian = Endian;
211 static constexpr std::size_t alignment = Alignment;
212
214
215 explicit packed_endian_specific_integral(value_type val) { *this = val; }
216
217 operator value_type() const {
218 return endian::read<value_type, endian, alignment>(
219 (const void*)Value.buffer);
220 }
221
222 void operator=(value_type newValue) {
223 endian::write<value_type, endian, alignment>(
224 (void*)Value.buffer, newValue);
225 }
226
228 *this = *this + newValue;
229 return *this;
230 }
231
233 *this = *this - newValue;
234 return *this;
235 }
236
238 *this = *this | newValue;
239 return *this;
240 }
241
243 *this = *this & newValue;
244 return *this;
245 }
246
247private:
248 struct {
249 alignas(ALIGN) char buffer[sizeof(value_type)];
250 } Value;
251
252public:
253 struct ref {
254 explicit ref(void *Ptr) : Ptr(Ptr) {}
255
256 operator value_type() const {
257 return endian::read<value_type, endian, alignment>(Ptr);
258 }
259
260 void operator=(value_type NewValue) {
261 endian::write<value_type, endian, alignment>(Ptr, NewValue);
262 }
263
264 private:
265 void *Ptr;
266 };
267};
268
269} // end namespace detail
270
277
284
291
298
299using ubig16_t =
301using ubig32_t =
303using ubig64_t =
305
306using big16_t =
308using big32_t =
310using big64_t =
312
319
326
333
340
341template <typename T>
343template <typename T>
345
346template <typename T>
349template <typename T>
351
352namespace endian {
353
354template <typename T> inline T read(const void *P, endianness E) {
355 return read<T, unaligned>(P, E);
356}
357
358template <typename T, endianness E> inline T read(const void *P) {
360}
361
362inline uint16_t read16(const void *P, endianness E) {
363 return read<uint16_t>(P, E);
364}
365inline uint32_t read32(const void *P, endianness E) {
366 return read<uint32_t>(P, E);
367}
368inline uint64_t read64(const void *P, endianness E) {
369 return read<uint64_t>(P, E);
370}
371
372template <endianness E> inline uint16_t read16(const void *P) {
373 return read<uint16_t, E>(P);
374}
375template <endianness E> inline uint32_t read32(const void *P) {
376 return read<uint32_t, E>(P);
377}
378template <endianness E> inline uint64_t read64(const void *P) {
379 return read<uint64_t, E>(P);
380}
381
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); }
388
389template <typename T> inline void write(void *P, T V, endianness E) {
390 write<T, unaligned>(P, V, E);
391}
392
393template <typename T, endianness E> inline void write(void *P, T V) {
395}
396
397inline void write16(void *P, uint16_t V, endianness E) {
398 write<uint16_t>(P, V, E);
399}
400inline void write32(void *P, uint32_t V, endianness E) {
401 write<uint32_t>(P, V, E);
402}
403inline void write64(void *P, uint64_t V, endianness E) {
404 write<uint64_t>(P, V, E);
405}
406
407template <endianness E> inline void write16(void *P, uint16_t V) {
408 write<uint16_t, E>(P, V);
409}
410template <endianness E> inline void write32(void *P, uint32_t V) {
411 write<uint32_t, E>(P, V);
412}
413template <endianness E> inline void write64(void *P, uint64_t V) {
414 write<uint64_t, E>(P, V);
415}
416
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); }
423
424} // end namespace endian
425
426} // end namespace support
427} // end namespace wpi
428
429#endif // WPIUTIL_WPI_ENDIAN_H
#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
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