arithmetic.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // boost/endian/arithmetic.hpp -------------------------------------------------------//
  2. // (C) Copyright Darin Adler 2000
  3. // (C) Copyright Beman Dawes 2006, 2009, 2014
  4. // (C) Copyright Peter Dimov 2019
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. // See library home page at http://www.boost.org/libs/endian
  8. //--------------------------------------------------------------------------------------//
  9. // Original design developed by Darin Adler based on classes developed by Mark
  10. // Borgerding. Four original class templates were combined into a single endian
  11. // class template by Beman Dawes, who also added the unrolled_byte_loops sign
  12. // partial specialization to correctly extend the sign when cover integer size
  13. // differs from endian representation size.
  14. // TODO: When a compiler supporting constexpr becomes available, try possible uses.
  15. #ifndef BOOST_ENDIAN_ARITHMETIC_HPP
  16. #define BOOST_ENDIAN_ARITHMETIC_HPP
  17. #if defined(_MSC_VER)
  18. # pragma warning(push)
  19. # pragma warning(disable:4365) // conversion ... signed/unsigned mismatch
  20. #endif
  21. #include <boost/endian/detail/requires_cxx11.hpp>
  22. #include <boost/endian/buffers.hpp>
  23. #include <boost/core/scoped_enum.hpp>
  24. #include <boost/static_assert.hpp>
  25. #include <boost/cstdint.hpp>
  26. #include <boost/config.hpp>
  27. #include <boost/config/workaround.hpp>
  28. #include <iosfwd>
  29. #include <climits>
  30. #if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
  31. # pragma pack(push, 1)
  32. #endif
  33. # if CHAR_BIT != 8
  34. # error Platforms with CHAR_BIT != 8 are not supported
  35. # endif
  36. # ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  37. # define BOOST_ENDIAN_DEFAULT_CONSTRUCT {} // C++03
  38. # else
  39. # define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x
  40. # endif
  41. // g++ pre-4.6 does not support unrestricted unions, but we have no Config macro for that
  42. # if (defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || BOOST_WORKAROUND(BOOST_GCC, < 40600)) && defined(BOOST_ENDIAN_FORCE_PODNESS)
  43. # define BOOST_ENDIAN_NO_CTORS
  44. # endif
  45. # ifndef BOOST_ENDIAN_EXPLICIT_CTORS
  46. # define BOOST_ENDIAN_EXPLICIT_OPT
  47. # else
  48. # define BOOST_ENDIAN_EXPLICIT_OPT explicit
  49. # endif
  50. //---------------------------------- synopsis ----------------------------------------//
  51. namespace boost
  52. {
  53. namespace endian
  54. {
  55. template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
  56. BOOST_SCOPED_ENUM(align) Align = align::no>
  57. class endian_arithmetic;
  58. // big endian signed integer aligned types
  59. typedef endian_arithmetic<order::big, int8_t, 8, align::yes> big_int8_at;
  60. typedef endian_arithmetic<order::big, int16_t, 16, align::yes> big_int16_at;
  61. typedef endian_arithmetic<order::big, int32_t, 32, align::yes> big_int32_at;
  62. typedef endian_arithmetic<order::big, int64_t, 64, align::yes> big_int64_at;
  63. // big endian unsigned integer aligned types
  64. typedef endian_arithmetic<order::big, uint8_t, 8, align::yes> big_uint8_at;
  65. typedef endian_arithmetic<order::big, uint16_t, 16, align::yes> big_uint16_at;
  66. typedef endian_arithmetic<order::big, uint32_t, 32, align::yes> big_uint32_at;
  67. typedef endian_arithmetic<order::big, uint64_t, 64, align::yes> big_uint64_at;
  68. // little endian signed integer aligned types
  69. typedef endian_arithmetic<order::little, int8_t, 8, align::yes> little_int8_at;
  70. typedef endian_arithmetic<order::little, int16_t, 16, align::yes> little_int16_at;
  71. typedef endian_arithmetic<order::little, int32_t, 32, align::yes> little_int32_at;
  72. typedef endian_arithmetic<order::little, int64_t, 64, align::yes> little_int64_at;
  73. // little endian unsigned integer aligned types
  74. typedef endian_arithmetic<order::little, uint8_t, 8, align::yes> little_uint8_at;
  75. typedef endian_arithmetic<order::little, uint16_t, 16, align::yes> little_uint16_at;
  76. typedef endian_arithmetic<order::little, uint32_t, 32, align::yes> little_uint32_at;
  77. typedef endian_arithmetic<order::little, uint64_t, 64, align::yes> little_uint64_at;
  78. // aligned floating point types
  79. typedef endian_arithmetic<order::big, float, 32, align::yes> big_float32_at;
  80. typedef endian_arithmetic<order::big, double, 64, align::yes> big_float64_at;
  81. typedef endian_arithmetic<order::little, float, 32, align::yes> little_float32_at;
  82. typedef endian_arithmetic<order::little, double, 64, align::yes> little_float64_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. // native endian signed integer unaligned types
  122. typedef endian_arithmetic<order::native, int_least8_t, 8> native_int8_t;
  123. typedef endian_arithmetic<order::native, int_least16_t, 16> native_int16_t;
  124. typedef endian_arithmetic<order::native, int_least32_t, 24> native_int24_t;
  125. typedef endian_arithmetic<order::native, int_least32_t, 32> native_int32_t;
  126. typedef endian_arithmetic<order::native, int_least64_t, 40> native_int40_t;
  127. typedef endian_arithmetic<order::native, int_least64_t, 48> native_int48_t;
  128. typedef endian_arithmetic<order::native, int_least64_t, 56> native_int56_t;
  129. typedef endian_arithmetic<order::native, int_least64_t, 64> native_int64_t;
  130. // native endian unsigned integer unaligned types
  131. typedef endian_arithmetic<order::native, uint_least8_t, 8> native_uint8_t;
  132. typedef endian_arithmetic<order::native, uint_least16_t, 16> native_uint16_t;
  133. typedef endian_arithmetic<order::native, uint_least32_t, 24> native_uint24_t;
  134. typedef endian_arithmetic<order::native, uint_least32_t, 32> native_uint32_t;
  135. typedef endian_arithmetic<order::native, uint_least64_t, 40> native_uint40_t;
  136. typedef endian_arithmetic<order::native, uint_least64_t, 48> native_uint48_t;
  137. typedef endian_arithmetic<order::native, uint_least64_t, 56> native_uint56_t;
  138. typedef endian_arithmetic<order::native, uint_least64_t, 64> native_uint64_t;
  139. // unaligned floating point types
  140. typedef endian_arithmetic<order::big, float, 32, align::no> big_float32_t;
  141. typedef endian_arithmetic<order::big, double, 64, align::no> big_float64_t;
  142. typedef endian_arithmetic<order::little, float, 32, align::no> little_float32_t;
  143. typedef endian_arithmetic<order::little, double, 64, align::no> little_float64_t;
  144. typedef endian_arithmetic<order::native, float, 32, align::no> native_float32_t;
  145. typedef endian_arithmetic<order::native, double, 64, align::no> native_float64_t;
  146. //---------------------------------- end synopsis ------------------------------------//
  147. template <BOOST_SCOPED_ENUM(order) Order, class T, std::size_t n_bits,
  148. BOOST_SCOPED_ENUM(align) Align>
  149. class endian_arithmetic
  150. {
  151. private:
  152. typedef endian_buffer<Order, T, n_bits, Align> buffer_type;
  153. #ifdef BOOST_ENDIAN_NO_CTORS
  154. public:
  155. #else
  156. private:
  157. #endif
  158. buffer_type buf_;
  159. public:
  160. typedef T value_type;
  161. #ifndef BOOST_ENDIAN_NO_CTORS
  162. endian_arithmetic() BOOST_ENDIAN_DEFAULT_CONSTRUCT
  163. BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic( T val ) BOOST_NOEXCEPT: buf_( val )
  164. {
  165. }
  166. #endif
  167. endian_arithmetic& operator=( T val ) BOOST_NOEXCEPT
  168. {
  169. buf_ = val;
  170. return *this;
  171. }
  172. value_type value() const BOOST_NOEXCEPT
  173. {
  174. return buf_.value();
  175. }
  176. unsigned char const * data() const BOOST_NOEXCEPT
  177. {
  178. return buf_.data();
  179. }
  180. unsigned char * data() BOOST_NOEXCEPT
  181. {
  182. return buf_.data();
  183. }
  184. operator value_type() const BOOST_NOEXCEPT
  185. {
  186. return this->value();
  187. }
  188. operator buffer_type& () BOOST_NOEXCEPT
  189. {
  190. return buf_;
  191. }
  192. operator buffer_type const& () BOOST_NOEXCEPT
  193. {
  194. return buf_;
  195. }
  196. // operators
  197. T operator+() const BOOST_NOEXCEPT
  198. {
  199. return this->value();
  200. }
  201. endian_arithmetic& operator+=( T y ) BOOST_NOEXCEPT
  202. {
  203. *this = static_cast<T>( this->value() + y );
  204. return *this;
  205. }
  206. endian_arithmetic& operator-=( T y ) BOOST_NOEXCEPT
  207. {
  208. *this = static_cast<T>( this->value() - y );
  209. return *this;
  210. }
  211. endian_arithmetic& operator*=( T y ) BOOST_NOEXCEPT
  212. {
  213. *this = static_cast<T>( this->value() * y );
  214. return *this;
  215. }
  216. endian_arithmetic& operator/=( T y ) BOOST_NOEXCEPT
  217. {
  218. *this = static_cast<T>( this->value() / y );
  219. return *this;
  220. }
  221. endian_arithmetic& operator%=( T y ) BOOST_NOEXCEPT
  222. {
  223. *this = static_cast<T>( this->value() % y );
  224. return *this;
  225. }
  226. endian_arithmetic& operator&=( T y ) BOOST_NOEXCEPT
  227. {
  228. *this = static_cast<T>( this->value() & y );
  229. return *this;
  230. }
  231. endian_arithmetic& operator|=( T y ) BOOST_NOEXCEPT
  232. {
  233. *this = static_cast<T>( this->value() | y );
  234. return *this;
  235. }
  236. endian_arithmetic& operator^=( T y ) BOOST_NOEXCEPT
  237. {
  238. *this = static_cast<T>( this->value() ^ y );
  239. return *this;
  240. }
  241. endian_arithmetic& operator<<=( T y ) BOOST_NOEXCEPT
  242. {
  243. *this = static_cast<T>( this->value() << y );
  244. return *this;
  245. }
  246. endian_arithmetic& operator>>=( T y ) BOOST_NOEXCEPT
  247. {
  248. *this = static_cast<T>( this->value() >> y );
  249. return *this;
  250. }
  251. endian_arithmetic& operator++() BOOST_NOEXCEPT
  252. {
  253. *this += 1;
  254. return *this;
  255. }
  256. endian_arithmetic& operator--() BOOST_NOEXCEPT
  257. {
  258. *this -= 1;
  259. return *this;
  260. }
  261. endian_arithmetic operator++(int) BOOST_NOEXCEPT
  262. {
  263. endian_arithmetic tmp( *this );
  264. *this += 1;
  265. return tmp;
  266. }
  267. endian_arithmetic operator--(int) BOOST_NOEXCEPT
  268. {
  269. endian_arithmetic tmp( *this );
  270. *this -= 1;
  271. return tmp;
  272. }
  273. template<class Ch, class Tr>
  274. friend std::basic_ostream<Ch, Tr>&
  275. operator<<( std::basic_ostream<Ch, Tr>& os, endian_arithmetic const& x )
  276. {
  277. return os << x.value();
  278. }
  279. template<class Ch, class Tr>
  280. friend std::basic_istream<Ch, Tr>&
  281. operator>>( std::basic_istream<Ch, Tr>& is, endian_arithmetic& x )
  282. {
  283. T i;
  284. if( is >> i )
  285. {
  286. x = i;
  287. }
  288. return is;
  289. }
  290. };
  291. } // namespace endian
  292. } // namespace boost
  293. #if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
  294. # pragma pack(pop)
  295. #endif
  296. #if defined(_MSC_VER)
  297. # pragma warning(pop)
  298. #endif
  299. #endif // BOOST_ENDIAN_ARITHMETIC_HPP