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
24 // We simply cannot include this header on gcc without getting copious warnings of the kind:
26 // boost/integer/integer_mask.hpp:93:35: warning: use of C99 long long integer constant
28 // And yet there is no other reasonable implementation, so we declare this a system header
29 // to suppress these warnings.
31 #if defined(__GNUC__) && (__GNUC__ >= 4)
32 #pragma GCC system_header
39 // Specified single-bit mask class declaration -----------------------------//
40 // (Lowest bit starts counting at 0.)
42 template < std::size_t Bit >
43 struct high_bit_mask_t
45 typedef typename uint_t<(Bit + 1)>::least least;
46 typedef typename uint_t<(Bit + 1)>::fast fast;
48 BOOST_STATIC_CONSTANT( least, high_bit = (least( 1u ) << Bit) );
49 BOOST_STATIC_CONSTANT( fast, high_bit_fast = (fast( 1u ) << Bit) );
51 BOOST_STATIC_CONSTANT( std::size_t, bit_position = Bit );
53 }; // boost::high_bit_mask_t
56 // Specified bit-block mask class declaration ------------------------------//
57 // Makes masks for the lowest N bits
58 // (Specializations are needed when N fills up a type.)
60 template < std::size_t Bits >
61 struct low_bits_mask_t
63 typedef typename uint_t<Bits>::least least;
64 typedef typename uint_t<Bits>::fast fast;
66 BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );
67 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
69 BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
71 }; // boost::low_bits_mask_t
74 #define BOOST_LOW_BITS_MASK_SPECIALIZE( Type ) \
75 template < > struct low_bits_mask_t< std::numeric_limits<Type>::digits > { \
76 typedef std::numeric_limits<Type> limits_type; \
77 typedef uint_t<limits_type::digits>::least least; \
78 typedef uint_t<limits_type::digits>::fast fast; \
79 BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) ); \
80 BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); \
81 BOOST_STATIC_CONSTANT( std::size_t, bit_count = limits_type::digits ); \
86 #pragma warning(disable:4245) // 'initializing' : conversion from 'int' to 'const boost::low_bits_mask_t<8>::least', signed/unsigned mismatch
89 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned char );
91 #if USHRT_MAX > UCHAR_MAX
92 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned short );
95 #if UINT_MAX > USHRT_MAX
96 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned int );
99 #if ULONG_MAX > UINT_MAX
100 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned long );
103 #if defined(BOOST_HAS_LONG_LONG)
104 #if ((defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)) ||\
105 (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX > ULONG_MAX)) ||\
106 (defined(ULONGLONG_MAX) && (ULONGLONG_MAX > ULONG_MAX)) ||\
107 (defined(_ULLONG_MAX) && (_ULLONG_MAX > ULONG_MAX)))
108 BOOST_LOW_BITS_MASK_SPECIALIZE( boost::ulong_long_type );
110 #elif defined(BOOST_HAS_MS_INT64)
111 #if 18446744073709551615ui64 > ULONG_MAX
112 BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned __int64 );
120 #undef BOOST_LOW_BITS_MASK_SPECIALIZE
126 #endif // BOOST_INTEGER_INTEGER_MASK_HPP