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 | #ifdef BOOST_MSVC |
61 | #pragma warning(push) |
62 | #pragma warning(disable:4310) // cast truncates constant value |
63 | #endif |
64 | |
65 | template < std::size_t Bits > |
66 | struct low_bits_mask_t |
67 | { |
68 | typedef typename uint_t<Bits>::least least; |
69 | typedef typename uint_t<Bits>::fast fast; |
70 | |
71 | BOOST_STATIC_CONSTANT( least, sig_bits = least(~(least(~(least( 0u ))) << Bits )) ); |
72 | BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); |
73 | |
74 | BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits ); |
75 | |
76 | }; // boost::low_bits_mask_t |
77 | |
78 | #ifdef BOOST_MSVC |
79 | #pragma warning(pop) |
80 | #endif |
81 | |
82 | #define BOOST_LOW_BITS_MASK_SPECIALIZE( Type ) \ |
83 | template < > struct low_bits_mask_t< std::numeric_limits<Type>::digits > { \ |
84 | typedef std::numeric_limits<Type> limits_type; \ |
85 | typedef uint_t<limits_type::digits>::least least; \ |
86 | typedef uint_t<limits_type::digits>::fast fast; \ |
87 | BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) ); \ |
88 | BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); \ |
89 | BOOST_STATIC_CONSTANT( std::size_t, bit_count = limits_type::digits ); \ |
90 | } |
91 | |
92 | #ifdef BOOST_MSVC |
93 | #pragma warning(push) |
94 | #pragma warning(disable:4245) // 'initializing' : conversion from 'int' to 'const boost::low_bits_mask_t<8>::least', signed/unsigned mismatch |
95 | #endif |
96 | |
97 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned char ); |
98 | |
99 | #if USHRT_MAX > UCHAR_MAX |
100 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned short ); |
101 | #endif |
102 | |
103 | #if UINT_MAX > USHRT_MAX |
104 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned int ); |
105 | #endif |
106 | |
107 | #if ULONG_MAX > UINT_MAX |
108 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned long ); |
109 | #endif |
110 | |
111 | #if defined(BOOST_HAS_LONG_LONG) |
112 | #if ((defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)) ||\ |
113 | (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX > ULONG_MAX)) ||\ |
114 | (defined(ULONGLONG_MAX) && (ULONGLONG_MAX > ULONG_MAX)) ||\ |
115 | (defined(_ULLONG_MAX) && (_ULLONG_MAX > ULONG_MAX))) |
116 | BOOST_LOW_BITS_MASK_SPECIALIZE( boost::ulong_long_type ); |
117 | #endif |
118 | #elif defined(BOOST_HAS_MS_INT64) |
119 | #if 18446744073709551615ui64 > ULONG_MAX |
120 | BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned __int64 ); |
121 | #endif |
122 | #endif |
123 | |
124 | #ifdef BOOST_MSVC |
125 | #pragma warning(pop) |
126 | #endif |
127 | |
128 | #undef BOOST_LOW_BITS_MASK_SPECIALIZE |
129 | |
130 | |
131 | } // namespace boost |
132 | |
133 | |
134 | #endif // BOOST_INTEGER_INTEGER_MASK_HPP |
135 | |