1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/detail/integer/integer_mask.hpp

73 lines
2.6 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2011-10-03 09:27:50 +00:00
#ifndef SPROUT_DETAIL_INTEGER_INTEGER_MASK_HPP
#define SPROUT_DETAIL_INTEGER_INTEGER_MASK_HPP
#include <climits>
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
2013-08-06 15:15:09 +00:00
#include <sprout/limits.hpp>
2011-10-03 09:27:50 +00:00
#include <sprout/detail/integer.hpp>
namespace sprout {
namespace detail {
template<std::size_t Bits>
struct high_bit_mask_t {
public:
typedef typename sprout::detail::uint_t<(Bits + 1)>::least least;
typedef typename sprout::detail::uint_t<(Bits + 1)>::fast fast;
2011-10-03 09:27:50 +00:00
public:
SPROUT_STATIC_CONSTEXPR least high_bit = least(1u) << Bits;
SPROUT_STATIC_CONSTEXPR fast high_bit_fast = fast(1u) << Bits;
SPROUT_STATIC_CONSTEXPR std::size_t bit_position = Bits;
};
template<std::size_t Bits>
struct low_bits_mask_t {
public:
typedef typename sprout::detail::uint_t<Bits>::least least;
typedef typename sprout::detail::uint_t<Bits>::fast fast;
2011-10-03 09:27:50 +00:00
public:
SPROUT_STATIC_CONSTEXPR least sig_bits = ~(~(least(0u)) << Bits);
SPROUT_STATIC_CONSTEXPR fast sig_bits_fast = fast(sig_bits);
SPROUT_STATIC_CONSTEXPR std::size_t bit_count = Bits;
};
2014-08-14 13:55:49 +00:00
#define SPROUT_LOW_BITS_MASK_SPECIALIZE(TYPE) \
2011-10-03 09:27:50 +00:00
template<> \
2014-08-14 13:55:49 +00:00
struct low_bits_mask_t<sprout::numeric_limits<TYPE>::digits> { \
2011-10-03 09:27:50 +00:00
public: \
2014-08-14 13:55:49 +00:00
typedef sprout::numeric_limits<TYPE> limits_type; \
2015-12-09 10:54:35 +00:00
typedef sprout::detail::uint_t<limits_type::digits>::least least; \
typedef sprout::detail::uint_t<limits_type::digits>::fast fast; \
2011-10-03 09:27:50 +00:00
public: \
SPROUT_STATIC_CONSTEXPR least sig_bits = ~(least(0u)); \
SPROUT_STATIC_CONSTEXPR fast sig_bits_fast = fast(sig_bits); \
SPROUT_STATIC_CONSTEXPR std::size_t bit_count = limits_type::digits; \
}
SPROUT_LOW_BITS_MASK_SPECIALIZE(unsigned char);
#if USHRT_MAX > UCHAR_MAX
SPROUT_LOW_BITS_MASK_SPECIALIZE(unsigned short);
#endif
#if UINT_MAX > USHRT_MAX
SPROUT_LOW_BITS_MASK_SPECIALIZE(unsigned int);
#endif
#if ULONG_MAX > UINT_MAX
SPROUT_LOW_BITS_MASK_SPECIALIZE(unsigned long);
#endif
#if ULLONG_MAX > ULONG_MAX
SPROUT_LOW_BITS_MASK_SPECIALIZE(unsigned long long);
2011-10-03 09:27:50 +00:00
#endif
#undef SPROUT_LOW_BITS_MASK_SPECIALIZE
} // namespace detail
} // namespace sprout
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_DETAIL_INTEGER_INTEGER_MASK_HPP