Brian Silverman | d711929 | 2018-08-04 23:36:43 -0700 | [diff] [blame^] | 1 | [article Boost.Integer |
| 2 | [quickbook 1.6] |
| 3 | [compatibility-mode 1.5] |
| 4 | [copyright 2001-2009 Beman Dawes, Daryle Walker, Gennaro Prota, John Maddock] |
| 5 | [purpose Integer Type Selection] |
| 6 | [license |
| 7 | Distributed under the Boost Software License, Version 1.0. |
| 8 | (See accompanying file LICENSE_1_0.txt or copy at |
| 9 | [@http://www.boost.org/LICENSE_1_0.txt]) |
| 10 | ] |
| 11 | [authors [Dawes, Beman], [Walker, Daryle], [Prota, Gennaro], [Maddock, John]] |
| 12 | [/last-revision $Date: 2008-02-21 12:58:15 +0000 (Thu, 21 Feb 2008) $] |
| 13 | ] |
| 14 | |
| 15 | [template super[x]'''<superscript>'''[x]'''</superscript>'''] |
| 16 | |
| 17 | [section:overview Overview] |
| 18 | |
| 19 | Boost.Integer provides integer type support, particularly helpful in generic programming. |
| 20 | It provides the means to select an integer type based upon its properties, like the number of bits or |
| 21 | the maximum supported value, as well as compile-time bit mask selection. There is a derivative of |
| 22 | std::numeric_limits that provides integral constant expressions for `min` and `max`. |
| 23 | Finally, it provides two compile-time algorithms: determining the highest power of two in a |
| 24 | compile-time value; and computing min and max of constant expressions. |
| 25 | |
| 26 | [table |
| 27 | [[Component][Header][Purpose]] |
| 28 | [ |
| 29 | [Forward Declarations.] |
| 30 | [[^[@../../../../boost/integer_fwd.hpp <boost/integer_fwd.hpp>]]] |
| 31 | [Forward declarations of classes and class templates - for use when just the name of a class is needed.] |
| 32 | ] |
| 33 | [ |
| 34 | [[link boost_integer.traits Integer Traits].] |
| 35 | [[^[@../../../../boost/integer_traits.hpp <boost/integer_traits.hpp>]]] |
| 36 | [Class template [^boost::integer_traits], derives from [^std::numeric_limits] and adds [^const_min] and [^const_max] members.] |
| 37 | ] |
| 38 | [ |
| 39 | [[link boost_integer.integer Integer Type Selection].] |
| 40 | [[^[@../../../../boost/integer.hpp <boost/integer.hpp>]]] |
| 41 | [Templates for integer type selection based on properties such as maximum value or number of bits: |
| 42 | Use to select the type of an integer when some property such as maximum value or number of bits is known. |
| 43 | Useful for generic programming. ] |
| 44 | ] |
| 45 | [ |
| 46 | [[link boost_integer.gcd_lcm Greatest Common Divisor and Least Common Multiple].] |
| 47 | [[^[@../../../../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>]]] |
| 48 | [Functions `gcd` and `lcm` plus function objects and compile time versions.] |
| 49 | ] |
| 50 | [ |
| 51 | [[link boost_integer.mask Integer Masks].] |
| 52 | [[^[@../../../../boost/integer/integer_mask.hpp <boost/integer/integer_mask.hpp>]]] |
| 53 | [Templates for the selection of integer masks, single or lowest group, based on the number of bits: |
| 54 | Use to select a particular mask when the bit position(s) are based on a compile-time variable. Useful for generic programming. ] |
| 55 | ] |
| 56 | [ |
| 57 | [[link boost_integer.log2 Compile time log2 Calculation].] |
| 58 | [[^[@../../../../boost/integer/static_log2.hpp <boost/integer/static_log2.hpp>]]] |
| 59 | [Template for finding the highest power of two in a number: |
| 60 | Use to find the bit-size/range based on a maximum value. Useful for generic programming. ] |
| 61 | ] |
| 62 | [ |
| 63 | [[link boost_integer.minmax Compile time min/max calculation].] |
| 64 | [[^[@../../../../boost/integer/static_min_max.hpp <boost/integer/static_min_max.hpp>]]] |
| 65 | [Templates for finding the extrema of two numbers: |
| 66 | Use to find a bound based on a minimum or maximum value. Useful for generic programming. ] |
| 67 | ] |
| 68 | ] |
| 69 | |
| 70 | [endsect] |
| 71 | |
| 72 | [section:traits Integer Traits] |
| 73 | |
| 74 | [section Motivation] |
| 75 | |
| 76 | The C++ Standard Library <limits> header supplies a class template `numeric_limits<>` with specializations for each fundamental type. |
| 77 | |
| 78 | For integer types, the interesting members of `std::numeric_limits<>` are: |
| 79 | |
| 80 | static const bool is_specialized; // Will be true for integer types. |
| 81 | static T min() throw(); // Smallest representable value. |
| 82 | static T max() throw(); // Largest representable value. |
| 83 | static const int digits; // For integers, the number of value bits. |
| 84 | static const int digits10; // The number of base 10 digits that can be represented. |
| 85 | static const bool is_signed; // True if the type is signed. |
| 86 | static const bool is_integer; // Will be true for all integer types. |
| 87 | |
| 88 | For many uses, these are sufficient. |
| 89 | But min() and max() are problematical because they are not constant expressions (std::5.19), |
| 90 | yet some usages require constant expressions. |
| 91 | |
| 92 | The template class [^integer_traits] addresses this problem. |
| 93 | |
| 94 | [endsect] |
| 95 | |
| 96 | [section Synopsis] |
| 97 | |
| 98 | namespace boost { |
| 99 | template<class T> |
| 100 | class integer_traits : public std::numeric_limits<T> |
| 101 | { |
| 102 | public: |
| 103 | static const bool is_integral = false; |
| 104 | // |
| 105 | // These members are defined only if T is a built-in |
| 106 | // integal type: |
| 107 | // |
| 108 | static const T const_min = ``['implementation-defined]``; |
| 109 | static const T const_max = ``['implementation-defined]``; |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | [endsect] |
| 114 | |
| 115 | [section Description] |
| 116 | |
| 117 | Template class [^integer_traits] is derived from [^std::numeric_limits]. The primary specialization adds the single |
| 118 | [^bool] member [^is_integral] with the compile-time constant value [^false]. |
| 119 | However, for all integral types [^T] (std::3.9.1/7 [basic.fundamental]), there are specializations |
| 120 | provided with the following compile-time constants defined: |
| 121 | |
| 122 | [table |
| 123 | [[member][type][value]] |
| 124 | [[[^is_integral]][bool][[^true]]] |
| 125 | [[[^const_min]][[^T]][equivalent to [^std::numeric_limits<T>::min()]]] |
| 126 | [[[^const_max]][[^T]][equivalent to [^std::numeric_limits<T>::max()]]] |
| 127 | ] |
| 128 | |
| 129 | Note: The /is_integral/ flag is provided, because a user-defined integer class should specialize |
| 130 | [^std::numeric_limits<>::is_integer = true], while compile-time constants |
| 131 | [^const_min] and [^const_max] are not provided for that user-defined class, unless boost::integer_traits is also specialized. |
| 132 | |
| 133 | [endsect] |
| 134 | |
| 135 | [section Test Program] |
| 136 | |
| 137 | The program [^[@../../test/integer_traits_test.cpp integer_traits_test.cpp]] exercises the [^integer_traits] class. |
| 138 | |
| 139 | [endsect] |
| 140 | |
| 141 | [section Acknowledgements] |
| 142 | |
| 143 | Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers discussed the integer traits idea on the boost mailing list in August 1999. |
| 144 | |
| 145 | [endsect] |
| 146 | [endsect] |
| 147 | |
| 148 | [section:integer Integer Type Selection] |
| 149 | |
| 150 | The [@../../../../boost/integer.hpp <boost/integer.hpp>] type selection templates allow |
| 151 | integer types to be selected based on desired characteristics such as number of bits or maximum value. |
| 152 | This facility is particularly useful for solving generic programming problems. |
| 153 | |
| 154 | [section:synopsis Synopsis] |
| 155 | |
| 156 | namespace boost |
| 157 | { |
| 158 | // fast integers from least integers |
| 159 | template<typename LeastInt> |
| 160 | struct int_fast_t |
| 161 | { |
| 162 | typedef ``['implementation-defined-type]`` type; |
| 163 | }; |
| 164 | |
| 165 | // signed |
| 166 | template<int Bits> |
| 167 | struct int_t |
| 168 | { |
| 169 | /* Member exact may or may not be defined depending upon Bits */ |
| 170 | typedef ``['implementation-defined-type]`` exact; |
| 171 | typedef ``['implementation-defined-type]`` least; |
| 172 | typedef int_fast_t<least>::fast fast; |
| 173 | }; |
| 174 | |
| 175 | // unsigned |
| 176 | template<int Bits> |
| 177 | struct uint_t |
| 178 | { |
| 179 | /* Member exact may or may not be defined depending upon Bits */ |
| 180 | typedef ``['implementation-defined-type]`` exact; |
| 181 | typedef ``['implementation-defined-type]`` least; |
| 182 | typedef int_fast_t<least>::fast fast; |
| 183 | }; |
| 184 | |
| 185 | // signed |
| 186 | template<long long MaxValue> |
| 187 | struct int_max_value_t |
| 188 | { |
| 189 | typedef ``['implementation-defined-type]`` least; |
| 190 | typedef int_fast_t<least>::fast fast; |
| 191 | }; |
| 192 | |
| 193 | template<long long MinValue> |
| 194 | struct int_min_value_t |
| 195 | { |
| 196 | typedef ``['implementation-defined-type]`` least; |
| 197 | typedef int_fast_t<least>::fast fast; |
| 198 | }; |
| 199 | |
| 200 | // unsigned |
| 201 | template<unsigned long long Value> |
| 202 | struct uint_value_t |
| 203 | { |
| 204 | typedef ``['implementation-defined-type]`` least; |
| 205 | typedef int_fast_t<least>::fast fast; |
| 206 | }; |
| 207 | } // namespace boost |
| 208 | |
| 209 | [endsect] |
| 210 | |
| 211 | [section:easiest Easiest-to-Manipulate Types] |
| 212 | |
| 213 | The [^int_fast_t] class template maps its input type to the next-largest type that the processor |
| 214 | can manipulate the easiest, or to itself if the input type is already an easy-to-manipulate type. |
| 215 | For instance, processing a bunch of [^char] objects may go faster if they were converted to [^int] objects before processing. |
| 216 | The input type, passed as the only template parameter, must be a built-in integral type, except [^bool]. |
| 217 | Unsigned integral types can be used, as well as signed integral types. |
| 218 | The output type is given as the nested type [^fast]. |
| 219 | |
| 220 | [*Implementation Notes:] |
| 221 | By default, the output type is identical to the input type. Eventually, this code's implementation should |
| 222 | be customized for each platform to give accurate mappings between the built-in types and the easiest-to-manipulate |
| 223 | built-in types. Also, there is no guarantee that the output type actually is easier to manipulate than the input type. |
| 224 | |
| 225 | [endsect] |
| 226 | |
| 227 | [section:sized Sized Types] |
| 228 | |
| 229 | The [^int_t], [^uint_t], [^int_max_value_t], [^int_min_value_t], and [^uint_value_t] class templates find |
| 230 | the most appropiate built-in integral type for the given template parameter. This type is given by the |
| 231 | nested type [^least]. The easiest-to-manipulate version of that type is given by the nested type [^fast]. |
| 232 | The following table describes each template's criteria. |
| 233 | |
| 234 | [table Criteria for the Sized Type Class Templates |
| 235 | [ |
| 236 | [Class Template][Template Parameter Mapping] |
| 237 | ] |
| 238 | [ |
| 239 | [[^boost::int_t<N>::least]] |
| 240 | [The smallest, built-in, signed integral type with at least /N/ bits, including the sign bit. |
| 241 | The parameter should be a positive number. A compile-time error results if the parameter is |
| 242 | larger than the number of bits in the largest integer type.] |
| 243 | ] |
| 244 | [ |
| 245 | [[^boost::int_t<N>::fast]] |
| 246 | [The easiest-to-manipulate, built-in, signed integral type with at least /N/ bits, including the sign bit. |
| 247 | The parameter should be a positive number. A compile-time error results if the parameter is |
| 248 | larger than the number of bits in the largest integer type.] |
| 249 | ] |
| 250 | [ |
| 251 | [[^boost::int_t<N>::exact]] |
| 252 | [A built-in, signed integral type with exactly /N/ bits, including the sign bit. |
| 253 | The parameter should be a positive number. Note that the member /exact/ is defined |
| 254 | [*only] if there exists a type with exactly /N/ bits.] |
| 255 | ] |
| 256 | [ |
| 257 | [[^boost::uint_t<N>::least]] |
| 258 | [The smallest, built-in, unsigned integral type with at least /N/ bits. |
| 259 | The parameter should be a positive number. A compile-time error results if the |
| 260 | parameter is larger than the number of bits in the largest integer type.] |
| 261 | ] |
| 262 | [ |
| 263 | [[^boost::uint_t<N>::fast]] |
| 264 | [The easiest-to-manipulate, built-in, unsigned integral type with at least /N/ bits. |
| 265 | The parameter should be a positive number. A compile-time error results if the |
| 266 | parameter is larger than the number of bits in the largest integer type.] |
| 267 | ] |
| 268 | [ |
| 269 | [[^boost::uint_t<N>::exact]] |
| 270 | [A built-in, unsigned integral type with exactly /N/ bits. |
| 271 | The parameter should be a positive number. A compile-time error results if the |
| 272 | parameter is larger than the number of bits in the largest integer type. |
| 273 | Note that the member /exact/ is defined |
| 274 | [*only] if there exists a type with exactly N bits.] |
| 275 | ] |
| 276 | [ |
| 277 | [[^boost::int_max_value_t<V>::last]] |
| 278 | [The smallest, built-in, signed integral type that can hold all the values in the inclusive range ['0 - V]. |
| 279 | The parameter should be a positive number.] |
| 280 | ] |
| 281 | [ |
| 282 | [[^boost::int_max_value_t<V>::fast]] |
| 283 | [The easiest-to-manipulate, built-in, signed integral type that can hold all the values in the inclusive range ['0 - V]. |
| 284 | The parameter should be a positive number.] |
| 285 | ] |
| 286 | [ |
| 287 | [[^boost::int_min_value_t<V>::least]] |
| 288 | [The smallest, built-in, signed integral type that can hold all the values in the inclusive range ['V - 0]. |
| 289 | The parameter should be a negative number.] |
| 290 | ] |
| 291 | [ |
| 292 | [[^boost::int_min_value_t<V>::fast]] |
| 293 | [The easiest-to-manipulate, built-in, signed integral type that can hold all the values in the inclusive range ['V - 0]. |
| 294 | The parameter should be a negative number.] |
| 295 | ] |
| 296 | [ |
| 297 | [[^boost::uint_value_t<V>::least]] |
| 298 | [The smallest, built-in, unsigned integral type that can hold all positive values |
| 299 | up to and including /V/. The parameter should be a positive number.] |
| 300 | ] |
| 301 | [ |
| 302 | [[^boost::uint_value_t<V>::fast]] |
| 303 | [The easiest-to-manipulate, built-in, unsigned integral type that can hold all positive values |
| 304 | up to and including /V/. The parameter should be a positive number.] |
| 305 | ] |
| 306 | ] |
| 307 | |
| 308 | [endsect] |
| 309 | |
| 310 | [section Example] |
| 311 | |
| 312 | #include <boost/integer.hpp> |
| 313 | |
| 314 | //... |
| 315 | |
| 316 | int main() |
| 317 | { |
| 318 | boost::int_t<24>::least my_var; // my_var has at least 24-bits |
| 319 | //... |
| 320 | // This one is guarenteed not to be truncated: |
| 321 | boost::int_max_value_t<1000>::least my1000 = 1000; |
| 322 | //... |
| 323 | // This one is guarenteed not to be truncated, and as fast |
| 324 | // to manipulate as possible, its size may be greater than |
| 325 | // that of my1000: |
| 326 | boost::int_max_value_t<1000>::fast my_fast1000 = 1000; |
| 327 | } |
| 328 | |
| 329 | [endsect] |
| 330 | |
| 331 | [section Demonstration Program] |
| 332 | |
| 333 | The program [@../../test/integer_test.cpp integer_test.cpp] is a simplistic demonstration of the results from instantiating |
| 334 | various examples of the sized type class templates. |
| 335 | |
| 336 | [endsect] |
| 337 | |
| 338 | [section Rationale] |
| 339 | |
| 340 | The rationale for the design of the templates in this header includes: |
| 341 | |
| 342 | * Avoid recursion because of concern about C++'s limited guaranteed recursion depth (17). |
| 343 | * Avoid macros on general principles. |
| 344 | * Try to keep the design as simple as possible. |
| 345 | |
| 346 | [endsect] |
| 347 | |
| 348 | [section Alternative] |
| 349 | |
| 350 | If the number of bits required is known beforehand, it may be more appropriate to use the types supplied |
| 351 | in [@../../../../boost/cstdint.hpp <boost/cstdint.hpp>]. |
| 352 | |
| 353 | [endsect] |
| 354 | |
| 355 | [section Credits] |
| 356 | |
| 357 | The author of most of the Boost integer type choosing templates is |
| 358 | [@http://www.boost.org/people/beman_dawes.html Beman Dawes]. |
| 359 | He gives thanks to Valentin Bonnard and [@http://www.boost.org/people/kevlin_henney.htm Kevlin Henney] |
| 360 | for sharing their designs for similar templates. |
| 361 | [@http://www.boost.org/people/daryle_walker.html Daryle Walker] designed the value-based sized templates. |
| 362 | |
| 363 | [endsect] |
| 364 | [endsect] |
| 365 | |
| 366 | [include gcd/math-gcd.qbk] |
| 367 | |
| 368 | |
| 369 | [section:mask Integer Masks] |
| 370 | |
| 371 | [section Overview] |
| 372 | |
| 373 | The class templates in [@../../../../boost/integer/integer_mask.hpp <boost/integer/integer_mask.hpp>] |
| 374 | provide bit masks for a certain bit position or a contiguous-bit pack of a certain size. |
| 375 | The types of the masking constants come from the [link boost_integer.integer integer type selection templates] header. |
| 376 | |
| 377 | [endsect] |
| 378 | |
| 379 | [section Synopsis] |
| 380 | |
| 381 | #include <cstddef> // for std::size_t |
| 382 | |
| 383 | namespace boost |
| 384 | { |
| 385 | |
| 386 | template <std::size_t Bit> |
| 387 | struct high_bit_mask_t |
| 388 | { |
| 389 | typedef ``['implementation-defined-type]`` least; |
| 390 | typedef ``['implementation-defined-type]`` fast; |
| 391 | |
| 392 | static const least high_bit = ``['implementation-defined]``; |
| 393 | static const fast high_bit_fast = ``['implementation-defined]``; |
| 394 | |
| 395 | static const std::size_t bit_position = Bit; |
| 396 | }; |
| 397 | |
| 398 | template <std::size_t Bits> |
| 399 | struct low_bits_mask_t |
| 400 | { |
| 401 | typedef ``['implementation-defined-type]`` least; |
| 402 | typedef ``['implementation-defined-type]`` fast; |
| 403 | |
| 404 | static const least sig_bits = ``['implementation-defined]``; |
| 405 | static const fast sig_bits_fast = ``['implementation-defined]``; |
| 406 | |
| 407 | static const std::size_t bit_count = Bits; |
| 408 | }; |
| 409 | |
| 410 | // Specializations for low_bits_mask_t exist for certain bit counts. |
| 411 | |
| 412 | } // namespace boost |
| 413 | |
| 414 | [endsect] |
| 415 | |
| 416 | [section Single Bit-Mask Class Template] |
| 417 | |
| 418 | The [^boost::high_bit_mask_t] class template provides constants for bit masks representing the bit at a |
| 419 | certain position. The masks are equivalent to the value 2[super Bit], where [^Bit] is the template parameter. |
| 420 | The bit position must be a nonnegative number from zero to ['Max], where Max is one less than the |
| 421 | number of bits supported by the largest unsigned built-in integral type. The following table describes |
| 422 | the members of an instantiation of [^high_bit_mask_t]. |
| 423 | |
| 424 | [table Members of the `boost::high_bit_mask_t` Class Template |
| 425 | [[Member][Meaning]] |
| 426 | [[[^least]][The smallest, unsigned, built-in type that supports the given bit position.]] |
| 427 | [[[^fast]][The easiest-to-manipulate analog of [^least].]] |
| 428 | [[[^high_bit]][A [^least] constant of the value 2[super Bit].]] |
| 429 | [[[^high_bit_fast]][A [^fast] analog of [^high_bit].]] |
| 430 | [[[^bit_position]][The value of the template parameter, in case its needed from a renamed instantiation of the class template.]] |
| 431 | ] |
| 432 | |
| 433 | [endsect] |
| 434 | |
| 435 | [section Group Bit-Mask Class Template] |
| 436 | |
| 437 | The [^boost::low_bits_mask_t] class template provides constants for bit masks |
| 438 | equivalent to the value (2[super Bits] - 1), where [^Bits] is the template parameter. |
| 439 | The parameter [^Bits] must be a non-negative integer from |
| 440 | zero to ['Max], where Max is the number of bits supported by the largest, unsigned, built-in integral type. |
| 441 | The following table describes the members of [^low_bits_mask_t]. |
| 442 | |
| 443 | [table Members of the [^boost::low_bits_mask_t] Class Template |
| 444 | [[Member][Meaning]] |
| 445 | [[[^least]][The smallest, unsigned built-in type that supports the given bit count.]] |
| 446 | [[[^fast]][The easiest-to-manipulate analog of [^least].]] |
| 447 | [[[^sig_bits]][A [^least] constant of the desired bit-masking value.]] |
| 448 | [[[^sig_bits_fast]][A [^fast] analog of [^sig_bits].]] |
| 449 | [[[^bit_count]][The value of the template parameter, in case its needed from a renamed instantiation of the class template.]] |
| 450 | ] |
| 451 | |
| 452 | [endsect] |
| 453 | |
| 454 | [section Implementation Notes] |
| 455 | |
| 456 | When [^Bits] is the exact size of a built-in unsigned type, the implementation has to change to |
| 457 | prevent undefined behavior. Therefore, there are specializations of [^low_bits_mask_t] at those bit counts. |
| 458 | |
| 459 | [endsect] |
| 460 | |
| 461 | [section Example] |
| 462 | |
| 463 | #include <boost/integer/integer_mask.hpp> |
| 464 | |
| 465 | //... |
| 466 | |
| 467 | int main() |
| 468 | { |
| 469 | typedef boost::high_bit_mask_t<29> mask1_type; |
| 470 | typedef boost::low_bits_mask_t<15> mask2_type; |
| 471 | |
| 472 | mask1_type::least my_var1; |
| 473 | mask2_type::fast my_var2; |
| 474 | //... |
| 475 | |
| 476 | my_var1 |= mask1_type::high_bit; |
| 477 | my_var2 &= mask2_type::sig_bits_fast; |
| 478 | |
| 479 | //... |
| 480 | } |
| 481 | |
| 482 | [endsect] |
| 483 | |
| 484 | [section Demonstration Program] |
| 485 | |
| 486 | The program [@../../test/integer_mask_test.cpp integer_mask_test.cpp] is a simplistic demonstration of the |
| 487 | results from instantiating various examples of the bit mask class templates. |
| 488 | |
| 489 | [endsect] |
| 490 | |
| 491 | [section Rationale] |
| 492 | |
| 493 | The class templates in this header are an extension of the [link boost_integer.integer integer type selection class templates]. |
| 494 | The new class templates provide the same sized types, but also convenient masks to use when extracting the |
| 495 | highest or all the significant bits when the containing built-in type contains more bits. |
| 496 | This prevents contamination of values by the higher, unused bits. |
| 497 | |
| 498 | [endsect] |
| 499 | |
| 500 | [section Credits] |
| 501 | |
| 502 | The author of the Boost bit mask class templates is [@http://www.boost.org/people/daryle_walker.html Daryle Walker]. |
| 503 | |
| 504 | [endsect] |
| 505 | [endsect] |
| 506 | |
| 507 | [section:log2 Compile Time log2 Calculation] |
| 508 | |
| 509 | The class template in [@../../../../boost/integer/static_log2.hpp <boost/integer/static_log2.hpp>] |
| 510 | determines the position of the highest bit in a given value. This facility is useful for solving generic programming problems. |
| 511 | |
| 512 | [section Synopsis] |
| 513 | |
| 514 | namespace boost |
| 515 | { |
| 516 | |
| 517 | typedef ``['implementation-defined]`` static_log2_argument_type; |
| 518 | typedef ``['implementation-defined]`` static_log2_result_type; |
| 519 | |
| 520 | template <static_log2_argument_type arg> |
| 521 | struct static_log2 |
| 522 | { |
| 523 | static const static_log2_result_type value = ``['implementation-defined]``; |
| 524 | }; |
| 525 | |
| 526 | |
| 527 | template < > |
| 528 | struct static_log2< 0 > |
| 529 | { |
| 530 | // The logarithm of zero is undefined. |
| 531 | }; |
| 532 | |
| 533 | |
| 534 | } // namespace boost |
| 535 | |
| 536 | [endsect] |
| 537 | |
| 538 | [section Usage] |
| 539 | |
| 540 | The [^boost::static_log2] class template takes one template parameter, a value of type |
| 541 | [^static_log2_argument_type]. The template only defines one member, [^value], which gives the |
| 542 | truncated, base-two logarithm of the template argument. |
| 543 | |
| 544 | Since the logarithm of zero, for any base, is undefined, there is a specialization of [^static_log2] |
| 545 | for a template argument of zero. This specialization has no members, so an attempt to use the base-two |
| 546 | logarithm of zero results in a compile-time error. |
| 547 | |
| 548 | Note: |
| 549 | |
| 550 | * [^static_log2_argument_type] is an ['unsigned integer type] (C++ standard, 3.9.1p3). |
| 551 | * [^static_log2_result_type] is an ['integer type] (C++ standard, 3.9.1p7). |
| 552 | |
| 553 | [endsect] |
| 554 | |
| 555 | [section Demonstration Program] |
| 556 | |
| 557 | The program [@../../test/static_log2_test.cpp static_log2_test.cpp] is a simplistic |
| 558 | demonstration of the results from instantiating various examples of the binary logarithm class template. |
| 559 | |
| 560 | [endsect] |
| 561 | |
| 562 | [section Rationale] |
| 563 | |
| 564 | The base-two (binary) logarithm, abbreviated lb, function is occasionally used to give order-estimates |
| 565 | of computer algorithms. The truncated logarithm can be considered the highest power-of-two in a value, |
| 566 | which corresponds to the value's highest set bit (for binary integers). Sometimes the highest-bit position |
| 567 | could be used in generic programming, which requires the position to be available statically (['i.e.] at compile-time). |
| 568 | |
| 569 | [endsect] |
| 570 | |
| 571 | [section Credits] |
| 572 | |
| 573 | The original version of the Boost binary logarithm class template was |
| 574 | written by [@http://www.boost.org/people/daryle_walker.html Daryle Walker] and then |
| 575 | enhanced by Giovanni Bajo with support for compilers without partial template specialization. |
| 576 | The current version was suggested, together with a reference implementation, by Vesa Karvonen. |
| 577 | Gennaro Prota wrote the actual source file. |
| 578 | |
| 579 | [endsect] |
| 580 | [endsect] |
| 581 | |
| 582 | [section:minmax Compile time min/max calculation] |
| 583 | |
| 584 | The class templates in [@../../../../boost/integer/static_min_max.hpp <boost/integer/static_min_max.hpp>] |
| 585 | provide a compile-time evaluation of the minimum or maximum of two integers. These facilities are useful |
| 586 | for generic programming problems. |
| 587 | |
| 588 | [section Synopsis] |
| 589 | |
| 590 | namespace boost |
| 591 | { |
| 592 | |
| 593 | typedef ``['implementation-defined]`` static_min_max_signed_type; |
| 594 | typedef ``['implementation-defined]`` static_min_max_unsigned_type; |
| 595 | |
| 596 | template <static_min_max_signed_type Value1, static_min_max_signed_type Value2 > |
| 597 | struct static_signed_min; |
| 598 | |
| 599 | template <static_min_max_signed_type Value1, static_min_max_signed_type Value2> |
| 600 | struct static_signed_max; |
| 601 | |
| 602 | template <static_min_max_unsigned_type Value1, static_min_max_unsigned_type Value2> |
| 603 | struct static_unsigned_min; |
| 604 | |
| 605 | template <static_min_max_unsigned_type Value1, static_min_max_unsigned_type Value2> |
| 606 | struct static_unsigned_max; |
| 607 | |
| 608 | } |
| 609 | |
| 610 | [endsect] |
| 611 | |
| 612 | [section Usage] |
| 613 | |
| 614 | The four class templates provide the combinations for finding the minimum or maximum of two [^signed] or |
| 615 | [^unsigned] ([^long]) parameters, /Value1/ and /Value2/, at compile-time. Each template has a single static data member, |
| 616 | [^value], which is set to the respective minimum or maximum of the template's parameters. |
| 617 | |
| 618 | [endsect] |
| 619 | |
| 620 | [section Example] |
| 621 | |
| 622 | #include <boost/integer/static_min_max.hpp> |
| 623 | |
| 624 | template < unsigned long AddendSize1, unsigned long AddendSize2 > |
| 625 | class adder |
| 626 | { |
| 627 | public: |
| 628 | static unsigned long const addend1_size = AddendSize1; |
| 629 | static unsigned long const addend2_size = AddendSize2; |
| 630 | static unsigned long const sum_size = boost::static_unsigned_max<AddendSize1, AddendSize2>::value + 1; |
| 631 | |
| 632 | typedef int addend1_type[ addend1_size ]; |
| 633 | typedef int addend2_type[ addend2_size ]; |
| 634 | typedef int sum_type[ sum_size ]; |
| 635 | |
| 636 | void operator ()( addend1_type const &a1, addend2_type const &a2, sum_type &s ) const; |
| 637 | }; |
| 638 | |
| 639 | //... |
| 640 | |
| 641 | int main() |
| 642 | { |
| 643 | int const a1[] = { 0, 4, 3 }; // 340 |
| 644 | int const a2[] = { 9, 8 }; // 89 |
| 645 | int s[ 4 ]; |
| 646 | adder<3,2> obj; |
| 647 | |
| 648 | obj( a1, a2, s ); // 's' should be 429 or { 9, 2, 4, 0 } |
| 649 | //... |
| 650 | } |
| 651 | |
| 652 | [endsect] |
| 653 | |
| 654 | [section Demonstration Program] |
| 655 | |
| 656 | The program [@../../test/static_min_max_test.cpp static_min_max_test.cpp] is a simplistic demonstration of |
| 657 | various comparisons using the compile-time extrema class templates. |
| 658 | |
| 659 | [endsect] |
| 660 | |
| 661 | [section Rationale] |
| 662 | |
| 663 | Sometimes the minimum or maximum of several values needs to be found for later compile-time processing, |
| 664 | ['e.g.] for a bound for another class template. |
| 665 | |
| 666 | [endsect] |
| 667 | |
| 668 | [section Credits] |
| 669 | |
| 670 | The author of the Boost compile-time extrema class templates is [@http://www.boost.org/people/daryle_walker.html Daryle Walker]. |
| 671 | |
| 672 | [endsect] |
| 673 | [endsect] |
| 674 | |
| 675 | [section:history History] |
| 676 | |
| 677 | [h4 1.56.0] |
| 678 | |
| 679 | * Moved `<boost/cstdint.hpp>` into [@boost:/libs/config/index.html |
| 680 | Boost.Config]. |
| 681 | |
| 682 | [h4 1.42.0] |
| 683 | |
| 684 | * Reverted Trunk to release branch state (i.e. a "known good state"). |
| 685 | * Fixed issues: [@https://svn.boost.org/trac/boost/ticket/653 653], |
| 686 | [@https://svn.boost.org/trac/boost/ticket/3084 3084], |
| 687 | [@https://svn.boost.org/trac/boost/ticket/3177 3177], |
| 688 | [@https://svn.boost.org/trac/boost/ticket/3180 3180], |
| 689 | [@https://svn.boost.org/trac/boost/ticket/3548 3568], |
| 690 | [@https://svn.boost.org/trac/boost/ticket/3657 3657], |
| 691 | [@https://svn.boost.org/trac/boost/ticket/2134 2134]. |
| 692 | * Added long long support to [^boost::static_log2], [^boost::static_signed_min], [^boost::static_signed_max], |
| 693 | [^boost::static_unsigned_min][^boost::static_unsigned_max], when available. |
| 694 | * The argument type and the result type of [^boost::static_signed_min] etc are now typedef'd. |
| 695 | Formerly, they were hardcoded as [^unsigned long] and [^int] respectively. Please, use the |
| 696 | provided typedefs in new code (and update old code as soon as possible). |
| 697 | |
| 698 | [h4 1.32.0] |
| 699 | |
| 700 | * The argument type and the result type of [^boost::static_log2] are now typedef'd. |
| 701 | Formerly, they were hardcoded as [^unsigned long] and [^int] respectively. Please, use the |
| 702 | provided typedefs in new code (and update old code as soon as possible). |
| 703 | |
| 704 | [endsect] |
| 705 | |
| 706 | [section:cstdint Removed from library: Standard Integer Types] |
| 707 | |
| 708 | The [@boost:/libs/config/doc/html/boost_config/cstdint.html Boost.Config] module provides |
| 709 | the typedefs useful for writing portable code that requires certain |
| 710 | integer widths. |
| 711 | |
| 712 | [endsect] |