integer.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // boost integer.hpp header file -------------------------------------------//
  2. // Copyright Beman Dawes and Daryle Walker 1999. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/integer for documentation.
  6. // Revision History
  7. // 22 Sep 01 Added value-based integer templates. (Daryle Walker)
  8. // 01 Apr 01 Modified to use new <boost/limits.hpp> header. (John Maddock)
  9. // 30 Jul 00 Add typename syntax fix (Jens Maurer)
  10. // 28 Aug 99 Initial version
  11. #ifndef BOOST_INTEGER_HPP
  12. #define BOOST_INTEGER_HPP
  13. #include <boost/integer_fwd.hpp> // self include
  14. #include <boost/integer_traits.hpp> // for boost::::boost::integer_traits
  15. #include <boost/limits.hpp> // for ::std::numeric_limits
  16. #include <boost/cstdint.hpp> // for boost::int64_t and BOOST_NO_INTEGRAL_INT64_T
  17. #include <boost/static_assert.hpp>
  18. //
  19. // We simply cannot include this header on gcc without getting copious warnings of the kind:
  20. //
  21. // boost/integer.hpp:77:30: warning: use of C99 long long integer constant
  22. //
  23. // And yet there is no other reasonable implementation, so we declare this a system header
  24. // to suppress these warnings.
  25. //
  26. #if defined(__GNUC__) && (__GNUC__ >= 4)
  27. #pragma GCC system_header
  28. #endif
  29. namespace boost
  30. {
  31. // Helper templates ------------------------------------------------------//
  32. // fast integers from least integers
  33. // int_fast_t<> works correctly for unsigned too, in spite of the name.
  34. template< typename LeastInt >
  35. struct int_fast_t
  36. {
  37. typedef LeastInt fast;
  38. typedef fast type;
  39. }; // imps may specialize
  40. namespace detail{
  41. // convert category to type
  42. template< int Category > struct int_least_helper {}; // default is empty
  43. template< int Category > struct uint_least_helper {}; // default is empty
  44. // specializatons: 1=long, 2=int, 3=short, 4=signed char,
  45. // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned char
  46. // no specializations for 0 and 5: requests for a type > long are in error
  47. #ifdef BOOST_HAS_LONG_LONG
  48. template<> struct int_least_helper<1> { typedef boost::long_long_type least; };
  49. #elif defined(BOOST_HAS_MS_INT64)
  50. template<> struct int_least_helper<1> { typedef __int64 least; };
  51. #endif
  52. template<> struct int_least_helper<2> { typedef long least; };
  53. template<> struct int_least_helper<3> { typedef int least; };
  54. template<> struct int_least_helper<4> { typedef short least; };
  55. template<> struct int_least_helper<5> { typedef signed char least; };
  56. #ifdef BOOST_HAS_LONG_LONG
  57. template<> struct uint_least_helper<1> { typedef boost::ulong_long_type least; };
  58. #elif defined(BOOST_HAS_MS_INT64)
  59. template<> struct uint_least_helper<1> { typedef unsigned __int64 least; };
  60. #endif
  61. template<> struct uint_least_helper<2> { typedef unsigned long least; };
  62. template<> struct uint_least_helper<3> { typedef unsigned int least; };
  63. template<> struct uint_least_helper<4> { typedef unsigned short least; };
  64. template<> struct uint_least_helper<5> { typedef unsigned char least; };
  65. template <int Bits>
  66. struct exact_signed_base_helper{};
  67. template <int Bits>
  68. struct exact_unsigned_base_helper{};
  69. template <> struct exact_signed_base_helper<sizeof(signed char)* CHAR_BIT> { typedef signed char exact; };
  70. template <> struct exact_unsigned_base_helper<sizeof(unsigned char)* CHAR_BIT> { typedef unsigned char exact; };
  71. #if USHRT_MAX != UCHAR_MAX
  72. template <> struct exact_signed_base_helper<sizeof(short)* CHAR_BIT> { typedef short exact; };
  73. template <> struct exact_unsigned_base_helper<sizeof(unsigned short)* CHAR_BIT> { typedef unsigned short exact; };
  74. #endif
  75. #if UINT_MAX != USHRT_MAX
  76. template <> struct exact_signed_base_helper<sizeof(int)* CHAR_BIT> { typedef int exact; };
  77. template <> struct exact_unsigned_base_helper<sizeof(unsigned int)* CHAR_BIT> { typedef unsigned int exact; };
  78. #endif
  79. #if ULONG_MAX != UINT_MAX
  80. template <> struct exact_signed_base_helper<sizeof(long)* CHAR_BIT> { typedef long exact; };
  81. template <> struct exact_unsigned_base_helper<sizeof(unsigned long)* CHAR_BIT> { typedef unsigned long exact; };
  82. #endif
  83. #if defined(BOOST_HAS_LONG_LONG) &&\
  84. ((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\
  85. (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\
  86. (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\
  87. (defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX)))
  88. template <> struct exact_signed_base_helper<sizeof(boost::long_long_type)* CHAR_BIT> { typedef boost::long_long_type exact; };
  89. template <> struct exact_unsigned_base_helper<sizeof(boost::ulong_long_type)* CHAR_BIT> { typedef boost::ulong_long_type exact; };
  90. #endif
  91. } // namespace detail
  92. // integer templates specifying number of bits ---------------------------//
  93. // signed
  94. template< int Bits > // bits (including sign) required
  95. struct int_t : public detail::exact_signed_base_helper<Bits>
  96. {
  97. BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(boost::intmax_t) * CHAR_BIT),
  98. "No suitable signed integer type with the requested number of bits is available.");
  99. typedef typename detail::int_least_helper
  100. <
  101. #ifdef BOOST_HAS_LONG_LONG
  102. (Bits <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
  103. #else
  104. 1 +
  105. #endif
  106. (Bits-1 <= ::std::numeric_limits<long>::digits) +
  107. (Bits-1 <= ::std::numeric_limits<int>::digits) +
  108. (Bits-1 <= ::std::numeric_limits<short>::digits) +
  109. (Bits-1 <= ::std::numeric_limits<signed char>::digits)
  110. >::least least;
  111. typedef typename int_fast_t<least>::type fast;
  112. };
  113. // unsigned
  114. template< int Bits > // bits required
  115. struct uint_t : public detail::exact_unsigned_base_helper<Bits>
  116. {
  117. BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(boost::uintmax_t) * CHAR_BIT),
  118. "No suitable unsigned integer type with the requested number of bits is available.");
  119. #if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T)
  120. // It's really not clear why this workaround should be needed... shrug I guess! JM
  121. BOOST_STATIC_CONSTANT(int, s =
  122. 6 +
  123. (Bits <= ::std::numeric_limits<unsigned long>::digits) +
  124. (Bits <= ::std::numeric_limits<unsigned int>::digits) +
  125. (Bits <= ::std::numeric_limits<unsigned short>::digits) +
  126. (Bits <= ::std::numeric_limits<unsigned char>::digits));
  127. typedef typename detail::int_least_helper< ::boost::uint_t<Bits>::s>::least least;
  128. #else
  129. typedef typename detail::uint_least_helper
  130. <
  131. #ifdef BOOST_HAS_LONG_LONG
  132. (Bits <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
  133. #else
  134. 1 +
  135. #endif
  136. (Bits <= ::std::numeric_limits<unsigned long>::digits) +
  137. (Bits <= ::std::numeric_limits<unsigned int>::digits) +
  138. (Bits <= ::std::numeric_limits<unsigned short>::digits) +
  139. (Bits <= ::std::numeric_limits<unsigned char>::digits)
  140. >::least least;
  141. #endif
  142. typedef typename int_fast_t<least>::type fast;
  143. // int_fast_t<> works correctly for unsigned too, in spite of the name.
  144. };
  145. // integer templates specifying extreme value ----------------------------//
  146. // signed
  147. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  148. template< boost::long_long_type MaxValue > // maximum value to require support
  149. #else
  150. template< long MaxValue > // maximum value to require support
  151. #endif
  152. struct int_max_value_t
  153. {
  154. typedef typename detail::int_least_helper
  155. <
  156. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  157. (MaxValue <= ::boost::integer_traits<boost::long_long_type>::const_max) +
  158. #else
  159. 1 +
  160. #endif
  161. (MaxValue <= ::boost::integer_traits<long>::const_max) +
  162. (MaxValue <= ::boost::integer_traits<int>::const_max) +
  163. (MaxValue <= ::boost::integer_traits<short>::const_max) +
  164. (MaxValue <= ::boost::integer_traits<signed char>::const_max)
  165. >::least least;
  166. typedef typename int_fast_t<least>::type fast;
  167. };
  168. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  169. template< boost::long_long_type MinValue > // minimum value to require support
  170. #else
  171. template< long MinValue > // minimum value to require support
  172. #endif
  173. struct int_min_value_t
  174. {
  175. typedef typename detail::int_least_helper
  176. <
  177. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  178. (MinValue >= ::boost::integer_traits<boost::long_long_type>::const_min) +
  179. #else
  180. 1 +
  181. #endif
  182. (MinValue >= ::boost::integer_traits<long>::const_min) +
  183. (MinValue >= ::boost::integer_traits<int>::const_min) +
  184. (MinValue >= ::boost::integer_traits<short>::const_min) +
  185. (MinValue >= ::boost::integer_traits<signed char>::const_min)
  186. >::least least;
  187. typedef typename int_fast_t<least>::type fast;
  188. };
  189. // unsigned
  190. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  191. template< boost::ulong_long_type MaxValue > // minimum value to require support
  192. #else
  193. template< unsigned long MaxValue > // minimum value to require support
  194. #endif
  195. struct uint_value_t
  196. {
  197. #if (defined(__BORLANDC__) || defined(__CODEGEAR__))
  198. // It's really not clear why this workaround should be needed... shrug I guess! JM
  199. #if defined(BOOST_NO_INTEGRAL_INT64_T)
  200. BOOST_STATIC_CONSTANT(unsigned, which =
  201. 1 +
  202. (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
  203. (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
  204. (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
  205. (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
  206. typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
  207. #else // BOOST_NO_INTEGRAL_INT64_T
  208. BOOST_STATIC_CONSTANT(unsigned, which =
  209. 1 +
  210. (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
  211. (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
  212. (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
  213. (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
  214. (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
  215. typedef typename detail::uint_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
  216. #endif // BOOST_NO_INTEGRAL_INT64_T
  217. #else
  218. typedef typename detail::uint_least_helper
  219. <
  220. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  221. (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
  222. #else
  223. 1 +
  224. #endif
  225. (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
  226. (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
  227. (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
  228. (MaxValue <= ::boost::integer_traits<unsigned char>::const_max)
  229. >::least least;
  230. #endif
  231. typedef typename int_fast_t<least>::type fast;
  232. };
  233. } // namespace boost
  234. #endif // BOOST_INTEGER_HPP