27template <
size_t LowerBits,
typename T>
31 using base_type =
typename std::remove_const<typename std::remove_reference<T>::type>::type;
32 constexpr static bool SIGNED = std::is_signed<base_type>::value;
33 static_assert(std::is_integral<base_type>::value,
"Base type must be integral");
34 static_assert(LowerBits > 0,
"LowerBits must be not zero");
35 static_assert(LowerBits <= (
sizeof(base_type) * 8 - (SIGNED ? 1 : 0)),
"LowerBits too large");
36 using unsigned_type =
typename std::make_unsigned<base_type>::type;
37 constexpr static unsigned_type UPPER_BITS =
sizeof(unsigned_type) * 8U - LowerBits - (SIGNED ? 1 : 0);
38 constexpr static unsigned_type LOWER_BITS =
static_cast<unsigned_type
>(LowerBits);
39 constexpr static unsigned_type UPPER_SHIFT = LOWER_BITS;
40 constexpr static unsigned_type UPPER_MASK = ((unsigned_type)1 << UPPER_BITS) - 1;
41 constexpr static unsigned_type LOWER_MASK = ((unsigned_type)1 << LOWER_BITS) - 1;
76 inline constexpr explicit operator bool()
const
81 inline constexpr operator base_type()
const
90 inline constexpr unsigned_type
upper()
const
92 return (_v >> UPPER_SHIFT) & UPPER_MASK;
95 inline constexpr unsigned_type
lower()
const
97 return _v & LOWER_MASK;
100 inline constexpr base_type
raw()
const
109 inline void upper(
const unsigned_type v)
111 _v = (_v & ~(UPPER_MASK << UPPER_SHIFT)) | ((v & UPPER_MASK) << UPPER_SHIFT);
114 inline void lower(
const unsigned_type v)
116 _v = (_v & ~LOWER_MASK) | (v & LOWER_MASK);
119 inline void raw(
const base_type v)
132template <
size_t LowerBits,
typename T>
133bool operator==(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
135 return a.raw() == b.raw();
137template <
size_t LowerBits,
typename T>
138bool operator!=(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
142template <
size_t LowerBits,
typename T>
143bool operator<(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
145 return a.raw() < b.raw();
147template <
size_t LowerBits,
typename T>
148bool operator>(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
152template <
size_t LowerBits,
typename T>
153bool operator<=(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
157template <
size_t LowerBits,
typename T>
158bool operator>=(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
167template <
size_t LowerBits,
typename T>
168bool operator==(
const BitSegment<LowerBits, T>& a,
const int b)
172template <
size_t LowerBits,
typename T>
173bool operator!=(
const BitSegment<LowerBits, T>& a,
const int b)
177template <
size_t LowerBits,
typename T>
178bool operator<(
const BitSegment<LowerBits, T>& a,
const int b)
182template <
size_t LowerBits,
typename T>
183bool operator>(
const BitSegment<LowerBits, T>& a,
const int b)
187template <
size_t LowerBits,
typename T>
188bool operator<=(
const BitSegment<LowerBits, T>& a,
const int b)
192template <
size_t LowerBits,
typename T>
193bool operator>=(
const BitSegment<LowerBits, T>& a,
const int b)
202template <
size_t LowerBits,
typename T>
203bool operator==(
const int a,
const BitSegment<LowerBits, T>& b)
207template <
size_t LowerBits,
typename T>
208bool operator!=(
const int a,
const BitSegment<LowerBits, T>& b)
212template <
size_t LowerBits,
typename T>
213bool operator<(
const int a,
const BitSegment<LowerBits, T>& b)
217template <
size_t LowerBits,
typename T>
218bool operator>(
const int a,
const BitSegment<LowerBits, T>& b)
223template <
size_t LowerBits,
typename T>
224bool operator<=(
const int a,
const BitSegment<LowerBits, T>& b)
228template <
size_t LowerBits,
typename T>
229bool operator>=(
const int a,
const BitSegment<LowerBits, T>& b)