Added exact-integer templates, which fixes #1225 (and completes #653)

[SVN r47434]
This commit is contained in:
Daryle Walker
2008-07-15 09:59:19 +00:00
parent 053be71261
commit 55e1796c7a
4 changed files with 526 additions and 59 deletions

View File

@ -7,6 +7,7 @@
// See http://www.boost.org/libs/integer for documentation.
// Revision History
// 15 Jul 08 Added exact-integer templates. (Daryle Walker)
// 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)
@ -24,6 +25,8 @@
#include <boost/integer_traits.hpp> // for boost::integer_traits
#include <boost/limits.hpp> // for std::numeric_limits
#include <climits> // for UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, etc.
namespace boost
{
@ -85,7 +88,7 @@ namespace detail
struct int_rank_helper
{
BOOST_STATIC_CONSTANT( int, mantissa = BitsIncludingSign - 1 );
BOOST_STATIC_CONSTANT( int, rank =
BOOST_STATIC_CONSTANT( int, rank = (BitsIncludingSign > 0) * (
#ifdef BOOST_HAS_LONG_LONG
(mantissa <= std::numeric_limits< long_long_type >::digits) +
#elif defined(BOOST_HAS_MS_INT64)
@ -96,13 +99,13 @@ namespace detail
(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) );
(mantissa <= std::numeric_limits< signed char >::digits)) );
};
template < int Bits >
struct uint_rank_helper
{
BOOST_STATIC_CONSTANT( int, rank =
BOOST_STATIC_CONSTANT( int, rank = (Bits >= 0) * (
#ifdef BOOST_HAS_LONG_LONG
(Bits <= std::numeric_limits< ulong_long_type >::digits) +
#elif defined(BOOST_HAS_MS_INT64)
@ -113,14 +116,53 @@ namespace detail
(Bits <= std::numeric_limits< unsigned long >::digits) +
(Bits <= std::numeric_limits< unsigned int >::digits) +
(Bits <= std::numeric_limits< unsigned short >::digits) +
(Bits <= std::numeric_limits< unsigned char >::digits) );
(Bits <= std::numeric_limits< unsigned char >::digits)) );
};
template < int BitsIncludingSign >
struct int_exact_rank_helper { BOOST_STATIC_CONSTANT( int, rank = 0 ); };
template < int Bits >
struct uint_exact_rank_helper { BOOST_STATIC_CONSTANT( int, rank = 0 ); };
#define BOOST_PRIVATE_INT_EXACT_BUILDER(Type, Rank) \
template < > \
struct int_exact_rank_helper<std::numeric_limits< Type >::digits + 1> \
{ BOOST_STATIC_CONSTANT( int, rank = Rank ); }
#define BOOST_PRIVATE_UINT_EXACT_BUILDER(Type, Rank) \
template < > \
struct uint_exact_rank_helper<std::numeric_limits< Type >::digits> \
{ BOOST_STATIC_CONSTANT( int, rank = Rank ); }
#if defined(BOOST_HAS_LONG_LONG) && ((ULLONG_MAX > ULONG_MAX) || (ULONGLONG_MAX > ULONG_MAX))
BOOST_PRIVATE_INT_EXACT_BUILDER( long_long_type, 1 );
BOOST_PRIVATE_UINT_EXACT_BUILDER( ulong_long_type, 1 );
#elif defined(BOOST_HAS_MS_INT64) && (0xFFFFFFFFFFFFFFFFui64 > ULONG_MAX)
BOOST_PRIVATE_INT_EXACT_BUILDER( __int64, 1 );
BOOST_PRIVATE_UINT_EXACT_BUILDER( unsigned __int64, 1 );
#endif
#if ULONG_MAX > UINT_MAX
BOOST_PRIVATE_INT_EXACT_BUILDER( long, 2 );
BOOST_PRIVATE_UINT_EXACT_BUILDER( unsigned long, 2 );
#endif
#if UINT_MAX > USHRT_MAX
BOOST_PRIVATE_INT_EXACT_BUILDER( int, 3 );
BOOST_PRIVATE_UINT_EXACT_BUILDER( unsigned, 3 );
#endif
#if USHRT_MAX > UCHAR_MAX
BOOST_PRIVATE_INT_EXACT_BUILDER( short, 4 );
BOOST_PRIVATE_UINT_EXACT_BUILDER( unsigned short, 4 );
#endif
BOOST_PRIVATE_INT_EXACT_BUILDER( signed char, 5 );
BOOST_PRIVATE_UINT_EXACT_BUILDER( unsigned char, 5 );
#undef BOOST_PRIVATE_INT_EXACT_BUILDER
#undef BOOST_PRIVATE_UINT_EXACT_BUILDER
// map an extreme value to a category
template < intmax_t MaxValue >
struct int_max_rank_helper
{
BOOST_STATIC_CONSTANT( int, rank =
BOOST_STATIC_CONSTANT( int, rank = (MaxValue > 0) * (
#ifdef BOOST_HAS_LONG_LONG
(MaxValue <= integer_traits< long_long_type >::const_max) +
#elif defined(BOOST_HAS_MS_INT64)
@ -131,13 +173,13 @@ namespace detail
(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) );
(MaxValue <= integer_traits< signed char >::const_max)) );
};
template < intmax_t MinValue >
struct int_min_rank_helper
{
BOOST_STATIC_CONSTANT( int, rank =
BOOST_STATIC_CONSTANT( int, rank = (MinValue < 0) * (
#ifdef BOOST_HAS_LONG_LONG
(MinValue >= integer_traits< long_long_type >::const_min) +
#elif defined(BOOST_HAS_MS_INT64)
@ -148,7 +190,7 @@ namespace detail
(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) );
(MinValue >= integer_traits< signed char >::const_min)) );
};
template < uintmax_t Value >
@ -168,12 +210,66 @@ namespace detail
(Value <= integer_traits< unsigned char >::const_max) );
};
// convert rank to type, Boost.MPL-style
template < int Rank, typename Signedness, class Enable = void >
struct integral_rank_to_type
{
BOOST_STATIC_CONSTANT( bool, is_specialized = false );
// No "signed" nor "type" here
};
template < int Rank >
struct integral_rank_to_type< Rank, signed, typename
enable_if_c<(lowest_integral_rank <= Rank) && (Rank <=
highest_integral_rank)>::type >
{
BOOST_STATIC_CONSTANT( bool, is_specialized = true );
BOOST_STATIC_CONSTANT( bool, is_signed = true );
typedef typename int_least_helper< Rank, signed >::least type;
};
template < int Rank >
struct integral_rank_to_type< Rank, unsigned, typename
enable_if_c<(lowest_integral_rank <= Rank) && (Rank <=
highest_integral_rank)>::type >
{
BOOST_STATIC_CONSTANT( bool, is_specialized = true );
BOOST_STATIC_CONSTANT( bool, is_signed = false );
typedef typename int_least_helper< Rank, unsigned >::least type;
};
} // namespace detail
// MPL-compatible integer-mapping class templates ------------------------//
// exact number of bits
template < int Bits, typename Signedness >
struct exact_integral
{
BOOST_STATIC_CONSTANT( bool, is_specialized = false );
BOOST_STATIC_CONSTANT( int, bit_count = Bits );
};
template < int BitsIncludingSign >
struct exact_integral< BitsIncludingSign, signed >
: detail::integral_rank_to_type<
detail::int_exact_rank_helper<BitsIncludingSign>::rank, signed >
{
BOOST_STATIC_CONSTANT( int, bit_count = BitsIncludingSign );
};
template < int Bits >
struct exact_integral< Bits, unsigned >
: detail::integral_rank_to_type<
detail::uint_exact_rank_helper<Bits>::rank, unsigned >
{
BOOST_STATIC_CONSTANT( int, bit_count = Bits );
};
// integer templates specifying number of bits ---------------------------//
// signed
template< int Bits > // bits (including sign) required
template< int Bits > // minimum bits (including sign) required
struct int_t
{
typedef typename detail::int_least_helper
@ -183,8 +279,14 @@ namespace detail
typedef typename int_fast_t<least>::fast fast;
};
template< int Bits > // exact bits (including sign) desired
struct int_exact_t
{
typedef typename exact_integral<Bits, signed>::type exact;
};
// unsigned
template< int Bits > // bits required
template< int Bits > // minimum bits required
struct uint_t
{
typedef typename detail::int_least_helper
@ -195,6 +297,12 @@ namespace detail
// int_fast_t<> works correctly for unsigned too, in spite of the name.
};
template< int Bits > // exact bits desired
struct uint_exact_t
{
typedef typename exact_integral<Bits, unsigned>::type exact;
};
// integer templates specifying extreme value ----------------------------//
// signed