conversion.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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/endian/detail/endian_reverse.hpp>
  8. #include <boost/endian/detail/endian_load.hpp>
  9. #include <boost/endian/detail/endian_store.hpp>
  10. #include <boost/endian/detail/order.hpp>
  11. #include <boost/endian/detail/static_assert.hpp>
  12. #include <boost/config.hpp>
  13. #include <type_traits>
  14. #include <cstdint>
  15. //------------------------------------- synopsis ---------------------------------------//
  16. namespace boost
  17. {
  18. namespace endian
  19. {
  20. //--------------------------------------------------------------------------------------//
  21. // //
  22. // return-by-value interfaces //
  23. // suggested by Phil Endecott //
  24. // //
  25. // user-defined types (UDTs) //
  26. // //
  27. // All return-by-value conversion function templates are required to be implemented in //
  28. // terms of an unqualified call to "endian_reverse(x)", a function returning the //
  29. // value of x with endianness reversed. This provides a customization point for any //
  30. // UDT that provides a "endian_reverse" free-function meeting the requirements. //
  31. // It must be defined in the same namespace as the UDT itself so that it will be found //
  32. // by argument dependent lookup (ADL). //
  33. // //
  34. //--------------------------------------------------------------------------------------//
  35. // reverse byte order
  36. // requires T to be a non-bool integral type
  37. // in detail/endian_reverse.hpp
  38. //
  39. // template<class T> inline BOOST_CONSTEXPR T endian_reverse( T x ) BOOST_NOEXCEPT;
  40. // reverse byte order unless native endianness is big
  41. template <class EndianReversible >
  42. inline BOOST_CONSTEXPR EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT;
  43. // Returns: x if native endian order is big, otherwise endian_reverse(x)
  44. template <class EndianReversible >
  45. inline BOOST_CONSTEXPR EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT;
  46. // Returns: x if native endian order is big, otherwise endian_reverse(x)
  47. // reverse byte order unless native endianness is little
  48. template <class EndianReversible >
  49. inline BOOST_CONSTEXPR EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT;
  50. // Returns: x if native endian order is little, otherwise endian_reverse(x)
  51. template <class EndianReversible >
  52. inline BOOST_CONSTEXPR EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT;
  53. // Returns: x if native endian order is little, otherwise endian_reverse(x)
  54. // generic conditional reverse byte order
  55. template <order From, order To,
  56. class EndianReversible>
  57. inline BOOST_CONSTEXPR EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT;
  58. // Returns: If From == To have different values, from.
  59. // Otherwise endian_reverse(from).
  60. // Remarks: The From == To test, and as a consequence which form the return takes, is
  61. // is determined at compile time.
  62. // runtime conditional reverse byte order
  63. template <class EndianReversible >
  64. inline BOOST_CONSTEXPR EndianReversible conditional_reverse(EndianReversible from,
  65. order from_order, order to_order)
  66. BOOST_NOEXCEPT;
  67. // Returns: from_order == to_order ? from : endian_reverse(from).
  68. //------------------------------------------------------------------------------------//
  69. // Q: What happened to bswap, htobe, and the other synonym functions based on names
  70. // popularized by BSD, OS X, and Linux?
  71. // A: Turned out these may be implemented as macros on some systems. Ditto POSIX names
  72. // for such functionality. Since macros would cause endless problems with functions
  73. // of the same names, and these functions are just synonyms anyhow, they have been
  74. // removed.
  75. //------------------------------------------------------------------------------------//
  76. // //
  77. // reverse in place interfaces //
  78. // //
  79. // user-defined types (UDTs) //
  80. // //
  81. // All reverse in place function templates are required to be implemented in terms //
  82. // of an unqualified call to "endian_reverse_inplace(x)", a function reversing //
  83. // the endianness of x, which is a non-const reference. This provides a //
  84. // customization point for any UDT that provides a "reverse_inplace" free-function //
  85. // meeting the requirements. The free-function must be declared in the same //
  86. // namespace as the UDT itself so that it will be found by argument-dependent //
  87. // lookup (ADL). //
  88. // //
  89. //------------------------------------------------------------------------------------//
  90. // reverse in place
  91. // in detail/endian_reverse.hpp
  92. //
  93. // template <class EndianReversible>
  94. // inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT;
  95. //
  96. // Effects: x = endian_reverse(x)
  97. // reverse in place unless native endianness is big
  98. template <class EndianReversibleInplace>
  99. inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  100. // Effects: none if native byte-order is big, otherwise endian_reverse_inplace(x)
  101. template <class EndianReversibleInplace>
  102. inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  103. // Effects: none if native byte-order is big, otherwise endian_reverse_inplace(x)
  104. // reverse in place unless native endianness is little
  105. template <class EndianReversibleInplace>
  106. inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  107. // Effects: none if native byte-order is little, otherwise endian_reverse_inplace(x);
  108. template <class EndianReversibleInplace>
  109. inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  110. // Effects: none if native byte-order is little, otherwise endian_reverse_inplace(x);
  111. // generic conditional reverse in place
  112. template <order From, order To,
  113. class EndianReversibleInplace>
  114. inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
  115. // runtime reverse in place
  116. template <class EndianReversibleInplace>
  117. inline void conditional_reverse_inplace(EndianReversibleInplace& x,
  118. order from_order, order to_order)
  119. BOOST_NOEXCEPT;
  120. //----------------------------------- end synopsis -------------------------------------//
  121. template <class EndianReversible>
  122. inline BOOST_CONSTEXPR EndianReversible big_to_native( EndianReversible x ) BOOST_NOEXCEPT
  123. {
  124. return boost::endian::conditional_reverse<order::big, order::native>( x );
  125. }
  126. template <class EndianReversible>
  127. inline BOOST_CONSTEXPR EndianReversible native_to_big( EndianReversible x ) BOOST_NOEXCEPT
  128. {
  129. return boost::endian::conditional_reverse<order::native, order::big>( x );
  130. }
  131. template <class EndianReversible>
  132. inline BOOST_CONSTEXPR EndianReversible little_to_native( EndianReversible x ) BOOST_NOEXCEPT
  133. {
  134. return boost::endian::conditional_reverse<order::little, order::native>( x );
  135. }
  136. template <class EndianReversible>
  137. inline BOOST_CONSTEXPR EndianReversible native_to_little( EndianReversible x ) BOOST_NOEXCEPT
  138. {
  139. return boost::endian::conditional_reverse<order::native, order::little>( x );
  140. }
  141. namespace detail
  142. {
  143. template<class EndianReversible>
  144. inline BOOST_CONSTEXPR EndianReversible conditional_reverse_impl( EndianReversible x, std::true_type ) BOOST_NOEXCEPT
  145. {
  146. return x;
  147. }
  148. template<class EndianReversible>
  149. inline BOOST_CONSTEXPR EndianReversible conditional_reverse_impl( EndianReversible x, std::false_type ) BOOST_NOEXCEPT
  150. {
  151. return endian_reverse( x );
  152. }
  153. } // namespace detail
  154. // generic conditional reverse
  155. template <order From, order To, class EndianReversible>
  156. inline BOOST_CONSTEXPR EndianReversible conditional_reverse( EndianReversible x ) BOOST_NOEXCEPT
  157. {
  158. BOOST_ENDIAN_STATIC_ASSERT( std::is_class<EndianReversible>::value || detail::is_endian_reversible<EndianReversible>::value );
  159. return detail::conditional_reverse_impl( x, std::integral_constant<bool, From == To>() );
  160. }
  161. // runtime conditional reverse
  162. template <class EndianReversible>
  163. inline BOOST_CONSTEXPR EndianReversible conditional_reverse( EndianReversible x,
  164. order from_order, order to_order ) BOOST_NOEXCEPT
  165. {
  166. BOOST_ENDIAN_STATIC_ASSERT( std::is_class<EndianReversible>::value || detail::is_endian_reversible<EndianReversible>::value );
  167. return from_order == to_order? x: endian_reverse( x );
  168. }
  169. //--------------------------------------------------------------------------------------//
  170. // reverse-in-place implementation //
  171. //--------------------------------------------------------------------------------------//
  172. template <class EndianReversibleInplace>
  173. inline void big_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
  174. {
  175. boost::endian::conditional_reverse_inplace<order::big, order::native>( x );
  176. }
  177. template <class EndianReversibleInplace>
  178. inline void native_to_big_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
  179. {
  180. boost::endian::conditional_reverse_inplace<order::native, order::big>( x );
  181. }
  182. template <class EndianReversibleInplace>
  183. inline void little_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
  184. {
  185. boost::endian::conditional_reverse_inplace<order::little, order::native>( x );
  186. }
  187. template <class EndianReversibleInplace>
  188. inline void native_to_little_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
  189. {
  190. boost::endian::conditional_reverse_inplace<order::native, order::little>( x );
  191. }
  192. namespace detail
  193. {
  194. template<class EndianReversibleInplace>
  195. inline void conditional_reverse_inplace_impl( EndianReversibleInplace&, std::true_type ) BOOST_NOEXCEPT
  196. {
  197. }
  198. template<class EndianReversibleInplace>
  199. inline void conditional_reverse_inplace_impl( EndianReversibleInplace& x, std::false_type ) BOOST_NOEXCEPT
  200. {
  201. endian_reverse_inplace( x );
  202. }
  203. } // namespace detail
  204. // generic conditional reverse in place
  205. template <order From, order To, class EndianReversibleInplace>
  206. inline void conditional_reverse_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
  207. {
  208. BOOST_ENDIAN_STATIC_ASSERT(
  209. std::is_class<EndianReversibleInplace>::value ||
  210. std::is_array<EndianReversibleInplace>::value ||
  211. detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
  212. detail::conditional_reverse_inplace_impl( x, std::integral_constant<bool, From == To>() );
  213. }
  214. // runtime reverse in place
  215. template <class EndianReversibleInplace>
  216. inline void conditional_reverse_inplace( EndianReversibleInplace& x,
  217. order from_order, order to_order ) BOOST_NOEXCEPT
  218. {
  219. BOOST_ENDIAN_STATIC_ASSERT(
  220. std::is_class<EndianReversibleInplace>::value ||
  221. std::is_array<EndianReversibleInplace>::value ||
  222. detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
  223. if( from_order != to_order )
  224. {
  225. endian_reverse_inplace( x );
  226. }
  227. }
  228. // load/store convenience functions
  229. // load 16
  230. inline std::int16_t load_little_s16( unsigned char const * p ) BOOST_NOEXCEPT
  231. {
  232. return boost::endian::endian_load<std::int16_t, 2, order::little>( p );
  233. }
  234. inline std::uint16_t load_little_u16( unsigned char const * p ) BOOST_NOEXCEPT
  235. {
  236. return boost::endian::endian_load<std::uint16_t, 2, order::little>( p );
  237. }
  238. inline std::int16_t load_big_s16( unsigned char const * p ) BOOST_NOEXCEPT
  239. {
  240. return boost::endian::endian_load<std::int16_t, 2, order::big>( p );
  241. }
  242. inline std::uint16_t load_big_u16( unsigned char const * p ) BOOST_NOEXCEPT
  243. {
  244. return boost::endian::endian_load<std::uint16_t, 2, order::big>( p );
  245. }
  246. // load 24
  247. inline std::int32_t load_little_s24( unsigned char const * p ) BOOST_NOEXCEPT
  248. {
  249. return boost::endian::endian_load<std::int32_t, 3, order::little>( p );
  250. }
  251. inline std::uint32_t load_little_u24( unsigned char const * p ) BOOST_NOEXCEPT
  252. {
  253. return boost::endian::endian_load<std::uint32_t, 3, order::little>( p );
  254. }
  255. inline std::int32_t load_big_s24( unsigned char const * p ) BOOST_NOEXCEPT
  256. {
  257. return boost::endian::endian_load<std::int32_t, 3, order::big>( p );
  258. }
  259. inline std::uint32_t load_big_u24( unsigned char const * p ) BOOST_NOEXCEPT
  260. {
  261. return boost::endian::endian_load<std::uint32_t, 3, order::big>( p );
  262. }
  263. // load 32
  264. inline std::int32_t load_little_s32( unsigned char const * p ) BOOST_NOEXCEPT
  265. {
  266. return boost::endian::endian_load<std::int32_t, 4, order::little>( p );
  267. }
  268. inline std::uint32_t load_little_u32( unsigned char const * p ) BOOST_NOEXCEPT
  269. {
  270. return boost::endian::endian_load<std::uint32_t, 4, order::little>( p );
  271. }
  272. inline std::int32_t load_big_s32( unsigned char const * p ) BOOST_NOEXCEPT
  273. {
  274. return boost::endian::endian_load<std::int32_t, 4, order::big>( p );
  275. }
  276. inline std::uint32_t load_big_u32( unsigned char const * p ) BOOST_NOEXCEPT
  277. {
  278. return boost::endian::endian_load<std::uint32_t, 4, order::big>( p );
  279. }
  280. // load 40
  281. inline std::int64_t load_little_s40( unsigned char const * p ) BOOST_NOEXCEPT
  282. {
  283. return boost::endian::endian_load<std::int64_t, 5, order::little>( p );
  284. }
  285. inline std::uint64_t load_little_u40( unsigned char const * p ) BOOST_NOEXCEPT
  286. {
  287. return boost::endian::endian_load<std::uint64_t, 5, order::little>( p );
  288. }
  289. inline std::int64_t load_big_s40( unsigned char const * p ) BOOST_NOEXCEPT
  290. {
  291. return boost::endian::endian_load<std::int64_t, 5, order::big>( p );
  292. }
  293. inline std::uint64_t load_big_u40( unsigned char const * p ) BOOST_NOEXCEPT
  294. {
  295. return boost::endian::endian_load<std::uint64_t, 5, order::big>( p );
  296. }
  297. // load 48
  298. inline std::int64_t load_little_s48( unsigned char const * p ) BOOST_NOEXCEPT
  299. {
  300. return boost::endian::endian_load<std::int64_t, 6, order::little>( p );
  301. }
  302. inline std::uint64_t load_little_u48( unsigned char const * p ) BOOST_NOEXCEPT
  303. {
  304. return boost::endian::endian_load<std::uint64_t, 6, order::little>( p );
  305. }
  306. inline std::int64_t load_big_s48( unsigned char const * p ) BOOST_NOEXCEPT
  307. {
  308. return boost::endian::endian_load<std::int64_t, 6, order::big>( p );
  309. }
  310. inline std::uint64_t load_big_u48( unsigned char const * p ) BOOST_NOEXCEPT
  311. {
  312. return boost::endian::endian_load<std::uint64_t, 6, order::big>( p );
  313. }
  314. // load 56
  315. inline std::int64_t load_little_s56( unsigned char const * p ) BOOST_NOEXCEPT
  316. {
  317. return boost::endian::endian_load<std::int64_t, 7, order::little>( p );
  318. }
  319. inline std::uint64_t load_little_u56( unsigned char const * p ) BOOST_NOEXCEPT
  320. {
  321. return boost::endian::endian_load<std::uint64_t, 7, order::little>( p );
  322. }
  323. inline std::int64_t load_big_s56( unsigned char const * p ) BOOST_NOEXCEPT
  324. {
  325. return boost::endian::endian_load<std::int64_t, 7, order::big>( p );
  326. }
  327. inline std::uint64_t load_big_u56( unsigned char const * p ) BOOST_NOEXCEPT
  328. {
  329. return boost::endian::endian_load<std::uint64_t, 7, order::big>( p );
  330. }
  331. // load 64
  332. inline std::int64_t load_little_s64( unsigned char const * p ) BOOST_NOEXCEPT
  333. {
  334. return boost::endian::endian_load<std::int64_t, 8, order::little>( p );
  335. }
  336. inline std::uint64_t load_little_u64( unsigned char const * p ) BOOST_NOEXCEPT
  337. {
  338. return boost::endian::endian_load<std::uint64_t, 8, order::little>( p );
  339. }
  340. inline std::int64_t load_big_s64( unsigned char const * p ) BOOST_NOEXCEPT
  341. {
  342. return boost::endian::endian_load<std::int64_t, 8, order::big>( p );
  343. }
  344. inline std::uint64_t load_big_u64( unsigned char const * p ) BOOST_NOEXCEPT
  345. {
  346. return boost::endian::endian_load<std::uint64_t, 8, order::big>( p );
  347. }
  348. // store 16
  349. inline void store_little_s16( unsigned char * p, std::int16_t v )
  350. {
  351. boost::endian::endian_store<std::int16_t, 2, order::little>( p, v );
  352. }
  353. inline void store_little_u16( unsigned char * p, std::uint16_t v )
  354. {
  355. boost::endian::endian_store<std::uint16_t, 2, order::little>( p, v );
  356. }
  357. inline void store_big_s16( unsigned char * p, std::int16_t v )
  358. {
  359. boost::endian::endian_store<std::int16_t, 2, order::big>( p, v );
  360. }
  361. inline void store_big_u16( unsigned char * p, std::uint16_t v )
  362. {
  363. boost::endian::endian_store<std::uint16_t, 2, order::big>( p, v );
  364. }
  365. // store 24
  366. inline void store_little_s24( unsigned char * p, std::int32_t v )
  367. {
  368. boost::endian::endian_store<std::int32_t, 3, order::little>( p, v );
  369. }
  370. inline void store_little_u24( unsigned char * p, std::uint32_t v )
  371. {
  372. boost::endian::endian_store<std::uint32_t, 3, order::little>( p, v );
  373. }
  374. inline void store_big_s24( unsigned char * p, std::int32_t v )
  375. {
  376. boost::endian::endian_store<std::int32_t, 3, order::big>( p, v );
  377. }
  378. inline void store_big_u24( unsigned char * p, std::uint32_t v )
  379. {
  380. boost::endian::endian_store<std::uint32_t, 3, order::big>( p, v );
  381. }
  382. // store 32
  383. inline void store_little_s32( unsigned char * p, std::int32_t v )
  384. {
  385. boost::endian::endian_store<std::int32_t, 4, order::little>( p, v );
  386. }
  387. inline void store_little_u32( unsigned char * p, std::uint32_t v )
  388. {
  389. boost::endian::endian_store<std::uint32_t, 4, order::little>( p, v );
  390. }
  391. inline void store_big_s32( unsigned char * p, std::int32_t v )
  392. {
  393. boost::endian::endian_store<std::int32_t, 4, order::big>( p, v );
  394. }
  395. inline void store_big_u32( unsigned char * p, std::uint32_t v )
  396. {
  397. boost::endian::endian_store<std::uint32_t, 4, order::big>( p, v );
  398. }
  399. // store 40
  400. inline void store_little_s40( unsigned char * p, std::int64_t v )
  401. {
  402. boost::endian::endian_store<std::int64_t, 5, order::little>( p, v );
  403. }
  404. inline void store_little_u40( unsigned char * p, std::uint64_t v )
  405. {
  406. boost::endian::endian_store<std::uint64_t, 5, order::little>( p, v );
  407. }
  408. inline void store_big_s40( unsigned char * p, std::int64_t v )
  409. {
  410. boost::endian::endian_store<std::int64_t, 5, order::big>( p, v );
  411. }
  412. inline void store_big_u40( unsigned char * p, std::uint64_t v )
  413. {
  414. boost::endian::endian_store<std::uint64_t, 5, order::big>( p, v );
  415. }
  416. // store 48
  417. inline void store_little_s48( unsigned char * p, std::int64_t v )
  418. {
  419. boost::endian::endian_store<std::int64_t, 6, order::little>( p, v );
  420. }
  421. inline void store_little_u48( unsigned char * p, std::uint64_t v )
  422. {
  423. boost::endian::endian_store<std::uint64_t, 6, order::little>( p, v );
  424. }
  425. inline void store_big_s48( unsigned char * p, std::int64_t v )
  426. {
  427. boost::endian::endian_store<std::int64_t, 6, order::big>( p, v );
  428. }
  429. inline void store_big_u48( unsigned char * p, std::uint64_t v )
  430. {
  431. boost::endian::endian_store<std::uint64_t, 6, order::big>( p, v );
  432. }
  433. // store 56
  434. inline void store_little_s56( unsigned char * p, std::int64_t v )
  435. {
  436. boost::endian::endian_store<std::int64_t, 7, order::little>( p, v );
  437. }
  438. inline void store_little_u56( unsigned char * p, std::uint64_t v )
  439. {
  440. boost::endian::endian_store<std::uint64_t, 7, order::little>( p, v );
  441. }
  442. inline void store_big_s56( unsigned char * p, std::int64_t v )
  443. {
  444. boost::endian::endian_store<std::int64_t, 7, order::big>( p, v );
  445. }
  446. inline void store_big_u56( unsigned char * p, std::uint64_t v )
  447. {
  448. boost::endian::endian_store<std::uint64_t, 7, order::big>( p, v );
  449. }
  450. // store 64
  451. inline void store_little_s64( unsigned char * p, std::int64_t v )
  452. {
  453. boost::endian::endian_store<std::int64_t, 8, order::little>( p, v );
  454. }
  455. inline void store_little_u64( unsigned char * p, std::uint64_t v )
  456. {
  457. boost::endian::endian_store<std::uint64_t, 8, order::little>( p, v );
  458. }
  459. inline void store_big_s64( unsigned char * p, std::int64_t v )
  460. {
  461. boost::endian::endian_store<std::int64_t, 8, order::big>( p, v );
  462. }
  463. inline void store_big_u64( unsigned char * p, std::uint64_t v )
  464. {
  465. boost::endian::endian_store<std::uint64_t, 8, order::big>( p, v );
  466. }
  467. } // namespace endian
  468. } // namespace boost
  469. #endif // BOOST_ENDIAN_CONVERSION_HPP