eal: remove useless code in bsf64 function
[dpdk.git] / lib / librte_eal / common / include / rte_bitmap.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef __INCLUDE_RTE_BITMAP_H__
6 #define __INCLUDE_RTE_BITMAP_H__
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 /**
13  * @file
14  * RTE Bitmap
15  *
16  * The bitmap component provides a mechanism to manage large arrays of bits
17  * through bit get/set/clear and bit array scan operations.
18  *
19  * The bitmap scan operation is optimized for 64-bit CPUs using 64/128 byte cache
20  * lines. The bitmap is hierarchically organized using two arrays (array1 and
21  * array2), with each bit in array1 being associated with a full cache line
22  * (512/1024 bits) of bitmap bits, which are stored in array2: the bit in array1
23  * is set only when there is at least one bit set within its associated array2
24  * bits, otherwise the bit in array1 is cleared. The read and write operations
25  * for array1 and array2 are always done in slabs of 64 bits.
26  *
27  * This bitmap is not thread safe. For lock free operation on a specific bitmap
28  * instance, a single writer thread performing bit set/clear operations is
29  * allowed, only the writer thread can do bitmap scan operations, while there
30  * can be several reader threads performing bit get operations in parallel with
31  * the writer thread. When the use of locking primitives is acceptable, the
32  * serialization of the bit set/clear and bitmap scan operations needs to be
33  * enforced by the caller, while the bit get operation does not require locking
34  * the bitmap.
35  *
36  ***/
37
38 #include <string.h>
39 #include <rte_common.h>
40 #include <rte_config.h>
41 #include <rte_debug.h>
42 #include <rte_memory.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_prefetch.h>
45
46 /* Slab */
47 #define RTE_BITMAP_SLAB_BIT_SIZE                 64
48 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2            6
49 #define RTE_BITMAP_SLAB_BIT_MASK                 (RTE_BITMAP_SLAB_BIT_SIZE - 1)
50
51 /* Cache line (CL) */
52 #define RTE_BITMAP_CL_BIT_SIZE                   (RTE_CACHE_LINE_SIZE * 8)
53 #define RTE_BITMAP_CL_BIT_SIZE_LOG2              (RTE_CACHE_LINE_SIZE_LOG2 + 3)
54 #define RTE_BITMAP_CL_BIT_MASK                   (RTE_BITMAP_CL_BIT_SIZE - 1)
55
56 #define RTE_BITMAP_CL_SLAB_SIZE                  (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
57 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2             (RTE_BITMAP_CL_BIT_SIZE_LOG2 - RTE_BITMAP_SLAB_BIT_SIZE_LOG2)
58 #define RTE_BITMAP_CL_SLAB_MASK                  (RTE_BITMAP_CL_SLAB_SIZE - 1)
59
60 /** Bitmap data structure */
61 struct rte_bitmap {
62         /* Context for array1 and array2 */
63         uint64_t *array1;                        /**< Bitmap array1 */
64         uint64_t *array2;                        /**< Bitmap array2 */
65         uint32_t array1_size;                    /**< Number of 64-bit slabs in array1 that are actually used */
66         uint32_t array2_size;                    /**< Number of 64-bit slabs in array2 */
67
68         /* Context for the "scan next" operation */
69         uint32_t index1;  /**< Bitmap scan: Index of current array1 slab */
70         uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */
71         uint32_t index2;  /**< Bitmap scan: Index of current array2 slab */
72         uint32_t go2;     /**< Bitmap scan: Go/stop condition for current array2 cache line */
73
74         /* Storage space for array1 and array2 */
75         uint8_t memory[];
76 };
77
78 static inline void
79 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
80 {
81         bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
82 }
83
84 static inline uint64_t
85 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
86 {
87         return (~1llu) << bmp->offset1;
88 }
89
90 static inline void
91 __rte_bitmap_index2_set(struct rte_bitmap *bmp)
92 {
93         bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
94 }
95
96 static inline int
97 rte_bsf64(uint64_t slab, uint32_t *pos)
98 {
99         if (slab == 0)
100                 return 0;
101
102         *pos = __builtin_ctzll(slab);
103         return 1;
104 }
105
106 static inline uint32_t
107 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
108         uint32_t *array1_byte_offset, uint32_t *array1_slabs,
109         uint32_t *array2_byte_offset, uint32_t *array2_slabs)
110 {
111         uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
112         uint32_t n_cache_lines_array2;
113         uint32_t n_bytes_total;
114
115         n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
116         n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
117         n_slabs_array1 = rte_align32pow2(n_slabs_array1);
118         n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
119         n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
120         n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
121
122         if (array1_byte_offset) {
123                 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
124         }
125         if (array1_slabs) {
126                 *array1_slabs = n_slabs_array1;
127         }
128         if (array2_byte_offset) {
129                 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
130         }
131         if (array2_slabs) {
132                 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
133         }
134
135         return n_bytes_total;
136 }
137
138 static inline void
139 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
140 {
141         bmp->index1 = bmp->array1_size - 1;
142         bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
143         __rte_bitmap_index2_set(bmp);
144         bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
145
146         bmp->go2 = 0;
147 }
148
149 /**
150  * Bitmap memory footprint calculation
151  *
152  * @param n_bits
153  *   Number of bits in the bitmap
154  * @return
155  *   Bitmap memory footprint measured in bytes on success, 0 on error
156  */
157 static inline uint32_t
158 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
159         /* Check input arguments */
160         if (n_bits == 0) {
161                 return 0;
162         }
163
164         return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
165 }
166
167 /**
168  * Bitmap initialization
169  *
170  * @param n_bits
171  *   Number of pre-allocated bits in array2.
172  * @param mem
173  *   Base address of array1 and array2.
174  * @param mem_size
175  *   Minimum expected size of bitmap.
176  * @return
177  *   Handle to bitmap instance.
178  */
179 static inline struct rte_bitmap *
180 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
181 {
182         struct rte_bitmap *bmp;
183         uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
184         uint32_t size;
185
186         /* Check input arguments */
187         if (n_bits == 0) {
188                 return NULL;
189         }
190
191         if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
192                 return NULL;
193         }
194
195         size = __rte_bitmap_get_memory_footprint(n_bits,
196                 &array1_byte_offset, &array1_slabs,
197                 &array2_byte_offset, &array2_slabs);
198         if (size < mem_size) {
199                 return NULL;
200         }
201
202         /* Setup bitmap */
203         memset(mem, 0, size);
204         bmp = (struct rte_bitmap *) mem;
205
206         bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
207         bmp->array1_size = array1_slabs;
208         bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
209         bmp->array2_size = array2_slabs;
210
211         __rte_bitmap_scan_init(bmp);
212
213         return bmp;
214 }
215
216 /**
217  * Bitmap free
218  *
219  * @param bmp
220  *   Handle to bitmap instance
221  * @return
222  *   0 upon success, error code otherwise
223  */
224 static inline int
225 rte_bitmap_free(struct rte_bitmap *bmp)
226 {
227         /* Check input arguments */
228         if (bmp == NULL) {
229                 return -1;
230         }
231
232         return 0;
233 }
234
235 /**
236  * Bitmap reset
237  *
238  * @param bmp
239  *   Handle to bitmap instance
240  */
241 static inline void
242 rte_bitmap_reset(struct rte_bitmap *bmp)
243 {
244         memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
245         memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
246         __rte_bitmap_scan_init(bmp);
247 }
248
249 /**
250  * Bitmap location prefetch into CPU L1 cache
251  *
252  * @param bmp
253  *   Handle to bitmap instance
254  * @param pos
255  *   Bit position
256  * @return
257  *   0 upon success, error code otherwise
258  */
259 static inline void
260 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
261 {
262         uint64_t *slab2;
263         uint32_t index2;
264
265         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
266         slab2 = bmp->array2 + index2;
267         rte_prefetch0((void *) slab2);
268 }
269
270 /**
271  * Bitmap bit get
272  *
273  * @param bmp
274  *   Handle to bitmap instance
275  * @param pos
276  *   Bit position
277  * @return
278  *   0 when bit is cleared, non-zero when bit is set
279  */
280 static inline uint64_t
281 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
282 {
283         uint64_t *slab2;
284         uint32_t index2, offset2;
285
286         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
287         offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
288         slab2 = bmp->array2 + index2;
289         return (*slab2) & (1llu << offset2);
290 }
291
292 /**
293  * Bitmap bit set
294  *
295  * @param bmp
296  *   Handle to bitmap instance
297  * @param pos
298  *   Bit position
299  */
300 static inline void
301 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
302 {
303         uint64_t *slab1, *slab2;
304         uint32_t index1, index2, offset1, offset2;
305
306         /* Set bit in array2 slab and set bit in array1 slab */
307         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
308         offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
309         index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
310         offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
311         slab2 = bmp->array2 + index2;
312         slab1 = bmp->array1 + index1;
313
314         *slab2 |= 1llu << offset2;
315         *slab1 |= 1llu << offset1;
316 }
317
318 /**
319  * Bitmap slab set
320  *
321  * @param bmp
322  *   Handle to bitmap instance
323  * @param pos
324  *   Bit position identifying the array2 slab
325  * @param slab
326  *   Value to be assigned to the 64-bit slab in array2
327  */
328 static inline void
329 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
330 {
331         uint64_t *slab1, *slab2;
332         uint32_t index1, index2, offset1;
333
334         /* Set bits in array2 slab and set bit in array1 slab */
335         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
336         index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
337         offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
338         slab2 = bmp->array2 + index2;
339         slab1 = bmp->array1 + index1;
340
341         *slab2 |= slab;
342         *slab1 |= 1llu << offset1;
343 }
344
345 static inline uint64_t
346 __rte_bitmap_line_not_empty(uint64_t *slab2)
347 {
348         uint64_t v1, v2, v3, v4;
349
350         v1 = slab2[0] | slab2[1];
351         v2 = slab2[2] | slab2[3];
352         v3 = slab2[4] | slab2[5];
353         v4 = slab2[6] | slab2[7];
354         v1 |= v2;
355         v3 |= v4;
356
357         return v1 | v3;
358 }
359
360 /**
361  * Bitmap bit clear
362  *
363  * @param bmp
364  *   Handle to bitmap instance
365  * @param pos
366  *   Bit position
367  */
368 static inline void
369 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
370 {
371         uint64_t *slab1, *slab2;
372         uint32_t index1, index2, offset1, offset2;
373
374         /* Clear bit in array2 slab */
375         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
376         offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
377         slab2 = bmp->array2 + index2;
378
379         /* Return if array2 slab is not all-zeros */
380         *slab2 &= ~(1llu << offset2);
381         if (*slab2){
382                 return;
383         }
384
385         /* Check the entire cache line of array2 for all-zeros */
386         index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
387         slab2 = bmp->array2 + index2;
388         if (__rte_bitmap_line_not_empty(slab2)) {
389                 return;
390         }
391
392         /* The array2 cache line is all-zeros, so clear bit in array1 slab */
393         index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
394         offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
395         slab1 = bmp->array1 + index1;
396         *slab1 &= ~(1llu << offset1);
397
398         return;
399 }
400
401 static inline int
402 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
403 {
404         uint64_t value1;
405         uint32_t i;
406
407         /* Check current array1 slab */
408         value1 = bmp->array1[bmp->index1];
409         value1 &= __rte_bitmap_mask1_get(bmp);
410
411         if (rte_bsf64(value1, &bmp->offset1)) {
412                 return 1;
413         }
414
415         __rte_bitmap_index1_inc(bmp);
416         bmp->offset1 = 0;
417
418         /* Look for another array1 slab */
419         for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
420                 value1 = bmp->array1[bmp->index1];
421
422                 if (rte_bsf64(value1, &bmp->offset1)) {
423                         return 1;
424                 }
425         }
426
427         return 0;
428 }
429
430 static inline void
431 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
432 {
433         __rte_bitmap_index2_set(bmp);
434         bmp->go2 = 1;
435         rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
436 }
437
438 static inline int
439 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
440 {
441         uint64_t *slab2;
442
443         slab2 = bmp->array2 + bmp->index2;
444         for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
445                 if (*slab2) {
446                         *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
447                         *slab = *slab2;
448
449                         bmp->index2 ++;
450                         slab2 ++;
451                         bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
452                         return 1;
453                 }
454         }
455
456         return 0;
457 }
458
459 /**
460  * Bitmap scan (with automatic wrap-around)
461  *
462  * @param bmp
463  *   Handle to bitmap instance
464  * @param pos
465  *   When function call returns 1, pos contains the position of the next set
466  *   bit, otherwise not modified
467  * @param slab
468  *   When function call returns 1, slab contains the value of the entire 64-bit
469  *   slab where the bit indicated by pos is located. Slabs are always 64-bit
470  *   aligned, so the position of the first bit of the slab (this bit is not
471  *   necessarily set) is pos / 64. Once a slab has been returned by the bitmap
472  *   scan operation, the internal pointers of the bitmap are updated to point
473  *   after this slab, so the same slab will not be returned again if it
474  *   contains more than one bit which is set. When function call returns 0,
475  *   slab is not modified.
476  * @return
477  *   0 if there is no bit set in the bitmap, 1 otherwise
478  */
479 static inline int
480 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
481 {
482         /* Return data from current array2 line if available */
483         if (__rte_bitmap_scan_read(bmp, pos, slab)) {
484                 return 1;
485         }
486
487         /* Look for non-empty array2 line */
488         if (__rte_bitmap_scan_search(bmp)) {
489                 __rte_bitmap_scan_read_init(bmp);
490                 __rte_bitmap_scan_read(bmp, pos, slab);
491                 return 1;
492         }
493
494         /* Empty bitmap */
495         return 0;
496 }
497
498 #ifdef __cplusplus
499 }
500 #endif
501
502 #endif /* __INCLUDE_RTE_BITMAP_H__ */