integer_mask.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace boost
  16. {
  17. // Specified single-bit mask class declaration -----------------------------//
  18. // (Lowest bit starts counting at 0.)
  19. template < std::size_t Bit >
  20. struct high_bit_mask_t
  21. {
  22. typedef typename uint_t<(Bit + 1)>::least least;
  23. typedef typename uint_t<(Bit + 1)>::fast fast;
  24. BOOST_STATIC_CONSTANT( least, high_bit = (least( 1u ) << Bit) );
  25. BOOST_STATIC_CONSTANT( fast, high_bit_fast = (fast( 1u ) << Bit) );
  26. BOOST_STATIC_CONSTANT( std::size_t, bit_position = Bit );
  27. }; // boost::high_bit_mask_t
  28. // Specified bit-block mask class declaration ------------------------------//
  29. // Makes masks for the lowest N bits
  30. // (Specializations are needed when N fills up a type.)
  31. template < std::size_t Bits >
  32. struct low_bits_mask_t
  33. {
  34. typedef typename uint_t<Bits>::least least;
  35. typedef typename uint_t<Bits>::fast fast;
  36. BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );
  37. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) );
  38. BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
  39. }; // boost::low_bits_mask_t
  40. #define BOOST_LOW_BITS_MASK_SPECIALIZE( Type ) \
  41. template < > struct low_bits_mask_t< std::numeric_limits<Type>::digits > { \
  42. typedef std::numeric_limits<Type> limits_type; \
  43. typedef uint_t<limits_type::digits>::least least; \
  44. typedef uint_t<limits_type::digits>::fast fast; \
  45. BOOST_STATIC_CONSTANT( least, sig_bits = (~( least(0u) )) ); \
  46. BOOST_STATIC_CONSTANT( fast, sig_bits_fast = fast(sig_bits) ); \
  47. BOOST_STATIC_CONSTANT( std::size_t, bit_count = limits_type::digits ); \
  48. }
  49. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned char );
  50. #if USHRT_MAX > UCHAR_MAX
  51. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned short );
  52. #endif
  53. #if UINT_MAX > USHRT_MAX
  54. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned int );
  55. #endif
  56. #if ULONG_MAX > UINT_MAX
  57. BOOST_LOW_BITS_MASK_SPECIALIZE( unsigned long );
  58. #endif
  59. #undef BOOST_LOW_BITS_MASK_SPECIALIZE
  60. } // namespace boost
  61. #endif // BOOST_INTEGER_INTEGER_MASK_HPP