11#ifndef M5_UTILITY_BIT_SEGMENT_HPP
12#define M5_UTILITY_BIT_SEGMENT_HPP
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 (
static_cast<unsigned_type
>(_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 =
static_cast<base_type
>((
static_cast<unsigned_type
>(_v) & ~(UPPER_MASK << UPPER_SHIFT)) |
112 ((v & UPPER_MASK) << UPPER_SHIFT));
115 inline void lower(
const unsigned_type v)
117 _v = (_v & ~LOWER_MASK) | (v & LOWER_MASK);
120 inline void raw(
const base_type v)
133template <
size_t LowerBits,
typename T>
134bool operator==(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
136 return a.raw() == b.raw();
138template <
size_t LowerBits,
typename T>
139bool operator!=(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
143template <
size_t LowerBits,
typename T>
144bool operator<(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
146 return a.raw() < b.raw();
148template <
size_t LowerBits,
typename T>
149bool operator>(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
153template <
size_t LowerBits,
typename T>
154bool operator<=(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
158template <
size_t LowerBits,
typename T>
159bool operator>=(
const BitSegment<LowerBits, T>& a,
const BitSegment<LowerBits, T>& b)
168template <
size_t LowerBits,
typename T>
169bool operator==(
const BitSegment<LowerBits, T>& a,
const int b)
173template <
size_t LowerBits,
typename T>
174bool operator!=(
const BitSegment<LowerBits, T>& a,
const int b)
178template <
size_t LowerBits,
typename T>
179bool operator<(
const BitSegment<LowerBits, T>& a,
const int b)
183template <
size_t LowerBits,
typename T>
184bool operator>(
const BitSegment<LowerBits, T>& a,
const int b)
188template <
size_t LowerBits,
typename T>
189bool operator<=(
const BitSegment<LowerBits, T>& a,
const int b)
193template <
size_t LowerBits,
typename T>
194bool operator>=(
const BitSegment<LowerBits, T>& a,
const int b)
203template <
size_t LowerBits,
typename T>
204bool operator==(
const int a,
const BitSegment<LowerBits, T>& b)
208template <
size_t LowerBits,
typename T>
209bool operator!=(
const int a,
const BitSegment<LowerBits, T>& b)
213template <
size_t LowerBits,
typename T>
214bool operator<(
const int a,
const BitSegment<LowerBits, T>& b)
218template <
size_t LowerBits,
typename T>
219bool operator>(
const int a,
const BitSegment<LowerBits, T>& b)
224template <
size_t LowerBits,
typename T>
225bool operator<=(
const int a,
const BitSegment<LowerBits, T>& b)
229template <
size_t LowerBits,
typename T>
230bool operator>=(
const int a,
const BitSegment<LowerBits, T>& b)
Definition bit_segment.hpp:28
void upper(const unsigned_type v)
Set the value of upper segment.
Definition bit_segment.hpp:109
void raw(const base_type v)
Set the raw value.
Definition bit_segment.hpp:120
constexpr unsigned_type upper() const
Gets the value of upper segment.
Definition bit_segment.hpp:90
constexpr unsigned_type lower() const
Gets the value of lower segment.
Definition bit_segment.hpp:95
void lower(const unsigned_type v)
Set the value of lower segment.
Definition bit_segment.hpp:115
constexpr BitSegment()=default
default
constexpr BitSegment(const base_type v)
Implicit conversion.
Definition bit_segment.hpp:52
constexpr base_type raw() const
Gets the raw value.
Definition bit_segment.hpp:100
constexpr BitSegment(const BitSegment &o)
Copy.
Definition bit_segment.hpp:48
Top level namespace of M5.
Definition base64.cpp:39