LLVM 22.0.0git
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 LLVM_SUPPORT_ENDIAN_H
14#define LLVM_SUPPORT_ENDIAN_H
15
16#include "llvm/ADT/bit.h"
19#include <cassert>
20#include <cstddef>
21#include <cstdint>
22#include <cstring>
23#include <type_traits>
24
25namespace llvm {
26namespace support {
27
28// These are named values for common alignments.
29enum {aligned = 0, unaligned = 1};
30
31namespace detail {
32
33/// ::value is either alignment, or alignof(T) if alignment is 0.
34template<class T, int alignment>
36 enum { value = alignment == 0 ? alignof(T) : alignment };
37};
38
39} // end namespace detail
40
41namespace endian {
42
43template <typename value_type>
44[[nodiscard]] inline value_type byte_swap(value_type value, endianness endian) {
47 return value;
48}
49
50/// Swap the bytes of value to match the given endianness.
51template <typename value_type, endianness endian>
52[[nodiscard]] inline value_type byte_swap(value_type value) {
53 return byte_swap(value, endian);
54}
55
56/// Read a value of a particular endianness from memory.
57template <typename value_type, std::size_t alignment = unaligned>
58[[nodiscard]] inline value_type read(const void *memory, endianness endian) {
59 value_type ret;
60
61 memcpy(static_cast<void *>(&ret),
64 sizeof(value_type));
65 return byte_swap<value_type>(ret, endian);
66}
67
68template <typename value_type, endianness endian, std::size_t alignment>
69[[nodiscard]] inline value_type read(const void *memory) {
70 return read<value_type, alignment>(memory, endian);
71}
72
73/// Read a value of a particular endianness from a buffer, and increment the
74/// buffer past that value.
75template <typename value_type, std::size_t alignment = unaligned,
76 typename CharT>
77[[nodiscard]] inline value_type readNext(const CharT *&memory,
79 value_type ret = read<value_type, alignment>(memory, endian);
80 memory += sizeof(value_type);
81 return ret;
82}
83
84template <typename value_type, endianness endian,
85 std::size_t alignment = unaligned, typename CharT>
86[[nodiscard]] inline value_type readNext(const CharT *&memory) {
87 return readNext<value_type, alignment, CharT>(memory, endian);
88}
89
90/// Write a value to memory with a particular endianness.
91template <typename value_type, std::size_t alignment = unaligned>
92inline void write(void *memory, value_type value, endianness endian) {
93 value = byte_swap<value_type>(value, endian);
96 &value, sizeof(value_type));
97}
98
99template<typename value_type,
101 std::size_t alignment>
102inline void write(void *memory, value_type value) {
103 write<value_type, alignment>(memory, value, endian);
104}
105
106/// Write a value of a particular endianness, and increment the buffer past that
107/// value.
108template <typename value_type, std::size_t alignment = unaligned,
109 typename CharT>
110inline void writeNext(CharT *&memory, value_type value, endianness endian) {
111 write(memory, value, endian);
112 memory += sizeof(value_type);
113}
114
115template <typename value_type, endianness endian,
116 std::size_t alignment = unaligned, typename CharT>
117inline void writeNext(CharT *&memory, value_type value) {
118 writeNext<value_type, alignment, CharT>(memory, value, endian);
119}
120
121template <typename value_type>
122using make_unsigned_t = std::make_unsigned_t<value_type>;
123
124/// Read a value of a particular endianness from memory, for a location
125/// that starts at the given bit offset within the first byte.
126template <typename value_type, endianness endian, std::size_t alignment>
127[[nodiscard]] inline value_type readAtBitAlignment(const void *memory,
128 uint64_t startBit) {
129 assert(startBit < 8);
130 if (startBit == 0)
131 return read<value_type, endian, alignment>(memory);
132 else {
133 // Read two values and compose the result from them.
134 value_type val[2];
135 memcpy(&val[0],
138 sizeof(value_type) * 2);
139 val[0] = byte_swap<value_type, endian>(val[0]);
140 val[1] = byte_swap<value_type, endian>(val[1]);
141
142 // Shift bits from the lower value into place.
143 make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
144 // Mask off upper bits after right shift in case of signed type.
145 make_unsigned_t<value_type> numBitsFirstVal =
146 (sizeof(value_type) * 8) - startBit;
147 lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
148
149 // Get the bits from the upper value.
151 val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
152 // Shift them in to place.
153 upperVal <<= numBitsFirstVal;
154
155 return lowerVal | upperVal;
156 }
157}
158
159/// Write a value to memory with a particular endianness, for a location
160/// that starts at the given bit offset within the first byte.
161template <typename value_type, endianness endian, std::size_t alignment>
162inline void writeAtBitAlignment(void *memory, value_type value,
163 uint64_t startBit) {
164 assert(startBit < 8);
165 if (startBit == 0)
166 write<value_type, endian, alignment>(memory, value);
167 else {
168 // Read two values and shift the result into them.
169 value_type val[2];
170 memcpy(&val[0],
173 sizeof(value_type) * 2);
174 val[0] = byte_swap<value_type, endian>(val[0]);
175 val[1] = byte_swap<value_type, endian>(val[1]);
176
177 // Mask off any existing bits in the upper part of the lower value that
178 // we want to replace.
179 val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
180 make_unsigned_t<value_type> numBitsFirstVal =
181 (sizeof(value_type) * 8) - startBit;
183 if (startBit > 0) {
184 // Mask off the upper bits in the new value that are not going to go into
185 // the lower value. This avoids a left shift of a negative value, which
186 // is undefined behavior.
187 lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
188 // Now shift the new bits into place
189 lowerVal <<= startBit;
190 }
191 val[0] |= lowerVal;
192
193 // Mask off any existing bits in the lower part of the upper value that
194 // we want to replace.
195 val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
196 // Next shift the bits that go into the upper value into position.
197 make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
198 // Mask off upper bits after right shift in case of signed type.
199 upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
200 val[1] |= upperVal;
201
202 // Finally, rewrite values.
203 val[0] = byte_swap<value_type, endian>(val[0]);
204 val[1] = byte_swap<value_type, endian>(val[1]);
205 memcpy(LLVM_ASSUME_ALIGNED(
207 &val[0], sizeof(value_type) * 2);
208 }
209}
210
211} // end namespace endian
212
213namespace detail {
214
215template <typename ValueType, endianness Endian, std::size_t Alignment,
219 static constexpr endianness endian = Endian;
220 static constexpr std::size_t alignment = Alignment;
221
223
224 explicit packed_endian_specific_integral(value_type val) { *this = val; }
225
227 return endian::read<value_type, endian, alignment>(
228 (const void*)Value.buffer);
229 }
230 operator value_type() const { return value(); }
231
232 void operator=(value_type newValue) {
233 endian::write<value_type, endian, alignment>(
234 (void*)Value.buffer, newValue);
235 }
236
238 *this = *this + newValue;
239 return *this;
240 }
241
243 *this = *this - newValue;
244 return *this;
245 }
246
248 *this = *this | newValue;
249 return *this;
250 }
251
253 *this = *this & newValue;
254 return *this;
255 }
256
257private:
258 struct {
259 alignas(ALIGN) char buffer[sizeof(value_type)];
260 } Value;
261
262public:
263 struct ref {
264 explicit ref(void *Ptr) : Ptr(Ptr) {}
265
266 operator value_type() const {
267 return endian::read<value_type, endian, alignment>(Ptr);
268 }
269
270 void operator=(value_type NewValue) {
271 endian::write<value_type, endian, alignment>(Ptr, NewValue);
272 }
273
274 private:
275 void *Ptr;
276 };
277};
278
279} // end namespace detail
280
283 unaligned>;
286 unaligned>;
289 unaligned>;
292 unaligned>;
293
296 unaligned>;
299 unaligned>;
302 unaligned>;
303
306 aligned>;
309 aligned>;
312 aligned>;
313
316 aligned>;
319 aligned>;
322 aligned>;
323
324using ubig16_t =
326 unaligned>;
327using ubig32_t =
329 unaligned>;
330using ubig64_t =
332 unaligned>;
333
334using big16_t =
336 unaligned>;
337using big32_t =
339 unaligned>;
340using big64_t =
342 unaligned>;
343
346 aligned>;
349 aligned>;
352 aligned>;
353
356 aligned>;
359 aligned>;
362 aligned>;
363
366 unaligned>;
369 unaligned>;
372 unaligned>;
373
376 unaligned>;
379 unaligned>;
382 unaligned>;
383
384template <typename T>
385using little_t =
387 unaligned>;
388template <typename T>
390 unaligned>;
391
392template <typename T>
395 aligned>;
396template <typename T>
399
400namespace endian {
401
402template <typename T, endianness E> [[nodiscard]] inline T read(const void *P) {
404}
405
406[[nodiscard]] inline uint16_t read16(const void *P, endianness E) {
407 return read<uint16_t>(P, E);
408}
409[[nodiscard]] inline uint32_t read32(const void *P, endianness E) {
410 return read<uint32_t>(P, E);
411}
412[[nodiscard]] inline uint64_t read64(const void *P, endianness E) {
413 return read<uint64_t>(P, E);
414}
415
416template <endianness E> [[nodiscard]] inline uint16_t read16(const void *P) {
417 return read<uint16_t, E>(P);
418}
419template <endianness E> [[nodiscard]] inline uint32_t read32(const void *P) {
420 return read<uint32_t, E>(P);
421}
422template <endianness E> [[nodiscard]] inline uint64_t read64(const void *P) {
423 return read<uint64_t, E>(P);
424}
425
426[[nodiscard]] inline uint16_t read16le(const void *P) {
427 return read16<llvm::endianness::little>(P);
428}
429[[nodiscard]] inline uint32_t read32le(const void *P) {
430 return read32<llvm::endianness::little>(P);
431}
432[[nodiscard]] inline uint64_t read64le(const void *P) {
433 return read64<llvm::endianness::little>(P);
434}
435[[nodiscard]] inline uint16_t read16be(const void *P) {
436 return read16<llvm::endianness::big>(P);
437}
438[[nodiscard]] inline uint32_t read32be(const void *P) {
439 return read32<llvm::endianness::big>(P);
440}
441[[nodiscard]] inline uint64_t read64be(const void *P) {
442 return read64<llvm::endianness::big>(P);
443}
444
445template <typename T, endianness E> inline void write(void *P, T V) {
447}
448
449inline void write16(void *P, uint16_t V, endianness E) {
450 write<uint16_t>(P, V, E);
451}
452inline void write32(void *P, uint32_t V, endianness E) {
453 write<uint32_t>(P, V, E);
454}
455inline void write64(void *P, uint64_t V, endianness E) {
456 write<uint64_t>(P, V, E);
457}
458
459template <endianness E> inline void write16(void *P, uint16_t V) {
460 write<uint16_t, E>(P, V);
461}
462template <endianness E> inline void write32(void *P, uint32_t V) {
463 write<uint32_t, E>(P, V);
464}
465template <endianness E> inline void write64(void *P, uint64_t V) {
466 write<uint64_t, E>(P, V);
467}
468
469inline void write16le(void *P, uint16_t V) {
470 write16<llvm::endianness::little>(P, V);
471}
472inline void write32le(void *P, uint32_t V) {
473 write32<llvm::endianness::little>(P, V);
474}
475inline void write64le(void *P, uint64_t V) {
476 write64<llvm::endianness::little>(P, V);
477}
478inline void write16be(void *P, uint16_t V) {
479 write16<llvm::endianness::big>(P, V);
480}
481inline void write32be(void *P, uint32_t V) {
482 write32<llvm::endianness::big>(P, V);
483}
484inline void write64be(void *P, uint64_t V) {
485 write64<llvm::endianness::big>(P, V);
486}
487
488} // end namespace endian
489
490} // end namespace support
491} // end namespace llvm
492
493#endif // LLVM_SUPPORT_ENDIAN_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ASSUME_ALIGNED(p, a)
\macro LLVM_ASSUME_ALIGNED Returns a pointer with an assumed alignment.
Definition: Compiler.h:504
Given that RA is a live value
#define T
#define P(N)
endianness Endian
This file implements the C++20 <bit> header.
LLVM Value Representation.
Definition: Value.h:75
uint64_t read64le(const void *P)
Definition: Endian.h:432
value_type byte_swap(value_type value, endianness endian)
Definition: Endian.h:44
uint16_t read16le(const void *P)
Definition: Endian.h:426
uint32_t read32(const void *P, endianness E)
Definition: Endian.h:409
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:110
void write16be(void *P, uint16_t V)
Definition: Endian.h:478
void write64le(void *P, uint64_t V)
Definition: Endian.h:475
uint64_t read64be(const void *P)
Definition: Endian.h:441
void write32le(void *P, uint32_t V)
Definition: Endian.h:472
void write32(void *P, uint32_t V, endianness E)
Definition: Endian.h:452
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:162
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:127
void write32be(void *P, uint32_t V)
Definition: Endian.h:481
uint64_t read64(const void *P, endianness E)
Definition: Endian.h:412
uint32_t read32be(const void *P)
Definition: Endian.h:438
void write16(void *P, uint16_t V, endianness E)
Definition: Endian.h:449
void write16le(void *P, uint16_t V)
Definition: Endian.h:469
void write64be(void *P, uint64_t V)
Definition: Endian.h:484
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:58
void write64(void *P, uint64_t V, endianness E)
Definition: Endian.h:455
uint16_t read16be(const void *P)
Definition: Endian.h:435
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:92
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:77
uint32_t read32le(const void *P)
Definition: Endian.h:429
uint16_t read16(const void *P, endianness E)
Definition: Endian.h:406
std::make_unsigned_t< value_type > make_unsigned_t
Definition: Endian.h:122
void swapByteOrder(T &Value)
Definition: SwapByteOrder.h:61
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
endianness
Definition: bit.h:71
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
value is either alignment, or alignof(T) if alignment is 0.
Definition: Endian.h:35
packed_endian_specific_integral & operator+=(value_type newValue)
Definition: Endian.h:237
packed_endian_specific_integral & operator&=(value_type newValue)
Definition: Endian.h:252
packed_endian_specific_integral & operator|=(value_type newValue)
Definition: Endian.h:247
packed_endian_specific_integral & operator-=(value_type newValue)
Definition: Endian.h:242