conversion.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/other/endian.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. # if BOOST_ENDIAN_BIG_BYTE
  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 static_cast<int16_t>(
  180. BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(static_cast<uint16_t>(x)));
  181. # endif
  182. }
  183. inline int32_t endian_reverse(int32_t x) BOOST_NOEXCEPT
  184. {
  185. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  186. uint32_t step16;
  187. step16 = static_cast<uint32_t>(x) << 16 | static_cast<uint32_t>(x) >> 16;
  188. return
  189. ((static_cast<uint32_t>(step16) << 8) & 0xff00ff00)
  190. | ((static_cast<uint32_t>(step16) >> 8) & 0x00ff00ff);
  191. # else
  192. return static_cast<int32_t>(
  193. BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(static_cast<uint32_t>(x)));
  194. # endif
  195. }
  196. inline int64_t endian_reverse(int64_t x) BOOST_NOEXCEPT
  197. {
  198. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  199. uint64_t step32, step16;
  200. step32 = static_cast<uint64_t>(x) << 32 | static_cast<uint64_t>(x) >> 32;
  201. step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16
  202. | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  203. return static_cast<int64_t>((step16 & 0x00FF00FF00FF00FFULL) << 8
  204. | (step16 & 0xFF00FF00FF00FF00ULL) >> 8);
  205. # else
  206. return static_cast<int64_t>(
  207. BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(static_cast<uint64_t>(x)));
  208. # endif
  209. }
  210. inline uint8_t endian_reverse(uint8_t x) BOOST_NOEXCEPT
  211. {
  212. return x;
  213. }
  214. inline uint16_t endian_reverse(uint16_t x) BOOST_NOEXCEPT
  215. {
  216. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  217. return (x << 8)
  218. | (x >> 8);
  219. # else
  220. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_2(x);
  221. # endif
  222. }
  223. inline uint32_t endian_reverse(uint32_t x) BOOST_NOEXCEPT
  224. {
  225. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  226. uint32_t step16;
  227. step16 = x << 16 | x >> 16;
  228. return
  229. ((step16 << 8) & 0xff00ff00)
  230. | ((step16 >> 8) & 0x00ff00ff);
  231. # else
  232. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(x);
  233. # endif
  234. }
  235. inline uint64_t endian_reverse(uint64_t x) BOOST_NOEXCEPT
  236. {
  237. # ifdef BOOST_ENDIAN_NO_INTRINSICS
  238. uint64_t step32, step16;
  239. step32 = x << 32 | x >> 32;
  240. step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16
  241. | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
  242. return (step16 & 0x00FF00FF00FF00FFULL) << 8
  243. | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
  244. # else
  245. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_8(x);
  246. # endif
  247. }
  248. template <class EndianReversible >
  249. inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT
  250. {
  251. # if BOOST_ENDIAN_BIG_BYTE
  252. return x;
  253. # else
  254. return endian_reverse(x);
  255. # endif
  256. }
  257. template <class EndianReversible >
  258. inline EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT
  259. {
  260. # if BOOST_ENDIAN_BIG_BYTE
  261. return x;
  262. # else
  263. return endian_reverse(x);
  264. # endif
  265. }
  266. template <class EndianReversible >
  267. inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT
  268. {
  269. # if BOOST_ENDIAN_LITTLE_BYTE
  270. return x;
  271. # else
  272. return endian_reverse(x);
  273. # endif
  274. }
  275. template <class EndianReversible >
  276. inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT
  277. {
  278. # if BOOST_ENDIAN_LITTLE_BYTE
  279. return x;
  280. # else
  281. return endian_reverse(x);
  282. # endif
  283. }
  284. namespace detail
  285. {
  286. // Primary template and specializations to support endian_reverse().
  287. // See rationale in endian_reverse() below.
  288. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  289. class EndianReversible>
  290. class value_converter ; // primary template
  291. template <class T> class value_converter <order::big, order::big, T>
  292. {public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
  293. template <class T> class value_converter <order::little, order::little, T>
  294. {public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
  295. template <class T> class value_converter <order::big, order::little, T>
  296. {public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}};
  297. template <class T> class value_converter <order::little, order::big, T>
  298. {public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}};
  299. }
  300. // generic conditional reverse
  301. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  302. class EndianReversible>
  303. inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT {
  304. // work around lack of function template partial specialization by instantiating
  305. // a function object of a class that is partially specialized on the two order
  306. // template parameters, and then calling its operator().
  307. detail::value_converter <From, To, EndianReversible> tmp;
  308. return tmp(from);
  309. }
  310. // runtime conditional reverse
  311. template <class EndianReversible >
  312. inline EndianReversible conditional_reverse(EndianReversible from,
  313. BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT
  314. {
  315. return from_order == to_order ? from : endian_reverse(from);
  316. }
  317. //--------------------------------------------------------------------------------------//
  318. // reverse-in-place implementation //
  319. //--------------------------------------------------------------------------------------//
  320. // reverse in place
  321. template <class EndianReversible>
  322. inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT
  323. {
  324. x = endian_reverse(x);
  325. }
  326. template <class EndianReversibleInplace>
  327. # if BOOST_ENDIAN_BIG_BYTE
  328. inline void big_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
  329. # else
  330. inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  331. { endian_reverse_inplace(x); }
  332. # endif
  333. template <class EndianReversibleInplace>
  334. # if BOOST_ENDIAN_BIG_BYTE
  335. inline void native_to_big_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
  336. # else
  337. inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  338. {
  339. endian_reverse_inplace(x);
  340. }
  341. # endif
  342. template <class EndianReversibleInplace>
  343. # if BOOST_ENDIAN_LITTLE_BYTE
  344. inline void little_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
  345. # else
  346. inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  347. { endian_reverse_inplace(x); }
  348. # endif
  349. template <class EndianReversibleInplace>
  350. # if BOOST_ENDIAN_LITTLE_BYTE
  351. inline void native_to_little_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
  352. # else
  353. inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  354. {
  355. endian_reverse_inplace(x);
  356. }
  357. # endif
  358. namespace detail
  359. {
  360. // Primary template and specializations support generic
  361. // endian_reverse_inplace().
  362. // See rationale in endian_reverse_inplace() below.
  363. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  364. class EndianReversibleInplace>
  365. class converter; // primary template
  366. template <class T> class converter<order::big, order::big, T>
  367. {public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}};
  368. template <class T> class converter<order::little, order::little, T>
  369. {public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}};
  370. template <class T> class converter<order::big, order::little, T>
  371. {public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }};
  372. template <class T> class converter<order::little, order::big, T>
  373. {public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }};
  374. } // namespace detail
  375. // generic conditional reverse in place
  376. template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
  377. class EndianReversibleInplace>
  378. inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
  379. {
  380. // work around lack of function template partial specialization by instantiating
  381. // a function object of a class that is partially specialized on the two order
  382. // template parameters, and then calling its operator().
  383. detail::converter<From, To, EndianReversibleInplace> tmp;
  384. tmp(x); // call operator ()
  385. }
  386. // runtime reverse in place
  387. template <class EndianReversibleInplace>
  388. inline void conditional_reverse_inplace(EndianReversibleInplace& x,
  389. BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order)
  390. BOOST_NOEXCEPT
  391. {
  392. if (from_order != to_order)
  393. endian_reverse_inplace(x);
  394. }
  395. namespace detail
  396. {
  397. template <class T>
  398. inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT
  399. {
  400. # if BOOST_ENDIAN_BIG_BYTE
  401. std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T));
  402. # else
  403. std::reverse_copy(reinterpret_cast<const char*>(&from),
  404. reinterpret_cast<const char*>(&from) + sizeof(T), to);
  405. # endif
  406. }
  407. template <class T>
  408. inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
  409. {
  410. # if BOOST_ENDIAN_BIG_BYTE
  411. std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
  412. # else
  413. std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
  414. # endif
  415. }
  416. template <class T>
  417. inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT
  418. {
  419. # if BOOST_ENDIAN_LITTLE_BYTE
  420. std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T));
  421. # else
  422. std::reverse_copy(reinterpret_cast<const char*>(&from),
  423. reinterpret_cast<const char*>(&from) + sizeof(T), to);
  424. # endif
  425. }
  426. template <class T>
  427. inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
  428. {
  429. # if BOOST_ENDIAN_LITTLE_BYTE
  430. std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
  431. # else
  432. std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
  433. # endif
  434. }
  435. } // namespace detail
  436. } // namespace endian
  437. } // namespace boost
  438. #endif // BOOST_ENDIAN_CONVERSION_HPP