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