Deluge Firmware
1.3.0
Build date: 2026.08.29
Toggle main menu visibility
Loading...
Searching...
No Matches
misc.h
1
#pragma once
2
#include <concepts>
// IWYU pragma: keep this is actually needed for std::integral, clangd is messing it up
3
#include <cstddef>
4
#include <cstdint>
5
#include <limits>
6
#include <type_traits>
7
8
namespace
util {
9
template
<auto Start, auto End, auto Inc,
class
F>
10
constexpr
void
constexpr_for(F&& f) {
11
if
constexpr
(Start < End) {
12
f.template operator()<Start>();
13
constexpr_for<Start + Inc, End, Inc>(f);
14
}
15
}
16
17
template
<
class
Enum>
18
concept
enumeration
= std::is_enum_v<Enum>;
19
20
template
<enumeration Enum>
21
constexpr
std::underlying_type_t<Enum> to_underlying(Enum e)
noexcept
{
22
return
static_cast<
std::underlying_type_t<Enum>
>
(e);
23
}
24
28
template
<std::
size_t
n,
typename
T =
int
32_t>
29
constexpr
T bit = T{1} << n;
30
37
template
<std::
size_t
w
id
th,
typename
T =
int
32_t>
38
constexpr
T bit_value = bit<width - 1, T>;
39
46
template
<std::
size_t
w
id
th,
typename
T =
int
32_t>
47
constexpr
T bitmask = bit<width, T> - 1;
48
49
template
<
typename
T>
50
constexpr
T bit_from_top(
const
std::size_t n) {
51
return
T{1} << (std::numeric_limits<T>::digits - (n + 1));
52
}
53
54
template
<
typename
T>
55
constexpr
T top_n_bits(
const
std::size_t n) {
56
T output{0};
57
for
(std::size_t i = 0; i < n; i++) {
58
output |= bit_from_top<T>(i);
59
}
60
return
output;
61
}
62
63
template
<
typename
T>
64
constexpr
T top_bit = T{1} << (std::numeric_limits<T>::digits - 1);
65
66
template
<std::
int
egral T>
67
constexpr
T median_value = (std::numeric_limits<T>::max() / 2) + (std::numeric_limits<T>::min() / 2) + 1;
68
69
template
<
typename
T>
70
constexpr
T set_top_bit(
const
T v) {
71
return
v | top_bit<T>;
72
}
73
74
template
<
typename
T, std::
size_t
n>
75
constexpr
T set_top_n_bits(
const
T v) {
76
T output = v;
77
for
(std::size_t i = 0; i < n; i++) {
78
output |= bit_from_top<T>(i);
79
}
80
return
output;
81
}
82
83
template
<
typename
T>
84
[[nodiscard]]
constexpr
T map(T x, T in_min, T in_max, T out_min, T out_max) {
85
return
out_min + ((x - in_min) * (out_max - out_min)) / (in_max - in_min);
86
}
87
92
template
<std::
int
egral T>
93
[[nodiscard]]
constexpr
T div_ceil(T a, T b) {
94
return
(a + (b - 1)) / b;
95
}
96
100
[[nodiscard]]
constexpr
bool
infinite_a_lt_b(uint32_t a, uint32_t b) {
101
return
((int32_t)(a - b) < 0);
102
}
103
104
}
// namespace util
105
106
// unsigned literal operators
107
consteval
uint_least64_t
operator
""
_u64(
unsigned
long
long
arg) {
108
return
arg;
109
};
110
consteval
uint_least32_t
operator
""
_u32(
unsigned
long
long
arg) {
111
return
arg;
112
};
113
consteval
uint_least16_t
operator
""
_u16(
unsigned
long
long
arg) {
114
return
arg;
115
};
116
consteval
uint_least8_t
operator
""
_u8(
unsigned
long
long
arg) {
117
return
arg;
118
};
119
120
// signed literal operators
121
consteval
int_least64_t
operator
""
_i64(
unsigned
long
long
arg) {
122
return
arg;
123
};
124
consteval
int_least32_t
operator
""
_i32(
unsigned
long
long
arg) {
125
return
arg;
126
};
127
consteval
int_least16_t
operator
""
_i16(
unsigned
long
long
arg) {
128
return
arg;
129
};
130
consteval
int_least8_t
operator
""
_i8(
unsigned
long
long
arg) {
131
return
arg;
132
};
133
134
consteval
std::byte
operator
""
_b(
unsigned
long
long
arg) {
135
return
std::byte(arg);
136
};
util::enumeration
Definition
misc.h:18