integer_mask.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // https://www.boost.org/LICENSE_1_0.txt)
  6. // See https://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. #ifdef BOOST_MSVC
  43. #pragma warning(push)
  44. #pragma warning(disable:4310) // cast truncates constant value
  45. #endif
  46. template < std::size_t Bits >
  47. struct low_bits_mask_t
  48. {
  49. typedef typename uint_t<Bits>::least least;
  50. typedef typename uint_t<Bits>::fast fast;
  51. BOOST_STATIC_CONSTANT( least, sig_bits = least(~(least(~(least( 0u ))) << Bits )) );
  52. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
  53. BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
  54. }; // boost::low_bits_mask_t
  55. #ifdef BOOST_MSVC
  56. #pragma warning(pop)
  57. #endif
  58. #define BOOST_LOW_BITS_MASK_SPECIALIZE( Type ) \
  59. template < > struct low_bits_mask_t< std::numeric_limits<Type>::digits > { \
  60. typedef std::numeric_limits<Type> limits_type; \
  61. typedef uint_t<limits_type::digits>::least least; \
  62. typedef uint_t<limits_type::digits>::fast fast; \
  63. BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) ); \
  64. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); \
  65. BOOST_STATIC_CONSTANT( std::size_t, bit_count = limits_type::digits ); \
  66. }
  67. #ifdef BOOST_MSVC
  68. #pragma warning(push)
  69. #pragma warning(disable:4245) // 'initializing' : conversion from 'int' to 'const boost::low_bits_mask_t<8>::least', signed/unsigned mismatch
  70. #endif
  71. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned char );
  72. #if USHRT_MAX > UCHAR_MAX
  73. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned short );
  74. #endif
  75. #if UINT_MAX > USHRT_MAX
  76. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned int );
  77. #endif
  78. #if ULONG_MAX > UINT_MAX
  79. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned long );
  80. #endif
  81. #if defined(BOOST_HAS_LONG_LONG)
  82. #if ((defined(ULLONG_MAX) && (ULLONG_MAX > ULONG_MAX)) ||\
  83. (defined(ULONG_LONG_MAX) && (ULONG_LONG_MAX > ULONG_MAX)) ||\
  84. (defined(ULONGLONG_MAX) && (ULONGLONG_MAX > ULONG_MAX)) ||\
  85. (defined(_ULLONG_MAX) && (_ULLONG_MAX > ULONG_MAX)))
  86. BOOST_LOW_BITS_MASK_SPECIALIZE( boost::ulong_long_type );
  87. #endif
  88. #elif defined(BOOST_HAS_MS_INT64)
  89. #if 18446744073709551615ui64 > ULONG_MAX
  90. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned __int64 );
  91. #endif
  92. #endif
  93. #ifdef BOOST_MSVC
  94. #pragma warning(pop)
  95. #endif
  96. #undef BOOST_LOW_BITS_MASK_SPECIALIZE
  97. } // namespace boost
  98. #endif // BOOST_INTEGER_INTEGER_MASK_HPP