os/ossrv/ossrv_pub/boost_apis/boost/integer/static_log2.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// -------------- Boost static_log2.hpp header file  ----------------------- //
sl@0
     2
//
sl@0
     3
//                 Copyright (C) 2001 Daryle Walker.
sl@0
     4
//                 Copyright (C) 2003 Vesa Karvonen.
sl@0
     5
//                 Copyright (C) 2003 Gennaro Prota.
sl@0
     6
//
sl@0
     7
//     Distributed under the Boost Software License, Version 1.0.
sl@0
     8
//        (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     9
//              http://www.boost.org/LICENSE_1_0.txt)
sl@0
    10
//
sl@0
    11
//         ---------------------------------------------------
sl@0
    12
//       See http://www.boost.org/libs/integer for documentation.
sl@0
    13
// ------------------------------------------------------------------------- //
sl@0
    14
sl@0
    15
sl@0
    16
#ifndef BOOST_INTEGER_STATIC_LOG2_HPP
sl@0
    17
#define BOOST_INTEGER_STATIC_LOG2_HPP
sl@0
    18
sl@0
    19
#include "boost/config.hpp" // for BOOST_STATIC_CONSTANT
sl@0
    20
sl@0
    21
namespace boost {
sl@0
    22
sl@0
    23
 namespace detail {
sl@0
    24
sl@0
    25
     namespace static_log2_impl {
sl@0
    26
sl@0
    27
     // choose_initial_n<>
sl@0
    28
     //
sl@0
    29
     // Recursively doubles its integer argument, until it
sl@0
    30
     // becomes >= of the "width" (C99, 6.2.6.2p4) of
sl@0
    31
     // static_log2_argument_type.
sl@0
    32
     //
sl@0
    33
     // Used to get the maximum power of two less then the width.
sl@0
    34
     //
sl@0
    35
     // Example: if on your platform argument_type has 48 value
sl@0
    36
     //          bits it yields n=32.
sl@0
    37
     //
sl@0
    38
     // It's easy to prove that, starting from such a value
sl@0
    39
     // of n, the core algorithm works correctly for any width
sl@0
    40
     // of static_log2_argument_type and that recursion always
sl@0
    41
     // terminates with x = 1 and n = 0 (see the algorithm's
sl@0
    42
     // invariant).
sl@0
    43
sl@0
    44
     typedef unsigned long argument_type;
sl@0
    45
     typedef          int  result_type;
sl@0
    46
sl@0
    47
sl@0
    48
     template <result_type n>
sl@0
    49
     struct choose_initial_n {
sl@0
    50
sl@0
    51
         enum { c = (argument_type(1) << n << n) != 0 };
sl@0
    52
         BOOST_STATIC_CONSTANT(
sl@0
    53
             result_type,
sl@0
    54
             value = !c*n + choose_initial_n<2*c*n>::value
sl@0
    55
         );
sl@0
    56
sl@0
    57
     };
sl@0
    58
sl@0
    59
     template <>
sl@0
    60
     struct choose_initial_n<0> {
sl@0
    61
         BOOST_STATIC_CONSTANT(result_type, value = 0);
sl@0
    62
     };
sl@0
    63
sl@0
    64
sl@0
    65
sl@0
    66
     // start computing from n_zero - must be a power of two
sl@0
    67
     const result_type n_zero = 16;
sl@0
    68
     const result_type initial_n = choose_initial_n<n_zero>::value;
sl@0
    69
sl@0
    70
     // static_log2_impl<>
sl@0
    71
     //
sl@0
    72
     // * Invariant:
sl@0
    73
     //                 2n
sl@0
    74
     //  1 <= x && x < 2    at the start of each recursion
sl@0
    75
     //                     (see also choose_initial_n<>)
sl@0
    76
     //
sl@0
    77
     // * Type requirements:
sl@0
    78
     //
sl@0
    79
     //   argument_type maybe any unsigned type with at least n_zero + 1
sl@0
    80
     //   value bits. (Note: If larger types will be standardized -e.g.
sl@0
    81
     //   unsigned long long- then the argument_type typedef can be
sl@0
    82
     //   changed without affecting the rest of the code.)
sl@0
    83
     //
sl@0
    84
sl@0
    85
     template <argument_type x, result_type n = initial_n>
sl@0
    86
     struct static_log2_impl {
sl@0
    87
sl@0
    88
         enum { c = (x >> n) > 0 }; // x >= 2**n ?
sl@0
    89
         BOOST_STATIC_CONSTANT(
sl@0
    90
             result_type,
sl@0
    91
             value = c*n + (static_log2_impl< (x>>c*n), n/2 >::value)
sl@0
    92
         );
sl@0
    93
sl@0
    94
     };
sl@0
    95
sl@0
    96
     template <>
sl@0
    97
     struct static_log2_impl<1, 0> {
sl@0
    98
        BOOST_STATIC_CONSTANT(result_type, value = 0);
sl@0
    99
     };
sl@0
   100
sl@0
   101
     }
sl@0
   102
 } // detail
sl@0
   103
sl@0
   104
sl@0
   105
sl@0
   106
 // --------------------------------------
sl@0
   107
 // static_log2<x>
sl@0
   108
 // ----------------------------------------
sl@0
   109
sl@0
   110
 typedef detail::static_log2_impl::argument_type static_log2_argument_type;
sl@0
   111
 typedef detail::static_log2_impl::result_type   static_log2_result_type;
sl@0
   112
sl@0
   113
sl@0
   114
 template <static_log2_argument_type x>
sl@0
   115
 struct static_log2 {
sl@0
   116
sl@0
   117
     BOOST_STATIC_CONSTANT(
sl@0
   118
         static_log2_result_type,
sl@0
   119
         value = detail::static_log2_impl::static_log2_impl<x>::value
sl@0
   120
     );
sl@0
   121
sl@0
   122
 };
sl@0
   123
sl@0
   124
sl@0
   125
 template <>
sl@0
   126
 struct static_log2<0> { };
sl@0
   127
sl@0
   128
}
sl@0
   129
sl@0
   130
sl@0
   131
sl@0
   132
#endif // include guard