integer.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 && ( !defined __TI_COMPILER_VERSION__ || \
  80. ( __TI_COMPILER_VERSION__ >= 7000000 && !defined __TI_40BIT_LONG__ ) )
  81. template <> struct exact_signed_base_helper<sizeof(long)* CHAR_BIT> { typedef long exact; };
  82. template <> struct exact_unsigned_base_helper<sizeof(unsigned long)* CHAR_BIT> { typedef unsigned long exact; };
  83. #endif
  84. #if defined(BOOST_HAS_LONG_LONG) &&\
  85. ((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\
  86. (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\
  87. (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\
  88. (defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX)))
  89. template <> struct exact_signed_base_helper<sizeof(boost::long_long_type)* CHAR_BIT> { typedef boost::long_long_type exact; };
  90. template <> struct exact_unsigned_base_helper<sizeof(boost::ulong_long_type)* CHAR_BIT> { typedef boost::ulong_long_type exact; };
  91. #endif
  92. } // namespace detail
  93. // integer templates specifying number of bits ---------------------------//
  94. // signed
  95. template< int Bits > // bits (including sign) required
  96. struct int_t : public boost::detail::exact_signed_base_helper<Bits>
  97. {
  98. BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(boost::intmax_t) * CHAR_BIT),
  99. "No suitable signed integer type with the requested number of bits is available.");
  100. typedef typename boost::detail::int_least_helper
  101. <
  102. #ifdef BOOST_HAS_LONG_LONG
  103. (Bits <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
  104. #else
  105. 1 +
  106. #endif
  107. (Bits-1 <= ::std::numeric_limits<long>::digits) +
  108. (Bits-1 <= ::std::numeric_limits<int>::digits) +
  109. (Bits-1 <= ::std::numeric_limits<short>::digits) +
  110. (Bits-1 <= ::std::numeric_limits<signed char>::digits)
  111. >::least least;
  112. typedef typename int_fast_t<least>::type fast;
  113. };
  114. // unsigned
  115. template< int Bits > // bits required
  116. struct uint_t : public boost::detail::exact_unsigned_base_helper<Bits>
  117. {
  118. BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(boost::uintmax_t) * CHAR_BIT),
  119. "No suitable unsigned integer type with the requested number of bits is available.");
  120. #if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T)
  121. // It's really not clear why this workaround should be needed... shrug I guess! JM
  122. BOOST_STATIC_CONSTANT(int, s =
  123. 6 +
  124. (Bits <= ::std::numeric_limits<unsigned long>::digits) +
  125. (Bits <= ::std::numeric_limits<unsigned int>::digits) +
  126. (Bits <= ::std::numeric_limits<unsigned short>::digits) +
  127. (Bits <= ::std::numeric_limits<unsigned char>::digits));
  128. typedef typename detail::int_least_helper< ::boost::uint_t<Bits>::s>::least least;
  129. #else
  130. typedef typename boost::detail::uint_least_helper
  131. <
  132. #ifdef BOOST_HAS_LONG_LONG
  133. (Bits <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
  134. #else
  135. 1 +
  136. #endif
  137. (Bits <= ::std::numeric_limits<unsigned long>::digits) +
  138. (Bits <= ::std::numeric_limits<unsigned int>::digits) +
  139. (Bits <= ::std::numeric_limits<unsigned short>::digits) +
  140. (Bits <= ::std::numeric_limits<unsigned char>::digits)
  141. >::least least;
  142. #endif
  143. typedef typename int_fast_t<least>::type fast;
  144. // int_fast_t<> works correctly for unsigned too, in spite of the name.
  145. };
  146. // integer templates specifying extreme value ----------------------------//
  147. // signed
  148. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  149. template< boost::long_long_type MaxValue > // maximum value to require support
  150. #else
  151. template< long MaxValue > // maximum value to require support
  152. #endif
  153. struct int_max_value_t
  154. {
  155. typedef typename boost::detail::int_least_helper
  156. <
  157. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  158. (MaxValue <= ::boost::integer_traits<boost::long_long_type>::const_max) +
  159. #else
  160. 1 +
  161. #endif
  162. (MaxValue <= ::boost::integer_traits<long>::const_max) +
  163. (MaxValue <= ::boost::integer_traits<int>::const_max) +
  164. (MaxValue <= ::boost::integer_traits<short>::const_max) +
  165. (MaxValue <= ::boost::integer_traits<signed char>::const_max)
  166. >::least least;
  167. typedef typename int_fast_t<least>::type fast;
  168. };
  169. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  170. template< boost::long_long_type MinValue > // minimum value to require support
  171. #else
  172. template< long MinValue > // minimum value to require support
  173. #endif
  174. struct int_min_value_t
  175. {
  176. typedef typename boost::detail::int_least_helper
  177. <
  178. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  179. (MinValue >= ::boost::integer_traits<boost::long_long_type>::const_min) +
  180. #else
  181. 1 +
  182. #endif
  183. (MinValue >= ::boost::integer_traits<long>::const_min) +
  184. (MinValue >= ::boost::integer_traits<int>::const_min) +
  185. (MinValue >= ::boost::integer_traits<short>::const_min) +
  186. (MinValue >= ::boost::integer_traits<signed char>::const_min)
  187. >::least least;
  188. typedef typename int_fast_t<least>::type fast;
  189. };
  190. // unsigned
  191. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  192. template< boost::ulong_long_type MaxValue > // minimum value to require support
  193. #else
  194. template< unsigned long MaxValue > // minimum value to require support
  195. #endif
  196. struct uint_value_t
  197. {
  198. #if (defined(__BORLANDC__) || defined(__CODEGEAR__))
  199. // It's really not clear why this workaround should be needed... shrug I guess! JM
  200. #if defined(BOOST_NO_INTEGRAL_INT64_T)
  201. BOOST_STATIC_CONSTANT(unsigned, which =
  202. 1 +
  203. (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
  204. (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
  205. (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
  206. (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
  207. typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
  208. #else // BOOST_NO_INTEGRAL_INT64_T
  209. BOOST_STATIC_CONSTANT(unsigned, which =
  210. 1 +
  211. (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
  212. (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
  213. (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
  214. (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
  215. (MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
  216. typedef typename detail::uint_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
  217. #endif // BOOST_NO_INTEGRAL_INT64_T
  218. #else
  219. typedef typename boost::detail::uint_least_helper
  220. <
  221. #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
  222. (MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
  223. #else
  224. 1 +
  225. #endif
  226. (MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
  227. (MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
  228. (MaxValue <= ::boost::integer_traits<unsigned short>::const_max) +
  229. (MaxValue <= ::boost::integer_traits<unsigned char>::const_max)
  230. >::least least;
  231. #endif
  232. typedef typename int_fast_t<least>::type fast;
  233. };
  234. } // namespace boost
  235. #endif // BOOST_INTEGER_HPP