]> git.lyx.org Git - lyx.git/blob - boost/boost/integer/static_log2.hpp
major boost update
[lyx.git] / boost / boost / integer / static_log2.hpp
1 //  Boost integer/static_log2.hpp header file  -------------------------------//
2
3 //  (C) Copyright Daryle Walker 2001.  Permission to copy, use, modify, sell and
4 //  distribute this software is granted provided this copyright notice appears 
5 //  in all copies.  This software is provided "as is" without express or
6 //  implied warranty, and with no claim as to its suitability for any purpose. 
7
8 //  See http://www.boost.org for updates, documentation, and revision history. 
9
10 #ifndef BOOST_INTEGER_STATIC_LOG2_HPP
11 #define BOOST_INTEGER_STATIC_LOG2_HPP
12
13 #include <boost/integer_fwd.hpp>  // self include
14
15 #include <boost/config.hpp>  // for BOOST_STATIC_CONSTANT
16 #include <boost/limits.hpp>  // for std::numeric_limits
17
18
19 namespace boost
20 {
21
22
23 //  Implementation details  --------------------------------------------------//
24
25 namespace detail
26 {
27
28 // Forward declarations
29 template < unsigned long Val, int Place = 0, int Index
30  = std::numeric_limits<unsigned long>::digits >
31     struct static_log2_helper_t;
32
33 template < unsigned long Val, int Place >
34     struct static_log2_helper_t< Val, Place, 1 >;
35
36 // Recursively build the logarithm by examining the upper bits
37 template < unsigned long Val, int Place, int Index >
38 struct static_log2_helper_t
39 {
40 private:
41     BOOST_STATIC_CONSTANT( int, half_place = Index / 2 );
42     BOOST_STATIC_CONSTANT( unsigned long, lower_mask = (1ul << half_place)
43      - 1ul );
44     BOOST_STATIC_CONSTANT( unsigned long, upper_mask = ~lower_mask );
45     BOOST_STATIC_CONSTANT( bool, do_shift = (Val & upper_mask) != 0ul );
46
47     BOOST_STATIC_CONSTANT( unsigned long, new_val = do_shift ? (Val
48      >> half_place) : Val );
49     BOOST_STATIC_CONSTANT( int, new_place = do_shift ? (Place + half_place)
50      : Place );
51     BOOST_STATIC_CONSTANT( int, new_index = Index - half_place );
52
53     typedef static_log2_helper_t<new_val, new_place, new_index>  next_step_type;
54
55 public:
56     BOOST_STATIC_CONSTANT( int, value = next_step_type::value );
57
58 };  // boost::detail::static_log2_helper_t
59
60 // Non-recursive case
61 template < unsigned long Val, int Place >
62 struct static_log2_helper_t< Val, Place, 1 >
63 {
64 public:
65     BOOST_STATIC_CONSTANT( int, value = Place );
66
67 };  // boost::detail::static_log2_helper_t
68
69 }  // namespace detail
70
71
72 //  Compile-time log-base-2 evaluator class declaration  ---------------------//
73
74 template < unsigned long Value >
75 struct static_log2
76 {
77     BOOST_STATIC_CONSTANT( int, value
78      = detail::static_log2_helper_t<Value>::value );
79 };
80
81 template < >
82 struct static_log2< 0ul >
83 {
84     // The logarithm of zero is undefined.
85 };
86
87
88 }  // namespace boost
89
90
91 #endif  // BOOST_INTEGER_STATIC_LOG2_HPP