util/bitset: add bitwise AND, OR and NOT

Signed-off-by: Christian Gmeiner <christian.gmeiner@gmail.com>
Reviewed-by: Rob Clark <robdclark@chromium.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11321>
This commit is contained in:
Christian Gmeiner 2021-05-07 09:51:01 +02:00 committed by Marge Bot
parent f8ea9fa0f8
commit cfa8828c62

View file

@ -65,6 +65,46 @@
#define BITSET_MASK(b) (((b) % BITSET_WORDBITS == 0) ? ~0 : BITSET_BIT(b) - 1) #define BITSET_MASK(b) (((b) % BITSET_WORDBITS == 0) ? ~0 : BITSET_BIT(b) - 1)
#define BITSET_RANGE(b, e) ((BITSET_MASK((e) + 1)) & ~(BITSET_BIT(b) - 1)) #define BITSET_RANGE(b, e) ((BITSET_MASK((e) + 1)) & ~(BITSET_BIT(b) - 1))
/* logic bit operations
*/
static inline void
__bitset_and(BITSET_WORD *r, const BITSET_WORD *x, const BITSET_WORD *y, unsigned n)
{
for (unsigned i = 0; i < n; i++)
r[i] = x[i] & y[i];
}
static inline void
__bitset_or(BITSET_WORD *r, const BITSET_WORD *x, const BITSET_WORD *y, unsigned n)
{
for (unsigned i = 0; i < n; i++)
r[i] = x[i] | y[i];
}
static inline void
__bitset_not(BITSET_WORD *x, unsigned n)
{
for (unsigned i = 0; i < n; i++)
x[i] = ~x[i];
}
#define BITSET_AND(r, x, y) \
do { \
assert(ARRAY_SIZE(r) == ARRAY_SIZE(x)); \
assert(ARRAY_SIZE(r) == ARRAY_SIZE(y)); \
__bitset_and(r, x, y, ARRAY_SIZE(r)); \
} while (0)
#define BITSET_OR(r, x, y) \
do { \
assert(ARRAY_SIZE(r) == ARRAY_SIZE(x)); \
assert(ARRAY_SIZE(r) == ARRAY_SIZE(y)); \
__bitset_or(r, x, y, ARRAY_SIZE(r)); \
} while (0)
#define BITSET_NOT(x) \
__bitset_not(x, ARRAY_SIZE(x))
/* bit range operations /* bit range operations
*/ */
#define BITSET_TEST_RANGE(x, b, e) \ #define BITSET_TEST_RANGE(x, b, e) \