endian_load.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. #ifndef BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED
  2. #define BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_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_load_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 T endian_load( unsigned char const * p ) 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_load_impl<T, sizeof(T), order::native, N, Order>()( p );
  37. }
  38. namespace detail
  39. {
  40. // same endianness, same size
  41. template<class T, std::size_t N, order O> struct endian_load_impl<T, N, O, N, O>
  42. {
  43. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  44. {
  45. BOOST_ENDIAN_STATIC_ASSERT( is_trivially_copyable<T>::value );
  46. T t;
  47. std::memcpy( &t, p, N );
  48. return t;
  49. }
  50. };
  51. // same size, reverse endianness
  52. template<class T, std::size_t N, order O1, order O2> struct endian_load_impl<T, N, O1, N, O2>
  53. {
  54. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  55. {
  56. BOOST_ENDIAN_STATIC_ASSERT( is_trivially_copyable<T>::value );
  57. typename integral_by_size<N>::type tmp;
  58. std::memcpy( &tmp, p, N );
  59. endian_reverse_inplace( tmp );
  60. T t;
  61. std::memcpy( &t, &tmp, N );
  62. return t;
  63. }
  64. };
  65. // expanding load 1 -> 2
  66. template<class T, order Order> struct endian_load_impl<T, 2, Order, 1, order::little>
  67. {
  68. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  69. {
  70. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  71. unsigned char tmp[ 2 ];
  72. tmp[0] = p[0];
  73. tmp[1] = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  74. return boost::endian::endian_load<T, 2, order::little>( tmp );
  75. }
  76. };
  77. template<class T, order Order> struct endian_load_impl<T, 2, Order, 1, order::big>
  78. {
  79. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  80. {
  81. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  82. unsigned char tmp[ 2 ];
  83. tmp[0] = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  84. tmp[1] = p[0];
  85. return boost::endian::endian_load<T, 2, order::big>( tmp );
  86. }
  87. };
  88. // expanding load 1 -> 4
  89. template<class T, order Order> struct endian_load_impl<T, 4, Order, 1, order::little>
  90. {
  91. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  92. {
  93. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  94. unsigned char tmp[ 4 ];
  95. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  96. tmp[0] = p[0];
  97. tmp[1] = fill;
  98. tmp[2] = fill;
  99. tmp[3] = fill;
  100. return boost::endian::endian_load<T, 4, order::little>( tmp );
  101. }
  102. };
  103. template<class T, order Order> struct endian_load_impl<T, 4, Order, 1, order::big>
  104. {
  105. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  106. {
  107. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  108. unsigned char tmp[ 4 ];
  109. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  110. tmp[0] = fill;
  111. tmp[1] = fill;
  112. tmp[2] = fill;
  113. tmp[3] = p[0];
  114. return boost::endian::endian_load<T, 4, order::big>( tmp );
  115. }
  116. };
  117. // expanding load 2 -> 4
  118. template<class T, order Order> struct endian_load_impl<T, 4, Order, 2, order::little>
  119. {
  120. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  121. {
  122. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  123. unsigned char tmp[ 4 ];
  124. unsigned char fill = std::is_signed<T>::value && ( p[1] & 0x80 )? 0xFF: 0x00;
  125. tmp[0] = p[0];
  126. tmp[1] = p[1];
  127. tmp[2] = fill;
  128. tmp[3] = fill;
  129. return boost::endian::endian_load<T, 4, order::little>( tmp );
  130. }
  131. };
  132. template<class T, order Order> struct endian_load_impl<T, 4, Order, 2, order::big>
  133. {
  134. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  135. {
  136. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  137. unsigned char tmp[ 4 ];
  138. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  139. tmp[0] = fill;
  140. tmp[1] = fill;
  141. tmp[2] = p[0];
  142. tmp[3] = p[1];
  143. return boost::endian::endian_load<T, 4, order::big>( tmp );
  144. }
  145. };
  146. // expanding load 3 -> 4
  147. template<class T, order Order> struct endian_load_impl<T, 4, Order, 3, order::little>
  148. {
  149. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  150. {
  151. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  152. unsigned char tmp[ 4 ];
  153. tmp[0] = p[0];
  154. tmp[1] = p[1];
  155. tmp[2] = p[2];
  156. tmp[3] = std::is_signed<T>::value && ( p[2] & 0x80 )? 0xFF: 0x00;
  157. return boost::endian::endian_load<T, 4, order::little>( tmp );
  158. }
  159. };
  160. template<class T, order Order> struct endian_load_impl<T, 4, Order, 3, order::big>
  161. {
  162. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  163. {
  164. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  165. unsigned char tmp[ 4 ];
  166. tmp[0] = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  167. tmp[1] = p[0];
  168. tmp[2] = p[1];
  169. tmp[3] = p[2];
  170. return boost::endian::endian_load<T, 4, order::big>( tmp );
  171. }
  172. };
  173. // expanding load 1 -> 8
  174. template<class T, order Order> struct endian_load_impl<T, 8, Order, 1, order::little>
  175. {
  176. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  177. {
  178. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  179. unsigned char tmp[ 8 ];
  180. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  181. tmp[0] = p[0];
  182. tmp[1] = fill;
  183. tmp[2] = fill;
  184. tmp[3] = fill;
  185. tmp[4] = fill;
  186. tmp[5] = fill;
  187. tmp[6] = fill;
  188. tmp[7] = fill;
  189. return boost::endian::endian_load<T, 8, order::little>( tmp );
  190. }
  191. };
  192. template<class T, order Order> struct endian_load_impl<T, 8, Order, 1, order::big>
  193. {
  194. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  195. {
  196. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  197. unsigned char tmp[ 8 ];
  198. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  199. tmp[0] = fill;
  200. tmp[1] = fill;
  201. tmp[2] = fill;
  202. tmp[3] = fill;
  203. tmp[4] = fill;
  204. tmp[5] = fill;
  205. tmp[6] = fill;
  206. tmp[7] = p[0];
  207. return boost::endian::endian_load<T, 8, order::big>( tmp );
  208. }
  209. };
  210. // expanding load 2 -> 8
  211. template<class T, order Order> struct endian_load_impl<T, 8, Order, 2, order::little>
  212. {
  213. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  214. {
  215. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  216. unsigned char tmp[ 8 ];
  217. unsigned char fill = std::is_signed<T>::value && ( p[1] & 0x80 )? 0xFF: 0x00;
  218. tmp[0] = p[0];
  219. tmp[1] = p[1];
  220. tmp[2] = fill;
  221. tmp[3] = fill;
  222. tmp[4] = fill;
  223. tmp[5] = fill;
  224. tmp[6] = fill;
  225. tmp[7] = fill;
  226. return boost::endian::endian_load<T, 8, order::little>( tmp );
  227. }
  228. };
  229. template<class T, order Order> struct endian_load_impl<T, 8, Order, 2, order::big>
  230. {
  231. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  232. {
  233. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  234. unsigned char tmp[ 8 ];
  235. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  236. tmp[0] = fill;
  237. tmp[1] = fill;
  238. tmp[2] = fill;
  239. tmp[3] = fill;
  240. tmp[4] = fill;
  241. tmp[5] = fill;
  242. tmp[6] = p[0];
  243. tmp[7] = p[1];
  244. return boost::endian::endian_load<T, 8, order::big>( tmp );
  245. }
  246. };
  247. // expanding load 3 -> 8
  248. template<class T, order Order> struct endian_load_impl<T, 8, Order, 3, order::little>
  249. {
  250. inline T operator()( unsigned char const * p ) 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. unsigned char fill = std::is_signed<T>::value && ( p[2] & 0x80 )? 0xFF: 0x00;
  255. tmp[0] = p[0];
  256. tmp[1] = p[1];
  257. tmp[2] = p[2];
  258. tmp[3] = fill;
  259. tmp[4] = fill;
  260. tmp[5] = fill;
  261. tmp[6] = fill;
  262. tmp[7] = fill;
  263. return boost::endian::endian_load<T, 8, order::little>( tmp );
  264. }
  265. };
  266. template<class T, order Order> struct endian_load_impl<T, 8, Order, 3, order::big>
  267. {
  268. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  269. {
  270. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  271. unsigned char tmp[ 8 ];
  272. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  273. tmp[0] = fill;
  274. tmp[1] = fill;
  275. tmp[2] = fill;
  276. tmp[3] = fill;
  277. tmp[4] = fill;
  278. tmp[5] = p[0];
  279. tmp[6] = p[1];
  280. tmp[7] = p[2];
  281. return boost::endian::endian_load<T, 8, order::big>( tmp );
  282. }
  283. };
  284. // expanding load 4 -> 8
  285. template<class T, order Order> struct endian_load_impl<T, 8, Order, 4, order::little>
  286. {
  287. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  288. {
  289. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  290. unsigned char tmp[ 8 ];
  291. unsigned char fill = std::is_signed<T>::value && ( p[3] & 0x80 )? 0xFF: 0x00;
  292. tmp[0] = p[0];
  293. tmp[1] = p[1];
  294. tmp[2] = p[2];
  295. tmp[3] = p[3];
  296. tmp[4] = fill;
  297. tmp[5] = fill;
  298. tmp[6] = fill;
  299. tmp[7] = fill;
  300. return boost::endian::endian_load<T, 8, order::little>( tmp );
  301. }
  302. };
  303. template<class T, order Order> struct endian_load_impl<T, 8, Order, 4, order::big>
  304. {
  305. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  306. {
  307. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  308. unsigned char tmp[ 8 ];
  309. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  310. tmp[0] = fill;
  311. tmp[1] = fill;
  312. tmp[2] = fill;
  313. tmp[3] = fill;
  314. tmp[4] = p[0];
  315. tmp[5] = p[1];
  316. tmp[6] = p[2];
  317. tmp[7] = p[3];
  318. return boost::endian::endian_load<T, 8, order::big>( tmp );
  319. }
  320. };
  321. // expanding load 5 -> 8
  322. template<class T, order Order> struct endian_load_impl<T, 8, Order, 5, order::little>
  323. {
  324. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  325. {
  326. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  327. unsigned char tmp[ 8 ];
  328. unsigned char fill = std::is_signed<T>::value && ( p[4] & 0x80 )? 0xFF: 0x00;
  329. tmp[0] = p[0];
  330. tmp[1] = p[1];
  331. tmp[2] = p[2];
  332. tmp[3] = p[3];
  333. tmp[4] = p[4];
  334. tmp[5] = fill;
  335. tmp[6] = fill;
  336. tmp[7] = fill;
  337. return boost::endian::endian_load<T, 8, order::little>( tmp );
  338. }
  339. };
  340. template<class T, order Order> struct endian_load_impl<T, 8, Order, 5, order::big>
  341. {
  342. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  343. {
  344. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  345. unsigned char tmp[ 8 ];
  346. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  347. tmp[0] = fill;
  348. tmp[1] = fill;
  349. tmp[2] = fill;
  350. tmp[3] = p[0];
  351. tmp[4] = p[1];
  352. tmp[5] = p[2];
  353. tmp[6] = p[3];
  354. tmp[7] = p[4];
  355. return boost::endian::endian_load<T, 8, order::big>( tmp );
  356. }
  357. };
  358. // expanding load 6 -> 8
  359. template<class T, order Order> struct endian_load_impl<T, 8, Order, 6, order::little>
  360. {
  361. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  362. {
  363. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  364. unsigned char tmp[ 8 ];
  365. unsigned char fill = std::is_signed<T>::value && ( p[5] & 0x80 )? 0xFF: 0x00;
  366. tmp[0] = p[0];
  367. tmp[1] = p[1];
  368. tmp[2] = p[2];
  369. tmp[3] = p[3];
  370. tmp[4] = p[4];
  371. tmp[5] = p[5];
  372. tmp[6] = fill;
  373. tmp[7] = fill;
  374. return boost::endian::endian_load<T, 8, order::little>( tmp );
  375. }
  376. };
  377. template<class T, order Order> struct endian_load_impl<T, 8, Order, 6, order::big>
  378. {
  379. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  380. {
  381. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  382. unsigned char tmp[ 8 ];
  383. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  384. tmp[0] = fill;
  385. tmp[1] = fill;
  386. tmp[2] = p[0];
  387. tmp[3] = p[1];
  388. tmp[4] = p[2];
  389. tmp[5] = p[3];
  390. tmp[6] = p[4];
  391. tmp[7] = p[5];
  392. return boost::endian::endian_load<T, 8, order::big>( tmp );
  393. }
  394. };
  395. // expanding load 7 -> 8
  396. template<class T, order Order> struct endian_load_impl<T, 8, Order, 7, order::little>
  397. {
  398. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  399. {
  400. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  401. unsigned char tmp[ 8 ];
  402. unsigned char fill = std::is_signed<T>::value && ( p[6] & 0x80 )? 0xFF: 0x00;
  403. tmp[0] = p[0];
  404. tmp[1] = p[1];
  405. tmp[2] = p[2];
  406. tmp[3] = p[3];
  407. tmp[4] = p[4];
  408. tmp[5] = p[5];
  409. tmp[6] = p[6];
  410. tmp[7] = fill;
  411. return boost::endian::endian_load<T, 8, order::little>( tmp );
  412. }
  413. };
  414. template<class T, order Order> struct endian_load_impl<T, 8, Order, 7, order::big>
  415. {
  416. inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT
  417. {
  418. BOOST_ENDIAN_STATIC_ASSERT( std::is_integral<T>::value || std::is_enum<T>::value );
  419. unsigned char tmp[ 8 ];
  420. unsigned char fill = std::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;
  421. tmp[0] = fill;
  422. tmp[1] = p[0];
  423. tmp[2] = p[1];
  424. tmp[3] = p[2];
  425. tmp[4] = p[3];
  426. tmp[5] = p[4];
  427. tmp[6] = p[5];
  428. tmp[7] = p[6];
  429. return boost::endian::endian_load<T, 8, order::big>( tmp );
  430. }
  431. };
  432. } // namespace detail
  433. } // namespace endian
  434. } // namespace boost
  435. #endif // BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED