First public contribution.
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/integer.hpp> // for boost::uint_t
18 #include <climits> // for UCHAR_MAX, etc.
19 #include <cstddef> // for std::size_t
21 #include <boost/limits.hpp> // for std::numeric_limits
28 // Specified single-bit mask class declaration -----------------------------//
29 // (Lowest bit starts counting at 0.)
31 template < std::size_t Bit >
32 struct high_bit_mask_t
34 typedef typename uint_t<(Bit + 1)>::least least;
35 typedef typename uint_t<(Bit + 1)>::fast fast;
37 BOOST_STATIC_CONSTANT( least, high_bit = (least( 1u ) << Bit) );
38 BOOST_STATIC_CONSTANT( fast, high_bit_fast = (fast( 1u ) << Bit) );
40 BOOST_STATIC_CONSTANT( std::size_t, bit_position = Bit );
42 }; // boost::high_bit_mask_t
45 // Specified bit-block mask class declaration ------------------------------//
46 // Makes masks for the lowest N bits
47 // (Specializations are needed when N fills up a type.)
49 template < std::size_t Bits >
50 struct low_bits_mask_t
52 typedef typename uint_t<Bits>::least least;
53 typedef typename uint_t<Bits>::fast fast;
55 BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );
56 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
58 BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
60 }; // boost::low_bits_mask_t
63 #define BOOST_LOW_BITS_MASK_SPECIALIZE( Type ) \
64 template < > struct low_bits_mask_t< std::numeric_limits<Type>::digits > { \
65 typedef std::numeric_limits<Type> limits_type; \
66 typedef uint_t<limits_type::digits>::least least; \
67 typedef uint_t<limits_type::digits>::fast fast; \
68 BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) ); \
69 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); \
70 BOOST_STATIC_CONSTANT( std::size_t, bit_count = limits_type::digits ); \
73 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned char );
75 #if USHRT_MAX > UCHAR_MAX
76 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned short );
79 #if UINT_MAX > USHRT_MAX
80 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned int );
83 #if ULONG_MAX > UINT_MAX
84 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned long );
87 #undef BOOST_LOW_BITS_MASK_SPECIALIZE
93 #endif // BOOST_INTEGER_INTEGER_MASK_HPP