diff --git a/include/boost/integer.hpp b/include/boost/integer.hpp index dc57dff..256fa27 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 +// 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) // 30 Jul 00 Add typename syntax fix (Jens Maurer) @@ -17,33 +18,105 @@ #include // self include +#include // for BOOST_STATIC_CONSTANT #include // for boost::integer_traits #include // for std::numeric_limits namespace boost { - // Helper templates ------------------------------------------------------// + // integer template mapping a type to its processor-optimized analog -----// // fast integers from least integers // int_fast_t<> works correctly for unsigned too, in spite of the name. template< typename LeastInt > struct int_fast_t { typedef LeastInt fast; }; // imps may specialize - // convert category to type - template< int Category > struct int_least_helper {}; // default is empty +namespace detail +{ - // specializatons: 1=long, 2=int, 3=short, 4=signed char, - // 6=unsigned long, 7=unsigned int, 8=unsigned short, 9=unsigned char - // no specializations for 0 and 5: requests for a type > long are in error - template<> struct int_least_helper<1> { typedef long least; }; - template<> struct int_least_helper<2> { typedef int least; }; - template<> struct int_least_helper<3> { typedef short least; }; - template<> struct int_least_helper<4> { typedef signed char least; }; - template<> struct int_least_helper<6> { typedef unsigned long least; }; - template<> struct int_least_helper<7> { typedef unsigned int least; }; - template<> struct int_least_helper<8> { typedef unsigned short least; }; - template<> struct int_least_helper<9> { typedef unsigned char least; }; + // Helper templates ------------------------------------------------------// + + // 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; }; + template<> struct int_least_helper<1, unsigned> + { typedef unsigned long least; }; + template<> struct int_least_helper<2, signed> { typedef int least; }; + template<> struct int_least_helper<2, unsigned> + { typedef unsigned int least; }; + template<> struct int_least_helper<3, signed> { typedef short least; }; + template<> struct int_least_helper<3, unsigned> + { typedef unsigned short least; }; + template<> struct int_least_helper<4, signed> { typedef signed char least; }; + template<> struct int_least_helper<4, unsigned> + { typedef unsigned char least; }; + + // category bounds + enum + { + lowest_integral_rank = 1, + highest_integral_rank = 4 + }; + + // map a bit count to a category + template < int BitsIncludingSign > + struct int_rank_helper + { + 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) ); + }; + + template < int Bits > + struct uint_rank_helper + { + BOOST_STATIC_CONSTANT( int, rank = + (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) ); + }; + + // map an extreme value to a category + template < long MaxValue > + struct int_max_rank_helper + { + BOOST_STATIC_CONSTANT( int, rank = + (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 > + struct int_min_rank_helper + { + BOOST_STATIC_CONSTANT( int, rank = + (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 > + struct uint_max_rank_helper + { + BOOST_STATIC_CONSTANT( int, rank = + (Value <= integer_traits< unsigned long >::const_max) + + (Value <= integer_traits< unsigned int >::const_max) + + (Value <= integer_traits< unsigned short >::const_max) + + (Value <= integer_traits< unsigned char >::const_max) ); + }; + +} // namespace detail // integer templates specifying number of bits ---------------------------// @@ -51,13 +124,10 @@ namespace boost template< int Bits > // bits (including sign) required struct int_t { - typedef typename int_least_helper + typedef typename detail::int_least_helper < - (Bits-1 <= std::numeric_limits::digits) + - (Bits-1 <= std::numeric_limits::digits) + - (Bits-1 <= std::numeric_limits::digits) + - (Bits-1 <= std::numeric_limits::digits) - >::least least; + detail::int_rank_helper::rank, + signed>::least least; typedef typename int_fast_t::fast fast; }; @@ -65,14 +135,10 @@ namespace boost template< int Bits > // bits required struct uint_t { - typedef typename int_least_helper + typedef typename detail::int_least_helper < - 5 + - (Bits <= std::numeric_limits::digits) + - (Bits <= std::numeric_limits::digits) + - (Bits <= std::numeric_limits::digits) + - (Bits <= std::numeric_limits::digits) - >::least least; + detail::uint_rank_helper::rank, + unsigned>::least least; typedef typename int_fast_t::fast fast; // int_fast_t<> works correctly for unsigned too, in spite of the name. }; @@ -83,26 +149,20 @@ namespace boost template< long MaxValue > // maximum value to require support struct int_max_value_t { - typedef typename int_least_helper + typedef typename detail::int_least_helper < - (MaxValue <= integer_traits::const_max) + - (MaxValue <= integer_traits::const_max) + - (MaxValue <= integer_traits::const_max) + - (MaxValue <= integer_traits::const_max) - >::least least; + detail::int_max_rank_helper::rank, + signed>::least least; typedef typename int_fast_t::fast fast; }; template< long MinValue > // minimum value to require support struct int_min_value_t { - typedef typename int_least_helper + typedef typename detail::int_least_helper < - (MinValue >= integer_traits::const_min) + - (MinValue >= integer_traits::const_min) + - (MinValue >= integer_traits::const_min) + - (MinValue >= integer_traits::const_min) - >::least least; + detail::int_min_rank_helper::rank, + signed>::least least; typedef typename int_fast_t::fast fast; }; @@ -110,14 +170,10 @@ namespace boost template< unsigned long Value > // maximum value to require support struct uint_value_t { - typedef typename int_least_helper + typedef typename detail::int_least_helper < - 5 + - (Value <= integer_traits::const_max) + - (Value <= integer_traits::const_max) + - (Value <= integer_traits::const_max) + - (Value <= integer_traits::const_max) - >::least least; + detail::uint_max_rank_helper::rank, + unsigned>::least least; typedef typename int_fast_t::fast fast; };