integer_mask.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Boost integer/integer_mask.hpp header file ------------------------------//
  2. // (C) Copyright Daryle Walker 2001.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #ifndef BOOST_INTEGER_INTEGER_MASK_HPP
  8. #define BOOST_INTEGER_INTEGER_MASK_HPP
  9. #include <boost/integer_fwd.hpp> // self include
  10. #include <boost/config.hpp> // for BOOST_STATIC_CONSTANT
  11. #include <boost/integer.hpp> // for boost::uint_t
  12. #include <climits> // for UCHAR_MAX, etc.
  13. #include <cstddef> // for std::size_t
  14. #include <boost/limits.hpp> // for std::numeric_limits
  15. //
  16. // We simply cannot include this header on gcc without getting copious warnings of the kind:
  17. //
  18. // boost/integer/integer_mask.hpp:93:35: warning: use of C99 long long integer constant
  19. //
  20. // And yet there is no other reasonable implementation, so we declare this a system header
  21. // to suppress these warnings.
  22. //
  23. #if defined(__GNUC__) && (__GNUC__ >= 4)
  24. #pragma GCC system_header
  25. #endif
  26. namespace boost
  27. {
  28. // Specified single-bit mask class declaration -----------------------------//
  29. // (Lowest bit starts counting at 0.)
  30. template < std::size_t Bit >
  31. struct high_bit_mask_t
  32. {
  33. typedef typename uint_t<(Bit + 1)>::least least;
  34. typedef typename uint_t<(Bit + 1)>::fast fast;
  35. BOOST_STATIC_CONSTANT( least, high_bit = (least( 1u ) << Bit) );
  36. BOOST_STATIC_CONSTANT( fast, high_bit_fast = (fast( 1u ) << Bit) );
  37. BOOST_STATIC_CONSTANT( std::size_t, bit_position = Bit );
  38. }; // boost::high_bit_mask_t
  39. // Specified bit-block mask class declaration ------------------------------//
  40. // Makes masks for the lowest N bits
  41. // (Specializations are needed when N fills up a type.)
  42. template < std::size_t Bits >
  43. struct low_bits_mask_t
  44. {
  45. typedef typename uint_t<Bits>::least least;
  46. typedef typename uint_t<Bits>::fast fast;
  47. BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );
  48. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
  49. BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
  50. }; // boost::low_bits_mask_t
  51. #define BOOST_LOW_BITS_MASK_SPECIALIZE( Type ) \
  52. template < > struct low_bits_mask_t< std::numeric_limits<Type>::digits > { \
  53. typedef std::numeric_limits<Type> limits_type; \
  54. typedef uint_t<limits_type::digits>::least least; \
  55. typedef uint_t<limits_type::digits>::fast fast; \
  56. BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) ); \
  57. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); \
  58. BOOST_STATIC_CONSTANT( std::size_t, bit_count = limits_type::digits ); \
  59. }
  60. #ifdef BOOST_MSVC
  61. #pragma warning(push)
  62. #pragma warning(disable:4245) // 'initializing' : conversion from 'int' to 'const boost::low_bits_mask_t<8>::least', signed/unsigned mismatch
  63. #endif
  64. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned char );
  65. #if USHRT_MAX > UCHAR_MAX
  66. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned short );
  67. #endif
  68. #if UINT_MAX > USHRT_MAX
  69. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned int );
  70. #endif
  71. #if ULONG_MAX > UINT_MAX
  72. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned long );
  73. #endif
  74. #if defined(BOOST_HAS_LONG_LONG)
  75. #if ((defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)) ||\
  76. (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX > ULONG_MAX)) ||\
  77. (defined(ULONGLONG_MAX) && (ULONGLONG_MAX > ULONG_MAX)) ||\
  78. (defined(_ULLONG_MAX) && (_ULLONG_MAX > ULONG_MAX)))
  79. BOOST_LOW_BITS_MASK_SPECIALIZE( boost::ulong_long_type );
  80. #endif
  81. #elif defined(BOOST_HAS_MS_INT64)
  82. #if 18446744073709551615ui64 > ULONG_MAX
  83. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned __int64 );
  84. #endif
  85. #endif
  86. #ifdef BOOST_MSVC
  87. #pragma warning(pop)
  88. #endif
  89. #undef BOOST_LOW_BITS_MASK_SPECIALIZE
  90. } // namespace boost
  91. #endif // BOOST_INTEGER_INTEGER_MASK_HPP