integer.hpp 11 KB

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