Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame] | 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 NDNBOOST_INTEGER_INTEGER_MASK_HPP |
| 11 | #define NDNBOOST_INTEGER_INTEGER_MASK_HPP |
| 12 | |
| 13 | #include <ndnboost/integer_fwd.hpp> // self include |
| 14 | |
| 15 | #include <ndnboost/config.hpp> // for NDNBOOST_STATIC_CONSTANT |
| 16 | #include <ndnboost/integer.hpp> // for ndnboost::uint_t |
| 17 | |
| 18 | #include <climits> // for UCHAR_MAX, etc. |
| 19 | #include <cstddef> // for std::size_t |
| 20 | |
| 21 | #include <ndnboost/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 | // ndnboost/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 ndnboost |
| 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 | NDNBOOST_STATIC_CONSTANT( least, high_bit = (least( 1u ) << Bit) ); |
| 49 | NDNBOOST_STATIC_CONSTANT( fast, high_bit_fast = (fast( 1u ) << Bit) ); |
| 50 | |
| 51 | NDNBOOST_STATIC_CONSTANT( std::size_t, bit_position = Bit ); |
| 52 | |
| 53 | }; // ndnboost::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 | NDNBOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) ); |
| 67 | NDNBOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); |
| 68 | |
| 69 | NDNBOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits ); |
| 70 | |
| 71 | }; // ndnboost::low_bits_mask_t |
| 72 | |
| 73 | |
| 74 | #define NDNBOOST_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 | NDNBOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) ); \ |
| 80 | NDNBOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); \ |
| 81 | NDNBOOST_STATIC_CONSTANT( std::size_t, bit_count = limits_type::digits ); \ |
| 82 | } |
| 83 | |
| 84 | #ifdef NDNBOOST_MSVC |
| 85 | #pragma warning(push) |
| 86 | #pragma warning(disable:4245) // 'initializing' : conversion from 'int' to 'const ndnboost::low_bits_mask_t<8>::least', signed/unsigned mismatch |
| 87 | #endif |
| 88 | |
| 89 | NDNBOOST_LOW_BITS_MASK_SPECIALIZE( unsigned char ); |
| 90 | |
| 91 | #if USHRT_MAX > UCHAR_MAX |
| 92 | NDNBOOST_LOW_BITS_MASK_SPECIALIZE( unsigned short ); |
| 93 | #endif |
| 94 | |
| 95 | #if UINT_MAX > USHRT_MAX |
| 96 | NDNBOOST_LOW_BITS_MASK_SPECIALIZE( unsigned int ); |
| 97 | #endif |
| 98 | |
| 99 | #if ULONG_MAX > UINT_MAX |
| 100 | NDNBOOST_LOW_BITS_MASK_SPECIALIZE( unsigned long ); |
| 101 | #endif |
| 102 | |
| 103 | #if defined(NDNBOOST_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 | NDNBOOST_LOW_BITS_MASK_SPECIALIZE( ndnboost::ulong_long_type ); |
| 109 | #endif |
| 110 | #elif defined(NDNBOOST_HAS_MS_INT64) |
| 111 | #if 18446744073709551615ui64 > ULONG_MAX |
| 112 | NDNBOOST_LOW_BITS_MASK_SPECIALIZE( unsigned __int64 ); |
| 113 | #endif |
| 114 | #endif |
| 115 | |
| 116 | #ifdef NDNBOOST_MSVC |
| 117 | #pragma warning(pop) |
| 118 | #endif |
| 119 | |
| 120 | #undef NDNBOOST_LOW_BITS_MASK_SPECIALIZE |
| 121 | |
| 122 | |
| 123 | } // namespace ndnboost |
| 124 | |
| 125 | |
| 126 | #endif // NDNBOOST_INTEGER_INTEGER_MASK_HPP |