arithmetic.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/buffers.hpp>
  22. #include <boost/endian/detail/static_assert.hpp>
  23. #include <boost/config.hpp>
  24. #include <cstdint>
  25. #include <iosfwd>
  26. #include <climits>
  27. #if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
  28. # pragma pack(push, 1)
  29. #endif
  30. # if CHAR_BIT != 8
  31. # error Platforms with CHAR_BIT != 8 are not supported
  32. # endif
  33. # ifndef BOOST_ENDIAN_EXPLICIT_CTORS
  34. # define BOOST_ENDIAN_EXPLICIT_OPT
  35. # else
  36. # define BOOST_ENDIAN_EXPLICIT_OPT explicit
  37. # endif
  38. //---------------------------------- synopsis ----------------------------------------//
  39. namespace boost
  40. {
  41. namespace endian
  42. {
  43. template <order Order, class T, std::size_t n_bits,
  44. align Align = align::no>
  45. class endian_arithmetic;
  46. // big endian signed integer aligned types
  47. typedef endian_arithmetic<order::big, std::int8_t, 8, align::yes> big_int8_at;
  48. typedef endian_arithmetic<order::big, std::int16_t, 16, align::yes> big_int16_at;
  49. typedef endian_arithmetic<order::big, std::int32_t, 32, align::yes> big_int32_at;
  50. typedef endian_arithmetic<order::big, std::int64_t, 64, align::yes> big_int64_at;
  51. // big endian unsigned integer aligned types
  52. typedef endian_arithmetic<order::big, std::uint8_t, 8, align::yes> big_uint8_at;
  53. typedef endian_arithmetic<order::big, std::uint16_t, 16, align::yes> big_uint16_at;
  54. typedef endian_arithmetic<order::big, std::uint32_t, 32, align::yes> big_uint32_at;
  55. typedef endian_arithmetic<order::big, std::uint64_t, 64, align::yes> big_uint64_at;
  56. // little endian signed integer aligned types
  57. typedef endian_arithmetic<order::little, std::int8_t, 8, align::yes> little_int8_at;
  58. typedef endian_arithmetic<order::little, std::int16_t, 16, align::yes> little_int16_at;
  59. typedef endian_arithmetic<order::little, std::int32_t, 32, align::yes> little_int32_at;
  60. typedef endian_arithmetic<order::little, std::int64_t, 64, align::yes> little_int64_at;
  61. // little endian unsigned integer aligned types
  62. typedef endian_arithmetic<order::little, std::uint8_t, 8, align::yes> little_uint8_at;
  63. typedef endian_arithmetic<order::little, std::uint16_t, 16, align::yes> little_uint16_at;
  64. typedef endian_arithmetic<order::little, std::uint32_t, 32, align::yes> little_uint32_at;
  65. typedef endian_arithmetic<order::little, std::uint64_t, 64, align::yes> little_uint64_at;
  66. // aligned floating point types
  67. typedef endian_arithmetic<order::big, float, 32, align::yes> big_float32_at;
  68. typedef endian_arithmetic<order::big, double, 64, align::yes> big_float64_at;
  69. typedef endian_arithmetic<order::little, float, 32, align::yes> little_float32_at;
  70. typedef endian_arithmetic<order::little, double, 64, align::yes> little_float64_at;
  71. // aligned native endian typedefs are not provided because
  72. // <cstdint> types are superior for this use case
  73. // big endian signed integer unaligned types
  74. typedef endian_arithmetic<order::big, std::int_least8_t, 8> big_int8_t;
  75. typedef endian_arithmetic<order::big, std::int_least16_t, 16> big_int16_t;
  76. typedef endian_arithmetic<order::big, std::int_least32_t, 24> big_int24_t;
  77. typedef endian_arithmetic<order::big, std::int_least32_t, 32> big_int32_t;
  78. typedef endian_arithmetic<order::big, std::int_least64_t, 40> big_int40_t;
  79. typedef endian_arithmetic<order::big, std::int_least64_t, 48> big_int48_t;
  80. typedef endian_arithmetic<order::big, std::int_least64_t, 56> big_int56_t;
  81. typedef endian_arithmetic<order::big, std::int_least64_t, 64> big_int64_t;
  82. // big endian unsigned integer unaligned types
  83. typedef endian_arithmetic<order::big, std::uint_least8_t, 8> big_uint8_t;
  84. typedef endian_arithmetic<order::big, std::uint_least16_t, 16> big_uint16_t;
  85. typedef endian_arithmetic<order::big, std::uint_least32_t, 24> big_uint24_t;
  86. typedef endian_arithmetic<order::big, std::uint_least32_t, 32> big_uint32_t;
  87. typedef endian_arithmetic<order::big, std::uint_least64_t, 40> big_uint40_t;
  88. typedef endian_arithmetic<order::big, std::uint_least64_t, 48> big_uint48_t;
  89. typedef endian_arithmetic<order::big, std::uint_least64_t, 56> big_uint56_t;
  90. typedef endian_arithmetic<order::big, std::uint_least64_t, 64> big_uint64_t;
  91. // little endian signed integer unaligned types
  92. typedef endian_arithmetic<order::little, std::int_least8_t, 8> little_int8_t;
  93. typedef endian_arithmetic<order::little, std::int_least16_t, 16> little_int16_t;
  94. typedef endian_arithmetic<order::little, std::int_least32_t, 24> little_int24_t;
  95. typedef endian_arithmetic<order::little, std::int_least32_t, 32> little_int32_t;
  96. typedef endian_arithmetic<order::little, std::int_least64_t, 40> little_int40_t;
  97. typedef endian_arithmetic<order::little, std::int_least64_t, 48> little_int48_t;
  98. typedef endian_arithmetic<order::little, std::int_least64_t, 56> little_int56_t;
  99. typedef endian_arithmetic<order::little, std::int_least64_t, 64> little_int64_t;
  100. // little endian unsigned integer unaligned types
  101. typedef endian_arithmetic<order::little, std::uint_least8_t, 8> little_uint8_t;
  102. typedef endian_arithmetic<order::little, std::uint_least16_t, 16> little_uint16_t;
  103. typedef endian_arithmetic<order::little, std::uint_least32_t, 24> little_uint24_t;
  104. typedef endian_arithmetic<order::little, std::uint_least32_t, 32> little_uint32_t;
  105. typedef endian_arithmetic<order::little, std::uint_least64_t, 40> little_uint40_t;
  106. typedef endian_arithmetic<order::little, std::uint_least64_t, 48> little_uint48_t;
  107. typedef endian_arithmetic<order::little, std::uint_least64_t, 56> little_uint56_t;
  108. typedef endian_arithmetic<order::little, std::uint_least64_t, 64> little_uint64_t;
  109. // native endian signed integer unaligned types
  110. typedef endian_arithmetic<order::native, std::int_least8_t, 8> native_int8_t;
  111. typedef endian_arithmetic<order::native, std::int_least16_t, 16> native_int16_t;
  112. typedef endian_arithmetic<order::native, std::int_least32_t, 24> native_int24_t;
  113. typedef endian_arithmetic<order::native, std::int_least32_t, 32> native_int32_t;
  114. typedef endian_arithmetic<order::native, std::int_least64_t, 40> native_int40_t;
  115. typedef endian_arithmetic<order::native, std::int_least64_t, 48> native_int48_t;
  116. typedef endian_arithmetic<order::native, std::int_least64_t, 56> native_int56_t;
  117. typedef endian_arithmetic<order::native, std::int_least64_t, 64> native_int64_t;
  118. // native endian unsigned integer unaligned types
  119. typedef endian_arithmetic<order::native, std::uint_least8_t, 8> native_uint8_t;
  120. typedef endian_arithmetic<order::native, std::uint_least16_t, 16> native_uint16_t;
  121. typedef endian_arithmetic<order::native, std::uint_least32_t, 24> native_uint24_t;
  122. typedef endian_arithmetic<order::native, std::uint_least32_t, 32> native_uint32_t;
  123. typedef endian_arithmetic<order::native, std::uint_least64_t, 40> native_uint40_t;
  124. typedef endian_arithmetic<order::native, std::uint_least64_t, 48> native_uint48_t;
  125. typedef endian_arithmetic<order::native, std::uint_least64_t, 56> native_uint56_t;
  126. typedef endian_arithmetic<order::native, std::uint_least64_t, 64> native_uint64_t;
  127. // unaligned floating point types
  128. typedef endian_arithmetic<order::big, float, 32, align::no> big_float32_t;
  129. typedef endian_arithmetic<order::big, double, 64, align::no> big_float64_t;
  130. typedef endian_arithmetic<order::little, float, 32, align::no> little_float32_t;
  131. typedef endian_arithmetic<order::little, double, 64, align::no> little_float64_t;
  132. typedef endian_arithmetic<order::native, float, 32, align::no> native_float32_t;
  133. typedef endian_arithmetic<order::native, double, 64, align::no> native_float64_t;
  134. //---------------------------------- end synopsis ------------------------------------//
  135. template <order Order, class T, std::size_t n_bits,
  136. align Align>
  137. class endian_arithmetic
  138. {
  139. private:
  140. typedef endian_buffer<Order, T, n_bits, Align> buffer_type;
  141. #ifdef BOOST_ENDIAN_NO_CTORS
  142. public:
  143. #else
  144. private:
  145. #endif
  146. buffer_type buf_;
  147. public:
  148. typedef T value_type;
  149. #ifndef BOOST_ENDIAN_NO_CTORS
  150. endian_arithmetic() = default;
  151. BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic( T val ) BOOST_NOEXCEPT: buf_( val )
  152. {
  153. }
  154. #endif
  155. endian_arithmetic& operator=( T val ) BOOST_NOEXCEPT
  156. {
  157. buf_ = val;
  158. return *this;
  159. }
  160. value_type value() const BOOST_NOEXCEPT
  161. {
  162. return buf_.value();
  163. }
  164. unsigned char const * data() const BOOST_NOEXCEPT
  165. {
  166. return buf_.data();
  167. }
  168. unsigned char * data() BOOST_NOEXCEPT
  169. {
  170. return buf_.data();
  171. }
  172. operator value_type() const BOOST_NOEXCEPT
  173. {
  174. return this->value();
  175. }
  176. operator buffer_type& () BOOST_NOEXCEPT
  177. {
  178. return buf_;
  179. }
  180. operator buffer_type const& () BOOST_NOEXCEPT
  181. {
  182. return buf_;
  183. }
  184. // operators
  185. T operator+() const BOOST_NOEXCEPT
  186. {
  187. return this->value();
  188. }
  189. endian_arithmetic& operator+=( T y ) BOOST_NOEXCEPT
  190. {
  191. *this = static_cast<T>( this->value() + y );
  192. return *this;
  193. }
  194. endian_arithmetic& operator-=( T y ) BOOST_NOEXCEPT
  195. {
  196. *this = static_cast<T>( this->value() - y );
  197. return *this;
  198. }
  199. endian_arithmetic& operator*=( T y ) BOOST_NOEXCEPT
  200. {
  201. *this = static_cast<T>( this->value() * y );
  202. return *this;
  203. }
  204. endian_arithmetic& operator/=( T y ) BOOST_NOEXCEPT
  205. {
  206. *this = static_cast<T>( this->value() / y );
  207. return *this;
  208. }
  209. endian_arithmetic& operator%=( T y ) BOOST_NOEXCEPT
  210. {
  211. *this = static_cast<T>( this->value() % y );
  212. return *this;
  213. }
  214. endian_arithmetic& operator&=( T y ) BOOST_NOEXCEPT
  215. {
  216. *this = static_cast<T>( this->value() & y );
  217. return *this;
  218. }
  219. endian_arithmetic& operator|=( T y ) BOOST_NOEXCEPT
  220. {
  221. *this = static_cast<T>( this->value() | y );
  222. return *this;
  223. }
  224. endian_arithmetic& operator^=( T y ) BOOST_NOEXCEPT
  225. {
  226. *this = static_cast<T>( this->value() ^ y );
  227. return *this;
  228. }
  229. endian_arithmetic& operator<<=( T y ) BOOST_NOEXCEPT
  230. {
  231. *this = static_cast<T>( this->value() << y );
  232. return *this;
  233. }
  234. endian_arithmetic& operator>>=( T y ) BOOST_NOEXCEPT
  235. {
  236. *this = static_cast<T>( this->value() >> y );
  237. return *this;
  238. }
  239. endian_arithmetic& operator++() BOOST_NOEXCEPT
  240. {
  241. *this += 1;
  242. return *this;
  243. }
  244. endian_arithmetic& operator--() BOOST_NOEXCEPT
  245. {
  246. *this -= 1;
  247. return *this;
  248. }
  249. endian_arithmetic operator++(int) BOOST_NOEXCEPT
  250. {
  251. endian_arithmetic tmp( *this );
  252. *this += 1;
  253. return tmp;
  254. }
  255. endian_arithmetic operator--(int) BOOST_NOEXCEPT
  256. {
  257. endian_arithmetic tmp( *this );
  258. *this -= 1;
  259. return tmp;
  260. }
  261. template<class Ch, class Tr>
  262. friend std::basic_ostream<Ch, Tr>&
  263. operator<<( std::basic_ostream<Ch, Tr>& os, endian_arithmetic const& x )
  264. {
  265. return os << x.value();
  266. }
  267. template<class Ch, class Tr>
  268. friend std::basic_istream<Ch, Tr>&
  269. operator>>( std::basic_istream<Ch, Tr>& is, endian_arithmetic& x )
  270. {
  271. T i;
  272. if( is >> i )
  273. {
  274. x = i;
  275. }
  276. return is;
  277. }
  278. };
  279. } // namespace endian
  280. } // namespace boost
  281. #if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
  282. # pragma pack(pop)
  283. #endif
  284. #if defined(_MSC_VER)
  285. # pragma warning(pop)
  286. #endif
  287. #endif // BOOST_ENDIAN_ARITHMETIC_HPP