| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-forward.h" |
| 5 |
|
| 6 |
/* Bit index (0-based) to mask of specified type. Assertion failure if index is out of range. */ |
| 7 |
#define _INDEX_TO_MASK(type, i, uniq) \ |
| 8 |
({ \ |
| 9 |
int UNIQ_T(_i, uniq) = (i); \ |
| 10 |
assert(UNIQ_T(_i, uniq) >= 0); \ |
| 11 |
assert(UNIQ_T(_i, uniq) < (int)sizeof(type) * 8); \ |
| 12 |
((type)1) << UNIQ_T(_i, uniq); \ |
| 13 |
}) |
| 14 |
#define INDEX_TO_MASK(type, i) \ |
| 15 |
({ \ |
| 16 |
assert_cc(sizeof(type) <= sizeof(unsigned long long)); \ |
| 17 |
assert_cc(__builtin_choose_expr(__builtin_constant_p(i), i, 0) < (int)(sizeof(type) * 8)); \ |
| 18 |
__builtin_choose_expr(__builtin_constant_p(i), \ |
| 19 |
((type)1) << (i), \ |
| 20 |
_INDEX_TO_MASK(type, i, UNIQ)); \ |
| 21 |
}) |
| 22 |
|
| 23 |
/* Builds a mask of specified type with multiple bits set. Note the result will not be constant, even if all |
| 24 |
* indexes are constant. */ |
| 25 |
#define INDEXES_TO_MASK(type, ...) \ |
| 26 |
UNIQ_INDEXES_TO_MASK(type, UNIQ, ##__VA_ARGS__) |
| 27 |
#define UNIQ_INDEXES_TO_MASK(type, uniq, ...) \ |
| 28 |
({ \ |
| 29 |
typeof(type) UNIQ_T(_mask, uniq) = (type)0; \ |
| 30 |
int UNIQ_T(_i, uniq); \ |
| 31 |
FOREACH_ARGUMENT(UNIQ_T(_i, uniq), ##__VA_ARGS__) \ |
| 32 |
UNIQ_T(_mask, uniq) |= INDEX_TO_MASK(type, UNIQ_T(_i, uniq)); \ |
| 33 |
UNIQ_T(_mask, uniq); \ |
| 34 |
}) |
| 35 |
|
| 36 |
/* Same as the FLAG macros, but accept a 0-based bit index instead of a mask. Results in assertion failure if |
| 37 |
* index is out of range for the type. */ |
| 38 |
#define SET_BIT(bits, i) SET_FLAG(bits, INDEX_TO_MASK(typeof(bits), i), true) |
| 39 |
#define CLEAR_BIT(bits, i) SET_FLAG(bits, INDEX_TO_MASK(typeof(bits), i), false) |
| 40 |
#define BIT_SET(bits, i) FLAGS_SET(bits, INDEX_TO_MASK(typeof(bits), i)) |
| 41 |
|
| 42 |
/* As above, but accepts multiple indexes. Note the result will not be constant, even if all indexes are |
| 43 |
* constant. */ |
| 44 |
#define SET_BITS(bits, ...) SET_FLAG(bits, INDEXES_TO_MASK(typeof(bits), ##__VA_ARGS__), true) |
| 45 |
#define CLEAR_BITS(bits, ...) SET_FLAG(bits, INDEXES_TO_MASK(typeof(bits), ##__VA_ARGS__), false) |
| 46 |
#define BITS_SET(bits, ...) FLAGS_SET(bits, INDEXES_TO_MASK(typeof(bits), ##__VA_ARGS__)) |
| 47 |
|
| 48 |
/* Iterate through each set bit. Index is 0-based and type int. */ |
| 49 |
#define BIT_FOREACH(index, bits) _BIT_FOREACH(index, bits, UNIQ) |
| 50 |
#define _BIT_FOREACH(index, bits, uniq) \ |
| 51 |
for (int UNIQ_T(_last, uniq) = -1, index; \ |
| 52 |
(index = BIT_NEXT_SET(bits, UNIQ_T(_last, uniq))) >= 0; \ |
| 53 |
UNIQ_T(_last, uniq) = index) |
| 54 |
|
| 55 |
/* Find the next set bit after 0-based index 'prev'. Result is 0-based index of next set bit, or -1 if no |
| 56 |
* more bits are set. */ |
| 57 |
#define BIT_FIRST_SET(bits) BIT_NEXT_SET(bits, -1) |
| 58 |
#define BIT_NEXT_SET(bits, prev) \ |
| 59 |
UNIQ_BIT_NEXT_SET(bits, prev, UNIQ) |
| 60 |
#define UNIQ_BIT_NEXT_SET(bits, prev, uniq) \ |
| 61 |
({ \ |
| 62 |
typeof(bits) UNIQ_T(_bits, uniq) = (bits); \ |
| 63 |
int UNIQ_T(_prev, uniq) = (prev); \ |
| 64 |
int UNIQ_T(_next, uniq); \ |
| 65 |
_BIT_NEXT_SET(UNIQ_T(_bits, uniq), \ |
| 66 |
UNIQ_T(_prev, uniq), \ |
| 67 |
UNIQ_T(_next, uniq)); \ |
| 68 |
}) |
| 69 |
#define _BIT_NEXT_SET(bits, prev, next) \ |
| 70 |
((int)(prev + 1) == (int)sizeof(bits) * 8 \ |
| 71 |
? -1 /* Prev index was msb. */ \ |
| 72 |
: ((next = __builtin_ffsll(((unsigned long long)(bits)) >> (prev + 1))) == 0 \ |
| 73 |
? -1 /* No more bits set. */ \ |
| 74 |
: prev + next)) |