1 //===- Endian.h - Utilities for IO with endian specific data ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares generic functions to read and write endian specific data.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_SUPPORT_ENDIAN_H
14 #define LLVM_SUPPORT_ENDIAN_H
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/SwapByteOrder.h"
22 #include <type_traits>
27 enum endianness {big, little, native};
29 // These are named values for common alignments.
30 enum {aligned = 0, unaligned = 1};
34 /// ::value is either alignment, or alignof(T) if alignment is 0.
35 template<class T, int alignment>
36 struct PickAlignment {
37 enum { value = alignment == 0 ? alignof(T) : alignment };
40 } // end namespace detail
44 constexpr endianness system_endianness() {
45 return sys::IsBigEndianHost ? big : little;
48 template <typename value_type>
49 inline value_type byte_swap(value_type value, endianness endian) {
50 if ((endian != native) && (endian != system_endianness()))
51 sys::swapByteOrder(value);
55 /// Swap the bytes of value to match the given endianness.
56 template<typename value_type, endianness endian>
57 inline value_type byte_swap(value_type value) {
58 return byte_swap(value, endian);
61 /// Read a value of a particular endianness from memory.
62 template <typename value_type, std::size_t alignment>
63 inline value_type read(const void *memory, endianness endian) {
68 memory, (detail::PickAlignment<value_type, alignment>::value)),
70 return byte_swap<value_type>(ret, endian);
73 template<typename value_type,
75 std::size_t alignment>
76 inline value_type read(const void *memory) {
77 return read<value_type, alignment>(memory, endian);
80 /// Read a value of a particular endianness from a buffer, and increment the
81 /// buffer past that value.
82 template <typename value_type, std::size_t alignment, typename CharT>
83 inline value_type readNext(const CharT *&memory, endianness endian) {
84 value_type ret = read<value_type, alignment>(memory, endian);
85 memory += sizeof(value_type);
89 template<typename value_type, endianness endian, std::size_t alignment,
91 inline value_type readNext(const CharT *&memory) {
92 return readNext<value_type, alignment, CharT>(memory, endian);
95 /// Write a value to memory with a particular endianness.
96 template <typename value_type, std::size_t alignment>
97 inline void write(void *memory, value_type value, endianness endian) {
98 value = byte_swap<value_type>(value, endian);
99 memcpy(LLVM_ASSUME_ALIGNED(
100 memory, (detail::PickAlignment<value_type, alignment>::value)),
101 &value, sizeof(value_type));
104 template<typename value_type,
106 std::size_t alignment>
107 inline void write(void *memory, value_type value) {
108 write<value_type, alignment>(memory, value, endian);
111 template <typename value_type>
112 using make_unsigned_t = std::make_unsigned_t<value_type>;
114 /// Read a value of a particular endianness from memory, for a location
115 /// that starts at the given bit offset within the first byte.
116 template <typename value_type, endianness endian, std::size_t alignment>
117 inline value_type readAtBitAlignment(const void *memory, uint64_t startBit) {
118 assert(startBit < 8);
120 return read<value_type, endian, alignment>(memory);
122 // Read two values and compose the result from them.
126 memory, (detail::PickAlignment<value_type, alignment>::value)),
127 sizeof(value_type) * 2);
128 val[0] = byte_swap<value_type, endian>(val[0]);
129 val[1] = byte_swap<value_type, endian>(val[1]);
131 // Shift bits from the lower value into place.
132 make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
133 // Mask off upper bits after right shift in case of signed type.
134 make_unsigned_t<value_type> numBitsFirstVal =
135 (sizeof(value_type) * 8) - startBit;
136 lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
138 // Get the bits from the upper value.
139 make_unsigned_t<value_type> upperVal =
140 val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
141 // Shift them in to place.
142 upperVal <<= numBitsFirstVal;
144 return lowerVal | upperVal;
148 /// Write a value to memory with a particular endianness, for a location
149 /// that starts at the given bit offset within the first byte.
150 template <typename value_type, endianness endian, std::size_t alignment>
151 inline void writeAtBitAlignment(void *memory, value_type value,
153 assert(startBit < 8);
155 write<value_type, endian, alignment>(memory, value);
157 // Read two values and shift the result into them.
161 memory, (detail::PickAlignment<value_type, alignment>::value)),
162 sizeof(value_type) * 2);
163 val[0] = byte_swap<value_type, endian>(val[0]);
164 val[1] = byte_swap<value_type, endian>(val[1]);
166 // Mask off any existing bits in the upper part of the lower value that
167 // we want to replace.
168 val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
169 make_unsigned_t<value_type> numBitsFirstVal =
170 (sizeof(value_type) * 8) - startBit;
171 make_unsigned_t<value_type> lowerVal = value;
173 // Mask off the upper bits in the new value that are not going to go into
174 // the lower value. This avoids a left shift of a negative value, which
175 // is undefined behavior.
176 lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
177 // Now shift the new bits into place
178 lowerVal <<= startBit;
182 // Mask off any existing bits in the lower part of the upper value that
183 // we want to replace.
184 val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
185 // Next shift the bits that go into the upper value into position.
186 make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
187 // Mask off upper bits after right shift in case of signed type.
188 upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
191 // Finally, rewrite values.
192 val[0] = byte_swap<value_type, endian>(val[0]);
193 val[1] = byte_swap<value_type, endian>(val[1]);
194 memcpy(LLVM_ASSUME_ALIGNED(
195 memory, (detail::PickAlignment<value_type, alignment>::value)),
196 &val[0], sizeof(value_type) * 2);
200 } // end namespace endian
204 template <typename ValueType, endianness Endian, std::size_t Alignment,
205 std::size_t ALIGN = PickAlignment<ValueType, Alignment>::value>
206 struct packed_endian_specific_integral {
207 using value_type = ValueType;
208 static constexpr endianness endian = Endian;
209 static constexpr std::size_t alignment = Alignment;
211 packed_endian_specific_integral() = default;
213 explicit packed_endian_specific_integral(value_type val) { *this = val; }
215 operator value_type() const {
216 return endian::read<value_type, endian, alignment>(
217 (const void*)Value.buffer);
220 void operator=(value_type newValue) {
221 endian::write<value_type, endian, alignment>(
222 (void*)Value.buffer, newValue);
225 packed_endian_specific_integral &operator+=(value_type newValue) {
226 *this = *this + newValue;
230 packed_endian_specific_integral &operator-=(value_type newValue) {
231 *this = *this - newValue;
235 packed_endian_specific_integral &operator|=(value_type newValue) {
236 *this = *this | newValue;
240 packed_endian_specific_integral &operator&=(value_type newValue) {
241 *this = *this & newValue;
247 alignas(ALIGN) char buffer[sizeof(value_type)];
252 explicit ref(void *Ptr) : Ptr(Ptr) {}
254 operator value_type() const {
255 return endian::read<value_type, endian, alignment>(Ptr);
258 void operator=(value_type NewValue) {
259 endian::write<value_type, endian, alignment>(Ptr, NewValue);
267 } // end namespace detail
270 detail::packed_endian_specific_integral<uint16_t, little, unaligned>;
272 detail::packed_endian_specific_integral<uint32_t, little, unaligned>;
274 detail::packed_endian_specific_integral<uint64_t, little, unaligned>;
277 detail::packed_endian_specific_integral<int16_t, little, unaligned>;
279 detail::packed_endian_specific_integral<int32_t, little, unaligned>;
281 detail::packed_endian_specific_integral<int64_t, little, unaligned>;
283 using aligned_ulittle16_t =
284 detail::packed_endian_specific_integral<uint16_t, little, aligned>;
285 using aligned_ulittle32_t =
286 detail::packed_endian_specific_integral<uint32_t, little, aligned>;
287 using aligned_ulittle64_t =
288 detail::packed_endian_specific_integral<uint64_t, little, aligned>;
290 using aligned_little16_t =
291 detail::packed_endian_specific_integral<int16_t, little, aligned>;
292 using aligned_little32_t =
293 detail::packed_endian_specific_integral<int32_t, little, aligned>;
294 using aligned_little64_t =
295 detail::packed_endian_specific_integral<int64_t, little, aligned>;
298 detail::packed_endian_specific_integral<uint16_t, big, unaligned>;
300 detail::packed_endian_specific_integral<uint32_t, big, unaligned>;
302 detail::packed_endian_specific_integral<uint64_t, big, unaligned>;
305 detail::packed_endian_specific_integral<int16_t, big, unaligned>;
307 detail::packed_endian_specific_integral<int32_t, big, unaligned>;
309 detail::packed_endian_specific_integral<int64_t, big, unaligned>;
311 using aligned_ubig16_t =
312 detail::packed_endian_specific_integral<uint16_t, big, aligned>;
313 using aligned_ubig32_t =
314 detail::packed_endian_specific_integral<uint32_t, big, aligned>;
315 using aligned_ubig64_t =
316 detail::packed_endian_specific_integral<uint64_t, big, aligned>;
318 using aligned_big16_t =
319 detail::packed_endian_specific_integral<int16_t, big, aligned>;
320 using aligned_big32_t =
321 detail::packed_endian_specific_integral<int32_t, big, aligned>;
322 using aligned_big64_t =
323 detail::packed_endian_specific_integral<int64_t, big, aligned>;
325 using unaligned_uint16_t =
326 detail::packed_endian_specific_integral<uint16_t, native, unaligned>;
327 using unaligned_uint32_t =
328 detail::packed_endian_specific_integral<uint32_t, native, unaligned>;
329 using unaligned_uint64_t =
330 detail::packed_endian_specific_integral<uint64_t, native, unaligned>;
332 using unaligned_int16_t =
333 detail::packed_endian_specific_integral<int16_t, native, unaligned>;
334 using unaligned_int32_t =
335 detail::packed_endian_specific_integral<int32_t, native, unaligned>;
336 using unaligned_int64_t =
337 detail::packed_endian_specific_integral<int64_t, native, unaligned>;
339 template <typename T>
340 using little_t = detail::packed_endian_specific_integral<T, little, unaligned>;
341 template <typename T>
342 using big_t = detail::packed_endian_specific_integral<T, big, unaligned>;
344 template <typename T>
345 using aligned_little_t =
346 detail::packed_endian_specific_integral<T, little, aligned>;
347 template <typename T>
348 using aligned_big_t = detail::packed_endian_specific_integral<T, big, aligned>;
352 template <typename T> inline T read(const void *P, endianness E) {
353 return read<T, unaligned>(P, E);
356 template <typename T, endianness E> inline T read(const void *P) {
357 return *(const detail::packed_endian_specific_integral<T, E, unaligned> *)P;
360 inline uint16_t read16(const void *P, endianness E) {
361 return read<uint16_t>(P, E);
363 inline uint32_t read32(const void *P, endianness E) {
364 return read<uint32_t>(P, E);
366 inline uint64_t read64(const void *P, endianness E) {
367 return read<uint64_t>(P, E);
370 template <endianness E> inline uint16_t read16(const void *P) {
371 return read<uint16_t, E>(P);
373 template <endianness E> inline uint32_t read32(const void *P) {
374 return read<uint32_t, E>(P);
376 template <endianness E> inline uint64_t read64(const void *P) {
377 return read<uint64_t, E>(P);
380 inline uint16_t read16le(const void *P) { return read16<little>(P); }
381 inline uint32_t read32le(const void *P) { return read32<little>(P); }
382 inline uint64_t read64le(const void *P) { return read64<little>(P); }
383 inline uint16_t read16be(const void *P) { return read16<big>(P); }
384 inline uint32_t read32be(const void *P) { return read32<big>(P); }
385 inline uint64_t read64be(const void *P) { return read64<big>(P); }
387 template <typename T> inline void write(void *P, T V, endianness E) {
388 write<T, unaligned>(P, V, E);
391 template <typename T, endianness E> inline void write(void *P, T V) {
392 *(detail::packed_endian_specific_integral<T, E, unaligned> *)P = V;
395 inline void write16(void *P, uint16_t V, endianness E) {
396 write<uint16_t>(P, V, E);
398 inline void write32(void *P, uint32_t V, endianness E) {
399 write<uint32_t>(P, V, E);
401 inline void write64(void *P, uint64_t V, endianness E) {
402 write<uint64_t>(P, V, E);
405 template <endianness E> inline void write16(void *P, uint16_t V) {
406 write<uint16_t, E>(P, V);
408 template <endianness E> inline void write32(void *P, uint32_t V) {
409 write<uint32_t, E>(P, V);
411 template <endianness E> inline void write64(void *P, uint64_t V) {
412 write<uint64_t, E>(P, V);
415 inline void write16le(void *P, uint16_t V) { write16<little>(P, V); }
416 inline void write32le(void *P, uint32_t V) { write32<little>(P, V); }
417 inline void write64le(void *P, uint64_t V) { write64<little>(P, V); }
418 inline void write16be(void *P, uint16_t V) { write16<big>(P, V); }
419 inline void write32be(void *P, uint32_t V) { write32<big>(P, V); }
420 inline void write64be(void *P, uint64_t V) { write64<big>(P, V); }
422 } // end namespace endian
424 } // end namespace support
425 } // end namespace llvm
427 #endif // LLVM_SUPPORT_ENDIAN_H