endian.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #ifndef BOOST_UUID_DETAIL_ENDIAN_INCLUDED
  2. #define BOOST_UUID_DETAIL_ENDIAN_INCLUDED
  3. // Copyright 2024 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #include <cstring>
  7. #include <cstdint>
  8. #if defined(_MSC_VER) && !defined(__clang__)
  9. # include <intrin.h>
  10. #endif
  11. namespace boost {
  12. namespace uuids {
  13. namespace detail {
  14. // Byte order macros
  15. #if defined(__BYTE_ORDER__)
  16. #define BOOST_UUID_BYTE_ORDER __BYTE_ORDER__
  17. #define BOOST_UUID_ORDER_LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
  18. #define BOOST_UUID_ORDER_BIG_ENDIAN __ORDER_BIG_ENDIAN__
  19. #elif defined(__LITTLE_ENDIAN__) || defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__)
  20. #define BOOST_UUID_BYTE_ORDER 1234
  21. #define BOOST_UUID_ORDER_LITTLE_ENDIAN 1234
  22. #define BOOST_UUID_ORDER_BIG_ENDIAN 4321
  23. #elif defined(__BIG_ENDIAN__)
  24. #define BOOST_UUID_BYTE_ORDER 4321
  25. #define BOOST_UUID_ORDER_LITTLE_ENDIAN 1234
  26. #define BOOST_UUID_ORDER_BIG_ENDIAN 4321
  27. #else
  28. # error Unrecognized platform
  29. #endif
  30. // byteswap
  31. #if defined(__GNUC__) || defined(__clang__)
  32. inline std::uint16_t byteswap( std::uint16_t x ) noexcept
  33. {
  34. return __builtin_bswap16( x );
  35. }
  36. inline std::uint32_t byteswap( std::uint32_t x ) noexcept
  37. {
  38. return __builtin_bswap32( x );
  39. }
  40. inline std::uint64_t byteswap( std::uint64_t x ) noexcept
  41. {
  42. return __builtin_bswap64( x );
  43. }
  44. #elif defined(_MSC_VER)
  45. inline std::uint16_t byteswap( std::uint16_t x ) noexcept
  46. {
  47. return _byteswap_ushort( x );
  48. }
  49. inline std::uint32_t byteswap( std::uint32_t x ) noexcept
  50. {
  51. return _byteswap_ulong( x );
  52. }
  53. inline std::uint64_t byteswap( std::uint64_t x ) noexcept
  54. {
  55. return _byteswap_uint64( x );
  56. }
  57. #else
  58. inline std::uint16_t byteswap( std::uint16_t x ) noexcept
  59. {
  60. return static_cast<std::uint16_t>( x << 8 | x >> 8 );
  61. }
  62. inline std::uint32_t byteswap( std::uint32_t x ) noexcept
  63. {
  64. std::uint32_t step16 = x << 16 | x >> 16;
  65. return ((step16 << 8) & 0xff00ff00) | ((step16 >> 8) & 0x00ff00ff);
  66. }
  67. inline std::uint64_t byteswap( std::uint64_t x ) noexcept
  68. {
  69. std::uint64_t step32 = x << 32 | x >> 32;
  70. std::uint64_t step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  71. return (step16 & 0x00FF00FF00FF00FFULL) << 8 | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
  72. }
  73. #endif
  74. #if defined(__SIZEOF_INT128__)
  75. inline __uint128_t byteswap( __uint128_t x ) noexcept
  76. {
  77. return ( static_cast<__uint128_t>( detail::byteswap( static_cast<std::uint64_t>( x ) ) ) << 64 ) | detail::byteswap( static_cast<std::uint64_t>( x >> 64 ) );
  78. }
  79. #endif
  80. // load_*_u16
  81. inline std::uint16_t load_native_u16( void const* p ) noexcept
  82. {
  83. std::uint16_t tmp;
  84. std::memcpy( &tmp, p, sizeof( tmp ) );
  85. return tmp;
  86. }
  87. inline std::uint16_t load_little_u16( void const* p ) noexcept
  88. {
  89. std::uint16_t tmp;
  90. std::memcpy( &tmp, p, sizeof( tmp ) );
  91. #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN
  92. return tmp;
  93. #else
  94. return detail::byteswap( tmp );
  95. #endif
  96. }
  97. inline std::uint16_t load_big_u16( void const* p ) noexcept
  98. {
  99. std::uint16_t tmp;
  100. std::memcpy( &tmp, p, sizeof( tmp ) );
  101. #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_BIG_ENDIAN
  102. return tmp;
  103. #else
  104. return detail::byteswap( tmp );
  105. #endif
  106. }
  107. // load_*_u32
  108. inline std::uint32_t load_native_u32( void const* p ) noexcept
  109. {
  110. std::uint32_t tmp;
  111. std::memcpy( &tmp, p, sizeof( tmp ) );
  112. return tmp;
  113. }
  114. inline std::uint32_t load_little_u32( void const* p ) noexcept
  115. {
  116. std::uint32_t tmp;
  117. std::memcpy( &tmp, p, sizeof( tmp ) );
  118. #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN
  119. return tmp;
  120. #else
  121. return detail::byteswap( tmp );
  122. #endif
  123. }
  124. inline std::uint32_t load_big_u32( void const* p ) noexcept
  125. {
  126. std::uint32_t tmp;
  127. std::memcpy( &tmp, p, sizeof( tmp ) );
  128. #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_BIG_ENDIAN
  129. return tmp;
  130. #else
  131. return detail::byteswap( tmp );
  132. #endif
  133. }
  134. // load_*_u64
  135. inline std::uint64_t load_native_u64( void const* p ) noexcept
  136. {
  137. std::uint64_t tmp;
  138. std::memcpy( &tmp, p, sizeof( tmp ) );
  139. return tmp;
  140. }
  141. inline std::uint64_t load_little_u64( void const* p ) noexcept
  142. {
  143. std::uint64_t tmp;
  144. std::memcpy( &tmp, p, sizeof( tmp ) );
  145. #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN
  146. return tmp;
  147. #else
  148. return detail::byteswap( tmp );
  149. #endif
  150. }
  151. inline std::uint64_t load_big_u64( void const* p ) noexcept
  152. {
  153. std::uint64_t tmp;
  154. std::memcpy( &tmp, p, sizeof( tmp ) );
  155. #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_BIG_ENDIAN
  156. return tmp;
  157. #else
  158. return detail::byteswap( tmp );
  159. #endif
  160. }
  161. // load_*_u128
  162. #if defined(__SIZEOF_INT128__)
  163. inline __uint128_t load_native_u128( void const* p ) noexcept
  164. {
  165. __uint128_t tmp;
  166. std::memcpy( &tmp, p, sizeof( tmp ) );
  167. return tmp;
  168. }
  169. inline __uint128_t load_little_u128( void const* p ) noexcept
  170. {
  171. __uint128_t tmp;
  172. std::memcpy( &tmp, p, sizeof( tmp ) );
  173. #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN
  174. return tmp;
  175. #else
  176. return detail::byteswap( tmp );
  177. #endif
  178. }
  179. inline __uint128_t load_big_u128( void const* p ) noexcept
  180. {
  181. __uint128_t tmp;
  182. std::memcpy( &tmp, p, sizeof( tmp ) );
  183. #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_BIG_ENDIAN
  184. return tmp;
  185. #else
  186. return detail::byteswap( tmp );
  187. #endif
  188. }
  189. #endif
  190. // store_*_u16
  191. inline void store_native_u16( void* p, std::uint16_t v ) noexcept
  192. {
  193. std::memcpy( p, &v, sizeof( v ) );
  194. }
  195. inline void store_little_u16( void* p, std::uint16_t v ) noexcept
  196. {
  197. #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_LITTLE_ENDIAN
  198. v = detail::byteswap( v );
  199. #endif
  200. std::memcpy( p, &v, sizeof( v ) );
  201. }
  202. inline void store_big_u16( void* p, std::uint16_t v ) noexcept
  203. {
  204. #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_BIG_ENDIAN
  205. v = detail::byteswap( v );
  206. #endif
  207. std::memcpy( p, &v, sizeof( v ) );
  208. }
  209. // store_*_u32
  210. inline void store_native_u32( void* p, std::uint32_t v ) noexcept
  211. {
  212. std::memcpy( p, &v, sizeof( v ) );
  213. }
  214. inline void store_little_u32( void* p, std::uint32_t v ) noexcept
  215. {
  216. #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_LITTLE_ENDIAN
  217. v = detail::byteswap( v );
  218. #endif
  219. std::memcpy( p, &v, sizeof( v ) );
  220. }
  221. inline void store_big_u32( void* p, std::uint32_t v ) noexcept
  222. {
  223. #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_BIG_ENDIAN
  224. v = detail::byteswap( v );
  225. #endif
  226. std::memcpy( p, &v, sizeof( v ) );
  227. }
  228. // store_*_u64
  229. inline void store_native_u64( void* p, std::uint64_t v ) noexcept
  230. {
  231. std::memcpy( p, &v, sizeof( v ) );
  232. }
  233. inline void store_little_u64( void* p, std::uint64_t v ) noexcept
  234. {
  235. #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_LITTLE_ENDIAN
  236. v = detail::byteswap( v );
  237. #endif
  238. std::memcpy( p, &v, sizeof( v ) );
  239. }
  240. inline void store_big_u64( void* p, std::uint64_t v ) noexcept
  241. {
  242. #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_BIG_ENDIAN
  243. v = detail::byteswap( v );
  244. #endif
  245. std::memcpy( p, &v, sizeof( v ) );
  246. }
  247. // store_*_u128
  248. #if defined(__SIZEOF_INT128__)
  249. inline void store_native_u128( void* p, __uint128_t v ) noexcept
  250. {
  251. std::memcpy( p, &v, sizeof( v ) );
  252. }
  253. inline void store_little_u128( void* p, __uint128_t v ) noexcept
  254. {
  255. #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_LITTLE_ENDIAN
  256. v = detail::byteswap( v );
  257. #endif
  258. std::memcpy( p, &v, sizeof( v ) );
  259. }
  260. inline void store_big_u128( void* p, __uint128_t v ) noexcept
  261. {
  262. #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_BIG_ENDIAN
  263. v = detail::byteswap( v );
  264. #endif
  265. std::memcpy( p, &v, sizeof( v ) );
  266. }
  267. #endif
  268. } // detail
  269. } // uuids
  270. } // boost
  271. #endif // #ifndef BOOST_UUID_DETAIL_ENDIAN_INCLUDED