1 // Boost integer/integer_mask.hpp header file ------------------------------//
3 // (C) Copyright Daryle Walker 2001.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org for updates, documentation, and revision history.
10 #ifndef BOOST_INTEGER_INTEGER_MASK_HPP
11 #define BOOST_INTEGER_INTEGER_MASK_HPP
13 #include <boost/integer_fwd.hpp> // self include
15 #include <boost/config.hpp> // for BOOST_STATIC_CONSTANT
16 #include <boost/cstdint.hpp> // for boost::uintmax_t
17 #include <boost/integer.hpp> // for boost::sized_integral
18 #include <boost/limits.hpp> // for std::numeric_limits
19 #include <boost/mpl/and.hpp> // for boost::mpl::and_
20 #include <boost/mpl/bitwise.hpp> // for boost::mpl::bitor_, shift_left
21 #include <boost/mpl/bool.hpp> // for boost::mpl::true_
22 #include <boost/mpl/comparison.hpp> // for boost::mpl::greater_equal, etc.
23 #include <boost/mpl/empty_base.hpp> // for boost::mpl::empty_base
24 #include <boost/mpl/if.hpp> // for boost::mpl::if_
25 #include <boost/mpl/int.hpp> // for boost::mpl::int_
26 #include <boost/mpl/integral_c.hpp> // for boost::integral_c
27 #include <boost/mpl/next_prior.hpp> // for boost::mpl::next, prior
28 #include <boost/utility/enable_if.hpp> // for boost::enable_if
30 #include <cstddef> // for std::size_t
38 // Helper templates --------------------------------------------------------//
41 struct hi_integer_mask_builder1
43 typedef boost::mpl::int_
<Bits
> bit_count_type
;
45 typedef typename
boost::mpl::next
<bit_count_type
>::type
47 typedef boost::sized_integral
<mask_length_type::value
, unsigned>
50 typedef boost::mpl::integral_c
<typename
mask_type::type
, 1> one_type
;
51 typedef boost::mpl::shift_left
<one_type
, bit_count_type
> result_type
;
55 struct hi_integer_mask_builder2
57 typedef boost::mpl::int_
<Bits
> bit_count_type
;
59 typedef boost::mpl::greater_equal
< bit_count_type
, boost::mpl::int_
<0> >
61 typedef boost::mpl::less
< bit_count_type
,
62 boost::mpl::int_
<std::numeric_limits
<boost::uintmax_t>::digits
> >
64 typedef boost::mpl::and_
<lo_bound_type
, hi_bound_type
> count_valid_type
;
67 template < int Bits
, class Enable
= void >
68 struct hi_integer_mask_builder3
70 BOOST_STATIC_CONSTANT( bool, is_specialized
= false );
74 struct hi_integer_mask_builder3
< Bits
, typename
boost::enable_if
<typename
75 hi_integer_mask_builder2
<Bits
>::count_valid_type
>::type
>
76 : hi_integer_mask_builder1
<Bits
>::result_type
78 BOOST_STATIC_CONSTANT( bool, is_specialized
= true );
82 struct lo_integer_mask_builder1
84 typedef boost::mpl::int_
<Bits
> bit_count_type
;
86 typedef typename
boost::mpl::prior
<bit_count_type
>::type
88 typedef boost::sized_integral
<bit_count_type::value
, unsigned>
91 typedef boost::mpl::integral_c
<typename
mask_type::type
, 1> one_type
;
92 typedef boost::mpl::shift_left
<one_type
, shift_length_type
>
94 typedef typename
boost::mpl::prior
<high_bit_type
>::type low_bits_type
;
95 typedef boost::mpl::bitor_
<high_bit_type
, low_bits_type
> result_type
;
99 struct lo_integer_mask_builder1
< 0 >
101 // Let's not deal with negative interim values....
102 typedef boost::mpl::integral_c
<unsigned char, 0u> result_type
;
105 template < int Bits
>
106 struct lo_integer_mask_builder2
108 typedef boost::mpl::int_
<Bits
> bit_count_type
;
110 typedef boost::mpl::greater_equal
< bit_count_type
, boost::mpl::int_
<0> >
112 typedef boost::mpl::less_equal
< bit_count_type
,
113 boost::mpl::int_
<std::numeric_limits
<boost::uintmax_t>::digits
> >
115 typedef boost::mpl::and_
<lo_bound_type
, hi_bound_type
> count_valid_type
;
119 struct lo_integer_mask_builder2
< 0 >
121 typedef boost::mpl::true_ count_valid_type
;
124 template < int Bits
, class Enable
= void >
125 struct lo_integer_mask_builder3
127 BOOST_STATIC_CONSTANT( bool, is_specialized
= false );
128 // No MPL Integral Constant to inherit from
131 template < int Bits
>
132 struct lo_integer_mask_builder3
< Bits
, typename enable_if
<typename
133 lo_integer_mask_builder2
<Bits
>::count_valid_type
>::type
>
134 : lo_integer_mask_builder1
<Bits
>::result_type
136 BOOST_STATIC_CONSTANT( bool, is_specialized
= true );
139 } // namespace detail
142 // MPL-compatible integer mask class templates -----------------------------//
144 // Displaced single-bit mask, 1 << Offset, 0 <= Offset < BitLengthOf(uintmax_t)
145 template < int Offset
>
146 struct integer_hi_mask
147 : detail::hi_integer_mask_builder3
<Offset
>
149 BOOST_STATIC_CONSTANT( int, bit_offset
= Offset
);
152 // Lowest bit-group mask, 2**Length - 1, 0 <= Length <= BitLengthOf(uintmax_t)
153 template < int Length
>
154 struct integer_lo_mask
155 : detail::lo_integer_mask_builder3
<Length
>
157 BOOST_STATIC_CONSTANT( int, bit_count
= Length
);
161 // Specified single-bit mask class declaration -----------------------------//
162 // (Lowest bit starts counting at 0.)
164 template < std::size_t Bit
>
165 class high_bit_mask_t
167 typedef integer_hi_mask
<Bit
> impl_type
;
170 typedef typename
impl_type::value_type least
;
171 typedef typename int_fast_t
<least
>::fast fast
;
173 BOOST_STATIC_CONSTANT( least
, high_bit
= impl_type::value
);
174 BOOST_STATIC_CONSTANT( fast
, high_bit_fast
= impl_type::value
);
176 BOOST_STATIC_CONSTANT( std::size_t, bit_position
= impl_type::bit_offset
);
178 }; // boost::high_bit_mask_t
181 // Specified bit-block mask class declaration ------------------------------//
182 // Makes masks for the lowest N bits
184 template < std::size_t Bits
>
185 class low_bits_mask_t
187 typedef integer_lo_mask
<Bits
> impl_type
;
190 typedef typename
impl_type::value_type least
;
191 typedef typename int_fast_t
<least
>::fast fast
;
193 BOOST_STATIC_CONSTANT( least
, sig_bits
= impl_type::value
);
194 BOOST_STATIC_CONSTANT( fast
, sig_bits_fast
= impl_type::value
);
196 BOOST_STATIC_CONSTANT( std::size_t, bit_count
= impl_type::bit_count
);
198 }; // boost::low_bits_mask_t
204 #endif // BOOST_INTEGER_INTEGER_MASK_HPP