LLVM 20.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(&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
226 operator value_type() const {
227 return endian::read<value_type, endian, alignment>(
228 (const void*)Value.buffer);
229 }
230
231 void operator=(value_type newValue) {
232 endian::write<value_type, endian, alignment>(
233 (void*)Value.buffer, newValue);
234 }
235
237 *this = *this + newValue;
238 return *this;
239 }
240
242 *this = *this - newValue;
243 return *this;
244 }
245
247 *this = *this | newValue;
248 return *this;
249 }
250
252 *this = *this & newValue;
253 return *this;
254 }
255
256private:
257 struct {
258 alignas(ALIGN) char buffer[sizeof(value_type)];
259 } Value;
260
261public:
262 struct ref {
263 explicit ref(void *Ptr) : Ptr(Ptr) {}
264
265 operator value_type() const {
266 return endian::read<value_type, endian, alignment>(Ptr);
267 }
268
269 void operator=(value_type NewValue) {
270 endian::write<value_type, endian, alignment>(Ptr, NewValue);
271 }
272
273 private:
274 void *Ptr;
275 };
276};
277
278} // end namespace detail
279
282 unaligned>;
285 unaligned>;
288 unaligned>;
289
292 unaligned>;
295 unaligned>;
298 unaligned>;
299
302 aligned>;
305 aligned>;
308 aligned>;
309
312 aligned>;
315 aligned>;
318 aligned>;
319
320using ubig16_t =
322 unaligned>;
323using ubig32_t =
325 unaligned>;
326using ubig64_t =
328 unaligned>;
329
330using big16_t =
332 unaligned>;
333using big32_t =
335 unaligned>;
336using big64_t =
338 unaligned>;
339
342 aligned>;
345 aligned>;
348 aligned>;
349
352 aligned>;
355 aligned>;
358 aligned>;
359
362 unaligned>;
365 unaligned>;
368 unaligned>;
369
372 unaligned>;
375 unaligned>;
378 unaligned>;
379
380template <typename T>
381using little_t =
383 unaligned>;
384template <typename T>
386 unaligned>;
387
388template <typename T>
391 aligned>;
392template <typename T>
395
396namespace endian {
397
398template <typename T, endianness E> [[nodiscard]] inline T read(const void *P) {
400}
401
402[[nodiscard]] inline uint16_t read16(const void *P, endianness E) {
403 return read<uint16_t>(P, E);
404}
405[[nodiscard]] inline uint32_t read32(const void *P, endianness E) {
406 return read<uint32_t>(P, E);
407}
408[[nodiscard]] inline uint64_t read64(const void *P, endianness E) {
409 return read<uint64_t>(P, E);
410}
411
412template <endianness E> [[nodiscard]] inline uint16_t read16(const void *P) {
413 return read<uint16_t, E>(P);
414}
415template <endianness E> [[nodiscard]] inline uint32_t read32(const void *P) {
416 return read<uint32_t, E>(P);
417}
418template <endianness E> [[nodiscard]] inline uint64_t read64(const void *P) {
419 return read<uint64_t, E>(P);
420}
421
422[[nodiscard]] inline uint16_t read16le(const void *P) {
423 return read16<llvm::endianness::little>(P);
424}
425[[nodiscard]] inline uint32_t read32le(const void *P) {
426 return read32<llvm::endianness::little>(P);
427}
428[[nodiscard]] inline uint64_t read64le(const void *P) {
429 return read64<llvm::endianness::little>(P);
430}
431[[nodiscard]] inline uint16_t read16be(const void *P) {
432 return read16<llvm::endianness::big>(P);
433}
434[[nodiscard]] inline uint32_t read32be(const void *P) {
435 return read32<llvm::endianness::big>(P);
436}
437[[nodiscard]] inline uint64_t read64be(const void *P) {
438 return read64<llvm::endianness::big>(P);
439}
440
441template <typename T, endianness E> inline void write(void *P, T V) {
443}
444
445inline void write16(void *P, uint16_t V, endianness E) {
446 write<uint16_t>(P, V, E);
447}
448inline void write32(void *P, uint32_t V, endianness E) {
449 write<uint32_t>(P, V, E);
450}
451inline void write64(void *P, uint64_t V, endianness E) {
452 write<uint64_t>(P, V, E);
453}
454
455template <endianness E> inline void write16(void *P, uint16_t V) {
456 write<uint16_t, E>(P, V);
457}
458template <endianness E> inline void write32(void *P, uint32_t V) {
459 write<uint32_t, E>(P, V);
460}
461template <endianness E> inline void write64(void *P, uint64_t V) {
462 write<uint64_t, E>(P, V);
463}
464
465inline void write16le(void *P, uint16_t V) {
466 write16<llvm::endianness::little>(P, V);
467}
468inline void write32le(void *P, uint32_t V) {
469 write32<llvm::endianness::little>(P, V);
470}
471inline void write64le(void *P, uint64_t V) {
472 write64<llvm::endianness::little>(P, V);
473}
474inline void write16be(void *P, uint16_t V) {
475 write16<llvm::endianness::big>(P, V);
476}
477inline void write32be(void *P, uint32_t V) {
478 write32<llvm::endianness::big>(P, V);
479}
480inline void write64be(void *P, uint64_t V) {
481 write64<llvm::endianness::big>(P, V);
482}
483
484} // end namespace endian
485
486} // end namespace support
487} // end namespace llvm
488
489#endif // LLVM_SUPPORT_ENDIAN_H
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:399
Given that RA is a live value
#define T
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
endianness Endian
This file implements the C++20 <bit> header.
LLVM Value Representation.
Definition: Value.h:74
uint64_t read64le(const void *P)
Definition: Endian.h:428
value_type byte_swap(value_type value, endianness endian)
Definition: Endian.h:44
uint16_t read16le(const void *P)
Definition: Endian.h:422
uint32_t read32(const void *P, endianness E)
Definition: Endian.h:405
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:474
void write64le(void *P, uint64_t V)
Definition: Endian.h:471
uint64_t read64be(const void *P)
Definition: Endian.h:437
void write32le(void *P, uint32_t V)
Definition: Endian.h:468
void write32(void *P, uint32_t V, endianness E)
Definition: Endian.h:448
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:477
uint64_t read64(const void *P, endianness E)
Definition: Endian.h:408
uint32_t read32be(const void *P)
Definition: Endian.h:434
void write16(void *P, uint16_t V, endianness E)
Definition: Endian.h:445
void write16le(void *P, uint16_t V)
Definition: Endian.h:465
void write64be(void *P, uint64_t V)
Definition: Endian.h:480
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:451
uint16_t read16be(const void *P)
Definition: Endian.h:431
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:425
uint16_t read16(const void *P, endianness E)
Definition: Endian.h:402
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:70
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:236
packed_endian_specific_integral & operator&=(value_type newValue)
Definition: Endian.h:251
packed_endian_specific_integral & operator|=(value_type newValue)
Definition: Endian.h:246
packed_endian_specific_integral & operator-=(value_type newValue)
Definition: Endian.h:241