1#include <compiler/core.h>
2#include <math/bit.h>
3#include <stdatomic.h>
4#include <stddef.h>
5#include <stdint.h>
6
7#define BR_BITFLAG_RANGE_ASSERT(lo, hi) \
8 (sizeof(struct { \
9 static_assert((hi) >= (lo), "hi must be >= lo"); \
10 uint8_t _b; \
11 }) * \
12 0)
13
14#define BR_EVAL(...) BR_EVAL64(__VA_ARGS__)
15#define BR_EVAL64(...) BR_EVAL32(BR_EVAL32(__VA_ARGS__))
16#define BR_EVAL32(...) BR_EVAL16(BR_EVAL16(__VA_ARGS__))
17#define BR_EVAL16(...) BR_EVAL8(BR_EVAL8(__VA_ARGS__))
18#define BR_EVAL8(...) BR_EVAL4(BR_EVAL4(__VA_ARGS__))
19#define BR_EVAL4(...) BR_EVAL2(BR_EVAL2(__VA_ARGS__))
20#define BR_EVAL2(...) BR_EVAL1(BR_EVAL1(__VA_ARGS__))
21#define BR_EVAL1(...) __VA_ARGS__
22
23#define BR_EMPTY()
24#define BR_DEFER(id) id BR_EMPTY()
25
26#define BR_IIF(cond) PP_CONCAT(BR_IIF_, cond)
27#define BR_IIF_0(t, f) f
28#define BR_IIF_1(t, f) t
29
30#define BR_HAS_COMMA(...) BR_HAS_COMMA_EVAL(__VA_ARGS__, 1, 0, 0)
31#define BR_HAS_COMMA_EVAL(_1, _2, N, ...) N
32#define BR_CMP_TOKEN(a, b) PP_CONCAT(BR_CMP_, PP_CONCAT(a, PP_CONCAT(_, b)))
33#define BR_IS_EQUAL(a, b) BR_HAS_COMMA(BR_CMP_TOKEN(a, b))
34
35#define BR_CMP_0_0 ,
36#define BR_CMP_1_1 ,
37#define BR_CMP_2_2 ,
38#define BR_CMP_3_3 ,
39#define BR_CMP_4_4 ,
40#define BR_CMP_5_5 ,
41#define BR_CMP_6_6 ,
42#define BR_CMP_7_7 ,
43#define BR_CMP_8_8 ,
44#define BR_CMP_9_9 ,
45#define BR_CMP_10_10 ,
46#define BR_CMP_11_11 ,
47#define BR_CMP_12_12 ,
48#define BR_CMP_13_13 ,
49#define BR_CMP_14_14 ,
50#define BR_CMP_15_15 ,
51#define BR_CMP_16_16 ,
52#define BR_CMP_17_17 ,
53#define BR_CMP_18_18 ,
54#define BR_CMP_19_19 ,
55#define BR_CMP_20_20 ,
56#define BR_CMP_21_21 ,
57#define BR_CMP_22_22 ,
58#define BR_CMP_23_23 ,
59#define BR_CMP_24_24 ,
60#define BR_CMP_25_25 ,
61#define BR_CMP_26_26 ,
62#define BR_CMP_27_27 ,
63#define BR_CMP_28_28 ,
64#define BR_CMP_29_29 ,
65#define BR_CMP_30_30 ,
66#define BR_CMP_31_31 ,
67
68#define BR_INC(x) PP_CONCAT(BR_INC_, x)
69#define BR_INC_0 1
70#define BR_INC_1 2
71#define BR_INC_2 3
72#define BR_INC_3 4
73#define BR_INC_4 5
74#define BR_INC_5 6
75#define BR_INC_6 7
76#define BR_INC_7 8
77#define BR_INC_8 9
78#define BR_INC_9 10
79#define BR_INC_10 11
80#define BR_INC_11 12
81#define BR_INC_12 13
82#define BR_INC_13 14
83#define BR_INC_14 15
84#define BR_INC_15 16
85#define BR_INC_16 17
86#define BR_INC_17 18
87#define BR_INC_18 19
88#define BR_INC_19 20
89#define BR_INC_20 21
90#define BR_INC_21 22
91#define BR_INC_22 23
92#define BR_INC_23 24
93#define BR_INC_24 25
94#define BR_INC_25 26
95#define BR_INC_26 27
96#define BR_INC_27 28
97#define BR_INC_28 29
98#define BR_INC_29 30
99#define BR_INC_30 31
100
101#define BR_RANGE_STOP(curr, hi)
102#define BR_RANGE_CONT(curr, hi) \
103 BR_DEFER(BR_RANGE_STEP_INDIRECT)()(BR_INC(curr), hi)
104#define BR_RANGE_STEP_INDIRECT() BR_RANGE_STEP
105
106#define BR_RANGE_STEP(curr, hi) \
107 , curr BR_IIF(BR_IS_EQUAL(curr, hi))(BR_RANGE_STOP, BR_RANGE_CONT)(curr, hi)
108
109#define BR_RANGE_GENERATE_I(lo, hi) \
110 (BR_BITFLAG_RANGE_ASSERT(lo, hi) + (lo)) BR_IIF(BR_IS_EQUAL(lo, hi))( \
111 BR_RANGE_STOP, BR_RANGE_STEP)(BR_INC(lo), hi)
112
113#define BITFLAG_RANGE(lo, hi) BR_EVAL(BR_RANGE_GENERATE_I(lo, hi))
114
115/* clang-format off */
116
117#define BR_MASK_BIT(type, idx) (((type) 1) << (idx))
118
119#define BR_MASK_1(type, x) BR_MASK_BIT(type, x)
120#define BR_MASK_2(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_1(type, __VA_ARGS__))
121#define BR_MASK_3(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_2(type, __VA_ARGS__))
122#define BR_MASK_4(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_3(type, __VA_ARGS__))
123#define BR_MASK_5(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_4(type, __VA_ARGS__))
124#define BR_MASK_6(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_5(type, __VA_ARGS__))
125#define BR_MASK_7(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_6(type, __VA_ARGS__))
126#define BR_MASK_8(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_7(type, __VA_ARGS__))
127#define BR_MASK_9(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_8(type, __VA_ARGS__))
128#define BR_MASK_10(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_9(type, __VA_ARGS__))
129#define BR_MASK_11(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_10(type, __VA_ARGS__))
130#define BR_MASK_12(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_11(type, __VA_ARGS__))
131#define BR_MASK_13(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_12(type, __VA_ARGS__))
132#define BR_MASK_14(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_13(type, __VA_ARGS__))
133#define BR_MASK_15(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_14(type, __VA_ARGS__))
134#define BR_MASK_16(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_15(type, __VA_ARGS__))
135#define BR_MASK_17(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_16(type, __VA_ARGS__))
136#define BR_MASK_18(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_17(type, __VA_ARGS__))
137#define BR_MASK_19(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_18(type, __VA_ARGS__))
138#define BR_MASK_20(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_19(type, __VA_ARGS__))
139#define BR_MASK_21(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_20(type, __VA_ARGS__))
140#define BR_MASK_22(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_21(type, __VA_ARGS__))
141#define BR_MASK_23(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_22(type, __VA_ARGS__))
142#define BR_MASK_24(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_23(type, __VA_ARGS__))
143#define BR_MASK_25(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_24(type, __VA_ARGS__))
144#define BR_MASK_26(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_25(type, __VA_ARGS__))
145#define BR_MASK_27(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_26(type, __VA_ARGS__))
146#define BR_MASK_28(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_27(type, __VA_ARGS__))
147#define BR_MASK_29(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_28(type, __VA_ARGS__))
148#define BR_MASK_30(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_29(type, __VA_ARGS__))
149#define BR_MASK_31(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_30(type, __VA_ARGS__))
150#define BR_MASK_32(type, x, ...) (BR_MASK_BIT(type, x) | BR_MASK_31(type, __VA_ARGS__))
151
152/* clang-format on */
153
154#define BR_MAKE_MASK_IMPL(type, ...) \
155 PP_OVERLOAD(BR_MASK, __VA_ARGS__)(type, __VA_ARGS__)
156
157#define BR_MAKE_MASK(type, ...) BR_MAKE_MASK_IMPL(type, __VA_ARGS__)
158