2009-11-25 12:38:09 +00:00
[article Boost.Integer
2014-06-01 20:50:31 +01:00
[quickbook 1.6]
[compatibility-mode 1.5]
2009-11-25 12:38:09 +00:00
[copyright 2001-2009 Beman Dawes, Daryle Walker, Gennaro Prota, John Maddock]
[license
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
2021-01-20 11:34:42 +03:00
[@https://www.boost.org/LICENSE_1_0.txt])
2009-11-25 12:38:09 +00:00
]
[authors [Dawes, Beman], [Walker, Daryle], [Prota, Gennaro], [Maddock, John]]
[/last-revision $Date: 2008-02-21 12:58:15 +0000 (Thu, 21 Feb 2008) $]
]
[template super[x]'''<superscript>'''[x]'''</superscript>''']
[section:overview Overview]
2014-06-01 09:59:12 -07:00
Boost.Integer provides integer type support, particularly helpful in generic programming.
2018-10-26 12:05:47 -06:00
It provides the means to select an integer type based upon its properties, like the number of bits or
2009-11-28 17:08:31 +00:00
the maximum supported value, as well as compile-time bit mask selection. There is a derivative of
2018-10-26 12:05:47 -06:00
std::numeric_limits that provides integral constant expressions for `min` and `max`.
Finally, it provides two compile-time algorithms: determining the highest power of two in a
2009-11-28 17:08:31 +00:00
compile-time value; and computing min and max of constant expressions.
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
[table
2009-11-25 12:38:09 +00:00
[[Component][Header][Purpose]]
[
[Forward Declarations.]
[[^[@../../../../boost/integer_fwd.hpp <boost/integer_fwd.hpp>]]]
[Forward declarations of classes and class templates - for use when just the name of a class is needed.]
]
[
[[link boost_integer.traits Integer Traits].]
[[^[@../../../../boost/integer_traits.hpp <boost/integer_traits.hpp>]]]
[Class template [^boost::integer_traits], derives from [^std::numeric_limits] and adds [^const_min] and [^const_max] members.]
]
[
[[link boost_integer.integer Integer Type Selection].]
2010-02-14 13:09:24 +00:00
[[^[@../../../../boost/integer.hpp <boost/integer.hpp>]]]
2018-10-26 12:05:47 -06:00
[Templates for integer type selection based on properties such as maximum value or number of bits:
Use to select the type of an integer when some property such as maximum value or number of bits is known.
2009-11-25 12:38:09 +00:00
Useful for generic programming. ]
]
2017-04-24 13:01:57 +01:00
[
[[link boost_integer.gcd_lcm Greatest Common Divisor and Least Common Multiple].]
[[^[@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]] and [^[@../../../../boost/integer/common_factor_ct.hpp <boost/integer/common_factor_ct.hpp>]]]
[Functions `gcd` and `lcm` plus function objects and compile time versions.]
]
2009-11-25 12:38:09 +00:00
[
[[link boost_integer.mask Integer Masks].]
[[^[@../../../../boost/integer/integer_mask.hpp <boost/integer/integer_mask.hpp>]]]
2018-10-26 12:05:47 -06:00
[Templates for the selection of integer masks, single or lowest group, based on the number of bits:
2009-11-25 12:38:09 +00:00
Use to select a particular mask when the bit position(s) are based on a compile-time variable. Useful for generic programming. ]
]
[
[[link boost_integer.log2 Compile time log2 Calculation].]
[[^[@../../../../boost/integer/static_log2.hpp <boost/integer/static_log2.hpp>]]]
2018-10-26 12:05:47 -06:00
[Template for finding the highest power of two in a number:
2009-11-25 12:38:09 +00:00
Use to find the bit-size/range based on a maximum value. Useful for generic programming. ]
]
[
[[link boost_integer.minmax Compile time min/max calculation].]
[[^[@../../../../boost/integer/static_min_max.hpp <boost/integer/static_min_max.hpp>]]]
2018-10-26 12:05:47 -06:00
[Templates for finding the extrema of two numbers:
2009-11-25 12:38:09 +00:00
Use to find a bound based on a minimum or maximum value. Useful for generic programming. ]
]
2018-10-26 12:05:47 -06:00
[
[[link boost_integer.extended_euclidean Extended Euclidean algorithm].]
[[^[@../../../../boost/integer/extended_euclidean.hpp <boost/integer/extended_euclidean.hpp>]]]
[Solves /mx + ny = gcd(x,y)/ for /x/ and /y/.]
]
[
[[link boost_integer.mod_inverse Modular multiplicative inverse].]
[[^[@../../../../boost/integer/mod_inverse.hpp <boost/integer/mod_inverse.hpp>]]]
[Given /a/ and /m/, solves /ax/ = 1 mod /m/ for /x/.]
]
2009-11-25 12:38:09 +00:00
]
[endsect]
[section:traits Integer Traits]
[section Motivation]
The C++ Standard Library <limits> header supplies a class template `numeric_limits<>` with specializations for each fundamental type.
2018-10-26 12:05:47 -06:00
For integer types, the interesting members of `std::numeric_limits<>` are:
2009-11-25 12:38:09 +00:00
static const bool is_specialized; // Will be true for integer types.
static T min() throw(); // Smallest representable value.
static T max() throw(); // Largest representable value.
static const int digits; // For integers, the number of value bits.
static const int digits10; // The number of base 10 digits that can be represented.
static const bool is_signed; // True if the type is signed.
static const bool is_integer; // Will be true for all integer types.
2018-10-26 12:05:47 -06:00
For many uses, these are sufficient.
But min() and max() are problematical because they are not constant expressions (std::5.19),
yet some usages require constant expressions.
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
The template class [^integer_traits] addresses this problem.
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Synopsis]
namespace boost {
template<class T>
class integer_traits : public std::numeric_limits<T>
{
2009-11-28 17:08:31 +00:00
public:
2009-11-25 12:38:09 +00:00
static const bool is_integral = false;
//
// These members are defined only if T is a built-in
// integal type:
//
static const T const_min = ``['implementation-defined]``;
static const T const_max = ``['implementation-defined]``;
};
}
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Description]
2018-10-26 12:05:47 -06:00
Template class [^integer_traits] is derived from [^std::numeric_limits]. The primary specialization adds the single
[^bool] member [^is_integral] with the compile-time constant value [^false].
However, for all integral types [^T] (std::3.9.1/7 [basic.fundamental]), there are specializations
provided with the following compile-time constants defined:
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
[table
2009-11-25 12:38:09 +00:00
[[member][type][value]]
[[[^is_integral]][bool][[^true]]]
[[[^const_min]][[^T]][equivalent to [^std::numeric_limits<T>::min()]]]
[[[^const_max]][[^T]][equivalent to [^std::numeric_limits<T>::max()]]]
]
2018-10-26 12:05:47 -06:00
Note: The /is_integral/ flag is provided, because a user-defined integer class should specialize
[^std::numeric_limits<>::is_integer = true], while compile-time constants
[^const_min] and [^const_max] are not provided for that user-defined class, unless boost::integer_traits is also specialized.
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Test Program]
2018-10-26 12:05:47 -06:00
The program [^[@../../test/integer_traits_test.cpp integer_traits_test.cpp]] exercises the [^integer_traits] class.
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Acknowledgements]
Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers discussed the integer traits idea on the boost mailing list in August 1999.
[endsect]
[endsect]
[section:integer Integer Type Selection]
2018-10-26 12:05:47 -06:00
The [@../../../../boost/integer.hpp <boost/integer.hpp>] type selection templates allow
integer types to be selected based on desired characteristics such as number of bits or maximum value.
2009-11-25 12:38:09 +00:00
This facility is particularly useful for solving generic programming problems.
[section:synopsis Synopsis]
namespace boost
{
// fast integers from least integers
template<typename LeastInt>
struct int_fast_t
{
2009-11-28 17:08:31 +00:00
typedef ``['implementation-defined-type]`` type;
2009-11-25 12:38:09 +00:00
};
// signed
template<int Bits>
2018-10-26 12:05:47 -06:00
struct int_t
2009-11-25 12:38:09 +00:00
{
2009-11-26 11:08:27 +00:00
/* Member exact may or may not be defined depending upon Bits */
typedef ``['implementation-defined-type]`` exact;
2009-11-25 12:38:09 +00:00
typedef ``['implementation-defined-type]`` least;
typedef int_fast_t<least>::fast fast;
};
// unsigned
template<int Bits>
2018-10-26 12:05:47 -06:00
struct uint_t
2009-11-25 12:38:09 +00:00
{
2009-11-26 11:08:27 +00:00
/* Member exact may or may not be defined depending upon Bits */
typedef ``['implementation-defined-type]`` exact;
2009-11-25 12:38:09 +00:00
typedef ``['implementation-defined-type]`` least;
typedef int_fast_t<least>::fast fast;
};
// signed
template<long long MaxValue>
2018-10-26 12:05:47 -06:00
struct int_max_value_t
2009-11-25 12:38:09 +00:00
{
typedef ``['implementation-defined-type]`` least;
typedef int_fast_t<least>::fast fast;
};
template<long long MinValue>
2018-10-26 12:05:47 -06:00
struct int_min_value_t
2009-11-25 12:38:09 +00:00
{
typedef ``['implementation-defined-type]`` least;
typedef int_fast_t<least>::fast fast;
};
// unsigned
template<unsigned long long Value>
2018-10-26 12:05:47 -06:00
struct uint_value_t
2009-11-25 12:38:09 +00:00
{
typedef ``['implementation-defined-type]`` least;
typedef int_fast_t<least>::fast fast;
};
} // namespace boost
[endsect]
[section:easiest Easiest-to-Manipulate Types]
2018-10-26 12:05:47 -06:00
The [^int_fast_t] class template maps its input type to the 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 instance, processing a bunch of [^char] objects may go faster if they were converted to [^int] objects before processing.
The input type, passed as the only template parameter, must be a built-in integral type, except [^bool].
Unsigned integral types can be used, as well as signed integral types.
2009-11-28 17:08:31 +00:00
The output type is given as the nested type [^fast].
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
[*Implementation Notes:]
By default, the output type is identical to the input type. Eventually, this code's implementation should
be customized for each platform to give accurate mappings between the built-in types and the easiest-to-manipulate
2009-11-25 12:38:09 +00:00
built-in types. Also, there is no guarantee that the output type actually is easier to manipulate than the input type.
[endsect]
[section:sized Sized Types]
2018-10-26 12:05:47 -06:00
The [^int_t], [^uint_t], [^int_max_value_t], [^int_min_value_t], and [^uint_value_t] class templates find
the most appropiate built-in integral type for the given template parameter. This type is given by the
nested type [^least]. The easiest-to-manipulate version of that type is given by the nested type [^fast].
2009-11-25 12:38:09 +00:00
The following table describes each template's criteria.
[table Criteria for the Sized Type Class Templates
[
[Class Template][Template Parameter Mapping]
]
[
2009-11-28 17:08:31 +00:00
[[^boost::int_t<N>::least]]
2018-10-26 12:05:47 -06:00
[The smallest, built-in, signed integral type with at least /N/ bits, including the sign bit.
The parameter should be a positive number. A compile-time error results if the parameter is
2009-11-28 17:08:31 +00:00
larger than the number of bits in the largest integer type.]
]
[
[[^boost::int_t<N>::fast]]
2018-10-26 12:05:47 -06:00
[The easiest-to-manipulate, built-in, signed integral type with at least /N/ bits, including the sign bit.
The parameter should be a positive number. A compile-time error results if the parameter is
2009-11-28 17:08:31 +00:00
larger than the number of bits in the largest integer type.]
]
[
[[^boost::int_t<N>::exact]]
2018-10-26 12:05:47 -06:00
[A built-in, signed integral type with exactly /N/ bits, including the sign bit.
2009-11-28 17:08:31 +00:00
The parameter should be a positive number. Note that the member /exact/ is defined
[*only] if there exists a type with exactly /N/ bits.]
2009-11-25 12:38:09 +00:00
]
[
2009-11-28 17:08:31 +00:00
[[^boost::uint_t<N>::least]]
2018-10-26 12:05:47 -06:00
[The smallest, built-in, unsigned integral type with at least /N/ bits.
The parameter should be a positive number. A compile-time error results if the
2009-11-28 17:08:31 +00:00
parameter is larger than the number of bits in the largest integer type.]
]
[
[[^boost::uint_t<N>::fast]]
2018-10-26 12:05:47 -06:00
[The easiest-to-manipulate, built-in, unsigned integral type with at least /N/ bits.
The parameter should be a positive number. A compile-time error results if the
2009-11-28 17:08:31 +00:00
parameter is larger than the number of bits in the largest integer type.]
]
[
[[^boost::uint_t<N>::exact]]
2018-10-26 12:05:47 -06:00
[A built-in, unsigned integral type with exactly /N/ bits.
The parameter should be a positive number. A compile-time error results if the
parameter is larger than the number of bits in the largest integer type.
2009-11-26 11:08:27 +00:00
Note that the member /exact/ is defined
2009-11-28 17:08:31 +00:00
[*only] if there exists a type with exactly N bits.]
2009-11-25 12:38:09 +00:00
]
[
2009-11-28 17:08:31 +00:00
[[^boost::int_max_value_t<V>::last]]
2018-10-26 12:05:47 -06:00
[The smallest, built-in, signed integral type that can hold all the values in the inclusive range ['0 - V].
2009-11-25 12:38:09 +00:00
The parameter should be a positive number.]
]
[
2009-11-28 17:08:31 +00:00
[[^boost::int_max_value_t<V>::fast]]
2018-10-26 12:05:47 -06:00
[The easiest-to-manipulate, built-in, signed integral type that can hold all the values in the inclusive range ['0 - V].
2009-11-28 17:08:31 +00:00
The parameter should be a positive number.]
]
[
[[^boost::int_min_value_t<V>::least]]
2018-10-26 12:05:47 -06:00
[The smallest, built-in, signed integral type that can hold all the values in the inclusive range ['V - 0].
2009-11-28 17:08:31 +00:00
The parameter should be a negative number.]
]
[
[[^boost::int_min_value_t<V>::fast]]
2018-10-26 12:05:47 -06:00
[The easiest-to-manipulate, built-in, signed integral type that can hold all the values in the inclusive range ['V - 0].
2009-11-25 12:38:09 +00:00
The parameter should be a negative number.]
]
[
2009-11-28 17:08:31 +00:00
[[^boost::uint_value_t<V>::least]]
[The smallest, built-in, unsigned integral type that can hold all positive values
up to and including /V/. The parameter should be a positive number.]
]
[
[[^boost::uint_value_t<V>::fast]]
[The easiest-to-manipulate, built-in, unsigned integral type that can hold all positive values
2009-11-25 12:38:09 +00:00
up to and including /V/. The parameter should be a positive number.]
]
]
[endsect]
[section Example]
#include <boost/integer.hpp>
//...
int main()
{
boost::int_t<24>::least my_var; // my_var has at least 24-bits
//...
2018-10-26 12:05:47 -06:00
// This one is guaranteed not to be truncated:
2009-11-25 12:38:09 +00:00
boost::int_max_value_t<1000>::least my1000 = 1000;
//...
2018-10-26 12:05:47 -06:00
// This one is guaranteed not to be truncated, and as fast
2009-11-26 15:43:32 +00:00
// to manipulate as possible, its size may be greater than
2009-11-25 12:38:09 +00:00
// that of my1000:
boost::int_max_value_t<1000>::fast my_fast1000 = 1000;
}
[endsect]
[section Demonstration Program]
2018-10-26 12:05:47 -06:00
The program [@../../test/integer_test.cpp integer_test.cpp] is a simplistic demonstration of the results from instantiating
2009-11-25 12:38:09 +00:00
various examples of the sized type class templates.
[endsect]
[section Rationale]
The rationale for the design of the templates in this header includes:
* Avoid recursion because of concern about C++'s limited guaranteed recursion depth (17).
* Avoid macros on general principles.
* Try to keep the design as simple as possible.
[endsect]
[section Alternative]
2018-10-26 12:05:47 -06:00
If the number of bits required is known beforehand, it may be more appropriate to use the types supplied
2009-11-25 12:38:09 +00:00
in [@../../../../boost/cstdint.hpp <boost/cstdint.hpp>].
[endsect]
[section Credits]
2018-10-26 12:05:47 -06:00
The author of most of the Boost integer type choosing templates is
2021-01-20 11:34:42 +03:00
[@https://www.boost.org/people/beman_dawes.html Beman Dawes].
He gives thanks to Valentin Bonnard and [@https://www.boost.org/people/kevlin_henney.htm Kevlin Henney]
2018-10-26 12:05:47 -06:00
for sharing their designs for similar templates.
2021-01-20 11:34:42 +03:00
[@https://www.boost.org/people/daryle_walker.html Daryle Walker] designed the value-based sized templates.
2009-11-25 12:38:09 +00:00
[endsect]
[endsect]
2017-04-24 13:01:57 +01:00
[include gcd/math-gcd.qbk]
2018-10-26 12:05:47 -06:00
[include modular_arithmetic/extended_euclidean.qbk]
[include modular_arithmetic/mod_inverse.qbk]
2009-11-25 12:38:09 +00:00
[section:mask Integer Masks]
[section Overview]
2018-10-26 12:05:47 -06:00
The class templates in [@../../../../boost/integer/integer_mask.hpp <boost/integer/integer_mask.hpp>]
provide bit masks for a certain bit position or a contiguous-bit pack of a certain size.
2009-11-25 12:38:09 +00:00
The types of the masking constants come from the [link boost_integer.integer integer type selection templates] header.
[endsect]
[section Synopsis]
#include <cstddef> // for std::size_t
namespace boost
{
template <std::size_t Bit>
struct high_bit_mask_t
{
typedef ``['implementation-defined-type]`` least;
typedef ``['implementation-defined-type]`` fast;
static const least high_bit = ``['implementation-defined]``;
static const fast high_bit_fast = ``['implementation-defined]``;
static const std::size_t bit_position = Bit;
};
template <std::size_t Bits>
struct low_bits_mask_t
{
typedef ``['implementation-defined-type]`` least;
typedef ``['implementation-defined-type]`` fast;
static const least sig_bits = ``['implementation-defined]``;
static const fast sig_bits_fast = ``['implementation-defined]``;
static const std::size_t bit_count = Bits;
};
// Specializations for low_bits_mask_t exist for certain bit counts.
} // namespace boost
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Single Bit-Mask Class Template]
2018-10-26 12:05:47 -06:00
The [^boost::high_bit_mask_t] class template provides constants for bit masks representing the bit at a
certain position. The masks are equivalent to the value 2[super Bit], where [^Bit] is the template parameter.
The bit position must be a nonnegative number from zero to ['Max], where Max is one less than the
number of bits supported by the largest unsigned built-in integral type. The following table describes
2009-11-25 12:38:09 +00:00
the members of an instantiation of [^high_bit_mask_t].
[table Members of the `boost::high_bit_mask_t` Class Template
[[Member][Meaning]]
2009-11-28 17:08:31 +00:00
[[[^least]][The smallest, unsigned, built-in type that supports the given bit position.]]
[[[^fast]][The easiest-to-manipulate analog of [^least].]]
[[[^high_bit]][A [^least] constant of the value 2[super Bit].]]
2009-11-25 12:38:09 +00:00
[[[^high_bit_fast]][A [^fast] analog of [^high_bit].]]
[[[^bit_position]][The value of the template parameter, in case its needed from a renamed instantiation of the class template.]]
]
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Group Bit-Mask Class Template]
2018-10-26 12:05:47 -06:00
The [^boost::low_bits_mask_t] class template provides constants for bit masks
equivalent to the value (2[super Bits] - 1), where [^Bits] is the template parameter.
The parameter [^Bits] must be a non-negative integer from
zero to ['Max], where Max is the number of bits supported by the largest, unsigned, built-in integral type.
2009-11-28 17:08:31 +00:00
The following table describes the members of [^low_bits_mask_t].
2009-11-25 12:38:09 +00:00
[table Members of the [^boost::low_bits_mask_t] Class Template
[[Member][Meaning]]
2009-11-28 17:08:31 +00:00
[[[^least]][The smallest, unsigned built-in type that supports the given bit count.]]
[[[^fast]][The easiest-to-manipulate analog of [^least].]]
2009-11-25 12:38:09 +00:00
[[[^sig_bits]][A [^least] constant of the desired bit-masking value.]]
[[[^sig_bits_fast]][A [^fast] analog of [^sig_bits].]]
[[[^bit_count]][The value of the template parameter, in case its needed from a renamed instantiation of the class template.]]
]
[endsect]
[section Implementation Notes]
2018-10-26 12:05:47 -06:00
When [^Bits] is the exact size of a built-in unsigned type, the implementation has to change to
2009-11-25 12:38:09 +00:00
prevent undefined behavior. Therefore, there are specializations of [^low_bits_mask_t] at those bit counts.
[endsect]
[section Example]
#include <boost/integer/integer_mask.hpp>
//...
int main()
{
typedef boost::high_bit_mask_t<29> mask1_type;
typedef boost::low_bits_mask_t<15> mask2_type;
mask1_type::least my_var1;
mask2_type::fast my_var2;
//...
my_var1 |= mask1_type::high_bit;
my_var2 &= mask2_type::sig_bits_fast;
//...
}
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Demonstration Program]
2018-10-26 12:05:47 -06:00
The program [@../../test/integer_mask_test.cpp integer_mask_test.cpp] is a simplistic demonstration of the
2009-11-25 12:38:09 +00:00
results from instantiating various examples of the bit mask class templates.
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Rationale]
2018-10-26 12:05:47 -06:00
The class templates in this header are an extension of the [link boost_integer.integer integer type selection class templates].
The new class templates provide the same sized types, but also convenient masks to use when extracting the
highest or all the significant bits when the containing built-in type contains more bits.
2009-11-26 15:43:32 +00:00
This prevents contamination of values by the higher, unused bits.
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Credits]
2021-01-20 11:34:42 +03:00
The author of the Boost bit mask class templates is [@https://www.boost.org/people/daryle_walker.html Daryle Walker].
2009-11-25 12:38:09 +00:00
[endsect]
[endsect]
2009-11-28 17:08:31 +00:00
[section:log2 Compile Time log2 Calculation]
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
The class template in [@../../../../boost/integer/static_log2.hpp <boost/integer/static_log2.hpp>]
2009-11-25 12:38:09 +00:00
determines the position of the highest bit in a given value. This facility is useful for solving generic programming problems.
[section Synopsis]
namespace boost
{
typedef ``['implementation-defined]`` static_log2_argument_type;
typedef ``['implementation-defined]`` static_log2_result_type;
template <static_log2_argument_type arg>
struct static_log2
{
static const static_log2_result_type value = ``['implementation-defined]``;
};
template < >
struct static_log2< 0 >
{
// The logarithm of zero is undefined.
};
} // namespace boost
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Usage]
2018-10-26 12:05:47 -06:00
The [^boost::static_log2] class template takes one template parameter, a value of type
[^static_log2_argument_type]. The template only defines one member, [^value], which gives the
2009-11-28 17:08:31 +00:00
truncated, base-two logarithm of the template argument.
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
Since the logarithm of zero, for any base, is undefined, there is a specialization of [^static_log2]
for a template argument of zero. This specialization has no members, so an attempt to use the base-two
2009-11-25 12:38:09 +00:00
logarithm of zero results in a compile-time error.
2018-10-26 12:05:47 -06:00
Note:
2009-11-25 12:38:09 +00:00
* [^static_log2_argument_type] is an ['unsigned integer type] (C++ standard, 3.9.1p3).
* [^static_log2_result_type] is an ['integer type] (C++ standard, 3.9.1p7).
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Demonstration Program]
2018-10-26 12:05:47 -06:00
The program [@../../test/static_log2_test.cpp static_log2_test.cpp] is a simplistic
2009-11-25 12:38:09 +00:00
demonstration of the results from instantiating various examples of the binary logarithm class template.
[endsect]
[section Rationale]
2018-10-26 12:05:47 -06:00
The base-two (binary) logarithm, abbreviated lb, function is occasionally used to give order-estimates
of computer algorithms. The truncated logarithm can be considered the highest power-of-two in a value,
which corresponds to the value's highest set bit (for binary integers). Sometimes the highest-bit position
2009-11-28 17:08:31 +00:00
could be used in generic programming, which requires the position to be available statically (['i.e.] at compile-time).
2009-11-25 12:38:09 +00:00
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Credits]
2018-10-26 12:05:47 -06:00
The original version of the Boost binary logarithm class template was
2021-01-20 11:34:42 +03:00
written by [@https://www.boost.org/people/daryle_walker.html Daryle Walker] and then
2018-10-26 12:05:47 -06:00
enhanced by Giovanni Bajo with support for compilers without partial template specialization.
The current version was suggested, together with a reference implementation, by Vesa Karvonen.
2009-11-25 12:38:09 +00:00
Gennaro Prota wrote the actual source file.
[endsect]
[endsect]
[section:minmax Compile time min/max calculation]
2018-10-26 12:05:47 -06:00
The class templates in [@../../../../boost/integer/static_min_max.hpp <boost/integer/static_min_max.hpp>]
provide a compile-time evaluation of the minimum or maximum of two integers. These facilities are useful
2009-11-25 12:38:09 +00:00
for generic programming problems.
[section Synopsis]
namespace boost
{
2018-10-26 12:05:47 -06:00
2009-11-25 12:38:09 +00:00
typedef ``['implementation-defined]`` static_min_max_signed_type;
typedef ``['implementation-defined]`` static_min_max_unsigned_type;
template <static_min_max_signed_type Value1, static_min_max_signed_type Value2 >
struct static_signed_min;
template <static_min_max_signed_type Value1, static_min_max_signed_type Value2>
struct static_signed_max;
template <static_min_max_unsigned_type Value1, static_min_max_unsigned_type Value2>
struct static_unsigned_min;
template <static_min_max_unsigned_type Value1, static_min_max_unsigned_type Value2>
struct static_unsigned_max;
}
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Usage]
2018-10-26 12:05:47 -06:00
The four class templates provide the combinations for finding the minimum or maximum of two [^signed] or
[^unsigned] ([^long]) parameters, /Value1/ and /Value2/, at compile-time. Each template has a single static data member,
2009-11-25 12:38:09 +00:00
[^value], which is set to the respective minimum or maximum of the template's parameters.
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Example]
#include <boost/integer/static_min_max.hpp>
template < unsigned long AddendSize1, unsigned long AddendSize2 >
class adder
{
public:
static unsigned long const addend1_size = AddendSize1;
static unsigned long const addend2_size = AddendSize2;
static unsigned long const sum_size = boost::static_unsigned_max<AddendSize1, AddendSize2>::value + 1;
typedef int addend1_type[ addend1_size ];
typedef int addend2_type[ addend2_size ];
typedef int sum_type[ sum_size ];
void operator ()( addend1_type const &a1, addend2_type const &a2, sum_type &s ) const;
};
//...
int main()
{
int const a1[] = { 0, 4, 3 }; // 340
int const a2[] = { 9, 8 }; // 89
int s[ 4 ];
adder<3,2> obj;
obj( a1, a2, s ); // 's' should be 429 or { 9, 2, 4, 0 }
//...
}
[endsect]
[section Demonstration Program]
2018-10-26 12:05:47 -06:00
The program [@../../test/static_min_max_test.cpp static_min_max_test.cpp] is a simplistic demonstration of
2009-11-25 12:38:09 +00:00
various comparisons using the compile-time extrema class templates.
2018-10-26 12:05:47 -06:00
[endsect]
2009-11-25 12:38:09 +00:00
[section Rationale]
2018-10-26 12:05:47 -06:00
Sometimes the minimum or maximum of several values needs to be found for later compile-time processing,
2009-11-25 12:38:09 +00:00
['e.g.] for a bound for another class template.
[endsect]
[section Credits]
2021-01-20 11:34:42 +03:00
The author of the Boost compile-time extrema class templates is [@https://www.boost.org/people/daryle_walker.html Daryle Walker].
2009-11-25 12:38:09 +00:00
[endsect]
[endsect]
[section:history History]
2014-06-01 20:45:44 +01:00
[h4 1.56.0]
* Moved `<boost/cstdint.hpp>` into [@boost:/libs/config/index.html
Boost.Config].
2009-11-25 12:38:09 +00:00
[h4 1.42.0]
* Reverted Trunk to release branch state (i.e. a "known good state").
2018-10-26 12:05:47 -06:00
* Fixed issues: [@https://svn.boost.org/trac/boost/ticket/653 653],
[@https://svn.boost.org/trac/boost/ticket/3084 3084],
[@https://svn.boost.org/trac/boost/ticket/3177 3177],
[@https://svn.boost.org/trac/boost/ticket/3180 3180],
[@https://svn.boost.org/trac/boost/ticket/3548 3568],
[@https://svn.boost.org/trac/boost/ticket/3657 3657],
2009-11-25 12:38:09 +00:00
[@https://svn.boost.org/trac/boost/ticket/2134 2134].
2018-10-26 12:05:47 -06:00
* Added long long support to [^boost::static_log2], [^boost::static_signed_min], [^boost::static_signed_max],
2009-11-25 12:38:09 +00:00
[^boost::static_unsigned_min][^boost::static_unsigned_max], when available.
2018-10-26 12:05:47 -06:00
* The argument type and the result type of [^boost::static_signed_min] etc are now typedef'd.
Formerly, they were hardcoded as [^unsigned long] and [^int] respectively. Please, use the
2009-11-25 12:38:09 +00:00
provided typedefs in new code (and update old code as soon as possible).
[h4 1.32.0]
2018-10-26 12:05:47 -06:00
* The argument type and the result type of [^boost::static_log2] are now typedef'd.
Formerly, they were hardcoded as [^unsigned long] and [^int] respectively. Please, use the
2009-11-25 12:38:09 +00:00
provided typedefs in new code (and update old code as soon as possible).
[endsect]
2014-06-01 20:56:15 +01:00
[section:cstdint Removed from library: Standard Integer Types]
2014-06-01 20:58:11 +01:00
The [@boost:/libs/config/doc/html/boost_config/cstdint.html Boost.Config] module provides
2014-06-01 20:56:15 +01:00
the typedefs useful for writing portable code that requires certain
integer widths.
[endsect]