endian_store.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #ifndef BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED
  2. #define BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED
  3. // Copyright 2019 Peter Dimov
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/endian/detail/endian_reverse.hpp>
  8. #include <boost/endian/detail/order.hpp>
  9. #include <boost/endian/detail/integral_by_size.hpp>
  10. #include <boost/endian/detail/is_trivially_copyable.hpp>
  11. #include <boost/endian/detail/static_assert.hpp>
  12. #include <type_traits>
  13. #include <cstddef>
  14. #include <cstring>
  15. namespace boost
  16. {
  17. namespace endian
  18. {
  19. namespace detail
  20. {
  21. template<class T, std::size_t N1, order O1, std::size_t N2, order O2> struct endian_store_impl
  22. {
  23. };
  24. } // namespace detail
  25. // Requires:
  26. //
  27. // sizeof(T) must be 1, 2, 4, or 8
  28. // 1 <= N <= sizeof(T)
  29. // T is TriviallyCopyable
  30. // if N < sizeof(T), T is integral or enum
  31. template<class T, std::size_t N, order Order>
  32. inline void endian_store( unsigned char * p, T const & v ) BOOST_NOEXCEPT
  33. {
  34. BOOST_ENDIAN_STATIC_ASSERT( sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8 );
  35. BOOST_ENDIAN_STATIC_ASSERT( N >= 1 && N <= sizeof(T) );
  36. return detail::endian_store_impl<T, sizeof(T), order::native, N, Order>()( p, v );
  37. }
  38. namespace detail
  39. {
  40. // same endianness, same size
  41. template<class T, std::size_t N, order O> struct endian_store_impl<T, N, O, N, O>
  42. {
  43. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  44. {
  45. BOOST_ENDIAN_STATIC_ASSERT( is_trivially_copyable<T>::value );
  46. std::memcpy( p, &v, N );
  47. }
  48. };
  49. // same size, reverse endianness
  50. template<class T, std::size_t N, order O1, order O2> struct endian_store_impl<T, N, O1, N, O2>
  51. {
  52. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  53. {
  54. BOOST_ENDIAN_STATIC_ASSERT( is_trivially_copyable<T>::value );
  55. typename integral_by_size<N>::type tmp;
  56. std::memcpy( &tmp, &v, N );
  57. endian_reverse_inplace( tmp );
  58. std::memcpy( p, &tmp, N );
  59. }
  60. };
  61. // truncating store 2 -> 1
  62. template<class T, order Order> struct endian_store_impl<T, 2, Order, 1, order::little>
  63. {
  64. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  65. {
  66. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  67. unsigned char tmp[ 2 ];
  68. boost::endian::endian_store<T, 2, order::little>( tmp, v );
  69. p[0] = tmp[0];
  70. }
  71. };
  72. template<class T, order Order> struct endian_store_impl<T, 2, Order, 1, order::big>
  73. {
  74. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  75. {
  76. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  77. unsigned char tmp[ 2 ];
  78. boost::endian::endian_store<T, 2, order::big>( tmp, v );
  79. p[0] = tmp[1];
  80. }
  81. };
  82. // truncating store 4 -> 1
  83. template<class T, order Order> struct endian_store_impl<T, 4, Order, 1, order::little>
  84. {
  85. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  86. {
  87. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  88. unsigned char tmp[ 4 ];
  89. boost::endian::endian_store<T, 4, order::little>( tmp, v );
  90. p[0] = tmp[0];
  91. }
  92. };
  93. template<class T, order Order> struct endian_store_impl<T, 4, Order, 1, order::big>
  94. {
  95. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  96. {
  97. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  98. unsigned char tmp[ 4 ];
  99. boost::endian::endian_store<T, 4, order::big>( tmp, v );
  100. p[0] = tmp[3];
  101. }
  102. };
  103. // truncating store 4 -> 2
  104. template<class T, order Order> struct endian_store_impl<T, 4, Order, 2, order::little>
  105. {
  106. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  107. {
  108. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  109. unsigned char tmp[ 4 ];
  110. boost::endian::endian_store<T, 4, order::little>( tmp, v );
  111. p[0] = tmp[0];
  112. p[1] = tmp[1];
  113. }
  114. };
  115. template<class T, order Order> struct endian_store_impl<T, 4, Order, 2, order::big>
  116. {
  117. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  118. {
  119. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  120. unsigned char tmp[ 4 ];
  121. boost::endian::endian_store<T, 4, order::big>( tmp, v );
  122. p[0] = tmp[2];
  123. p[1] = tmp[3];
  124. }
  125. };
  126. // truncating store 4 -> 3
  127. template<class T, order Order> struct endian_store_impl<T, 4, Order, 3, order::little>
  128. {
  129. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  130. {
  131. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  132. unsigned char tmp[ 4 ];
  133. boost::endian::endian_store<T, 4, order::little>( tmp, v );
  134. p[0] = tmp[0];
  135. p[1] = tmp[1];
  136. p[2] = tmp[2];
  137. }
  138. };
  139. template<class T, order Order> struct endian_store_impl<T, 4, Order, 3, order::big>
  140. {
  141. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  142. {
  143. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  144. unsigned char tmp[ 4 ];
  145. boost::endian::endian_store<T, 4, order::big>( tmp, v );
  146. p[0] = tmp[1];
  147. p[1] = tmp[2];
  148. p[2] = tmp[3];
  149. }
  150. };
  151. // truncating store 8 -> 1
  152. template<class T, order Order> struct endian_store_impl<T, 8, Order, 1, order::little>
  153. {
  154. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  155. {
  156. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  157. unsigned char tmp[ 8 ];
  158. boost::endian::endian_store<T, 8, order::little>( tmp, v );
  159. p[0] = tmp[0];
  160. }
  161. };
  162. template<class T, order Order> struct endian_store_impl<T, 8, Order, 1, order::big>
  163. {
  164. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  165. {
  166. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  167. unsigned char tmp[ 8 ];
  168. boost::endian::endian_store<T, 8, order::big>( tmp, v );
  169. p[0] = tmp[7];
  170. }
  171. };
  172. // truncating store 8 -> 2
  173. template<class T, order Order> struct endian_store_impl<T, 8, Order, 2, order::little>
  174. {
  175. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  176. {
  177. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  178. unsigned char tmp[ 8 ];
  179. boost::endian::endian_store<T, 8, order::little>( tmp, v );
  180. p[0] = tmp[0];
  181. p[1] = tmp[1];
  182. }
  183. };
  184. template<class T, order Order> struct endian_store_impl<T, 8, Order, 2, order::big>
  185. {
  186. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  187. {
  188. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  189. unsigned char tmp[ 8 ];
  190. boost::endian::endian_store<T, 8, order::big>( tmp, v );
  191. p[0] = tmp[6];
  192. p[1] = tmp[7];
  193. }
  194. };
  195. // truncating store 8 -> 3
  196. template<class T, order Order> struct endian_store_impl<T, 8, Order, 3, order::little>
  197. {
  198. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  199. {
  200. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  201. unsigned char tmp[ 8 ];
  202. boost::endian::endian_store<T, 8, order::little>( tmp, v );
  203. p[0] = tmp[0];
  204. p[1] = tmp[1];
  205. p[2] = tmp[2];
  206. }
  207. };
  208. template<class T, order Order> struct endian_store_impl<T, 8, Order, 3, order::big>
  209. {
  210. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  211. {
  212. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  213. unsigned char tmp[ 8 ];
  214. boost::endian::endian_store<T, 8, order::big>( tmp, v );
  215. p[0] = tmp[5];
  216. p[1] = tmp[6];
  217. p[2] = tmp[7];
  218. }
  219. };
  220. // truncating store 8 -> 4
  221. template<class T, order Order> struct endian_store_impl<T, 8, Order, 4, order::little>
  222. {
  223. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  224. {
  225. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  226. unsigned char tmp[ 8 ];
  227. boost::endian::endian_store<T, 8, order::little>( tmp, v );
  228. p[0] = tmp[0];
  229. p[1] = tmp[1];
  230. p[2] = tmp[2];
  231. p[3] = tmp[3];
  232. }
  233. };
  234. template<class T, order Order> struct endian_store_impl<T, 8, Order, 4, order::big>
  235. {
  236. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  237. {
  238. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  239. unsigned char tmp[ 8 ];
  240. boost::endian::endian_store<T, 8, order::big>( tmp, v );
  241. p[0] = tmp[4];
  242. p[1] = tmp[5];
  243. p[2] = tmp[6];
  244. p[3] = tmp[7];
  245. }
  246. };
  247. // truncating store 8 -> 5
  248. template<class T, order Order> struct endian_store_impl<T, 8, Order, 5, order::little>
  249. {
  250. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  251. {
  252. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  253. unsigned char tmp[ 8 ];
  254. boost::endian::endian_store<T, 8, order::little>( tmp, v );
  255. p[0] = tmp[0];
  256. p[1] = tmp[1];
  257. p[2] = tmp[2];
  258. p[3] = tmp[3];
  259. p[4] = tmp[4];
  260. }
  261. };
  262. template<class T, order Order> struct endian_store_impl<T, 8, Order, 5, order::big>
  263. {
  264. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  265. {
  266. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  267. unsigned char tmp[ 8 ];
  268. boost::endian::endian_store<T, 8, order::big>( tmp, v );
  269. p[0] = tmp[3];
  270. p[1] = tmp[4];
  271. p[2] = tmp[5];
  272. p[3] = tmp[6];
  273. p[4] = tmp[7];
  274. }
  275. };
  276. // truncating store 8 -> 6
  277. template<class T, order Order> struct endian_store_impl<T, 8, Order, 6, order::little>
  278. {
  279. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  280. {
  281. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  282. unsigned char tmp[ 8 ];
  283. boost::endian::endian_store<T, 8, order::little>( tmp, v );
  284. p[0] = tmp[0];
  285. p[1] = tmp[1];
  286. p[2] = tmp[2];
  287. p[3] = tmp[3];
  288. p[4] = tmp[4];
  289. p[5] = tmp[5];
  290. }
  291. };
  292. template<class T, order Order> struct endian_store_impl<T, 8, Order, 6, order::big>
  293. {
  294. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  295. {
  296. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  297. unsigned char tmp[ 8 ];
  298. boost::endian::endian_store<T, 8, order::big>( tmp, v );
  299. p[0] = tmp[2];
  300. p[1] = tmp[3];
  301. p[2] = tmp[4];
  302. p[3] = tmp[5];
  303. p[4] = tmp[6];
  304. p[5] = tmp[7];
  305. }
  306. };
  307. // truncating store 8 -> 7
  308. template<class T, order Order> struct endian_store_impl<T, 8, Order, 7, order::little>
  309. {
  310. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  311. {
  312. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  313. unsigned char tmp[ 8 ];
  314. boost::endian::endian_store<T, 8, order::little>( tmp, v );
  315. p[0] = tmp[0];
  316. p[1] = tmp[1];
  317. p[2] = tmp[2];
  318. p[3] = tmp[3];
  319. p[4] = tmp[4];
  320. p[5] = tmp[5];
  321. p[6] = tmp[6];
  322. }
  323. };
  324. template<class T, order Order> struct endian_store_impl<T, 8, Order, 7, order::big>
  325. {
  326. inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT
  327. {
  328. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  329. unsigned char tmp[ 8 ];
  330. boost::endian::endian_store<T, 8, order::big>( tmp, v );
  331. p[0] = tmp[1];
  332. p[1] = tmp[2];
  333. p[2] = tmp[3];
  334. p[3] = tmp[4];
  335. p[4] = tmp[5];
  336. p[5] = tmp[6];
  337. p[6] = tmp[7];
  338. }
  339. };
  340. } // namespace detail
  341. } // namespace endian
  342. } // namespace boost
  343. #endif // BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED