34 #ifndef __INCLUDE_RTE_BITMAP_H__
35 #define __INCLUDE_RTE_BITMAP_H__
73 #ifndef RTE_BITMAP_OPTIMIZATIONS
74 #define RTE_BITMAP_OPTIMIZATIONS 1
76 #if RTE_BITMAP_OPTIMIZATIONS
77 #include <tmmintrin.h>
81 #define RTE_BITMAP_SLAB_BIT_SIZE 64
82 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2 6
83 #define RTE_BITMAP_SLAB_BIT_MASK (RTE_BITMAP_SLAB_BIT_SIZE - 1)
86 #define RTE_BITMAP_CL_BIT_SIZE (RTE_CACHE_LINE_SIZE * 8)
87 #define RTE_BITMAP_CL_BIT_SIZE_LOG2 9
88 #define RTE_BITMAP_CL_BIT_MASK (RTE_BITMAP_CL_BIT_SIZE - 1)
90 #define RTE_BITMAP_CL_SLAB_SIZE (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
91 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2 3
92 #define RTE_BITMAP_CL_SLAB_MASK (RTE_BITMAP_CL_SLAB_SIZE - 1)
113 __rte_bitmap_index1_inc(
struct rte_bitmap *bmp)
118 static inline uint64_t
119 __rte_bitmap_mask1_get(
struct rte_bitmap *bmp)
121 return ((~1lu) << bmp->
offset1);
125 __rte_bitmap_index2_set(
struct rte_bitmap *bmp)
127 bmp->
index2 = (((bmp->
index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->
offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
130 #if RTE_BITMAP_OPTIMIZATIONS
133 rte_bsf64(uint64_t slab, uint32_t *pos)
139 *pos = __builtin_ctzll(slab);
146 rte_bsf64(uint64_t slab, uint32_t *pos)
155 for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
167 static inline uint32_t
168 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
169 uint32_t *array1_byte_offset, uint32_t *array1_slabs,
170 uint32_t *array2_byte_offset, uint32_t *array2_slabs)
172 uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
173 uint32_t n_cache_lines_array2;
174 uint32_t n_bytes_total;
176 n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
177 n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
179 n_slabs_context = (
sizeof(
struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
180 n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
181 n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) *
RTE_CACHE_LINE_SIZE;
183 if (array1_byte_offset) {
184 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
187 *array1_slabs = n_slabs_array1;
189 if (array2_byte_offset) {
193 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
196 return n_bytes_total;
200 __rte_bitmap_scan_init(
struct rte_bitmap *bmp)
203 bmp->
offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
204 __rte_bitmap_index2_set(bmp);
205 bmp->
index2 += RTE_BITMAP_CL_SLAB_SIZE;
218 static inline uint32_t
225 return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
244 uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
256 size = __rte_bitmap_get_memory_footprint(n_bits,
257 &array1_byte_offset, &array1_slabs,
258 &array2_byte_offset, &array2_slabs);
259 if (size < mem_size) {
264 memset(mem, 0, size);
267 bmp->
array1 = (uint64_t *) &mem[array1_byte_offset];
269 bmp->
array2 = (uint64_t *) &mem[array2_byte_offset];
272 __rte_bitmap_scan_init(bmp);
307 __rte_bitmap_scan_init(bmp);
326 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
341 static inline uint64_t
347 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
348 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
350 return ((*slab2) & (1lu << offset2));
364 uint64_t *slab1, *slab2;
368 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
369 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
370 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
371 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
375 *slab2 |= 1lu << offset2;
392 uint64_t *slab1, *slab2;
396 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
397 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
398 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
406 static inline uint64_t
407 __rte_bitmap_line_not_empty(uint64_t *slab2)
409 uint64_t v1, v2, v3, v4;
411 v1 = slab2[0] | slab2[1];
412 v2 = slab2[2] | slab2[3];
413 v3 = slab2[4] | slab2[5];
414 v4 = slab2[6] | slab2[7];
432 uint64_t *slab1, *slab2;
436 index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
437 offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
441 *slab2 &= ~(1lu << offset2);
447 index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
449 if (__rte_bitmap_line_not_empty(slab2)) {
454 index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
455 offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
463 __rte_bitmap_scan_search(
struct rte_bitmap *bmp)
470 value1 &= __rte_bitmap_mask1_get(bmp);
472 if (rte_bsf64(value1, &bmp->
offset1)) {
476 __rte_bitmap_index1_inc(bmp);
480 for (i = 0; i < bmp->
array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
483 if (rte_bsf64(value1, &bmp->
offset1)) {
492 __rte_bitmap_scan_read_init(
struct rte_bitmap *bmp)
494 __rte_bitmap_index2_set(bmp);
500 __rte_bitmap_scan_read(
struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
505 for ( ; bmp->
go2 ; bmp->
index2 ++, slab2 ++, bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK) {
507 *pos = bmp->
index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
512 bmp->
go2 = bmp->
index2 & RTE_BITMAP_CL_SLAB_MASK;
544 if (__rte_bitmap_scan_read(bmp, pos, slab)) {
549 if (__rte_bitmap_scan_search(bmp)) {
550 __rte_bitmap_scan_read_init(bmp);
551 __rte_bitmap_scan_read(bmp, pos, slab);