LLVM 19.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, typename CharT>
76[[nodiscard]] inline value_type readNext(const CharT *&memory,
78 value_type ret = read<value_type, alignment>(memory, endian);
79 memory += sizeof(value_type);
80 return ret;
81}
82
83template <typename value_type, endianness endian,
84 std::size_t alignment = unaligned, typename CharT>
85[[nodiscard]] inline value_type readNext(const CharT *&memory) {
86 return readNext<value_type, alignment, CharT>(memory, endian);
87}
88
89/// Write a value to memory with a particular endianness.
90template <typename value_type, std::size_t alignment = unaligned>
91inline void write(void *memory, value_type value, endianness endian) {
92 value = byte_swap<value_type>(value, endian);
95 &value, sizeof(value_type));
96}
97
98template<typename value_type,
100 std::size_t alignment>
101inline void write(void *memory, value_type value) {
102 write<value_type, alignment>(memory, value, endian);
103}
104
105/// Write a value of a particular endianness, and increment the buffer past that
106/// value.
107template <typename value_type, std::size_t alignment = unaligned,
108 typename CharT>
109inline void writeNext(CharT *&memory, value_type value, endianness endian) {
110 write(memory, value, endian);
111 memory += sizeof(value_type);
112}
113
114template <typename value_type, endianness endian,
115 std::size_t alignment = unaligned, typename CharT>
116inline void writeNext(CharT *&memory, value_type value) {
117 writeNext<value_type, alignment, CharT>(memory, value, endian);
118}
119
120template <typename value_type>
121using make_unsigned_t = std::make_unsigned_t<value_type>;
122
123/// Read a value of a particular endianness from memory, for a location
124/// that starts at the given bit offset within the first byte.
125template <typename value_type, endianness endian, std::size_t alignment>
126[[nodiscard]] inline value_type readAtBitAlignment(const void *memory,
127 uint64_t startBit) {
128 assert(startBit < 8);
129 if (startBit == 0)
130 return read<value_type, endian, alignment>(memory);
131 else {
132 // Read two values and compose the result from them.
133 value_type val[2];
134 memcpy(&val[0],
137 sizeof(value_type) * 2);
138 val[0] = byte_swap<value_type, endian>(val[0]);
139 val[1] = byte_swap<value_type, endian>(val[1]);
140
141 // Shift bits from the lower value into place.
142 make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
143 // Mask off upper bits after right shift in case of signed type.
144 make_unsigned_t<value_type> numBitsFirstVal =
145 (sizeof(value_type) * 8) - startBit;
146 lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
147
148 // Get the bits from the upper value.
150 val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
151 // Shift them in to place.
152 upperVal <<= numBitsFirstVal;
153
154 return lowerVal | upperVal;
155 }
156}
157
158/// Write a value to memory with a particular endianness, for a location
159/// that starts at the given bit offset within the first byte.
160template <typename value_type, endianness endian, std::size_t alignment>
161inline void writeAtBitAlignment(void *memory, value_type value,
162 uint64_t startBit) {
163 assert(startBit < 8);
164 if (startBit == 0)
165 write<value_type, endian, alignment>(memory, value);
166 else {
167 // Read two values and shift the result into them.
168 value_type val[2];
169 memcpy(&val[0],
172 sizeof(value_type) * 2);
173 val[0] = byte_swap<value_type, endian>(val[0]);
174 val[1] = byte_swap<value_type, endian>(val[1]);
175
176 // Mask off any existing bits in the upper part of the lower value that
177 // we want to replace.
178 val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
179 make_unsigned_t<value_type> numBitsFirstVal =
180 (sizeof(value_type) * 8) - startBit;
182 if (startBit > 0) {
183 // Mask off the upper bits in the new value that are not going to go into
184 // the lower value. This avoids a left shift of a negative value, which
185 // is undefined behavior.
186 lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
187 // Now shift the new bits into place
188 lowerVal <<= startBit;
189 }
190 val[0] |= lowerVal;
191
192 // Mask off any existing bits in the lower part of the upper value that
193 // we want to replace.
194 val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
195 // Next shift the bits that go into the upper value into position.
196 make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
197 // Mask off upper bits after right shift in case of signed type.
198 upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
199 val[1] |= upperVal;
200
201 // Finally, rewrite values.
202 val[0] = byte_swap<value_type, endian>(val[0]);
203 val[1] = byte_swap<value_type, endian>(val[1]);
204 memcpy(LLVM_ASSUME_ALIGNED(
206 &val[0], sizeof(value_type) * 2);
207 }
208}
209
210} // end namespace endian
211
212namespace detail {
213
214template <typename ValueType, endianness Endian, std::size_t Alignment,
218 static constexpr endianness endian = Endian;
219 static constexpr std::size_t alignment = Alignment;
220
222
223 explicit packed_endian_specific_integral(value_type val) { *this = val; }
224
225 operator value_type() const {
226 return endian::read<value_type, endian, alignment>(
227 (const void*)Value.buffer);
228 }
229
230 void operator=(value_type newValue) {
231 endian::write<value_type, endian, alignment>(
232 (void*)Value.buffer, newValue);
233 }
234
236 *this = *this + newValue;
237 return *this;
238 }
239
241 *this = *this - newValue;
242 return *this;
243 }
244
246 *this = *this | newValue;
247 return *this;
248 }
249
251 *this = *this & newValue;
252 return *this;
253 }
254
255private:
256 struct {
257 alignas(ALIGN) char buffer[sizeof(value_type)];
258 } Value;
259
260public:
261 struct ref {
262 explicit ref(void *Ptr) : Ptr(Ptr) {}
263
264 operator value_type() const {
265 return endian::read<value_type, endian, alignment>(Ptr);
266 }
267
268 void operator=(value_type NewValue) {
269 endian::write<value_type, endian, alignment>(Ptr, NewValue);
270 }
271
272 private:
273 void *Ptr;
274 };
275};
276
277} // end namespace detail
278
281 unaligned>;
284 unaligned>;
287 unaligned>;
288
291 unaligned>;
294 unaligned>;
297 unaligned>;
298
301 aligned>;
304 aligned>;
307 aligned>;
308
311 aligned>;
314 aligned>;
317 aligned>;
318
319using ubig16_t =
321 unaligned>;
322using ubig32_t =
324 unaligned>;
325using ubig64_t =
327 unaligned>;
328
329using big16_t =
331 unaligned>;
332using big32_t =
334 unaligned>;
335using big64_t =
337 unaligned>;
338
341 aligned>;
344 aligned>;
347 aligned>;
348
351 aligned>;
354 aligned>;
357 aligned>;
358
361 unaligned>;
364 unaligned>;
367 unaligned>;
368
371 unaligned>;
374 unaligned>;
377 unaligned>;
378
379template <typename T>
380using little_t =
382 unaligned>;
383template <typename T>
385 unaligned>;
386
387template <typename T>
390 aligned>;
391template <typename T>
394
395namespace endian {
396
397template <typename T, endianness E> [[nodiscard]] inline T read(const void *P) {
399}
400
401[[nodiscard]] inline uint16_t read16(const void *P, endianness E) {
402 return read<uint16_t>(P, E);
403}
404[[nodiscard]] inline uint32_t read32(const void *P, endianness E) {
405 return read<uint32_t>(P, E);
406}
407[[nodiscard]] inline uint64_t read64(const void *P, endianness E) {
408 return read<uint64_t>(P, E);
409}
410
411template <endianness E> [[nodiscard]] inline uint16_t read16(const void *P) {
412 return read<uint16_t, E>(P);
413}
414template <endianness E> [[nodiscard]] inline uint32_t read32(const void *P) {
415 return read<uint32_t, E>(P);
416}
417template <endianness E> [[nodiscard]] inline uint64_t read64(const void *P) {
418 return read<uint64_t, E>(P);
419}
420
421[[nodiscard]] inline uint16_t read16le(const void *P) {
422 return read16<llvm::endianness::little>(P);
423}
424[[nodiscard]] inline uint32_t read32le(const void *P) {
425 return read32<llvm::endianness::little>(P);
426}
427[[nodiscard]] inline uint64_t read64le(const void *P) {
428 return read64<llvm::endianness::little>(P);
429}
430[[nodiscard]] inline uint16_t read16be(const void *P) {
431 return read16<llvm::endianness::big>(P);
432}
433[[nodiscard]] inline uint32_t read32be(const void *P) {
434 return read32<llvm::endianness::big>(P);
435}
436[[nodiscard]] inline uint64_t read64be(const void *P) {
437 return read64<llvm::endianness::big>(P);
438}
439
440template <typename T, endianness E> inline void write(void *P, T V) {
442}
443
444inline void write16(void *P, uint16_t V, endianness E) {
445 write<uint16_t>(P, V, E);
446}
447inline void write32(void *P, uint32_t V, endianness E) {
448 write<uint32_t>(P, V, E);
449}
450inline void write64(void *P, uint64_t V, endianness E) {
451 write<uint64_t>(P, V, E);
452}
453
454template <endianness E> inline void write16(void *P, uint16_t V) {
455 write<uint16_t, E>(P, V);
456}
457template <endianness E> inline void write32(void *P, uint32_t V) {
458 write<uint32_t, E>(P, V);
459}
460template <endianness E> inline void write64(void *P, uint64_t V) {
461 write<uint64_t, E>(P, V);
462}
463
464inline void write16le(void *P, uint16_t V) {
465 write16<llvm::endianness::little>(P, V);
466}
467inline void write32le(void *P, uint32_t V) {
468 write32<llvm::endianness::little>(P, V);
469}
470inline void write64le(void *P, uint64_t V) {
471 write64<llvm::endianness::little>(P, V);
472}
473inline void write16be(void *P, uint16_t V) {
474 write16<llvm::endianness::big>(P, V);
475}
476inline void write32be(void *P, uint32_t V) {
477 write32<llvm::endianness::big>(P, V);
478}
479inline void write64be(void *P, uint64_t V) {
480 write64<llvm::endianness::big>(P, V);
481}
482
483} // end namespace endian
484
485} // end namespace support
486} // end namespace llvm
487
488#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:395
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:427
value_type byte_swap(value_type value, endianness endian)
Definition: Endian.h:44
uint16_t read16le(const void *P)
Definition: Endian.h:421
uint32_t read32(const void *P, endianness E)
Definition: Endian.h:404
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:109
void write16be(void *P, uint16_t V)
Definition: Endian.h:473
void write64le(void *P, uint64_t V)
Definition: Endian.h:470
uint64_t read64be(const void *P)
Definition: Endian.h:436
void write32le(void *P, uint32_t V)
Definition: Endian.h:467
void write32(void *P, uint32_t V, endianness E)
Definition: Endian.h:447
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:161
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:126
void write32be(void *P, uint32_t V)
Definition: Endian.h:476
uint64_t read64(const void *P, endianness E)
Definition: Endian.h:407
uint32_t read32be(const void *P)
Definition: Endian.h:433
void write16(void *P, uint16_t V, endianness E)
Definition: Endian.h:444
void write16le(void *P, uint16_t V)
Definition: Endian.h:464
void write64be(void *P, uint64_t V)
Definition: Endian.h:479
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:450
uint16_t read16be(const void *P)
Definition: Endian.h:430
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:91
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:76
uint32_t read32le(const void *P)
Definition: Endian.h:424
uint16_t read16(const void *P, endianness E)
Definition: Endian.h:401
std::make_unsigned_t< value_type > make_unsigned_t
Definition: Endian.h:121
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:235
packed_endian_specific_integral & operator&=(value_type newValue)
Definition: Endian.h:250
packed_endian_specific_integral & operator|=(value_type newValue)
Definition: Endian.h:245
packed_endian_specific_integral & operator-=(value_type newValue)
Definition: Endian.h:240