NeKernel dev
Loading...
Searching...
No Matches
bit.h
Go to the documentation of this file.
1#ifndef UTL_BIT_H
2#define UTL_BIT_H
3
4#include <bit>
5
6namespace utl {
7
14template <class T>
15constexpr auto bit_size() {
16 return sizeof(T) * 8;
17}
18
25template <class T>
26constexpr T bit_full() {
27 return T(-1);
28}
29
40template <class T>
41constexpr T bit_wrap() {
42 return bit_size<T>() - 1;
43}
44
55template <class T>
56constexpr auto bit_shft() {
57 return std::bit_width(bit_wrap<T>());
58}
59
68template <class T>
69constexpr auto bit_ceil(auto x) {
70 return (x + bit_wrap<T>()) >> bit_shft<T>();
71}
72
79constexpr unsigned cntlz(auto x) {
80 if constexpr (std::is_same_v<decltype(x), int>)
81 return std::countl_zero(unsigned(x));
82 else
83 return std::countl_zero(x);
84}
85
92constexpr unsigned cnttz(auto x) {
93 if constexpr (std::is_same_v<decltype(x), int>)
94 return std::countr_zero(unsigned(x));
95 else
96 return std::countr_zero(x);
97}
98
106template <class T>
107constexpr size_t words_in_bits(size_t n) {
108 return (n >> bit_shft<T>()) + !!(n & bit_wrap<T>());
109}
110
117constexpr size_t bytes_in_bits(size_t n) {
118 return words_in_bits<uint8_t>(n);
119}
120
128template <class T = unsigned>
129constexpr T bit(int n) {
130 return T(1) << n;
131}
132
141template <class T>
142constexpr bool get_bit(T x, int n) {
143 return (x >> n) & 1;
144}
145
153template <class T>
154constexpr void set_bit(T& x, int n) {
155 x |= 1 << n;
156}
157
165template <class T>
166constexpr void clr_bit(T& x, int n) {
167 x &= ~(1 << n);
168}
169
178template <class T>
179constexpr bool get_arr_bit(const T* p, unsigned n) {
180 return get_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>());
181}
182
190template <class T>
191constexpr void set_arr_bit(T* p, unsigned n) {
192 set_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>());
193}
194
202template <class T>
203constexpr void clr_arr_bit(T* p, unsigned n) {
204 clr_bit(p[n >> bit_shft<T>()], n & bit_wrap<T>());
205}
206
217template <class T, size_t L>
218constexpr void shift_left(T (&x)[L]) {
219 for (int i = L - 1; i > 0; --i) {
220 x[i] <<= 1;
221 x[i] |= x[i - 1] >> bit_wrap<T>();
222 }
223 x[0] <<= 1;
224}
225
226} // namespace utl
227
228#endif
Definition base.h:8
constexpr T bit_full()
Integer with all bits set to 1.
Definition bit.h:26
constexpr unsigned cnttz(auto x)
Count trailing zeros.
Definition bit.h:92
constexpr void clr_arr_bit(T *p, unsigned n)
Clear n-th bit in array of words (starting from LSB).
Definition bit.h:203
constexpr auto bit_ceil(auto x)
Round up division by number of bits within given integer type, which sizeof(T) * 8 is power of two.
Definition bit.h:69
constexpr size_t words_in_bits(size_t n)
Get number of words (integers) required to store N bits.
Definition bit.h:107
constexpr auto bit_shft()
Number of bits to fit bit_wrap<T> result, in other words bit width of (sizeof(T) * 8 - 1)....
Definition bit.h:56
constexpr size_t bytes_in_bits(size_t n)
Get number of bytes required to store N bits.
Definition bit.h:117
constexpr void set_arr_bit(T *p, unsigned n)
Set n-th bit in array of words (starting from LSB).
Definition bit.h:191
constexpr T bit_wrap()
Wrap around mask for power of two number of bits within given integer type. For example: [ bit_wrap<u...
Definition bit.h:41
constexpr bool get_arr_bit(const T *p, unsigned n)
Get n-th bit in array of words (starting from LSB).
Definition bit.h:179
constexpr unsigned cntlz(auto x)
Count leading zeros.
Definition bit.h:79
constexpr auto bit_size()
Size of object in terms of bits.
Definition bit.h:15
constexpr void set_bit(T &x, int n)
Set n-th bit of an integer.
Definition bit.h:154
constexpr void shift_left(T(&x)[L])
Shift bits left in array of integer elements from least significant bit and considering 0-th byte as ...
Definition bit.h:218
constexpr T bit(int n)
Make integer with bit at given position.
Definition bit.h:129
constexpr bool get_bit(T x, int n)
Get n-th bit of an integer.
Definition bit.h:142
constexpr void clr_bit(T &x, int n)
Clear n-th bit of an integer.
Definition bit.h:166