forked from boostorg/integer
Added MPL-compatible variant of the processor-optimized integer template
[SVN r47452]
This commit is contained in:
@ -7,7 +7,8 @@
|
|||||||
// See http://www.boost.org/libs/integer for documentation.
|
// See http://www.boost.org/libs/integer for documentation.
|
||||||
|
|
||||||
// Revision History
|
// Revision History
|
||||||
// 15 Jul 08 Added exact-integer templates. (Daryle Walker)
|
// 15 Jul 08 Added exact-integer templates; added MPL-compatible variant of
|
||||||
|
// processor-optimized integer template. (Daryle Walker)
|
||||||
// 14 Jul 08 Added extended-integer support. (Daryle Walker)
|
// 14 Jul 08 Added extended-integer support. (Daryle Walker)
|
||||||
// 13 Jul 08 Redid implmentation. (Daryle Walker)
|
// 13 Jul 08 Redid implmentation. (Daryle Walker)
|
||||||
// 22 Sep 01 Added value-based integer templates. (Daryle Walker)
|
// 22 Sep 01 Added value-based integer templates. (Daryle Walker)
|
||||||
@ -20,10 +21,11 @@
|
|||||||
|
|
||||||
#include <boost/integer_fwd.hpp> // self include
|
#include <boost/integer_fwd.hpp> // self include
|
||||||
|
|
||||||
#include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
|
#include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
|
||||||
#include <boost/cstdint.hpp> // for boost::uintmax_t, intmax_t
|
#include <boost/cstdint.hpp> // for boost::uintmax_t, intmax_t
|
||||||
#include <boost/integer_traits.hpp> // for boost::integer_traits
|
#include <boost/integer_traits.hpp> // for boost::integer_traits
|
||||||
#include <boost/limits.hpp> // for std::numeric_limits
|
#include <boost/limits.hpp> // for std::numeric_limits
|
||||||
|
#include <boost/utility/enable_if.hpp> // for boost::enable_if_c
|
||||||
|
|
||||||
#include <climits> // for UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, etc.
|
#include <climits> // for UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, etc.
|
||||||
|
|
||||||
@ -32,10 +34,21 @@ namespace boost
|
|||||||
|
|
||||||
// integer template mapping a type to its processor-optimized analog -----//
|
// integer template mapping a type to its processor-optimized analog -----//
|
||||||
|
|
||||||
|
// Some types can be handled better by the processor than others. This
|
||||||
|
// template metafunction should map various built-in integral types to
|
||||||
|
// the processor's perferred type for the given type's value range
|
||||||
|
template < typename BaseInt >
|
||||||
|
struct fast_integral
|
||||||
|
{
|
||||||
|
typedef BaseInt type;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Platform-specific specializations should go here.
|
||||||
|
|
||||||
// fast integers from least integers
|
// fast integers from least integers
|
||||||
// int_fast_t<> works correctly for unsigned too, in spite of the name.
|
// int_fast_t<> works correctly for unsigned too, in spite of the name.
|
||||||
template< typename LeastInt >
|
template< typename LeastInt >
|
||||||
struct int_fast_t { typedef LeastInt fast; }; // imps may specialize
|
struct int_fast_t { typedef typename fast_integral<LeastInt>::type fast; };
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
@ -84,6 +84,9 @@ template < >
|
|||||||
|
|
||||||
// From <boost/integer.hpp> ------------------------------------------------//
|
// From <boost/integer.hpp> ------------------------------------------------//
|
||||||
|
|
||||||
|
template < typename BaseInt >
|
||||||
|
struct fast_integral;
|
||||||
|
|
||||||
template < typename LeastInt >
|
template < typename LeastInt >
|
||||||
struct int_fast_t;
|
struct int_fast_t;
|
||||||
|
|
||||||
|
51
integer.htm
51
integer.htm
@ -40,10 +40,27 @@ is particularly useful for solving generic programming problems.</p>
|
|||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
// fast integers from least integers
|
// fast integers from least integers
|
||||||
|
template< typename BaseInt >
|
||||||
|
struct fast_integral
|
||||||
|
{
|
||||||
|
typedef <em>implementation_supplied</em> type;
|
||||||
|
};
|
||||||
|
|
||||||
template< typename LeastInt >
|
template< typename LeastInt >
|
||||||
struct int_fast_t
|
struct int_fast_t
|
||||||
{
|
{
|
||||||
typedef <em>implementation_supplied</em> fast;
|
typedef typename fast_integral<LeastInt>::type fast;
|
||||||
|
};
|
||||||
|
|
||||||
|
// MPL-compatible
|
||||||
|
template< int Bits, typename Signedness >
|
||||||
|
struct exact_integral
|
||||||
|
{
|
||||||
|
static bool const is_specialized = <em>implementation_supplied</em>;
|
||||||
|
static bool const is_signed = <em>implementation_supplied</em>;
|
||||||
|
static int const bit_count = Bits;
|
||||||
|
|
||||||
|
typedef <em>implementation_supplied</em> type;
|
||||||
};
|
};
|
||||||
|
|
||||||
// signed
|
// signed
|
||||||
@ -57,7 +74,7 @@ namespace boost
|
|||||||
template< int Bits >
|
template< int Bits >
|
||||||
struct int_exact_t
|
struct int_exact_t
|
||||||
{
|
{
|
||||||
typedef <em>implementation_supplied</em> exact;
|
typedef typename exact_integral<Bits, signed>::type exact;
|
||||||
};
|
};
|
||||||
|
|
||||||
// unsigned
|
// unsigned
|
||||||
@ -71,7 +88,7 @@ namespace boost
|
|||||||
template< int Bits >
|
template< int Bits >
|
||||||
struct uint_exact_t
|
struct uint_exact_t
|
||||||
{
|
{
|
||||||
typedef <em>implementation_supplied</em> exact;
|
typedef typename exact_integral<Bits, unsigned>::type exact;
|
||||||
};
|
};
|
||||||
|
|
||||||
// signed
|
// signed
|
||||||
@ -96,31 +113,25 @@ namespace boost
|
|||||||
typedef <em>implementation_supplied</em> least;
|
typedef <em>implementation_supplied</em> least;
|
||||||
typedef int_fast_t<least>::fast fast;
|
typedef int_fast_t<least>::fast fast;
|
||||||
};
|
};
|
||||||
|
|
||||||
// MPL-compatible
|
|
||||||
template< int Bits, typename Signedness >
|
|
||||||
struct exact_integral
|
|
||||||
{
|
|
||||||
static bool const is_specialized = <em>implementation_supplied</em>;
|
|
||||||
static bool const is_signed = <em>implementation_supplied</em>;
|
|
||||||
static int const bit_count = Bits;
|
|
||||||
|
|
||||||
typedef <em>implementation_supplied</em> type;
|
|
||||||
};
|
|
||||||
} // namespace boost
|
} // namespace boost
|
||||||
</pre></blockquote>
|
</pre></blockquote>
|
||||||
|
|
||||||
<h2><a name="easy">Processor-Optimized Types</a></h2>
|
<h2><a name="easy">Processor-Optimized Types</a></h2>
|
||||||
|
|
||||||
<p>The <code>int_fast_t</code> class template maps its input type to the
|
<p>The <code>fast_integral</code> class template maps its input type to the
|
||||||
next-largest type that the processor can manipulate the easiest, or to
|
next-largest type that the processor can manipulate the easiest, or to
|
||||||
itself if the input type is already an easy-to-manipulate type. For
|
itself if the input type is already an easy-to-manipulate type. For
|
||||||
instance, processing a bunch of <code>char</code> objects may go faster
|
instance, processing a bunch of <code>char</code> objects may go faster
|
||||||
if they were converted to <code>int</code> objects before processing.
|
if they were converted to <code>int</code> objects before processing.
|
||||||
The input type, passed as the only template parameter, must be a
|
The input type, passed as the only template parameter, can be any built-in
|
||||||
built-in integral type, except <code>bool</code>. Unsigned integral
|
integral type besides <code>bool</code>. The output type is given as the class
|
||||||
types can be used, as well as signed integral types, despite the name.
|
member <code>type</code>.</p>
|
||||||
The output type is given as the class member <code>fast</code>.</p>
|
|
||||||
|
<p>The <code>int_fast_t</code> class template is the classic meta-function for
|
||||||
|
this operation. Despite the name, it works for unsigned integral types just
|
||||||
|
like it works for the signed integral types. The output type is given as the
|
||||||
|
class member <code>fast</code>, defined to be the same as the corresponding
|
||||||
|
result from the <code>fast_integral</code> meta-function.</p>
|
||||||
|
|
||||||
<p><strong>Implementation Notes</strong><br>
|
<p><strong>Implementation Notes</strong><br>
|
||||||
By default, the output type is identical to the input type. Eventually,
|
By default, the output type is identical to the input type. Eventually,
|
||||||
@ -282,7 +293,7 @@ struct <var>name</var>
|
|||||||
<td><code>type</code></td>
|
<td><code>type</code></td>
|
||||||
<td><code>is_specialized == true</code></td>
|
<td><code>is_specialized == true</code></td>
|
||||||
<td>The meta-function's result. It appears only if the input parameters
|
<td>The meta-function's result. It appears only if the input parameters
|
||||||
satisfy the template's requirements. It's presence, or lack thereof,
|
satisfy the template's requirements. Its presence, or lack thereof,
|
||||||
enables "Substitution Failure Is Not An Error" (SFINAE)
|
enables "Substitution Failure Is Not An Error" (SFINAE)
|
||||||
techniques, instead of a hard compiler diagnostic.</td>
|
techniques, instead of a hard compiler diagnostic.</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -8,7 +8,8 @@
|
|||||||
// See http://www.boost.org/libs/integer for documentation.
|
// See http://www.boost.org/libs/integer for documentation.
|
||||||
|
|
||||||
// Revision History
|
// Revision History
|
||||||
// 15 Jul 08 Added exact-integer templates. (Daryle Walker)
|
// 15 Jul 08 Added exact-integer templates; added MPL-compatible variant of
|
||||||
|
// processor-optimized integer template. (Daryle Walker)
|
||||||
// 14 Jul 08 Improved testing of processor-optimized integer template; added
|
// 14 Jul 08 Improved testing of processor-optimized integer template; added
|
||||||
// extended-integer support. (Daryle Walker)
|
// extended-integer support. (Daryle Walker)
|
||||||
// 13 Jul 08 Modernized tests w/ MPL instead of giant macros (Daryle Walker)
|
// 13 Jul 08 Modernized tests w/ MPL instead of giant macros (Daryle Walker)
|
||||||
@ -69,15 +70,15 @@
|
|||||||
|
|
||||||
|
|
||||||
// If specializations have not already been done, then we can confirm
|
// If specializations have not already been done, then we can confirm
|
||||||
// the effects of the "fast" types by making a specialization. If there
|
// the effects of the fast types by making a specialization. If there
|
||||||
// is a specialization for "short," make sure that CONTROL_FAST_SHORT
|
// is a specialization for "short," make sure that CONTROL_FAST_SHORT
|
||||||
// is set to a type distinct from "short" and the default implementation.
|
// is set to a type distinct from "short" and the default implementation.
|
||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
template < >
|
template < >
|
||||||
struct int_fast_t< short >
|
struct fast_integral< short >
|
||||||
{
|
{
|
||||||
typedef CONTROL_FAST_SHORT fast;
|
typedef CONTROL_FAST_SHORT type;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,8 +274,10 @@ BOOST_AUTO_TEST_CASE( fast_type_test )
|
|||||||
{
|
{
|
||||||
#ifndef BOOST_NO_USING_TEMPLATE
|
#ifndef BOOST_NO_USING_TEMPLATE
|
||||||
using std::numeric_limits;
|
using std::numeric_limits;
|
||||||
|
using boost::is_same;
|
||||||
#else
|
#else
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef short least_type;
|
typedef short least_type;
|
||||||
@ -282,8 +285,12 @@ BOOST_AUTO_TEST_CASE( fast_type_test )
|
|||||||
typedef numeric_limits<least_type> least_limits;
|
typedef numeric_limits<least_type> least_limits;
|
||||||
typedef numeric_limits<fast_type> fast_limits;
|
typedef numeric_limits<fast_type> fast_limits;
|
||||||
|
|
||||||
BOOST_MPL_ASSERT_RELATION( (boost::is_same<least_type, fast_type>::value),
|
typedef boost::fast_integral<least_type>::type real_fast_type;
|
||||||
==, false );
|
|
||||||
|
BOOST_MPL_ASSERT_RELATION( (is_same<least_type, fast_type>::value), ==,
|
||||||
|
false );
|
||||||
|
BOOST_MPL_ASSERT_RELATION( (is_same<fast_type, real_fast_type>::value), ==,
|
||||||
|
true );
|
||||||
BOOST_MPL_ASSERT_RELATION( fast_limits::is_specialized, ==, true );
|
BOOST_MPL_ASSERT_RELATION( fast_limits::is_specialized, ==, true );
|
||||||
BOOST_MPL_ASSERT_RELATION( fast_limits::is_signed &&
|
BOOST_MPL_ASSERT_RELATION( fast_limits::is_signed &&
|
||||||
fast_limits::is_bounded, ==, true );
|
fast_limits::is_bounded, ==, true );
|
||||||
|
Reference in New Issue
Block a user