xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Support/Endian.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
25 namespace llvm {
26 namespace support {
27 
28 // These are named values for common alignments.
29 enum {aligned = 0, unaligned = 1};
30 
31 namespace detail {
32 
33 /// ::value is either alignment, or alignof(T) if alignment is 0.
34 template<class T, int alignment>
35 struct PickAlignment {
36  enum { value = alignment == 0 ? alignof(T) : alignment };
37 };
38 
39 } // end namespace detail
40 
41 namespace endian {
42 
43 template <typename value_type>
byte_swap(value_type value,endianness endian)44 [[nodiscard]] inline value_type byte_swap(value_type value, endianness endian) {
45   if (endian != llvm::endianness::native)
46     sys::swapByteOrder(value);
47   return value;
48 }
49 
50 /// Swap the bytes of value to match the given endianness.
51 template <typename value_type, endianness endian>
byte_swap(value_type value)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.
57 template <typename value_type, std::size_t alignment = unaligned>
read(const void * memory,endianness endian)58 [[nodiscard]] inline value_type read(const void *memory, endianness endian) {
59   value_type ret;
60 
61   memcpy(static_cast<void *>(&ret),
62          LLVM_ASSUME_ALIGNED(
63              memory, (detail::PickAlignment<value_type, alignment>::value)),
64          sizeof(value_type));
65   return byte_swap<value_type>(ret, endian);
66 }
67 
68 template <typename value_type, endianness endian, std::size_t alignment>
read(const void * memory)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.
75 template <typename value_type, std::size_t alignment = unaligned,
76           typename CharT>
readNext(const CharT * & memory,endianness endian)77 [[nodiscard]] inline value_type readNext(const CharT *&memory,
78                                          endianness endian) {
79   value_type ret = read<value_type, alignment>(memory, endian);
80   memory += sizeof(value_type);
81   return ret;
82 }
83 
84 template <typename value_type, endianness endian,
85           std::size_t alignment = unaligned, typename CharT>
readNext(const CharT * & memory)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.
91 template <typename value_type, std::size_t alignment = unaligned>
write(void * memory,value_type value,endianness endian)92 inline void write(void *memory, value_type value, endianness endian) {
93   value = byte_swap<value_type>(value, endian);
94   memcpy(LLVM_ASSUME_ALIGNED(
95              memory, (detail::PickAlignment<value_type, alignment>::value)),
96          &value, sizeof(value_type));
97 }
98 
99 template<typename value_type,
100          endianness endian,
101          std::size_t alignment>
write(void * memory,value_type value)102 inline 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.
108 template <typename value_type, std::size_t alignment = unaligned,
109           typename CharT>
writeNext(CharT * & memory,value_type value,endianness endian)110 inline void writeNext(CharT *&memory, value_type value, endianness endian) {
111   write(memory, value, endian);
112   memory += sizeof(value_type);
113 }
114 
115 template <typename value_type, endianness endian,
116           std::size_t alignment = unaligned, typename CharT>
writeNext(CharT * & memory,value_type value)117 inline void writeNext(CharT *&memory, value_type value) {
118   writeNext<value_type, alignment, CharT>(memory, value, endian);
119 }
120 
121 template <typename value_type>
122 using 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.
126 template <typename value_type, endianness endian, std::size_t alignment>
readAtBitAlignment(const void * memory,uint64_t startBit)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],
136            LLVM_ASSUME_ALIGNED(
137                memory, (detail::PickAlignment<value_type, alignment>::value)),
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.
150     make_unsigned_t<value_type> upperVal =
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.
161 template <typename value_type, endianness endian, std::size_t alignment>
writeAtBitAlignment(void * memory,value_type value,uint64_t startBit)162 inline 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],
171            LLVM_ASSUME_ALIGNED(
172                memory, (detail::PickAlignment<value_type, alignment>::value)),
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;
182     make_unsigned_t<value_type> lowerVal = value;
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(
206                memory, (detail::PickAlignment<value_type, alignment>::value)),
207            &val[0], sizeof(value_type) * 2);
208   }
209 }
210 
211 } // end namespace endian
212 
213 namespace detail {
214 
215 template <typename ValueType, endianness Endian, std::size_t Alignment,
216           std::size_t ALIGN = PickAlignment<ValueType, Alignment>::value>
217 struct packed_endian_specific_integral {
218   using value_type = ValueType;
219   static constexpr endianness endian = Endian;
220   static constexpr std::size_t alignment = Alignment;
221 
222   packed_endian_specific_integral() = default;
223 
packed_endian_specific_integralpacked_endian_specific_integral224   explicit packed_endian_specific_integral(value_type val) { *this = val; }
225 
valuepacked_endian_specific_integral226   value_type value() const {
227     return endian::read<value_type, endian, alignment>(
228       (const void*)Value.buffer);
229   }
value_typepacked_endian_specific_integral230   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 
237   packed_endian_specific_integral &operator+=(value_type newValue) {
238     *this = *this + newValue;
239     return *this;
240   }
241 
242   packed_endian_specific_integral &operator-=(value_type newValue) {
243     *this = *this - newValue;
244     return *this;
245   }
246 
247   packed_endian_specific_integral &operator|=(value_type newValue) {
248     *this = *this | newValue;
249     return *this;
250   }
251 
252   packed_endian_specific_integral &operator&=(value_type newValue) {
253     *this = *this & newValue;
254     return *this;
255   }
256 
257 private:
258   struct {
259     alignas(ALIGN) char buffer[sizeof(value_type)];
260   } Value;
261 
262 public:
263   struct ref {
refpacked_endian_specific_integral::ref264     explicit ref(void *Ptr) : Ptr(Ptr) {}
265 
value_typepacked_endian_specific_integral::ref266     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 
281 using ulittle8_t =
282     detail::packed_endian_specific_integral<uint8_t, llvm::endianness::little,
283                                             unaligned>;
284 using ulittle16_t =
285     detail::packed_endian_specific_integral<uint16_t, llvm::endianness::little,
286                                             unaligned>;
287 using ulittle32_t =
288     detail::packed_endian_specific_integral<uint32_t, llvm::endianness::little,
289                                             unaligned>;
290 using ulittle64_t =
291     detail::packed_endian_specific_integral<uint64_t, llvm::endianness::little,
292                                             unaligned>;
293 
294 using little16_t =
295     detail::packed_endian_specific_integral<int16_t, llvm::endianness::little,
296                                             unaligned>;
297 using little32_t =
298     detail::packed_endian_specific_integral<int32_t, llvm::endianness::little,
299                                             unaligned>;
300 using little64_t =
301     detail::packed_endian_specific_integral<int64_t, llvm::endianness::little,
302                                             unaligned>;
303 
304 using aligned_ulittle16_t =
305     detail::packed_endian_specific_integral<uint16_t, llvm::endianness::little,
306                                             aligned>;
307 using aligned_ulittle32_t =
308     detail::packed_endian_specific_integral<uint32_t, llvm::endianness::little,
309                                             aligned>;
310 using aligned_ulittle64_t =
311     detail::packed_endian_specific_integral<uint64_t, llvm::endianness::little,
312                                             aligned>;
313 
314 using aligned_little16_t =
315     detail::packed_endian_specific_integral<int16_t, llvm::endianness::little,
316                                             aligned>;
317 using aligned_little32_t =
318     detail::packed_endian_specific_integral<int32_t, llvm::endianness::little,
319                                             aligned>;
320 using aligned_little64_t =
321     detail::packed_endian_specific_integral<int64_t, llvm::endianness::little,
322                                             aligned>;
323 
324 using ubig16_t =
325     detail::packed_endian_specific_integral<uint16_t, llvm::endianness::big,
326                                             unaligned>;
327 using ubig32_t =
328     detail::packed_endian_specific_integral<uint32_t, llvm::endianness::big,
329                                             unaligned>;
330 using ubig64_t =
331     detail::packed_endian_specific_integral<uint64_t, llvm::endianness::big,
332                                             unaligned>;
333 
334 using big16_t =
335     detail::packed_endian_specific_integral<int16_t, llvm::endianness::big,
336                                             unaligned>;
337 using big32_t =
338     detail::packed_endian_specific_integral<int32_t, llvm::endianness::big,
339                                             unaligned>;
340 using big64_t =
341     detail::packed_endian_specific_integral<int64_t, llvm::endianness::big,
342                                             unaligned>;
343 
344 using aligned_ubig16_t =
345     detail::packed_endian_specific_integral<uint16_t, llvm::endianness::big,
346                                             aligned>;
347 using aligned_ubig32_t =
348     detail::packed_endian_specific_integral<uint32_t, llvm::endianness::big,
349                                             aligned>;
350 using aligned_ubig64_t =
351     detail::packed_endian_specific_integral<uint64_t, llvm::endianness::big,
352                                             aligned>;
353 
354 using aligned_big16_t =
355     detail::packed_endian_specific_integral<int16_t, llvm::endianness::big,
356                                             aligned>;
357 using aligned_big32_t =
358     detail::packed_endian_specific_integral<int32_t, llvm::endianness::big,
359                                             aligned>;
360 using aligned_big64_t =
361     detail::packed_endian_specific_integral<int64_t, llvm::endianness::big,
362                                             aligned>;
363 
364 using unaligned_uint16_t =
365     detail::packed_endian_specific_integral<uint16_t, llvm::endianness::native,
366                                             unaligned>;
367 using unaligned_uint32_t =
368     detail::packed_endian_specific_integral<uint32_t, llvm::endianness::native,
369                                             unaligned>;
370 using unaligned_uint64_t =
371     detail::packed_endian_specific_integral<uint64_t, llvm::endianness::native,
372                                             unaligned>;
373 
374 using unaligned_int16_t =
375     detail::packed_endian_specific_integral<int16_t, llvm::endianness::native,
376                                             unaligned>;
377 using unaligned_int32_t =
378     detail::packed_endian_specific_integral<int32_t, llvm::endianness::native,
379                                             unaligned>;
380 using unaligned_int64_t =
381     detail::packed_endian_specific_integral<int64_t, llvm::endianness::native,
382                                             unaligned>;
383 
384 template <typename T>
385 using little_t =
386     detail::packed_endian_specific_integral<T, llvm::endianness::little,
387                                             unaligned>;
388 template <typename T>
389 using big_t = detail::packed_endian_specific_integral<T, llvm::endianness::big,
390                                                       unaligned>;
391 
392 template <typename T>
393 using aligned_little_t =
394     detail::packed_endian_specific_integral<T, llvm::endianness::little,
395                                             aligned>;
396 template <typename T>
397 using aligned_big_t =
398     detail::packed_endian_specific_integral<T, llvm::endianness::big, aligned>;
399 
400 namespace endian {
401 
read(const void * P)402 template <typename T, endianness E> [[nodiscard]] inline T read(const void *P) {
403   return *(const detail::packed_endian_specific_integral<T, E, unaligned> *)P;
404 }
405 
read16(const void * P,endianness E)406 [[nodiscard]] inline uint16_t read16(const void *P, endianness E) {
407   return read<uint16_t>(P, E);
408 }
read32(const void * P,endianness E)409 [[nodiscard]] inline uint32_t read32(const void *P, endianness E) {
410   return read<uint32_t>(P, E);
411 }
read64(const void * P,endianness E)412 [[nodiscard]] inline uint64_t read64(const void *P, endianness E) {
413   return read<uint64_t>(P, E);
414 }
415 
read16(const void * P)416 template <endianness E> [[nodiscard]] inline uint16_t read16(const void *P) {
417   return read<uint16_t, E>(P);
418 }
read32(const void * P)419 template <endianness E> [[nodiscard]] inline uint32_t read32(const void *P) {
420   return read<uint32_t, E>(P);
421 }
read64(const void * P)422 template <endianness E> [[nodiscard]] inline uint64_t read64(const void *P) {
423   return read<uint64_t, E>(P);
424 }
425 
read16le(const void * P)426 [[nodiscard]] inline uint16_t read16le(const void *P) {
427   return read16<llvm::endianness::little>(P);
428 }
read32le(const void * P)429 [[nodiscard]] inline uint32_t read32le(const void *P) {
430   return read32<llvm::endianness::little>(P);
431 }
read64le(const void * P)432 [[nodiscard]] inline uint64_t read64le(const void *P) {
433   return read64<llvm::endianness::little>(P);
434 }
read16be(const void * P)435 [[nodiscard]] inline uint16_t read16be(const void *P) {
436   return read16<llvm::endianness::big>(P);
437 }
read32be(const void * P)438 [[nodiscard]] inline uint32_t read32be(const void *P) {
439   return read32<llvm::endianness::big>(P);
440 }
read64be(const void * P)441 [[nodiscard]] inline uint64_t read64be(const void *P) {
442   return read64<llvm::endianness::big>(P);
443 }
444 
write(void * P,T V)445 template <typename T, endianness E> inline void write(void *P, T V) {
446   *(detail::packed_endian_specific_integral<T, E, unaligned> *)P = V;
447 }
448 
write16(void * P,uint16_t V,endianness E)449 inline void write16(void *P, uint16_t V, endianness E) {
450   write<uint16_t>(P, V, E);
451 }
write32(void * P,uint32_t V,endianness E)452 inline void write32(void *P, uint32_t V, endianness E) {
453   write<uint32_t>(P, V, E);
454 }
write64(void * P,uint64_t V,endianness E)455 inline void write64(void *P, uint64_t V, endianness E) {
456   write<uint64_t>(P, V, E);
457 }
458 
write16(void * P,uint16_t V)459 template <endianness E> inline void write16(void *P, uint16_t V) {
460   write<uint16_t, E>(P, V);
461 }
write32(void * P,uint32_t V)462 template <endianness E> inline void write32(void *P, uint32_t V) {
463   write<uint32_t, E>(P, V);
464 }
write64(void * P,uint64_t V)465 template <endianness E> inline void write64(void *P, uint64_t V) {
466   write<uint64_t, E>(P, V);
467 }
468 
write16le(void * P,uint16_t V)469 inline void write16le(void *P, uint16_t V) {
470   write16<llvm::endianness::little>(P, V);
471 }
write32le(void * P,uint32_t V)472 inline void write32le(void *P, uint32_t V) {
473   write32<llvm::endianness::little>(P, V);
474 }
write64le(void * P,uint64_t V)475 inline void write64le(void *P, uint64_t V) {
476   write64<llvm::endianness::little>(P, V);
477 }
write16be(void * P,uint16_t V)478 inline void write16be(void *P, uint16_t V) {
479   write16<llvm::endianness::big>(P, V);
480 }
write32be(void * P,uint32_t V)481 inline void write32be(void *P, uint32_t V) {
482   write32<llvm::endianness::big>(P, V);
483 }
write64be(void * P,uint64_t V)484 inline 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
494