arithmetic.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // boost/endian/arithmetic.hpp -------------------------------------------------------//
  2. // (C) Copyright Darin Adler 2000
  3. // (C) Copyright Beman Dawes 2006, 2009, 2014
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. // See library home page at http://www.boost.org/libs/endian
  7. //--------------------------------------------------------------------------------------//
  8. // Original design developed by Darin Adler based on classes developed by Mark
  9. // Borgerding. Four original class templates were combined into a single endian
  10. // class template by Beman Dawes, who also added the unrolled_byte_loops sign
  11. // partial specialization to correctly extend the sign when cover integer size
  12. // differs from endian representation size.
  13. // TODO: When a compiler supporting constexpr becomes available, try possible uses.
  14. #ifndef BOOST_ENDIAN_ARITHMETIC_HPP
  15. #define BOOST_ENDIAN_ARITHMETIC_HPP
  16. #if defined(_MSC_VER)
  17. # pragma warning(push)
  18. # pragma warning(disable:4365) // conversion ... signed/unsigned mismatch
  19. #endif
  20. #ifdef BOOST_ENDIAN_LOG
  21. # include <iostream>
  22. #endif
  23. #if defined(__BORLANDC__) || defined( __CODEGEARC__)
  24. # pragma pack(push, 1)
  25. #endif
  26. #include <boost/config.hpp>
  27. #include <boost/config/workaround.hpp>
  28. #include <boost/predef/other/endian.h>
  29. #include <boost/endian/conversion.hpp>
  30. #include <boost/endian/buffers.hpp>
  31. #define BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
  32. #include <boost/endian/detail/cover_operators.hpp>
  33. #undef BOOST_ENDIAN_MINIMAL_COVER_OPERATORS
  34. #include <boost/type_traits/is_signed.hpp>
  35. #include <boost/cstdint.hpp>
  36. #include <boost/static_assert.hpp>
  37. #include <boost/core/scoped_enum.hpp>
  38. #include <iosfwd>
  39. #include <climits>
  40. # if CHAR_BIT != 8
  41. # error Platforms with CHAR_BIT != 8 are not supported
  42. # endif
  43. # ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  44. # define BOOST_ENDIAN_DEFAULT_CONSTRUCT {} // C++03
  45. # else
  46. # define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x
  47. # endif
  48. // g++ pre-4.6 does not support unrestricted unions, but we have no Config macro for that
  49. # if (defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || BOOST_WORKAROUND(BOOST_GCC, < 40600)) && defined(BOOST_ENDIAN_FORCE_PODNESS)
  50. # define BOOST_ENDIAN_NO_CTORS
  51. # endif
  52. # ifndef BOOST_ENDIAN_EXPLICIT_CTORS
  53. # define BOOST_ENDIAN_EXPLICIT_OPT
  54. # else
  55. # define BOOST_ENDIAN_EXPLICIT_OPT explicit
  56. # endif
  57. //---------------------------------- synopsis ----------------------------------------//
  58. namespace boost
  59. {
  60. namespace endian
  61. {
  62. template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
  63. BOOST_SCOPED_ENUM(align) A = align::no>
  64. class endian_arithmetic;
  65. // big endian signed integer aligned types
  66. typedef endian_arithmetic<order::big, int8_t, 8, align::yes> big_int8_at;
  67. typedef endian_arithmetic<order::big, int16_t, 16, align::yes> big_int16_at;
  68. typedef endian_arithmetic<order::big, int32_t, 32, align::yes> big_int32_at;
  69. typedef endian_arithmetic<order::big, int64_t, 64, align::yes> big_int64_at;
  70. // big endian unsigned integer aligned types
  71. typedef endian_arithmetic<order::big, uint8_t, 8, align::yes> big_uint8_at;
  72. typedef endian_arithmetic<order::big, uint16_t, 16, align::yes> big_uint16_at;
  73. typedef endian_arithmetic<order::big, uint32_t, 32, align::yes> big_uint32_at;
  74. typedef endian_arithmetic<order::big, uint64_t, 64, align::yes> big_uint64_at;
  75. // little endian signed integer aligned types
  76. typedef endian_arithmetic<order::little, int8_t, 8, align::yes> little_int8_at;
  77. typedef endian_arithmetic<order::little, int16_t, 16, align::yes> little_int16_at;
  78. typedef endian_arithmetic<order::little, int32_t, 32, align::yes> little_int32_at;
  79. typedef endian_arithmetic<order::little, int64_t, 64, align::yes> little_int64_at;
  80. // little endian unsigned integer aligned types
  81. typedef endian_arithmetic<order::little, uint8_t, 8, align::yes> little_uint8_at;
  82. typedef endian_arithmetic<order::little, uint16_t, 16, align::yes> little_uint16_at;
  83. typedef endian_arithmetic<order::little, uint32_t, 32, align::yes> little_uint32_at;
  84. typedef endian_arithmetic<order::little, uint64_t, 64, align::yes> little_uint64_at;
  85. // aligned native endian typedefs are not provided because
  86. // <cstdint> types are superior for this use case
  87. // big endian signed integer unaligned types
  88. typedef endian_arithmetic<order::big, int_least8_t, 8> big_int8_t;
  89. typedef endian_arithmetic<order::big, int_least16_t, 16> big_int16_t;
  90. typedef endian_arithmetic<order::big, int_least32_t, 24> big_int24_t;
  91. typedef endian_arithmetic<order::big, int_least32_t, 32> big_int32_t;
  92. typedef endian_arithmetic<order::big, int_least64_t, 40> big_int40_t;
  93. typedef endian_arithmetic<order::big, int_least64_t, 48> big_int48_t;
  94. typedef endian_arithmetic<order::big, int_least64_t, 56> big_int56_t;
  95. typedef endian_arithmetic<order::big, int_least64_t, 64> big_int64_t;
  96. // big endian unsigned integer unaligned types
  97. typedef endian_arithmetic<order::big, uint_least8_t, 8> big_uint8_t;
  98. typedef endian_arithmetic<order::big, uint_least16_t, 16> big_uint16_t;
  99. typedef endian_arithmetic<order::big, uint_least32_t, 24> big_uint24_t;
  100. typedef endian_arithmetic<order::big, uint_least32_t, 32> big_uint32_t;
  101. typedef endian_arithmetic<order::big, uint_least64_t, 40> big_uint40_t;
  102. typedef endian_arithmetic<order::big, uint_least64_t, 48> big_uint48_t;
  103. typedef endian_arithmetic<order::big, uint_least64_t, 56> big_uint56_t;
  104. typedef endian_arithmetic<order::big, uint_least64_t, 64> big_uint64_t;
  105. // little endian signed integer unaligned types
  106. typedef endian_arithmetic<order::little, int_least8_t, 8> little_int8_t;
  107. typedef endian_arithmetic<order::little, int_least16_t, 16> little_int16_t;
  108. typedef endian_arithmetic<order::little, int_least32_t, 24> little_int24_t;
  109. typedef endian_arithmetic<order::little, int_least32_t, 32> little_int32_t;
  110. typedef endian_arithmetic<order::little, int_least64_t, 40> little_int40_t;
  111. typedef endian_arithmetic<order::little, int_least64_t, 48> little_int48_t;
  112. typedef endian_arithmetic<order::little, int_least64_t, 56> little_int56_t;
  113. typedef endian_arithmetic<order::little, int_least64_t, 64> little_int64_t;
  114. // little endian unsigned integer unaligned types
  115. typedef endian_arithmetic<order::little, uint_least8_t, 8> little_uint8_t;
  116. typedef endian_arithmetic<order::little, uint_least16_t, 16> little_uint16_t;
  117. typedef endian_arithmetic<order::little, uint_least32_t, 24> little_uint24_t;
  118. typedef endian_arithmetic<order::little, uint_least32_t, 32> little_uint32_t;
  119. typedef endian_arithmetic<order::little, uint_least64_t, 40> little_uint40_t;
  120. typedef endian_arithmetic<order::little, uint_least64_t, 48> little_uint48_t;
  121. typedef endian_arithmetic<order::little, uint_least64_t, 56> little_uint56_t;
  122. typedef endian_arithmetic<order::little, uint_least64_t, 64> little_uint64_t;
  123. # if BOOST_ENDIAN_BIG_BYTE
  124. // native endian signed integer unaligned types
  125. typedef big_int8_t native_int8_t;
  126. typedef big_int16_t native_int16_t;
  127. typedef big_int24_t native_int24_t;
  128. typedef big_int32_t native_int32_t;
  129. typedef big_int40_t native_int40_t;
  130. typedef big_int48_t native_int48_t;
  131. typedef big_int56_t native_int56_t;
  132. typedef big_int64_t native_int64_t;
  133. // native endian unsigned integer unaligned types
  134. typedef big_uint8_t native_uint8_t;
  135. typedef big_uint16_t native_uint16_t;
  136. typedef big_uint24_t native_uint24_t;
  137. typedef big_uint32_t native_uint32_t;
  138. typedef big_uint40_t native_uint40_t;
  139. typedef big_uint48_t native_uint48_t;
  140. typedef big_uint56_t native_uint56_t;
  141. typedef big_uint64_t native_uint64_t;
  142. # else
  143. // native endian signed integer unaligned types
  144. typedef little_int8_t native_int8_t;
  145. typedef little_int16_t native_int16_t;
  146. typedef little_int24_t native_int24_t;
  147. typedef little_int32_t native_int32_t;
  148. typedef little_int40_t native_int40_t;
  149. typedef little_int48_t native_int48_t;
  150. typedef little_int56_t native_int56_t;
  151. typedef little_int64_t native_int64_t;
  152. // native endian unsigned integer unaligned types
  153. typedef little_uint8_t native_uint8_t;
  154. typedef little_uint16_t native_uint16_t;
  155. typedef little_uint24_t native_uint24_t;
  156. typedef little_uint32_t native_uint32_t;
  157. typedef little_uint40_t native_uint40_t;
  158. typedef little_uint48_t native_uint48_t;
  159. typedef little_uint56_t native_uint56_t;
  160. typedef little_uint64_t native_uint64_t;
  161. # endif
  162. # ifdef BOOST_ENDIAN_DEPRECATED_NAMES
  163. typedef order endianness;
  164. typedef align alignment;
  165. # ifndef BOOST_NO_CXX11_TEMPLATE_ALIASES
  166. template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
  167. BOOST_SCOPED_ENUM(align) Align = align::no>
  168. using endian = endian_arithmetic<Order, T, n_bits, Align>;
  169. # endif
  170. // unaligned big endian signed integer types
  171. typedef endian_arithmetic< order::big, int_least8_t, 8 > big8_t;
  172. typedef endian_arithmetic< order::big, int_least16_t, 16 > big16_t;
  173. typedef endian_arithmetic< order::big, int_least32_t, 24 > big24_t;
  174. typedef endian_arithmetic< order::big, int_least32_t, 32 > big32_t;
  175. typedef endian_arithmetic< order::big, int_least64_t, 40 > big40_t;
  176. typedef endian_arithmetic< order::big, int_least64_t, 48 > big48_t;
  177. typedef endian_arithmetic< order::big, int_least64_t, 56 > big56_t;
  178. typedef endian_arithmetic< order::big, int_least64_t, 64 > big64_t;
  179. // unaligned big endian_arithmetic unsigned integer types
  180. typedef endian_arithmetic< order::big, uint_least8_t, 8 > ubig8_t;
  181. typedef endian_arithmetic< order::big, uint_least16_t, 16 > ubig16_t;
  182. typedef endian_arithmetic< order::big, uint_least32_t, 24 > ubig24_t;
  183. typedef endian_arithmetic< order::big, uint_least32_t, 32 > ubig32_t;
  184. typedef endian_arithmetic< order::big, uint_least64_t, 40 > ubig40_t;
  185. typedef endian_arithmetic< order::big, uint_least64_t, 48 > ubig48_t;
  186. typedef endian_arithmetic< order::big, uint_least64_t, 56 > ubig56_t;
  187. typedef endian_arithmetic< order::big, uint_least64_t, 64 > ubig64_t;
  188. // unaligned little endian_arithmetic signed integer types
  189. typedef endian_arithmetic< order::little, int_least8_t, 8 > little8_t;
  190. typedef endian_arithmetic< order::little, int_least16_t, 16 > little16_t;
  191. typedef endian_arithmetic< order::little, int_least32_t, 24 > little24_t;
  192. typedef endian_arithmetic< order::little, int_least32_t, 32 > little32_t;
  193. typedef endian_arithmetic< order::little, int_least64_t, 40 > little40_t;
  194. typedef endian_arithmetic< order::little, int_least64_t, 48 > little48_t;
  195. typedef endian_arithmetic< order::little, int_least64_t, 56 > little56_t;
  196. typedef endian_arithmetic< order::little, int_least64_t, 64 > little64_t;
  197. // unaligned little endian_arithmetic unsigned integer types
  198. typedef endian_arithmetic< order::little, uint_least8_t, 8 > ulittle8_t;
  199. typedef endian_arithmetic< order::little, uint_least16_t, 16 > ulittle16_t;
  200. typedef endian_arithmetic< order::little, uint_least32_t, 24 > ulittle24_t;
  201. typedef endian_arithmetic< order::little, uint_least32_t, 32 > ulittle32_t;
  202. typedef endian_arithmetic< order::little, uint_least64_t, 40 > ulittle40_t;
  203. typedef endian_arithmetic< order::little, uint_least64_t, 48 > ulittle48_t;
  204. typedef endian_arithmetic< order::little, uint_least64_t, 56 > ulittle56_t;
  205. typedef endian_arithmetic< order::little, uint_least64_t, 64 > ulittle64_t;
  206. // unaligned native endian_arithmetic signed integer types
  207. typedef endian_arithmetic< order::native, int_least8_t, 8 > native8_t;
  208. typedef endian_arithmetic< order::native, int_least16_t, 16 > native16_t;
  209. typedef endian_arithmetic< order::native, int_least32_t, 24 > native24_t;
  210. typedef endian_arithmetic< order::native, int_least32_t, 32 > native32_t;
  211. typedef endian_arithmetic< order::native, int_least64_t, 40 > native40_t;
  212. typedef endian_arithmetic< order::native, int_least64_t, 48 > native48_t;
  213. typedef endian_arithmetic< order::native, int_least64_t, 56 > native56_t;
  214. typedef endian_arithmetic< order::native, int_least64_t, 64 > native64_t;
  215. // unaligned native endian_arithmetic unsigned integer types
  216. typedef endian_arithmetic< order::native, uint_least8_t, 8 > unative8_t;
  217. typedef endian_arithmetic< order::native, uint_least16_t, 16 > unative16_t;
  218. typedef endian_arithmetic< order::native, uint_least32_t, 24 > unative24_t;
  219. typedef endian_arithmetic< order::native, uint_least32_t, 32 > unative32_t;
  220. typedef endian_arithmetic< order::native, uint_least64_t, 40 > unative40_t;
  221. typedef endian_arithmetic< order::native, uint_least64_t, 48 > unative48_t;
  222. typedef endian_arithmetic< order::native, uint_least64_t, 56 > unative56_t;
  223. typedef endian_arithmetic< order::native, uint_least64_t, 64 > unative64_t;
  224. // aligned native endian_arithmetic typedefs are not provided because
  225. // <cstdint> types are superior for this use case
  226. typedef endian_arithmetic< order::big, int16_t, 16, align::yes > aligned_big16_t;
  227. typedef endian_arithmetic< order::big, uint16_t, 16, align::yes > aligned_ubig16_t;
  228. typedef endian_arithmetic< order::little, int16_t, 16, align::yes > aligned_little16_t;
  229. typedef endian_arithmetic< order::little, uint16_t, 16, align::yes > aligned_ulittle16_t;
  230. typedef endian_arithmetic< order::big, int32_t, 32, align::yes > aligned_big32_t;
  231. typedef endian_arithmetic< order::big, uint32_t, 32, align::yes > aligned_ubig32_t;
  232. typedef endian_arithmetic< order::little, int32_t, 32, align::yes > aligned_little32_t;
  233. typedef endian_arithmetic< order::little, uint32_t, 32, align::yes > aligned_ulittle32_t;
  234. typedef endian_arithmetic< order::big, int64_t, 64, align::yes > aligned_big64_t;
  235. typedef endian_arithmetic< order::big, uint64_t, 64, align::yes > aligned_ubig64_t;
  236. typedef endian_arithmetic< order::little, int64_t, 64, align::yes > aligned_little64_t;
  237. typedef endian_arithmetic< order::little, uint64_t, 64, align::yes > aligned_ulittle64_t;
  238. # endif
  239. //---------------------------------- end synopsis ------------------------------------//
  240. // endian class template specializations ---------------------------------------------//
  241. // Specializations that represent unaligned bytes.
  242. // Taking an integer type as a parameter provides a nice way to pass both
  243. // the size and signness of the desired integer and get the appropriate
  244. // corresponding integer type for the interface.
  245. // unaligned integer big endian specialization
  246. template <typename T, std::size_t n_bits>
  247. class endian_arithmetic< order::big, T, n_bits, align::no >
  248. : public endian_buffer< order::big, T, n_bits, align::no >,
  249. cover_operators<endian_arithmetic<order::big, T, n_bits>, T>
  250. {
  251. BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  252. public:
  253. typedef T value_type;
  254. # ifndef BOOST_ENDIAN_NO_CTORS
  255. endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  256. BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT
  257. {
  258. # ifdef BOOST_ENDIAN_LOG
  259. if ( endian_log )
  260. std::cout << "big, unaligned, " << n_bits << "-bits, construct(" << val << ")\n";
  261. # endif
  262. detail::store_big_endian<T, n_bits/8>(this->m_value, val);
  263. }
  264. # endif
  265. endian_arithmetic& operator=(T val) BOOST_NOEXCEPT
  266. { detail::store_big_endian<T, n_bits/8>(this->m_value, val); return *this; }
  267. operator value_type() const BOOST_NOEXCEPT { return this->value(); }
  268. };
  269. // unaligned little endian specialization
  270. template <typename T, std::size_t n_bits>
  271. class endian_arithmetic< order::little, T, n_bits, align::no >
  272. : public endian_buffer< order::little, T, n_bits, align::no >,
  273. cover_operators< endian_arithmetic< order::little, T, n_bits >, T >
  274. {
  275. BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  276. public:
  277. typedef T value_type;
  278. # ifndef BOOST_ENDIAN_NO_CTORS
  279. endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  280. BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT
  281. {
  282. # ifdef BOOST_ENDIAN_LOG
  283. if ( endian_log )
  284. std::cout << "little, unaligned, " << n_bits << "-bits, construct(" << val << ")\n";
  285. # endif
  286. detail::store_little_endian<T, n_bits/8>(this->m_value, val);
  287. }
  288. # endif
  289. endian_arithmetic& operator=(T val) BOOST_NOEXCEPT
  290. { detail::store_little_endian<T, n_bits/8>(this->m_value, val); return *this; }
  291. operator value_type() const BOOST_NOEXCEPT { return this->value(); }
  292. };
  293. // align::yes specializations; only n_bits == 16/32/64 supported
  294. // aligned big endian specialization
  295. template <typename T, std::size_t n_bits>
  296. class endian_arithmetic<order::big, T, n_bits, align::yes>
  297. : public endian_buffer< order::big, T, n_bits, align::yes >,
  298. cover_operators<endian_arithmetic<order::big, T, n_bits, align::yes>, T>
  299. {
  300. BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  301. BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
  302. public:
  303. typedef T value_type;
  304. # ifndef BOOST_ENDIAN_NO_CTORS
  305. endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  306. BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT
  307. {
  308. # ifdef BOOST_ENDIAN_LOG
  309. if ( endian_log )
  310. std::cout << "big, aligned, " << n_bits << "-bits, construct(" << val << ")\n";
  311. # endif
  312. this->m_value = ::boost::endian::native_to_big(val);
  313. }
  314. # endif
  315. endian_arithmetic& operator=(T val) BOOST_NOEXCEPT
  316. {
  317. this->m_value = ::boost::endian::native_to_big(val);
  318. return *this;
  319. }
  320. operator value_type() const BOOST_NOEXCEPT { return this->value(); }
  321. };
  322. // aligned little endian specialization
  323. template <typename T, std::size_t n_bits>
  324. class endian_arithmetic<order::little, T, n_bits, align::yes>
  325. : public endian_buffer< order::little, T, n_bits, align::yes >,
  326. cover_operators<endian_arithmetic<order::little, T, n_bits, align::yes>, T>
  327. {
  328. BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits );
  329. BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 );
  330. public:
  331. typedef T value_type;
  332. # ifndef BOOST_ENDIAN_NO_CTORS
  333. endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  334. BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic(T val) BOOST_NOEXCEPT
  335. {
  336. # ifdef BOOST_ENDIAN_LOG
  337. if ( endian_log )
  338. std::cout << "little, aligned, " << n_bits << "-bits, construct(" << val << ")\n";
  339. # endif
  340. this->m_value = ::boost::endian::native_to_little(val);
  341. }
  342. # endif
  343. endian_arithmetic& operator=(T val) BOOST_NOEXCEPT
  344. {
  345. this->m_value = ::boost::endian::native_to_little(val);
  346. return *this;
  347. }
  348. operator value_type() const BOOST_NOEXCEPT { return this->value(); }
  349. };
  350. } // namespace endian
  351. } // namespace boost
  352. #if defined(__BORLANDC__) || defined( __CODEGEARC__)
  353. # pragma pack(pop)
  354. #endif
  355. #if defined(_MSC_VER)
  356. # pragma warning(pop)
  357. #endif
  358. #endif // BOOST_ENDIAN_ARITHMETIC_HPP