41#include <rte_compat.h>
53#define RTE_BITSET_WORD_SIZE (sizeof(uint64_t))
59#define RTE_BITSET_WORD_BITS (RTE_BITSET_WORD_SIZE * CHAR_BIT)
64#define RTE_BITSET_NUM_WORDS(size) \
65 ((size + RTE_BITSET_WORD_BITS - 1) / RTE_BITSET_WORD_BITS)
71#define RTE_BITSET_SIZE(size) \
72 ((size_t)(RTE_BITSET_NUM_WORDS(size) * RTE_BITSET_WORD_SIZE))
74#define __RTE_BITSET_WORD_IDX(bit_num) ((bit_num) / RTE_BITSET_WORD_BITS)
75#define __RTE_BITSET_BIT_OFFSET(bit_num) ((bit_num) % RTE_BITSET_WORD_BITS)
76#define __RTE_BITSET_UNUSED(size) \
77 ((RTE_BITSET_NUM_WORDS(size) * RTE_BITSET_WORD_BITS) - (size))
78#define __RTE_BITSET_USED_MASK(size) \
79 (UINT64_MAX >> __RTE_BITSET_UNUSED(size))
81#define __RTE_BITSET_DELEGATE_N(fun, bitset, bit_num, ...) \
82 fun(&(bitset)[__RTE_BITSET_WORD_IDX(bit_num)], __RTE_BITSET_BIT_OFFSET(bit_num), \
86#define __RTE_BITSET_DELEGATE(fun, bitset, bit_num) \
87 fun(&(bitset)[__RTE_BITSET_WORD_IDX(bit_num)], __RTE_BITSET_BIT_OFFSET(bit_num))
104#define RTE_BITSET_DECLARE(name, size) \
105 uint64_t name[RTE_BITSET_NUM_WORDS(size)]
107#define __RTE_BITSET_FOREACH_LEFT(var, size, start_bit, len) \
108 ((len) - 1 - ((var) >= (start_bit) ? (var) - (start_bit) : (size) - (start_bit) + (var)))
110#define __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, flags) \
111 for ((var) = __rte_bitset_find(bitset, size, start_bit, len, flags); \
113 (var) = __RTE_BITSET_FOREACH_LEFT(var, size, start_bit, len) > 0 ? \
114 __rte_bitset_find(bitset, size, ((var) + 1) % (size), \
115 __RTE_BITSET_FOREACH_LEFT(var, size, start_bit, len), flags) : -1)
135#define RTE_BITSET_FOREACH_SET(var, bitset, size) \
136 __RTE_BITSET_FOREACH(var, bitset, size, 0, size, 0)
155#define RTE_BITSET_FOREACH_CLEAR(var, bitset, size) \
156 __RTE_BITSET_FOREACH(var, bitset, size, 0, size, __RTE_BITSET_FIND_FLAG_FIND_CLEAR)
181#define RTE_BITSET_FOREACH_SET_RANGE(var, bitset, size, start_bit, len) \
182 __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, 0)
207#define RTE_BITSET_FOREACH_CLEAR_RANGE(var, bitset, size, start_bit, len) \
208 __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, __RTE_BITSET_FIND_FLAG_FIND_CLEAR)
210#define RTE_BITSET_FOREACH_SET_WRAP(var, bitset, size, start_bit, len) \
211 __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, __RTE_BITSET_FIND_FLAG_WRAP)
213#define RTE_BITSET_FOREACH_CLEAR_WRAP(var, bitset, size, start_bit, len) \
214 __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, \
215 __RTE_BITSET_FIND_FLAG_WRAP | __RTE_BITSET_FIND_FLAG_FIND_CLEAR)
258#ifdef ALLOW_EXPERIMENTAL_API
259 return __RTE_BITSET_DELEGATE(
rte_bit_test, bitset, bit_num);
286#ifdef ALLOW_EXPERIMENTAL_API
287 __RTE_BITSET_DELEGATE(
rte_bit_set, bitset, bit_num);
314#ifdef ALLOW_EXPERIMENTAL_API
344#ifdef ALLOW_EXPERIMENTAL_API
345 __RTE_BITSET_DELEGATE_N(
rte_bit_assign, bitset, bit_num, bit_value);
373#ifdef ALLOW_EXPERIMENTAL_API
404#ifdef ALLOW_EXPERIMENTAL_API
440#ifdef ALLOW_EXPERIMENTAL_API
476#ifdef ALLOW_EXPERIMENTAL_API
514#ifdef ALLOW_EXPERIMENTAL_API
551#ifdef ALLOW_EXPERIMENTAL_API
594#ifdef ALLOW_EXPERIMENTAL_API
630 total +=
rte_popcount64(bitset[i] & __RTE_BITSET_USED_MASK(size));
652#ifdef ALLOW_EXPERIMENTAL_API
661#define __RTE_BITSET_FIND_FLAG_FIND_CLEAR (1U << 0)
662#define __RTE_BITSET_FIND_FLAG_WRAP (1U << 1)
666__rte_bitset_find_nowrap(
const uint64_t *bitset,
size_t __rte_unused size,
size_t start_bit,
667 size_t len,
bool find_clear)
671 size_t end_bit = start_bit + len;
673 RTE_ASSERT(end_bit <= size);
678 word_idx = __RTE_BITSET_WORD_IDX(start_bit);
679 offset = __RTE_BITSET_BIT_OFFSET(start_bit);
681 while (word_idx <= __RTE_BITSET_WORD_IDX(end_bit - 1)) {
684 word = bitset[word_idx];
691 size_t ffs = start_bit +
rte_bsf64(word);
714__rte_bitset_find(
const uint64_t *bitset,
size_t size,
size_t start_bit,
size_t len,
717#ifdef ALLOW_EXPERIMENTAL_API
718 bool find_clear = flags & __RTE_BITSET_FIND_FLAG_FIND_CLEAR;
719 bool may_wrap = flags & __RTE_BITSET_FIND_FLAG_WRAP;
720 bool does_wrap = (start_bit + len) > size;
723 RTE_ASSERT(len <= size);
725 RTE_ASSERT(!does_wrap);
727 if (may_wrap && does_wrap) {
728 size_t len0 = size - start_bit;
729 size_t len1 = len - len0;
731 rc = __rte_bitset_find_nowrap(bitset, size, start_bit, len0, find_clear);
733 rc = __rte_bitset_find_nowrap(bitset, size, 0, len1, find_clear);
735 rc = __rte_bitset_find_nowrap(bitset, size, start_bit, len, find_clear);
769#ifdef ALLOW_EXPERIMENTAL_API
770 return __rte_bitset_find(bitset, size, 0, size, 0);
805#ifdef ALLOW_EXPERIMENTAL_API
806 return __rte_bitset_find(bitset, size, start_bit, len, 0);
844#ifdef ALLOW_EXPERIMENTAL_API
845 return __rte_bitset_find(bitset, size, start_bit, len, __RTE_BITSET_FIND_FLAG_WRAP);
876#ifdef ALLOW_EXPERIMENTAL_API
877 return __rte_bitset_find(bitset, size, 0, size, __RTE_BITSET_FIND_FLAG_FIND_CLEAR);
912#ifdef ALLOW_EXPERIMENTAL_API
913 return __rte_bitset_find(bitset, size, start_bit, len, __RTE_BITSET_FIND_FLAG_FIND_CLEAR);
951#ifdef ALLOW_EXPERIMENTAL_API
952 return __rte_bitset_find(bitset, size, start_bit, len,
953 __RTE_BITSET_FIND_FLAG_FIND_CLEAR | __RTE_BITSET_FIND_FLAG_WRAP);
1009rte_bitset_or(uint64_t *dst_bitset,
const uint64_t *src_bitset0,
const uint64_t *src_bitset1,
1015 dst_bitset[i] = src_bitset0[i] | src_bitset1[i];
1039rte_bitset_and(uint64_t *dst_bitset,
const uint64_t *src_bitset0,
const uint64_t *src_bitset1,
1045 dst_bitset[i] = src_bitset0[i] & src_bitset1[i];
1069rte_bitset_xor(uint64_t *dst_bitset,
const uint64_t *src_bitset0,
const uint64_t *src_bitset1,
1075 dst_bitset[i] = src_bitset0[i] ^ src_bitset1[i];
1101 dst_bitset[i] = ~src_bitset[i];
1129 unsigned int dst_idx;
1132 int src_high_idx = dst_idx - src_word_offset;
1133 uint64_t low_bits = 0;
1134 uint64_t high_bits = 0;
1136 if (src_high_idx >= 0) {
1137 int src_low_idx = src_high_idx - 1;
1139 high_bits = src_bitset[src_high_idx] << src_bit_offset;
1141 if (src_bit_offset > 0 && src_low_idx >= 0)
1142 low_bits = src_bitset[src_low_idx] >>
1145 dst_bitset[dst_idx] = low_bits | high_bits;
1173 const uint64_t used_mask = __RTE_BITSET_USED_MASK(size);
1178 for (dst_idx = 0; dst_idx < num_words; dst_idx++) {
1179 int src_low_idx = src_word_offset + dst_idx;
1180 int src_high_idx = src_low_idx + 1;
1181 uint64_t src_low_word_bits = 0;
1182 uint64_t src_high_word_bits = 0;
1184 if (src_low_idx < num_words) {
1185 src_low_word_bits = src_bitset[src_low_idx];
1187 if (src_low_idx == (num_words - 1))
1188 src_low_word_bits &= used_mask;
1190 src_low_word_bits >>= src_bit_offset;
1192 if (src_bit_offset > 0 && src_high_idx < num_words) {
1193 src_high_word_bits = src_bitset[src_high_idx];
1195 if (src_high_idx == (num_words - 1))
1196 src_high_word_bits &= used_mask;
1201 dst_bitset[dst_idx] = src_low_word_bits | src_high_word_bits;
1225 uint64_t last_a, last_b;
1228 if (bitset_a[i] != bitset_b[i])
1231 last_a = bitset_a[i] << __RTE_BITSET_UNUSED(size);
1232 last_b = bitset_b[i] << __RTE_BITSET_UNUSED(size);
1234 return last_a == last_b;
#define rte_bit_atomic_assign(addr, nr, value, memory_order)
#define rte_bit_flip(addr, nr)
#define rte_bit_set(addr, nr)
#define rte_bit_atomic_flip(addr, nr, memory_order)
static uint32_t rte_bsf64(uint64_t v)
#define rte_bit_assign(addr, nr, value)
#define rte_bit_test(addr, nr)
static unsigned int rte_popcount64(uint64_t v)
#define rte_bit_atomic_set(addr, nr, memory_order)
#define rte_bit_clear(addr, nr)
#define rte_bit_atomic_clear(addr, nr, memory_order)
#define rte_bit_atomic_test(addr, nr, memory_order)
#define RTE_BITSET_WORD_BITS
static __rte_experimental ssize_t rte_bitset_find_clear_wrap(const uint64_t *bitset, size_t size, size_t start_bit, size_t len)
static __rte_experimental void rte_bitset_or(uint64_t *dst_bitset, const uint64_t *src_bitset0, const uint64_t *src_bitset1, size_t size)
static __rte_experimental size_t rte_bitset_count_set(const uint64_t *bitset, size_t size)
static __rte_experimental ssize_t rte_bitset_find_first_set(const uint64_t *bitset, size_t size)
static __rte_experimental void rte_bitset_assign(uint64_t *bitset, size_t bit_num, bool bit_value)
static __rte_experimental void rte_bitset_copy(uint64_t *__rte_restrict dst_bitset, const uint64_t *__rte_restrict src_bitset, size_t size)
static __rte_experimental ssize_t rte_bitset_find_first_clear(const uint64_t *bitset, size_t size)
static __rte_experimental void rte_bitset_and(uint64_t *dst_bitset, const uint64_t *src_bitset0, const uint64_t *src_bitset1, size_t size)
static __rte_experimental void rte_bitset_xor(uint64_t *dst_bitset, const uint64_t *src_bitset0, const uint64_t *src_bitset1, size_t size)
static __rte_experimental bool rte_bitset_atomic_test(const uint64_t *bitset, size_t bit_num, int memory_order)
static __rte_experimental void rte_bitset_shift_right(uint64_t *dst_bitset, const uint64_t *src_bitset, size_t size, size_t shift_bits)
static __rte_experimental ssize_t rte_bitset_find_set_wrap(const uint64_t *bitset, size_t size, size_t start_bit, size_t len)
static __rte_experimental void rte_bitset_set_all(uint64_t *bitset, size_t size)
static __rte_experimental void rte_bitset_init(uint64_t *bitset, size_t size)
static __rte_experimental void rte_bitset_complement(uint64_t *dst_bitset, const uint64_t *src_bitset, size_t size)
static __rte_experimental bool rte_bitset_equal(const uint64_t *bitset_a, const uint64_t *bitset_b, size_t size)
static __rte_experimental void rte_bitset_atomic_flip(uint64_t *bitset, size_t bit_num, int memory_order)
static __rte_experimental void rte_bitset_shift_left(uint64_t *dst_bitset, const uint64_t *src_bitset, size_t size, size_t shift_bits)
static __rte_experimental void rte_bitset_atomic_set(uint64_t *bitset, size_t bit_num, int memory_order)
static __rte_experimental void rte_bitset_flip(uint64_t *bitset, size_t bit_num)
static __rte_experimental void rte_bitset_clear(uint64_t *bitset, size_t bit_num)
static __rte_experimental bool rte_bitset_test(const uint64_t *bitset, size_t bit_num)
static __rte_experimental void rte_bitset_clear_all(uint64_t *bitset, size_t size)
static __rte_experimental void rte_bitset_atomic_assign(uint64_t *bitset, size_t bit_num, bool bit_value, int memory_order)
static __rte_experimental size_t rte_bitset_count_clear(const uint64_t *bitset, size_t size)
static __rte_experimental void rte_bitset_set(uint64_t *bitset, size_t bit_num)
static __rte_experimental void rte_bitset_atomic_clear(uint64_t *bitset, size_t bit_num, int memory_order)
static __rte_experimental ssize_t rte_bitset_find_clear(const uint64_t *bitset, size_t size, size_t start_bit, size_t len)
#define RTE_BITSET_NUM_WORDS(size)
#define RTE_BITSET_SIZE(size)
static __rte_experimental ssize_t rte_bitset_find_set(const uint64_t *bitset, size_t size, size_t start_bit, size_t len)
__rte_experimental ssize_t rte_bitset_to_str(const uint64_t *bitset, size_t size, char *buf, size_t capacity)
static void * rte_memcpy(void *dst, const void *src, size_t n)