conversion.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // boost/endian/conversion.hpp -------------------------------------------------------//
  2. // Copyright Beman Dawes 2010, 2011, 2014
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // http://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_ENDIAN_CONVERSION_HPP
  6. #define BOOST_ENDIAN_CONVERSION_HPP
  7. #include <boost/config.hpp>
  8. #include <boost/predef/detail/endian_compat.h>
  9. #include <boost/cstdint.hpp>
  10. #include <boost/endian/detail/intrinsic.hpp>
  11. #include <boost/core/scoped_enum.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <algorithm>
  14. #include <cstring> // for memcpy
  15. //------------------------------------- synopsis ---------------------------------------//
  16. namespace boost
  17. {
  18. namespace endian
  19. {
  20. BOOST_SCOPED_ENUM_START(order)
  21. {
  22. big, little,
  23. # ifdef BOOST_BIG_ENDIAN
  24. native = big
  25. # else
  26. native = little
  27. # endif
  28. }; BOOST_SCOPED_ENUM_END
  29. //--------------------------------------------------------------------------------------//
  30. // //
  31. // return-by-value interfaces //
  32. // suggested by Phil Endecott //
  33. // //
  34. // user-defined types (UDTs) //
  35. // //
  36. // All return-by-value conversion function templates are required to be implemented in //
  37. // terms of an unqualified call to "endian_reverse(x)", a function returning the //
  38. // value of x with endianness reversed. This provides a customization point for any //
  39. // UDT that provides a "endian_reverse" free-function meeting the requirements. //
  40. // It must be defined in the same namespace as the UDT itself so that it will be found //
  41. // by argument dependent lookup (ADL). //
  42. // //
  43. //--------------------------------------------------------------------------------------//
  44. // customization for exact-length arithmetic types. See doc/conversion.html/#FAQ.
  45. // Note: The omission of a overloads for the arithmetic type (typically long, or
  46. // long long) not assigned to one of the exact length typedefs is a deliberate
  47. // design decision. Such overloads would be non-portable and thus error prone.
  48. inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT;
  49. inline int16_t endian_reverse(int16_t x) BOOST_NOEXCEPT;
  50. inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT;
  51. inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT;
  52. inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT;
  53. inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT;
  54. inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT;
  55. inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT;
  56. // reverse byte order unless native endianness is big
  57. template <class EndianReversible >
  58. inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT;
  59. // Returns: x if native endian order is big, otherwise endian_reverse(x)
  60. template <class EndianReversible >
  61. inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT;
  62. // Returns: x if native endian order is big, otherwise endian_reverse(x)
  63. // reverse byte order unless native endianness is little
  64. template <class EndianReversible >
  65. inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT;
  66. // Returns: x if native endian order is little, otherwise endian_reverse(x)
  67. template <class EndianReversible >
  68. inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT;
  69. // Returns: x if native endian order is little, otherwise endian_reverse(x)
  70. // generic conditional reverse byte order
  71. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  72. class EndianReversible>
  73. inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT;
  74. // Returns: If From == To have different values, from.
  75. // Otherwise endian_reverse(from).
  76. // Remarks: The From == To test, and as a consequence which form the return takes, is
  77. // is determined at compile time.
  78. // runtime conditional reverse byte order
  79. template <class EndianReversible >
  80. inline EndianReversible conditional_reverse(EndianReversible from,
  81. BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order)
  82. BOOST_NOEXCEPT;
  83. // Returns: from_order == to_order ? from : endian_reverse(from).
  84. //------------------------------------------------------------------------------------//
  85. // Q: What happened to bswap, htobe, and the other synonym functions based on names
  86. // popularized by BSD, OS X, and Linux?
  87. // A: Turned out these may be implemented as macros on some systems. Ditto POSIX names
  88. // for such functionality. Since macros would cause endless problems with functions
  89. // of the same names, and these functions are just synonyms anyhow, they have been
  90. // removed.
  91. //------------------------------------------------------------------------------------//
  92. // //
  93. // reverse in place interfaces //
  94. // //
  95. // user-defined types (UDTs) //
  96. // //
  97. // All reverse in place function templates are required to be implemented in terms //
  98. // of an unqualified call to "endian_reverse_inplace(x)", a function reversing //
  99. // the endianness of x, which is a non-const reference. This provides a //
  100. // customization point for any UDT that provides a "reverse_inplace" free-function //
  101. // meeting the requirements. The free-function must be declared in the same //
  102. // namespace as the UDT itself so that it will be found by argument-dependent //
  103. // lookup (ADL). //
  104. // //
  105. //------------------------------------------------------------------------------------//
  106. // reverse in place
  107. template <class EndianReversible>
  108. inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT;
  109. // Effects: x = endian_reverse(x)
  110. // reverse in place unless native endianness is big
  111. template <class EndianReversibleInplace>
  112. inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  113. // Effects: none if native byte-order is big, otherwise endian_reverse_inplace(x)
  114. template <class EndianReversibleInplace>
  115. inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  116. // Effects: none if native byte-order is big, otherwise endian_reverse_inplace(x)
  117. // reverse in place unless native endianness is little
  118. template <class EndianReversibleInplace>
  119. inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  120. // Effects: none if native byte-order is little, otherwise endian_reverse_inplace(x);
  121. template <class EndianReversibleInplace>
  122. inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  123. // Effects: none if native byte-order is little, otherwise endian_reverse_inplace(x);
  124. // generic conditional reverse in place
  125. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  126. class EndianReversibleInplace>
  127. inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  128. // runtime reverse in place
  129. template <class EndianReversibleInplace>
  130. inline void conditional_reverse_inplace(EndianReversibleInplace& x,
  131. BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order)
  132. BOOST_NOEXCEPT;
  133. //----------------------------------- end synopsis -------------------------------------//
  134. namespace detail
  135. {
  136. // generic reverse function template implementation approach using std::reverse
  137. // suggested by Mathias Gaunard. Primary motivation for inclusion is to have an
  138. // independent implementation to test against.
  139. template <class T>
  140. inline T std_endian_reverse(T x) BOOST_NOEXCEPT
  141. {
  142. T tmp(x);
  143. std::reverse(
  144. reinterpret_cast<unsigned char*>(&tmp),
  145. reinterpret_cast<unsigned char*>(&tmp) + sizeof(T));
  146. return tmp;
  147. }
  148. // conditional unaligned reverse copy, patterned after std::reverse_copy
  149. template <class T>
  150. inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT;
  151. template <class T>
  152. inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT;
  153. template <class T>
  154. inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT;
  155. template <class T>
  156. inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT;
  157. } // namespace detail
  158. //--------------------------------------------------------------------------------------//
  159. // //
  160. // return-by-value implementation //
  161. // //
  162. // -- portable approach suggested by tymofey, with avoidance of undefined behavior //
  163. // as suggested by Giovanni Piero Deretta, with a further refinement suggested //
  164. // by Pyry Jahkola. //
  165. // -- intrinsic approach suggested by reviewers, and by David Stone, who provided //
  166. // his Boost licensed macro implementation (detail/intrinsic.hpp) //
  167. // //
  168. //--------------------------------------------------------------------------------------//
  169. inline int8_t endian_reverse(int8_t x) BOOST_NOEXCEPT
  170. {
  171. return x;
  172. }
  173. inline int16_t endian_reverse(int16_t x) BOOST_NOEXCEPT
  174. {
  175. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  176. return (static_cast<uint16_t>(x) << 8)
  177. | (static_cast<uint16_t>(x) >> 8);
  178. # else
  179. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(static_cast<uint16_t>(x));
  180. # endif
  181. }
  182. inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT
  183. {
  184. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  185. uint32_t step16;
  186. step16 = static_cast<uint32_t>(x) << 16 | static_cast<uint32_t>(x) >> 16;
  187. return
  188. ((static_cast<uint32_t>(step16) << 8) & 0xff00ff00)
  189. | ((static_cast<uint32_t>(step16) >> 8) & 0x00ff00ff);
  190. # else
  191. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(static_cast<uint32_t>(x));
  192. # endif
  193. }
  194. inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT
  195. {
  196. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  197. uint64_t step32, step16;
  198. step32 = static_cast<uint64_t>(x) << 32 | static_cast<uint64_t>(x) >> 32;
  199. step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16
  200. | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  201. return static_cast<int64_t>((step16 & 0x00FF00FF00FF00FFULL) << 8
  202. | (step16 & 0xFF00FF00FF00FF00ULL) >> 8);
  203. # else
  204. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(static_cast<uint64_t>(x));
  205. # endif
  206. }
  207. inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT
  208. {
  209. return x;
  210. }
  211. inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT
  212. {
  213. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  214. return (x << 8)
  215. | (x >> 8);
  216. # else
  217. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x);
  218. # endif
  219. }
  220. inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT
  221. {
  222. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  223. uint32_t step16;
  224. step16 = x << 16 | x >> 16;
  225. return
  226. ((step16 << 8) & 0xff00ff00)
  227. | ((step16 >> 8) & 0x00ff00ff);
  228. # else
  229. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x);
  230. # endif
  231. }
  232. inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT
  233. {
  234. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  235. uint64_t step32, step16;
  236. step32 = x << 32 | x >> 32;
  237. step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16
  238. | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  239. return (step16 & 0x00FF00FF00FF00FFULL) << 8
  240. | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
  241. # else
  242. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x);
  243. # endif
  244. }
  245. template <class EndianReversible >
  246. inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT
  247. {
  248. # ifdef BOOST_BIG_ENDIAN
  249. return x;
  250. # else
  251. return endian_reverse(x);
  252. # endif
  253. }
  254. template <class EndianReversible >
  255. inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT
  256. {
  257. # ifdef BOOST_BIG_ENDIAN
  258. return x;
  259. # else
  260. return endian_reverse(x);
  261. # endif
  262. }
  263. template <class EndianReversible >
  264. inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT
  265. {
  266. # ifdef BOOST_LITTLE_ENDIAN
  267. return x;
  268. # else
  269. return endian_reverse(x);
  270. # endif
  271. }
  272. template <class EndianReversible >
  273. inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT
  274. {
  275. # ifdef BOOST_LITTLE_ENDIAN
  276. return x;
  277. # else
  278. return endian_reverse(x);
  279. # endif
  280. }
  281. namespace detail
  282. {
  283. // Primary template and specializations to support endian_reverse().
  284. // See rationale in endian_reverse() below.
  285. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  286. class EndianReversible>
  287. class value_converter ; // primary template
  288. template <class T> class value_converter <order::big, order::big, T>
  289. {public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
  290. template <class T> class value_converter <order::little, order::little, T>
  291. {public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
  292. template <class T> class value_converter <order::big, order::little, T>
  293. {public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}};
  294. template <class T> class value_converter <order::little, order::big, T>
  295. {public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}};
  296. }
  297. // generic conditional reverse
  298. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  299. class EndianReversible>
  300. inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT {
  301. // work around lack of function template partial specialization by instantiating
  302. // a function object of a class that is partially specialized on the two order
  303. // template parameters, and then calling its operator().
  304. detail::value_converter <From, To, EndianReversible> tmp;
  305. return tmp(from);
  306. }
  307. // runtime conditional reverse
  308. template <class EndianReversible >
  309. inline EndianReversible conditional_reverse(EndianReversible from,
  310. BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT
  311. {
  312. return from_order == to_order ? from : endian_reverse(from);
  313. }
  314. //--------------------------------------------------------------------------------------//
  315. // reverse-in-place implementation //
  316. //--------------------------------------------------------------------------------------//
  317. // reverse in place
  318. template <class EndianReversible>
  319. inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT
  320. {
  321. x = endian_reverse(x);
  322. }
  323. template <class EndianReversibleInplace>
  324. # ifdef BOOST_BIG_ENDIAN
  325. inline void big_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
  326. # else
  327. inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  328. { endian_reverse_inplace(x); }
  329. # endif
  330. template <class EndianReversibleInplace>
  331. # ifdef BOOST_BIG_ENDIAN
  332. inline void native_to_big_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
  333. # else
  334. inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  335. {
  336. endian_reverse_inplace(x);
  337. }
  338. # endif
  339. template <class EndianReversibleInplace>
  340. # ifdef BOOST_LITTLE_ENDIAN
  341. inline void little_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
  342. # else
  343. inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  344. { endian_reverse_inplace(x); }
  345. # endif
  346. template <class EndianReversibleInplace>
  347. # ifdef BOOST_LITTLE_ENDIAN
  348. inline void native_to_little_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
  349. # else
  350. inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  351. {
  352. endian_reverse_inplace(x);
  353. }
  354. # endif
  355. namespace detail
  356. {
  357. // Primary template and specializations support generic
  358. // endian_reverse_inplace().
  359. // See rationale in endian_reverse_inplace() below.
  360. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  361. class EndianReversibleInplace>
  362. class converter; // primary template
  363. template <class T> class converter<order::big, order::big, T>
  364. {public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}};
  365. template <class T> class converter<order::little, order::little, T>
  366. {public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}};
  367. template <class T> class converter<order::big, order::little, T>
  368. {public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }};
  369. template <class T> class converter<order::little, order::big, T>
  370. {public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }};
  371. } // namespace detail
  372. // generic conditional reverse in place
  373. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  374. class EndianReversibleInplace>
  375. inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  376. {
  377. // work around lack of function template partial specialization by instantiating
  378. // a function object of a class that is partially specialized on the two order
  379. // template parameters, and then calling its operator().
  380. detail::converter<From, To, EndianReversibleInplace> tmp;
  381. tmp(x); // call operator ()
  382. }
  383. // runtime reverse in place
  384. template <class EndianReversibleInplace>
  385. inline void conditional_reverse_inplace(EndianReversibleInplace& x,
  386. BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order)
  387. BOOST_NOEXCEPT
  388. {
  389. if (from_order != to_order)
  390. endian_reverse_inplace(x);
  391. }
  392. namespace detail
  393. {
  394. template <class T>
  395. inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT
  396. {
  397. # ifdef BOOST_BIG_ENDIAN
  398. std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T));
  399. # else
  400. std::reverse_copy(reinterpret_cast<const char*>(&from),
  401. reinterpret_cast<const char*>(&from) + sizeof(T), to);
  402. # endif
  403. }
  404. template <class T>
  405. inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
  406. {
  407. # ifdef BOOST_BIG_ENDIAN
  408. std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
  409. # else
  410. std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
  411. # endif
  412. }
  413. template <class T>
  414. inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT
  415. {
  416. # ifdef BOOST_LITTLE_ENDIAN
  417. std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T));
  418. # else
  419. std::reverse_copy(reinterpret_cast<const char*>(&from),
  420. reinterpret_cast<const char*>(&from) + sizeof(T), to);
  421. # endif
  422. }
  423. template <class T>
  424. inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
  425. {
  426. # ifdef BOOST_LITTLE_ENDIAN
  427. std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
  428. # else
  429. std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
  430. # endif
  431. }
  432. } // namespace detail
  433. } // namespace endian
  434. } // namespace boost
  435. #endif // BOOST_ENDIAN_CONVERSION_HPP