Chris@16: // boost integer.hpp header file -------------------------------------------// Chris@16: Chris@16: // Copyright Beman Dawes and Daryle Walker 1999. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/integer for documentation. Chris@16: Chris@16: // Revision History Chris@16: // 22 Sep 01 Added value-based integer templates. (Daryle Walker) Chris@16: // 01 Apr 01 Modified to use new header. (John Maddock) Chris@16: // 30 Jul 00 Add typename syntax fix (Jens Maurer) Chris@16: // 28 Aug 99 Initial version Chris@16: Chris@16: #ifndef BOOST_INTEGER_HPP Chris@16: #define BOOST_INTEGER_HPP Chris@16: Chris@16: #include // self include Chris@16: Chris@16: #include // for boost::::boost::integer_traits Chris@16: #include // for ::std::numeric_limits Chris@16: #include // for boost::int64_t and BOOST_NO_INTEGRAL_INT64_T Chris@16: #include Chris@16: Chris@16: // Chris@16: // We simply cannot include this header on gcc without getting copious warnings of the kind: Chris@16: // Chris@16: // boost/integer.hpp:77:30: warning: use of C99 long long integer constant Chris@16: // Chris@16: // And yet there is no other reasonable implementation, so we declare this a system header Chris@16: // to suppress these warnings. Chris@16: // Chris@16: #if defined(__GNUC__) && (__GNUC__ >= 4) Chris@16: #pragma GCC system_header Chris@16: #endif Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: // Helper templates ------------------------------------------------------// Chris@16: Chris@16: // fast integers from least integers Chris@16: // int_fast_t<> works correctly for unsigned too, in spite of the name. Chris@16: template< typename LeastInt > Chris@101: struct int_fast_t Chris@101: { Chris@101: typedef LeastInt fast; Chris@16: typedef fast type; Chris@16: }; // imps may specialize Chris@16: Chris@16: namespace detail{ Chris@16: Chris@101: // convert category to type Chris@16: template< int Category > struct int_least_helper {}; // default is empty Chris@16: template< int Category > struct uint_least_helper {}; // default is empty Chris@16: Chris@16: // specializatons: 1=long, 2=int, 3=short, 4=signed char, Chris@16: // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned char Chris@16: // no specializations for 0 and 5: requests for a type > long are in error Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template<> struct int_least_helper<1> { typedef boost::long_long_type least; }; Chris@16: #elif defined(BOOST_HAS_MS_INT64) Chris@16: template<> struct int_least_helper<1> { typedef __int64 least; }; Chris@16: #endif Chris@16: template<> struct int_least_helper<2> { typedef long least; }; Chris@16: template<> struct int_least_helper<3> { typedef int least; }; Chris@16: template<> struct int_least_helper<4> { typedef short least; }; Chris@16: template<> struct int_least_helper<5> { typedef signed char least; }; Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: template<> struct uint_least_helper<1> { typedef boost::ulong_long_type least; }; Chris@16: #elif defined(BOOST_HAS_MS_INT64) Chris@16: template<> struct uint_least_helper<1> { typedef unsigned __int64 least; }; Chris@16: #endif Chris@16: template<> struct uint_least_helper<2> { typedef unsigned long least; }; Chris@16: template<> struct uint_least_helper<3> { typedef unsigned int least; }; Chris@16: template<> struct uint_least_helper<4> { typedef unsigned short least; }; Chris@16: template<> struct uint_least_helper<5> { typedef unsigned char least; }; Chris@16: Chris@16: template Chris@16: struct exact_signed_base_helper{}; Chris@16: template Chris@16: struct exact_unsigned_base_helper{}; Chris@16: Chris@16: template <> struct exact_signed_base_helper { typedef signed char exact; }; Chris@16: template <> struct exact_unsigned_base_helper { typedef unsigned char exact; }; Chris@16: #if USHRT_MAX != UCHAR_MAX Chris@16: template <> struct exact_signed_base_helper { typedef short exact; }; Chris@16: template <> struct exact_unsigned_base_helper { typedef unsigned short exact; }; Chris@16: #endif Chris@16: #if UINT_MAX != USHRT_MAX Chris@16: template <> struct exact_signed_base_helper { typedef int exact; }; Chris@16: template <> struct exact_unsigned_base_helper { typedef unsigned int exact; }; Chris@16: #endif Chris@101: #if ULONG_MAX != UINT_MAX && ( !defined __TI_COMPILER_VERSION__ || \ Chris@101: ( __TI_COMPILER_VERSION__ >= 7000000 && !defined __TI_40BIT_LONG__ ) ) Chris@16: template <> struct exact_signed_base_helper { typedef long exact; }; Chris@16: template <> struct exact_unsigned_base_helper { typedef unsigned long exact; }; Chris@16: #endif Chris@16: #if defined(BOOST_HAS_LONG_LONG) &&\ Chris@16: ((defined(ULLONG_MAX) && (ULLONG_MAX != ULONG_MAX)) ||\ Chris@16: (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX != ULONG_MAX)) ||\ Chris@16: (defined(ULONGLONG_MAX) && (ULONGLONG_MAX != ULONG_MAX)) ||\ Chris@16: (defined(_ULLONG_MAX) && (_ULLONG_MAX != ULONG_MAX))) Chris@16: template <> struct exact_signed_base_helper { typedef boost::long_long_type exact; }; Chris@16: template <> struct exact_unsigned_base_helper { typedef boost::ulong_long_type exact; }; Chris@16: #endif Chris@16: Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: // integer templates specifying number of bits ---------------------------// Chris@16: Chris@16: // signed Chris@16: template< int Bits > // bits (including sign) required Chris@101: struct int_t : public boost::detail::exact_signed_base_helper Chris@16: { Chris@16: BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(boost::intmax_t) * CHAR_BIT), Chris@16: "No suitable signed integer type with the requested number of bits is available."); Chris@101: typedef typename boost::detail::int_least_helper Chris@16: < Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: (Bits <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) + Chris@16: #else Chris@16: 1 + Chris@16: #endif Chris@16: (Bits-1 <= ::std::numeric_limits::digits) + Chris@16: (Bits-1 <= ::std::numeric_limits::digits) + Chris@16: (Bits-1 <= ::std::numeric_limits::digits) + Chris@16: (Bits-1 <= ::std::numeric_limits::digits) Chris@16: >::least least; Chris@16: typedef typename int_fast_t::type fast; Chris@16: }; Chris@16: Chris@16: // unsigned Chris@16: template< int Bits > // bits required Chris@101: struct uint_t : public boost::detail::exact_unsigned_base_helper Chris@16: { Chris@16: BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(boost::uintmax_t) * CHAR_BIT), Chris@16: "No suitable unsigned integer type with the requested number of bits is available."); Chris@16: #if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T) Chris@16: // It's really not clear why this workaround should be needed... shrug I guess! JM Chris@101: BOOST_STATIC_CONSTANT(int, s = Chris@16: 6 + Chris@16: (Bits <= ::std::numeric_limits::digits) + Chris@16: (Bits <= ::std::numeric_limits::digits) + Chris@16: (Bits <= ::std::numeric_limits::digits) + Chris@16: (Bits <= ::std::numeric_limits::digits)); Chris@16: typedef typename detail::int_least_helper< ::boost::uint_t::s>::least least; Chris@16: #else Chris@101: typedef typename boost::detail::uint_least_helper Chris@101: < Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: (Bits <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) + Chris@16: #else Chris@16: 1 + Chris@16: #endif Chris@16: (Bits <= ::std::numeric_limits::digits) + Chris@16: (Bits <= ::std::numeric_limits::digits) + Chris@16: (Bits <= ::std::numeric_limits::digits) + Chris@16: (Bits <= ::std::numeric_limits::digits) Chris@16: >::least least; Chris@16: #endif Chris@16: typedef typename int_fast_t::type fast; Chris@16: // int_fast_t<> works correctly for unsigned too, in spite of the name. Chris@16: }; Chris@16: Chris@16: // integer templates specifying extreme value ----------------------------// Chris@16: Chris@16: // signed Chris@101: #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) && defined(BOOST_HAS_LONG_LONG) Chris@16: template< boost::long_long_type MaxValue > // maximum value to require support Chris@16: #else Chris@16: template< long MaxValue > // maximum value to require support Chris@16: #endif Chris@101: struct int_max_value_t Chris@16: { Chris@101: typedef typename boost::detail::int_least_helper Chris@16: < Chris@101: #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) && defined(BOOST_HAS_LONG_LONG) Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: #else Chris@16: 1 + Chris@16: #endif Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) Chris@16: >::least least; Chris@16: typedef typename int_fast_t::type fast; Chris@16: }; Chris@16: Chris@101: #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) && defined(BOOST_HAS_LONG_LONG) Chris@16: template< boost::long_long_type MinValue > // minimum value to require support Chris@16: #else Chris@16: template< long MinValue > // minimum value to require support Chris@16: #endif Chris@101: struct int_min_value_t Chris@16: { Chris@101: typedef typename boost::detail::int_least_helper Chris@16: < Chris@101: #if !defined(BOOST_NO_INTEGRAL_INT64_T) && !defined(BOOST_NO_INT64_T) && defined(BOOST_HAS_LONG_LONG) Chris@16: (MinValue >= ::boost::integer_traits::const_min) + Chris@16: #else Chris@16: 1 + Chris@16: #endif Chris@16: (MinValue >= ::boost::integer_traits::const_min) + Chris@16: (MinValue >= ::boost::integer_traits::const_min) + Chris@16: (MinValue >= ::boost::integer_traits::const_min) + Chris@16: (MinValue >= ::boost::integer_traits::const_min) Chris@16: >::least least; Chris@16: typedef typename int_fast_t::type fast; Chris@16: }; Chris@16: Chris@16: // unsigned Chris@16: #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG) Chris@16: template< boost::ulong_long_type MaxValue > // minimum value to require support Chris@16: #else Chris@16: template< unsigned long MaxValue > // minimum value to require support Chris@16: #endif Chris@101: struct uint_value_t Chris@16: { Chris@16: #if (defined(__BORLANDC__) || defined(__CODEGEAR__)) Chris@16: // It's really not clear why this workaround should be needed... shrug I guess! JM Chris@16: #if defined(BOOST_NO_INTEGRAL_INT64_T) Chris@101: BOOST_STATIC_CONSTANT(unsigned, which = Chris@16: 1 + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max)); Chris@16: typedef typename detail::int_least_helper< ::boost::uint_value_t::which>::least least; Chris@16: #else // BOOST_NO_INTEGRAL_INT64_T Chris@101: BOOST_STATIC_CONSTANT(unsigned, which = Chris@16: 1 + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max)); Chris@16: typedef typename detail::uint_least_helper< ::boost::uint_value_t::which>::least least; Chris@16: #endif // BOOST_NO_INTEGRAL_INT64_T Chris@16: #else Chris@101: typedef typename boost::detail::uint_least_helper Chris@101: < Chris@16: #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG) Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: #else Chris@16: 1 + Chris@16: #endif Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) + Chris@16: (MaxValue <= ::boost::integer_traits::const_max) Chris@16: >::least least; Chris@16: #endif Chris@16: typedef typename int_fast_t::type fast; Chris@16: }; Chris@16: Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_INTEGER_HPP