From 053be71261effcd8b0e9cf7be0d3e704f76a88a3 Mon Sep 17 00:00:00 2001 From: Daryle Walker Date: Mon, 14 Jul 2008 06:28:54 +0000 Subject: [PATCH] Added extended-integer support, which fixes #653 (the main part; the secondary part is split off as #1225) [SVN r47414] --- include/boost/integer.hpp | 98 +++++++++++++++++++++++++++-------- include/boost/integer_fwd.hpp | 18 +++++-- integer.htm | 19 ++++--- test/integer_test.cpp | 57 ++++++++++++-------- 4 files changed, 135 insertions(+), 57 deletions(-) diff --git a/include/boost/integer.hpp b/include/boost/integer.hpp index 256fa27..cb285f8 100644 --- a/include/boost/integer.hpp +++ b/include/boost/integer.hpp @@ -7,6 +7,7 @@ // See http://www.boost.org/libs/integer for documentation. // Revision History +// 14 Jul 08 Added extended-integer support. (Daryle Walker) // 13 Jul 08 Redid implmentation. (Daryle Walker) // 22 Sep 01 Added value-based integer templates. (Daryle Walker) // 01 Apr 01 Modified to use new header. (John Maddock) @@ -18,7 +19,8 @@ #include // self include -#include // for BOOST_STATIC_CONSTANT +#include // for BOOST_STATIC_CONSTANT, etc. +#include // for boost::uintmax_t, intmax_t #include // for boost::integer_traits #include // for std::numeric_limits @@ -40,45 +42,74 @@ namespace detail // convert integer category to type ; default is empty template< int Rank, typename Signedness > struct int_least_helper {}; - // specializatons: 1=(unsigned) long, 2=unsigned/int, 3=(unsigned) short, - // 4=(un)signed char - // no specializations for 0: requests for a type > (unsigned) long are in - // error - template<> struct int_least_helper<1, signed> { typedef long least; }; + // specializatons: 1=(unsigned) __int64/long long, 2=(unsigned) long, + // 3=unsigned/int, 4=(unsigned) short, 5=(un)signed char + // no specializations for 0: requests for a type > (unsigned) (long) long are + // in error +#ifdef BOOST_HAS_LONG_LONG + template<> struct int_least_helper<1, signed> + { typedef long_long_type least; }; template<> struct int_least_helper<1, unsigned> - { typedef unsigned long least; }; - template<> struct int_least_helper<2, signed> { typedef int least; }; + { typedef ulong_long_type least; }; +#elif defined(BOOST_HAS_MS_INT64) + template<> struct int_least_helper<1, signed> { typedef __int64 least; }; + template<> struct int_least_helper<1, unsigned> + { typedef unsigned __int64 least; }; +#endif + template<> struct int_least_helper<2, signed> { typedef long least; }; template<> struct int_least_helper<2, unsigned> - { typedef unsigned int least; }; - template<> struct int_least_helper<3, signed> { typedef short least; }; + { typedef unsigned long least; }; + template<> struct int_least_helper<3, signed> { typedef int least; }; template<> struct int_least_helper<3, unsigned> - { typedef unsigned short least; }; - template<> struct int_least_helper<4, signed> { typedef signed char least; }; + { typedef unsigned int least; }; + template<> struct int_least_helper<4, signed> { typedef short least; }; template<> struct int_least_helper<4, unsigned> + { typedef unsigned short least; }; + template<> struct int_least_helper<5, signed> { typedef signed char least; }; + template<> struct int_least_helper<5, unsigned> { typedef unsigned char least; }; // category bounds enum { +#if defined(BOOST_HAS_LONG_LONG) || defined(BOOST_HAS_MS_INT64) lowest_integral_rank = 1, - highest_integral_rank = 4 +#else + lowest_integral_rank = 2, +#endif + highest_integral_rank = 5 }; // map a bit count to a category template < int BitsIncludingSign > struct int_rank_helper { + BOOST_STATIC_CONSTANT( int, mantissa = BitsIncludingSign - 1 ); BOOST_STATIC_CONSTANT( int, rank = - (BitsIncludingSign - 1 <= std::numeric_limits< long >::digits) + - (BitsIncludingSign - 1 <= std::numeric_limits< int >::digits) + - (BitsIncludingSign - 1 <= std::numeric_limits< short >::digits) + - (BitsIncludingSign - 1 <= std::numeric_limits< signed char >::digits) ); +#ifdef BOOST_HAS_LONG_LONG + (mantissa <= std::numeric_limits< long_long_type >::digits) + +#elif defined(BOOST_HAS_MS_INT64) + (mantissa <= std::numeric_limits< __int64 >::digits) + +#else + 1 + +#endif + (mantissa <= std::numeric_limits< long >::digits) + + (mantissa <= std::numeric_limits< int >::digits) + + (mantissa <= std::numeric_limits< short >::digits) + + (mantissa <= std::numeric_limits< signed char >::digits) ); }; template < int Bits > struct uint_rank_helper { BOOST_STATIC_CONSTANT( int, rank = +#ifdef BOOST_HAS_LONG_LONG + (Bits <= std::numeric_limits< ulong_long_type >::digits) + +#elif defined(BOOST_HAS_MS_INT64) + (Bits <= std::numeric_limits< unsigned __int64 >::digits) + +#else + 1 + +#endif (Bits <= std::numeric_limits< unsigned long >::digits) + (Bits <= std::numeric_limits< unsigned int >::digits) + (Bits <= std::numeric_limits< unsigned short >::digits) + @@ -86,30 +117,51 @@ namespace detail }; // map an extreme value to a category - template < long MaxValue > + template < intmax_t MaxValue > struct int_max_rank_helper { BOOST_STATIC_CONSTANT( int, rank = +#ifdef BOOST_HAS_LONG_LONG + (MaxValue <= integer_traits< long_long_type >::const_max) + +#elif defined(BOOST_HAS_MS_INT64) + (MaxValue <= integer_traits< __int64 >::const_max) + +#else + 1 + +#endif (MaxValue <= integer_traits< long >::const_max) + (MaxValue <= integer_traits< int >::const_max) + (MaxValue <= integer_traits< short >::const_max) + (MaxValue <= integer_traits< signed char >::const_max) ); }; - template < long MinValue > + template < intmax_t MinValue > struct int_min_rank_helper { BOOST_STATIC_CONSTANT( int, rank = +#ifdef BOOST_HAS_LONG_LONG + (MinValue >= integer_traits< long_long_type >::const_min) + +#elif defined(BOOST_HAS_MS_INT64) + (MinValue >= integer_traits< __int64 >::const_min) + +#else + 1 + +#endif (MinValue >= integer_traits< long >::const_min) + (MinValue >= integer_traits< int >::const_min) + (MinValue >= integer_traits< short >::const_min) + (MinValue >= integer_traits< signed char >::const_min) ); }; - template < unsigned long Value > + template < uintmax_t Value > struct uint_max_rank_helper { BOOST_STATIC_CONSTANT( int, rank = +#ifdef BOOST_HAS_LONG_LONG + (Value <= integer_traits< ulong_long_type >::const_max) + +#elif defined(BOOST_HAS_MS_INT64) + (Value <= integer_traits< unsigned __int64 >::const_max) + +#else + 1 + +#endif (Value <= integer_traits< unsigned long >::const_max) + (Value <= integer_traits< unsigned int >::const_max) + (Value <= integer_traits< unsigned short >::const_max) + @@ -146,7 +198,7 @@ namespace detail // integer templates specifying extreme value ----------------------------// // signed - template< long MaxValue > // maximum value to require support + template< intmax_t MaxValue > // maximum value to require support struct int_max_value_t { typedef typename detail::int_least_helper @@ -156,7 +208,7 @@ namespace detail typedef typename int_fast_t::fast fast; }; - template< long MinValue > // minimum value to require support + template< intmax_t MinValue > // minimum value to require support struct int_min_value_t { typedef typename detail::int_least_helper @@ -167,7 +219,7 @@ namespace detail }; // unsigned - template< unsigned long Value > // maximum value to require support + template< uintmax_t Value > // maximum value to require support struct uint_value_t { typedef typename detail::int_least_helper diff --git a/include/boost/integer_fwd.hpp b/include/boost/integer_fwd.hpp index 33cfc99..312cd7b 100644 --- a/include/boost/integer_fwd.hpp +++ b/include/boost/integer_fwd.hpp @@ -12,8 +12,9 @@ #include // for UCHAR_MAX, etc. #include // for std::size_t -#include // for BOOST_NO_INTRINSIC_WCHAR_T -#include // for std::numeric_limits +#include // for BOOST_NO_INTRINSIC_WCHAR_T +#include // for boost::uintmax_t, intmax_t +#include // for std::numeric_limits namespace boost @@ -24,6 +25,13 @@ namespace boost // Only has typedefs or using statements, with #conditionals +// ALERT: the forward declarations of items in need items +// from this header. That means that cannot #include this +// forwarding header, to avoid infinite recursion! One day, maybe +// boost::uintmax_t and boost::intmax_t could be segregated into their own +// header file (which can't #include this header), will use +// that header, and could refer to . + // From -----------------------------------------// @@ -85,13 +93,13 @@ template< int Bits > template< int Bits > struct uint_t; -template< long MaxValue > +template< intmax_t MaxValue > struct int_max_value_t; -template< long MinValue > +template< intmax_t MinValue > struct int_min_value_t; -template< unsigned long Value > +template< uintmax_t Value > struct uint_value_t; diff --git a/integer.htm b/integer.htm index 3d3f9c5..11b2f5b 100644 --- a/integer.htm +++ b/integer.htm @@ -21,7 +21,7 @@ is particularly useful for solving generic programming problems.