M5Utility 0.0.12 git rev:13bfbd6
Loading...
Searching...
No Matches
bit_segment.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
11#ifndef M5_UTILITY_BIT_SEGMENT_HPP
12#define M5_UTILITY_BIT_SEGMENT_HPP
13
14#include <cstddef>
15#include <type_traits>
16
17namespace m5 {
18namespace utility {
19
27template <size_t LowerBits, typename T>
29public:
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;
43
46 inline constexpr BitSegment() = default;
48 inline constexpr BitSegment(const BitSegment& o) : _v(o._v)
49 {
50 }
52 inline constexpr BitSegment(const base_type v) : _v(v)
53 {
54 }
56
59 BitSegment& operator=(const BitSegment& o)
60 {
61 if (this != &o) {
62 _v = o._v;
63 }
64 return *this;
65 }
66 BitSegment& operator=(const base_type v)
67 {
68 _v = v;
69 return *this;
70 }
72
75
76 inline constexpr explicit operator bool() const
77 {
78 return _v;
79 }
81 inline constexpr operator base_type() const
82 {
83 return _v;
84 }
86
89
90 inline constexpr unsigned_type upper() const
91 {
92 return (static_cast<unsigned_type>(_v) >> UPPER_SHIFT) & UPPER_MASK;
93 }
95 inline constexpr unsigned_type lower() const
96 {
97 return _v & LOWER_MASK;
98 }
100 inline constexpr base_type raw() const
101 {
102 return _v;
103 }
105
108
109 inline void upper(const unsigned_type v)
110 {
111 _v = static_cast<base_type>((static_cast<unsigned_type>(_v) & ~(UPPER_MASK << UPPER_SHIFT)) |
112 ((v & UPPER_MASK) << UPPER_SHIFT));
113 }
115 inline void lower(const unsigned_type v)
116 {
117 _v = (_v & ~LOWER_MASK) | (v & LOWER_MASK);
118 }
120 inline void raw(const base_type v)
121 {
122 _v = v;
123 }
125
126private:
127 base_type _v{};
128};
129
133template <size_t LowerBits, typename T>
134bool operator==(const BitSegment<LowerBits, T>& a, const BitSegment<LowerBits, T>& b)
135{
136 return a.raw() == b.raw();
137}
138template <size_t LowerBits, typename T>
139bool operator!=(const BitSegment<LowerBits, T>& a, const BitSegment<LowerBits, T>& b)
140{
141 return !(a == b);
142}
143template <size_t LowerBits, typename T>
144bool operator<(const BitSegment<LowerBits, T>& a, const BitSegment<LowerBits, T>& b)
145{
146 return a.raw() < b.raw();
147}
148template <size_t LowerBits, typename T>
149bool operator>(const BitSegment<LowerBits, T>& a, const BitSegment<LowerBits, T>& b)
150{
151 return b < a;
152}
153template <size_t LowerBits, typename T>
154bool operator<=(const BitSegment<LowerBits, T>& a, const BitSegment<LowerBits, T>& b)
155{
156 return !(a > b);
157}
158template <size_t LowerBits, typename T>
159bool operator>=(const BitSegment<LowerBits, T>& a, const BitSegment<LowerBits, T>& b)
160{
161 return !(a < b);
162}
164
168template <size_t LowerBits, typename T>
169bool operator==(const BitSegment<LowerBits, T>& a, const int b)
170{
171 return a.raw() == b;
172}
173template <size_t LowerBits, typename T>
174bool operator!=(const BitSegment<LowerBits, T>& a, const int b)
175{
176 return !(a == b);
177}
178template <size_t LowerBits, typename T>
179bool operator<(const BitSegment<LowerBits, T>& a, const int b)
180{
181 return a.raw() < b;
182}
183template <size_t LowerBits, typename T>
184bool operator>(const BitSegment<LowerBits, T>& a, const int b)
185{
186 return b < a;
187}
188template <size_t LowerBits, typename T>
189bool operator<=(const BitSegment<LowerBits, T>& a, const int b)
190{
191 return !(a > b);
192}
193template <size_t LowerBits, typename T>
194bool operator>=(const BitSegment<LowerBits, T>& a, const int b)
195{
196 return !(a < b);
197}
199
203template <size_t LowerBits, typename T>
204bool operator==(const int a, const BitSegment<LowerBits, T>& b)
205{
206 return a == b.raw();
207}
208template <size_t LowerBits, typename T>
209bool operator!=(const int a, const BitSegment<LowerBits, T>& b)
210{
211 return !(a == b);
212}
213template <size_t LowerBits, typename T>
214bool operator<(const int a, const BitSegment<LowerBits, T>& b)
215{
216 return a < b.raw();
217}
218template <size_t LowerBits, typename T>
219bool operator>(const int a, const BitSegment<LowerBits, T>& b)
220{
221 return b < a;
222}
223
224template <size_t LowerBits, typename T>
225bool operator<=(const int a, const BitSegment<LowerBits, T>& b)
226{
227 return !(a > b);
228}
229template <size_t LowerBits, typename T>
230bool operator>=(const int a, const BitSegment<LowerBits, T>& b)
231{
232 return !(a < b);
233}
235
236} // namespace utility
237} // namespace m5
238#endif
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
For utilities.