endian_load.hpp 16 KB

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