1 | // Boost integer/integer_mask.hpp header file ------------------------------// |
2 | |
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) |
7 | |
8 | // See http://www.boost.org for updates, documentation, and revision history. |
9 | |
10 | #ifndef BOOST_INTEGER_INTEGER_MASK_HPP |
11 | #define BOOST_INTEGER_INTEGER_MASK_HPP |
12 | |
13 | #include <boost/integer_fwd.hpp> // self include |
14 | |
15 | #include <boost/config.hpp> // for BOOST_STATIC_CONSTANT |
16 | #include <boost/integer.hpp> // for boost::uint_t |
17 | |
18 | #include <climits> // for UCHAR_MAX, etc. |
19 | #include <cstddef> // for std::size_t |
20 | |
21 | #include <boost/limits.hpp> // for std::numeric_limits |
22 | |
23 | // |
24 | // We simply cannot include this header on gcc without getting copious warnings of the kind: |
25 | // |
26 | // boost/integer/integer_mask.hpp:93:35: warning: use of C99 long long integer constant |
27 | // |
28 | // And yet there is no other reasonable implementation, so we declare this a system header |
29 | // to suppress these warnings. |
30 | // |
31 | #if defined(__GNUC__) && (__GNUC__ >= 4) |
32 | #pragma GCC system_header |
33 | #endif |
34 | |
35 | namespace boost |
36 | { |
37 | |
38 | |
39 | // Specified single-bit mask class declaration -----------------------------// |
40 | // (Lowest bit starts counting at 0.) |
41 | |
42 | template < std::size_t Bit > |
43 | struct high_bit_mask_t |
44 | { |
45 | typedef typename uint_t<(Bit + 1)>::least least; |
46 | typedef typename uint_t<(Bit + 1)>::fast fast; |
47 | |
48 | BOOST_STATIC_CONSTANT( least, high_bit = (least( 1u ) << Bit) ); |
49 | BOOST_STATIC_CONSTANT( fast, high_bit_fast = (fast( 1u ) << Bit) ); |
50 | |
51 | BOOST_STATIC_CONSTANT( std::size_t, bit_position = Bit ); |
52 | |
53 | }; // boost::high_bit_mask_t |
54 | |
55 | |
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.) |
59 | |
60 | template < std::size_t Bits > |
61 | struct low_bits_mask_t |
62 | { |
63 | typedef typename uint_t<Bits>::least least; |
64 | typedef typename uint_t<Bits>::fast fast; |
65 | |
66 | BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) ); |
67 | BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); |
68 | |
69 | BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits ); |
70 | |
71 | }; // boost::low_bits_mask_t |
72 | |
73 | |
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 ); \ |
82 | } |
83 | |
84 | #ifdef BOOST_MSVC |
85 | #pragma warning(push) |
86 | #pragma warning(disable:4245) // 'initializing' : conversion from 'int' to 'const boost::low_bits_mask_t<8>::least', signed/unsigned mismatch |
87 | #endif |
88 | |
89 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned char ); |
90 | |
91 | #if USHRT_MAX > UCHAR_MAX |
92 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned short ); |
93 | #endif |
94 | |
95 | #if UINT_MAX > USHRT_MAX |
96 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned int ); |
97 | #endif |
98 | |
99 | #if ULONG_MAX > UINT_MAX |
100 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned long ); |
101 | #endif |
102 | |
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 ); |
109 | #endif |
110 | #elif defined(BOOST_HAS_MS_INT64) |
111 | #if 18446744073709551615ui64 > ULONG_MAX |
112 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned __int64 ); |
113 | #endif |
114 | #endif |
115 | |
116 | #ifdef BOOST_MSVC |
117 | #pragma warning(pop) |
118 | #endif |
119 | |
120 | #undef BOOST_LOW_BITS_MASK_SPECIALIZE |
121 | |
122 | |
123 | } // namespace boost |
124 | |
125 | |
126 | #endif // BOOST_INTEGER_INTEGER_MASK_HPP |
127 | |