Endian.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #ifndef LLVM_SUPPORT_ENDIAN_H
  13. #define LLVM_SUPPORT_ENDIAN_H
  14. #include "llvm/Support/Compiler.h"
  15. #include "llvm/Support/SwapByteOrder.h"
  16. #include <cassert>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <cstring>
  20. #include <type_traits>
  21. namespace llvm {
  22. namespace support {
  23. enum endianness {big, little, native};
  24. // These are named values for common alignments.
  25. enum {aligned = 0, unaligned = 1};
  26. namespace detail {
  27. /// ::value is either alignment, or alignof(T) if alignment is 0.
  28. template<class T, int alignment>
  29. struct PickAlignment {
  30. enum { value = alignment == 0 ? alignof(T) : alignment };
  31. };
  32. } // end namespace detail
  33. namespace endian {
  34. constexpr endianness system_endianness() {
  35. return sys::IsBigEndianHost ? big : little;
  36. }
  37. template <typename value_type>
  38. inline value_type byte_swap(value_type value, endianness endian) {
  39. if ((endian != native) && (endian != system_endianness()))
  40. sys::swapByteOrder(value);
  41. return value;
  42. }
  43. /// Swap the bytes of value to match the given endianness.
  44. template<typename value_type, endianness endian>
  45. inline value_type byte_swap(value_type value) {
  46. return byte_swap(value, endian);
  47. }
  48. /// Read a value of a particular endianness from memory.
  49. template <typename value_type, std::size_t alignment>
  50. inline value_type read(const void *memory, endianness endian) {
  51. value_type ret;
  52. memcpy(&ret,
  53. LLVM_ASSUME_ALIGNED(
  54. memory, (detail::PickAlignment<value_type, alignment>::value)),
  55. sizeof(value_type));
  56. return byte_swap<value_type>(ret, endian);
  57. }
  58. template<typename value_type,
  59. endianness endian,
  60. std::size_t alignment>
  61. inline value_type read(const void *memory) {
  62. return read<value_type, alignment>(memory, endian);
  63. }
  64. /// Read a value of a particular endianness from a buffer, and increment the
  65. /// buffer past that value.
  66. template <typename value_type, std::size_t alignment, typename CharT>
  67. inline value_type readNext(const CharT *&memory, endianness endian) {
  68. value_type ret = read<value_type, alignment>(memory, endian);
  69. memory += sizeof(value_type);
  70. return ret;
  71. }
  72. template<typename value_type, endianness endian, std::size_t alignment,
  73. typename CharT>
  74. inline value_type readNext(const CharT *&memory) {
  75. return readNext<value_type, alignment, CharT>(memory, endian);
  76. }
  77. /// Write a value to memory with a particular endianness.
  78. template <typename value_type, std::size_t alignment>
  79. inline void write(void *memory, value_type value, endianness endian) {
  80. value = byte_swap<value_type>(value, endian);
  81. memcpy(LLVM_ASSUME_ALIGNED(
  82. memory, (detail::PickAlignment<value_type, alignment>::value)),
  83. &value, sizeof(value_type));
  84. }
  85. template<typename value_type,
  86. endianness endian,
  87. std::size_t alignment>
  88. inline void write(void *memory, value_type value) {
  89. write<value_type, alignment>(memory, value, endian);
  90. }
  91. template <typename value_type>
  92. using make_unsigned_t = std::make_unsigned_t<value_type>;
  93. /// Read a value of a particular endianness from memory, for a location
  94. /// that starts at the given bit offset within the first byte.
  95. template <typename value_type, endianness endian, std::size_t alignment>
  96. inline value_type readAtBitAlignment(const void *memory, uint64_t startBit) {
  97. assert(startBit < 8);
  98. if (startBit == 0)
  99. return read<value_type, endian, alignment>(memory);
  100. else {
  101. // Read two values and compose the result from them.
  102. value_type val[2];
  103. memcpy(&val[0],
  104. LLVM_ASSUME_ALIGNED(
  105. memory, (detail::PickAlignment<value_type, alignment>::value)),
  106. sizeof(value_type) * 2);
  107. val[0] = byte_swap<value_type, endian>(val[0]);
  108. val[1] = byte_swap<value_type, endian>(val[1]);
  109. // Shift bits from the lower value into place.
  110. make_unsigned_t<value_type> lowerVal = val[0] >> startBit;
  111. // Mask off upper bits after right shift in case of signed type.
  112. make_unsigned_t<value_type> numBitsFirstVal =
  113. (sizeof(value_type) * 8) - startBit;
  114. lowerVal &= ((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1;
  115. // Get the bits from the upper value.
  116. make_unsigned_t<value_type> upperVal =
  117. val[1] & (((make_unsigned_t<value_type>)1 << startBit) - 1);
  118. // Shift them in to place.
  119. upperVal <<= numBitsFirstVal;
  120. return lowerVal | upperVal;
  121. }
  122. }
  123. /// Write a value to memory with a particular endianness, for a location
  124. /// that starts at the given bit offset within the first byte.
  125. template <typename value_type, endianness endian, std::size_t alignment>
  126. inline void writeAtBitAlignment(void *memory, value_type value,
  127. uint64_t startBit) {
  128. assert(startBit < 8);
  129. if (startBit == 0)
  130. write<value_type, endian, alignment>(memory, value);
  131. else {
  132. // Read two values and shift the result into them.
  133. value_type val[2];
  134. memcpy(&val[0],
  135. LLVM_ASSUME_ALIGNED(
  136. memory, (detail::PickAlignment<value_type, alignment>::value)),
  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. // Mask off any existing bits in the upper part of the lower value that
  141. // we want to replace.
  142. val[0] &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
  143. make_unsigned_t<value_type> numBitsFirstVal =
  144. (sizeof(value_type) * 8) - startBit;
  145. make_unsigned_t<value_type> lowerVal = value;
  146. if (startBit > 0) {
  147. // Mask off the upper bits in the new value that are not going to go into
  148. // the lower value. This avoids a left shift of a negative value, which
  149. // is undefined behavior.
  150. lowerVal &= (((make_unsigned_t<value_type>)1 << numBitsFirstVal) - 1);
  151. // Now shift the new bits into place
  152. lowerVal <<= startBit;
  153. }
  154. val[0] |= lowerVal;
  155. // Mask off any existing bits in the lower part of the upper value that
  156. // we want to replace.
  157. val[1] &= ~(((make_unsigned_t<value_type>)1 << startBit) - 1);
  158. // Next shift the bits that go into the upper value into position.
  159. make_unsigned_t<value_type> upperVal = value >> numBitsFirstVal;
  160. // Mask off upper bits after right shift in case of signed type.
  161. upperVal &= ((make_unsigned_t<value_type>)1 << startBit) - 1;
  162. val[1] |= upperVal;
  163. // Finally, rewrite values.
  164. val[0] = byte_swap<value_type, endian>(val[0]);
  165. val[1] = byte_swap<value_type, endian>(val[1]);
  166. memcpy(LLVM_ASSUME_ALIGNED(
  167. memory, (detail::PickAlignment<value_type, alignment>::value)),
  168. &val[0], sizeof(value_type) * 2);
  169. }
  170. }
  171. } // end namespace endian
  172. namespace detail {
  173. template <typename ValueType, endianness Endian, std::size_t Alignment,
  174. std::size_t ALIGN = PickAlignment<ValueType, Alignment>::value>
  175. struct packed_endian_specific_integral {
  176. using value_type = ValueType;
  177. static constexpr endianness endian = Endian;
  178. static constexpr std::size_t alignment = Alignment;
  179. packed_endian_specific_integral() = default;
  180. explicit packed_endian_specific_integral(value_type val) { *this = val; }
  181. operator value_type() const {
  182. return endian::read<value_type, endian, alignment>(
  183. (const void*)Value.buffer);
  184. }
  185. void operator=(value_type newValue) {
  186. endian::write<value_type, endian, alignment>(
  187. (void*)Value.buffer, newValue);
  188. }
  189. packed_endian_specific_integral &operator+=(value_type newValue) {
  190. *this = *this + newValue;
  191. return *this;
  192. }
  193. packed_endian_specific_integral &operator-=(value_type newValue) {
  194. *this = *this - newValue;
  195. return *this;
  196. }
  197. packed_endian_specific_integral &operator|=(value_type newValue) {
  198. *this = *this | newValue;
  199. return *this;
  200. }
  201. packed_endian_specific_integral &operator&=(value_type newValue) {
  202. *this = *this & newValue;
  203. return *this;
  204. }
  205. private:
  206. struct {
  207. alignas(ALIGN) char buffer[sizeof(value_type)];
  208. } Value;
  209. public:
  210. struct ref {
  211. explicit ref(void *Ptr) : Ptr(Ptr) {}
  212. operator value_type() const {
  213. return endian::read<value_type, endian, alignment>(Ptr);
  214. }
  215. void operator=(value_type NewValue) {
  216. endian::write<value_type, endian, alignment>(Ptr, NewValue);
  217. }
  218. private:
  219. void *Ptr;
  220. };
  221. };
  222. } // end namespace detail
  223. using ulittle16_t =
  224. detail::packed_endian_specific_integral<uint16_t, little, unaligned>;
  225. using ulittle32_t =
  226. detail::packed_endian_specific_integral<uint32_t, little, unaligned>;
  227. using ulittle64_t =
  228. detail::packed_endian_specific_integral<uint64_t, little, unaligned>;
  229. using little16_t =
  230. detail::packed_endian_specific_integral<int16_t, little, unaligned>;
  231. using little32_t =
  232. detail::packed_endian_specific_integral<int32_t, little, unaligned>;
  233. using little64_t =
  234. detail::packed_endian_specific_integral<int64_t, little, unaligned>;
  235. using aligned_ulittle16_t =
  236. detail::packed_endian_specific_integral<uint16_t, little, aligned>;
  237. using aligned_ulittle32_t =
  238. detail::packed_endian_specific_integral<uint32_t, little, aligned>;
  239. using aligned_ulittle64_t =
  240. detail::packed_endian_specific_integral<uint64_t, little, aligned>;
  241. using aligned_little16_t =
  242. detail::packed_endian_specific_integral<int16_t, little, aligned>;
  243. using aligned_little32_t =
  244. detail::packed_endian_specific_integral<int32_t, little, aligned>;
  245. using aligned_little64_t =
  246. detail::packed_endian_specific_integral<int64_t, little, aligned>;
  247. using ubig16_t =
  248. detail::packed_endian_specific_integral<uint16_t, big, unaligned>;
  249. using ubig32_t =
  250. detail::packed_endian_specific_integral<uint32_t, big, unaligned>;
  251. using ubig64_t =
  252. detail::packed_endian_specific_integral<uint64_t, big, unaligned>;
  253. using big16_t =
  254. detail::packed_endian_specific_integral<int16_t, big, unaligned>;
  255. using big32_t =
  256. detail::packed_endian_specific_integral<int32_t, big, unaligned>;
  257. using big64_t =
  258. detail::packed_endian_specific_integral<int64_t, big, unaligned>;
  259. using aligned_ubig16_t =
  260. detail::packed_endian_specific_integral<uint16_t, big, aligned>;
  261. using aligned_ubig32_t =
  262. detail::packed_endian_specific_integral<uint32_t, big, aligned>;
  263. using aligned_ubig64_t =
  264. detail::packed_endian_specific_integral<uint64_t, big, aligned>;
  265. using aligned_big16_t =
  266. detail::packed_endian_specific_integral<int16_t, big, aligned>;
  267. using aligned_big32_t =
  268. detail::packed_endian_specific_integral<int32_t, big, aligned>;
  269. using aligned_big64_t =
  270. detail::packed_endian_specific_integral<int64_t, big, aligned>;
  271. using unaligned_uint16_t =
  272. detail::packed_endian_specific_integral<uint16_t, native, unaligned>;
  273. using unaligned_uint32_t =
  274. detail::packed_endian_specific_integral<uint32_t, native, unaligned>;
  275. using unaligned_uint64_t =
  276. detail::packed_endian_specific_integral<uint64_t, native, unaligned>;
  277. using unaligned_int16_t =
  278. detail::packed_endian_specific_integral<int16_t, native, unaligned>;
  279. using unaligned_int32_t =
  280. detail::packed_endian_specific_integral<int32_t, native, unaligned>;
  281. using unaligned_int64_t =
  282. detail::packed_endian_specific_integral<int64_t, native, unaligned>;
  283. template <typename T>
  284. using little_t = detail::packed_endian_specific_integral<T, little, unaligned>;
  285. template <typename T>
  286. using big_t = detail::packed_endian_specific_integral<T, big, unaligned>;
  287. template <typename T>
  288. using aligned_little_t =
  289. detail::packed_endian_specific_integral<T, little, aligned>;
  290. template <typename T>
  291. using aligned_big_t = detail::packed_endian_specific_integral<T, big, aligned>;
  292. namespace endian {
  293. template <typename T> inline T read(const void *P, endianness E) {
  294. return read<T, unaligned>(P, E);
  295. }
  296. template <typename T, endianness E> inline T read(const void *P) {
  297. return *(const detail::packed_endian_specific_integral<T, E, unaligned> *)P;
  298. }
  299. inline uint16_t read16(const void *P, endianness E) {
  300. return read<uint16_t>(P, E);
  301. }
  302. inline uint32_t read32(const void *P, endianness E) {
  303. return read<uint32_t>(P, E);
  304. }
  305. inline uint64_t read64(const void *P, endianness E) {
  306. return read<uint64_t>(P, E);
  307. }
  308. template <endianness E> inline uint16_t read16(const void *P) {
  309. return read<uint16_t, E>(P);
  310. }
  311. template <endianness E> inline uint32_t read32(const void *P) {
  312. return read<uint32_t, E>(P);
  313. }
  314. template <endianness E> inline uint64_t read64(const void *P) {
  315. return read<uint64_t, E>(P);
  316. }
  317. inline uint16_t read16le(const void *P) { return read16<little>(P); }
  318. inline uint32_t read32le(const void *P) { return read32<little>(P); }
  319. inline uint64_t read64le(const void *P) { return read64<little>(P); }
  320. inline uint16_t read16be(const void *P) { return read16<big>(P); }
  321. inline uint32_t read32be(const void *P) { return read32<big>(P); }
  322. inline uint64_t read64be(const void *P) { return read64<big>(P); }
  323. template <typename T> inline void write(void *P, T V, endianness E) {
  324. write<T, unaligned>(P, V, E);
  325. }
  326. template <typename T, endianness E> inline void write(void *P, T V) {
  327. *(detail::packed_endian_specific_integral<T, E, unaligned> *)P = V;
  328. }
  329. inline void write16(void *P, uint16_t V, endianness E) {
  330. write<uint16_t>(P, V, E);
  331. }
  332. inline void write32(void *P, uint32_t V, endianness E) {
  333. write<uint32_t>(P, V, E);
  334. }
  335. inline void write64(void *P, uint64_t V, endianness E) {
  336. write<uint64_t>(P, V, E);
  337. }
  338. template <endianness E> inline void write16(void *P, uint16_t V) {
  339. write<uint16_t, E>(P, V);
  340. }
  341. template <endianness E> inline void write32(void *P, uint32_t V) {
  342. write<uint32_t, E>(P, V);
  343. }
  344. template <endianness E> inline void write64(void *P, uint64_t V) {
  345. write<uint64_t, E>(P, V);
  346. }
  347. inline void write16le(void *P, uint16_t V) { write16<little>(P, V); }
  348. inline void write32le(void *P, uint32_t V) { write32<little>(P, V); }
  349. inline void write64le(void *P, uint64_t V) { write64<little>(P, V); }
  350. inline void write16be(void *P, uint16_t V) { write16<big>(P, V); }
  351. inline void write32be(void *P, uint32_t V) { write32<big>(P, V); }
  352. inline void write64be(void *P, uint64_t V) { write64<big>(P, V); }
  353. } // end namespace endian
  354. } // end namespace support
  355. } // end namespace llvm
  356. #endif // LLVM_SUPPORT_ENDIAN_H