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"
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/SwapByteOrder.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>
35struct PickAlignment {
36 enum { value = alignment == 0 ? alignof(T) : alignment };
37};
38
39} // end namespace detail
40
41namespace endian {
42
43/// Swap the bytes of value to match the given endianness.
44template <typename value_type>
45[[nodiscard]] inline value_type byte_swap(value_type value, endianness endian) {
46 if (endian != llvm::endianness::native)
47 sys::swapByteOrder(value);
48 return value;
49}
50
51/// Read a value of a particular endianness from memory.
52template <typename value_type, std::size_t alignment = unaligned>
53[[nodiscard]] inline value_type read(const void *memory, endianness endian) {
54 value_type ret;
55
56 memcpy(static_cast<void *>(&ret),
57 LLVM_ASSUME_ALIGNED(
58 memory, (detail::PickAlignment<value_type, alignment>::value)),
59 sizeof(value_type));
60 return byte_swap<value_type>(ret, endian);
61}
62
63/// Read a value of a particular endianness from a buffer, and increment the
64/// buffer past that value.
65template <typename value_type, std::size_t alignment = unaligned,
66 typename CharT>
67[[nodiscard]] inline value_type readNext(const CharT *&memory,
68 endianness endian) {
69 value_type ret = read<value_type, alignment>(memory, endian);
70 memory += sizeof(value_type);
71 return ret;
72}
73
74template <typename value_type, endianness endian,
75 std::size_t alignment = unaligned, typename CharT>
76[[nodiscard]] inline value_type readNext(const CharT *&memory) {
77 return readNext<value_type, alignment, CharT>(memory, endian);
78}
79
80/// Write a value to memory with a particular endianness.
81template <typename value_type, std::size_t alignment = unaligned>
82inline void write(void *memory, value_type value, endianness endian) {
83 value = byte_swap<value_type>(value, endian);
84 memcpy(LLVM_ASSUME_ALIGNED(
85 memory, (detail::PickAlignment<value_type, alignment>::value)),
86 &value, sizeof(value_type));
87}
88
89/// Write a value of a particular endianness, and increment the buffer past that
90/// value.
91template <typename value_type, std::size_t alignment = unaligned,
92 typename CharT>
93inline void writeNext(CharT *&memory, value_type value, endianness endian) {
94 write(memory, value, endian);
95 memory += sizeof(value_type);
96}
97
98template <typename value_type, endianness endian,
99 std::size_t alignment = unaligned, typename CharT>
100inline void writeNext(CharT *&memory, value_type value) {
101 writeNext<value_type, alignment, CharT>(memory, value, endian);
102}
103
104template <typename value_type>
105using make_unsigned_t = std::make_unsigned_t<value_type>;
106
107/// Read a value of a particular endianness from memory, for a location
108/// that starts at the given bit offset within the first byte.
109template <typename value_type, endianness endian, std::size_t alignment>
110[[nodiscard]] inline value_type readAtBitAlignment(const void *memory,
111 uint64_t startBit) {
112 assert(startBit < 8);
113 if (startBit == 0)
114 return read<value_type, alignment>(memory, endian);
115 else {
116 // Read two values and compose the result from them.
117 value_type val[2];
118 memcpy(&val[0],
119 LLVM_ASSUME_ALIGNED(
120 memory, (detail::PickAlignment<value_type, alignment>::value)),
121 sizeof(value_type) * 2);
122 val[0] = byte_swap<value_type>(val[0], endian);
123 val[1] = byte_swap<value_type>(val[1], endian);
124
125 // Shift bits from the lower value into place.
126 make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
127 // Mask off upper bits after right shift in case of signed type.
128 make_unsigned_t<value_type> numBitsFirstVal =
129 (sizeof(value_type) * 8) - startBit;
130 lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
131
132 // Get the bits from the upper value.
133 make_unsigned_t<value_type> upperVal =
134 val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
135 // Shift them in to place.
136 upperVal <<= numBitsFirstVal;
137
138 return lowerVal | upperVal;
139 }
140}
141
142/// Write a value to memory with a particular endianness, for a location
143/// that starts at the given bit offset within the first byte.
144template <typename value_type, endianness endian, std::size_t alignment>
145inline void writeAtBitAlignment(void *memory, value_type value,
146 uint64_t startBit) {
147 assert(startBit < 8);
148 if (startBit == 0)
149 write<value_type, alignment>(memory, value, endian);
150 else {
151 // Read two values and shift the result into them.
152 value_type val[2];
153 memcpy(&val[0],
154 LLVM_ASSUME_ALIGNED(
155 memory, (detail::PickAlignment<value_type, alignment>::value)),
156 sizeof(value_type) * 2);
157 val[0] = byte_swap<value_type>(val[0], endian);
158 val[1] = byte_swap<value_type>(val[1], endian);
159
160 // Mask off any existing bits in the upper part of the lower value that
161 // we want to replace.
162 val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
163 make_unsigned_t<value_type> numBitsFirstVal =
164 (sizeof(value_type) * 8) - startBit;
165 make_unsigned_t<value_type> lowerVal = value;
166 if (startBit > 0) {
167 // Mask off the upper bits in the new value that are not going to go into
168 // the lower value. This avoids a left shift of a negative value, which
169 // is undefined behavior.
170 lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
171 // Now shift the new bits into place
172 lowerVal <<= startBit;
173 }
174 val[0] |= lowerVal;
175
176 // Mask off any existing bits in the lower part of the upper value that
177 // we want to replace.
178 val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
179 // Next shift the bits that go into the upper value into position.
180 make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
181 // Mask off upper bits after right shift in case of signed type.
182 upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
183 val[1] |= upperVal;
184
185 // Finally, rewrite values.
186 val[0] = byte_swap<value_type>(val[0], endian);
187 val[1] = byte_swap<value_type>(val[1], endian);
188 memcpy(LLVM_ASSUME_ALIGNED(
189 memory, (detail::PickAlignment<value_type, alignment>::value)),
190 &val[0], sizeof(value_type) * 2);
191 }
192}
193
194} // end namespace endian
195
196namespace detail {
197
198template <typename ValueType, endianness Endian, std::size_t Alignment,
199 std::size_t ALIGN = PickAlignment<ValueType, Alignment>::value>
200struct packed_endian_specific_integral {
201 using value_type = ValueType;
202 static constexpr endianness endian = Endian;
203 static constexpr std::size_t alignment = Alignment;
204
205 packed_endian_specific_integral() = default;
206
207 explicit packed_endian_specific_integral(value_type val) { *this = val; }
208
209 value_type value() const {
210 return endian::read<value_type, alignment>((const void *)Value.buffer,
211 endian);
212 }
213 operator value_type() const { return value(); }
214
215 void operator=(value_type newValue) {
216 endian::write<value_type, alignment>((void *)Value.buffer, newValue,
217 endian);
218 }
219
220 packed_endian_specific_integral &operator+=(value_type newValue) {
221 *this = *this + newValue;
222 return *this;
223 }
224
225 packed_endian_specific_integral &operator-=(value_type newValue) {
226 *this = *this - newValue;
227 return *this;
228 }
229
230 packed_endian_specific_integral &operator|=(value_type newValue) {
231 *this = *this | newValue;
232 return *this;
233 }
234
235 packed_endian_specific_integral &operator&=(value_type newValue) {
236 *this = *this & newValue;
237 return *this;
238 }
239
240private:
241 struct {
242 alignas(ALIGN) char buffer[sizeof(value_type)];
243 } Value;
244
245public:
246 struct ref {
247 explicit ref(void *Ptr) : Ptr(Ptr) {}
248
249 operator value_type() const {
250 return endian::read<value_type, alignment>(Ptr, endian);
251 }
252
253 void operator=(value_type NewValue) {
254 endian::write<value_type, alignment>(Ptr, NewValue, endian);
255 }
256
257 private:
258 void *Ptr;
259 };
260};
261
262} // end namespace detail
263
264using ulittle8_t =
265 detail::packed_endian_specific_integral<uint8_t, llvm::endianness::little,
266 unaligned>;
267using ulittle16_t =
268 detail::packed_endian_specific_integral<uint16_t, llvm::endianness::little,
269 unaligned>;
270using ulittle32_t =
271 detail::packed_endian_specific_integral<uint32_t, llvm::endianness::little,
272 unaligned>;
273using ulittle64_t =
274 detail::packed_endian_specific_integral<uint64_t, llvm::endianness::little,
275 unaligned>;
276
277using little16_t =
278 detail::packed_endian_specific_integral<int16_t, llvm::endianness::little,
279 unaligned>;
280using little32_t =
281 detail::packed_endian_specific_integral<int32_t, llvm::endianness::little,
282 unaligned>;
283using little64_t =
284 detail::packed_endian_specific_integral<int64_t, llvm::endianness::little,
285 unaligned>;
286
287using aligned_ulittle16_t =
288 detail::packed_endian_specific_integral<uint16_t, llvm::endianness::little,
289 aligned>;
290using aligned_ulittle32_t =
291 detail::packed_endian_specific_integral<uint32_t, llvm::endianness::little,
292 aligned>;
293using aligned_ulittle64_t =
294 detail::packed_endian_specific_integral<uint64_t, llvm::endianness::little,
295 aligned>;
296
297using aligned_little16_t =
298 detail::packed_endian_specific_integral<int16_t, llvm::endianness::little,
299 aligned>;
300using aligned_little32_t =
301 detail::packed_endian_specific_integral<int32_t, llvm::endianness::little,
302 aligned>;
303using aligned_little64_t =
304 detail::packed_endian_specific_integral<int64_t, llvm::endianness::little,
305 aligned>;
306
307using ubig16_t =
308 detail::packed_endian_specific_integral<uint16_t, llvm::endianness::big,
309 unaligned>;
310using ubig32_t =
311 detail::packed_endian_specific_integral<uint32_t, llvm::endianness::big,
312 unaligned>;
313using ubig64_t =
314 detail::packed_endian_specific_integral<uint64_t, llvm::endianness::big,
315 unaligned>;
316
317using big16_t =
318 detail::packed_endian_specific_integral<int16_t, llvm::endianness::big,
319 unaligned>;
320using big32_t =
321 detail::packed_endian_specific_integral<int32_t, llvm::endianness::big,
322 unaligned>;
323using big64_t =
324 detail::packed_endian_specific_integral<int64_t, llvm::endianness::big,
325 unaligned>;
326
327using aligned_ubig16_t =
328 detail::packed_endian_specific_integral<uint16_t, llvm::endianness::big,
329 aligned>;
330using aligned_ubig32_t =
331 detail::packed_endian_specific_integral<uint32_t, llvm::endianness::big,
332 aligned>;
333using aligned_ubig64_t =
334 detail::packed_endian_specific_integral<uint64_t, llvm::endianness::big,
335 aligned>;
336
337using aligned_big16_t =
338 detail::packed_endian_specific_integral<int16_t, llvm::endianness::big,
339 aligned>;
340using aligned_big32_t =
341 detail::packed_endian_specific_integral<int32_t, llvm::endianness::big,
342 aligned>;
343using aligned_big64_t =
344 detail::packed_endian_specific_integral<int64_t, llvm::endianness::big,
345 aligned>;
346
347using unaligned_uint16_t =
348 detail::packed_endian_specific_integral<uint16_t, llvm::endianness::native,
349 unaligned>;
350using unaligned_uint32_t =
351 detail::packed_endian_specific_integral<uint32_t, llvm::endianness::native,
352 unaligned>;
353using unaligned_uint64_t =
354 detail::packed_endian_specific_integral<uint64_t, llvm::endianness::native,
355 unaligned>;
356
357using unaligned_int16_t =
358 detail::packed_endian_specific_integral<int16_t, llvm::endianness::native,
359 unaligned>;
360using unaligned_int32_t =
361 detail::packed_endian_specific_integral<int32_t, llvm::endianness::native,
362 unaligned>;
363using unaligned_int64_t =
364 detail::packed_endian_specific_integral<int64_t, llvm::endianness::native,
365 unaligned>;
366
367template <typename T>
368using little_t =
369 detail::packed_endian_specific_integral<T, llvm::endianness::little,
370 unaligned>;
371template <typename T>
372using big_t = detail::packed_endian_specific_integral<T, llvm::endianness::big,
373 unaligned>;
374
375template <typename T>
376using aligned_little_t =
377 detail::packed_endian_specific_integral<T, llvm::endianness::little,
378 aligned>;
379template <typename T>
380using aligned_big_t =
381 detail::packed_endian_specific_integral<T, llvm::endianness::big, aligned>;
382
383namespace endian {
384
385template <typename T, endianness E> [[nodiscard]] inline T read(const void *P) {
386 return *(const detail::packed_endian_specific_integral<T, E, unaligned> *)P;
387}
388
389[[nodiscard]] inline uint16_t read16(const void *P, endianness E) {
390 return read<uint16_t>(memory: P, endian: E);
391}
392[[nodiscard]] inline uint32_t read32(const void *P, endianness E) {
393 return read<uint32_t>(memory: P, endian: E);
394}
395[[nodiscard]] inline uint64_t read64(const void *P, endianness E) {
396 return read<uint64_t>(memory: P, endian: E);
397}
398
399template <endianness E> [[nodiscard]] inline uint16_t read16(const void *P) {
400 return read<uint16_t, E>(P);
401}
402template <endianness E> [[nodiscard]] inline uint32_t read32(const void *P) {
403 return read<uint32_t, E>(P);
404}
405template <endianness E> [[nodiscard]] inline uint64_t read64(const void *P) {
406 return read<uint64_t, E>(P);
407}
408
409[[nodiscard]] inline uint16_t read16le(const void *P) {
410 return read16<llvm::endianness::little>(P);
411}
412[[nodiscard]] inline uint32_t read32le(const void *P) {
413 return read32<llvm::endianness::little>(P);
414}
415[[nodiscard]] inline uint64_t read64le(const void *P) {
416 return read64<llvm::endianness::little>(P);
417}
418[[nodiscard]] inline uint16_t read16be(const void *P) {
419 return read16<llvm::endianness::big>(P);
420}
421[[nodiscard]] inline uint32_t read32be(const void *P) {
422 return read32<llvm::endianness::big>(P);
423}
424[[nodiscard]] inline uint64_t read64be(const void *P) {
425 return read64<llvm::endianness::big>(P);
426}
427
428template <typename T, endianness E> inline void write(void *P, T V) {
429 *(detail::packed_endian_specific_integral<T, E, unaligned> *)P = V;
430}
431
432inline void write16(void *P, uint16_t V, endianness E) {
433 write<uint16_t>(memory: P, value: V, endian: E);
434}
435inline void write32(void *P, uint32_t V, endianness E) {
436 write<uint32_t>(memory: P, value: V, endian: E);
437}
438inline void write64(void *P, uint64_t V, endianness E) {
439 write<uint64_t>(memory: P, value: V, endian: E);
440}
441
442template <endianness E> inline void write16(void *P, uint16_t V) {
443 write<uint16_t, E>(P, V);
444}
445template <endianness E> inline void write32(void *P, uint32_t V) {
446 write<uint32_t, E>(P, V);
447}
448template <endianness E> inline void write64(void *P, uint64_t V) {
449 write<uint64_t, E>(P, V);
450}
451
452inline void write16le(void *P, uint16_t V) {
453 write16<llvm::endianness::little>(P, V);
454}
455inline void write32le(void *P, uint32_t V) {
456 write32<llvm::endianness::little>(P, V);
457}
458inline void write64le(void *P, uint64_t V) {
459 write64<llvm::endianness::little>(P, V);
460}
461inline void write16be(void *P, uint16_t V) {
462 write16<llvm::endianness::big>(P, V);
463}
464inline void write32be(void *P, uint32_t V) {
465 write32<llvm::endianness::big>(P, V);
466}
467inline void write64be(void *P, uint64_t V) {
468 write64<llvm::endianness::big>(P, V);
469}
470
471} // end namespace endian
472
473} // end namespace support
474} // end namespace llvm
475
476#endif // LLVM_SUPPORT_ENDIAN_H
477