]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/integer/static_log2.hpp
Logo AND Algorithmique Numérique Distribuée

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