arithmetic.hpp 19 KB

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