LCOV - code coverage report
Current view: top level - lib/eal/include - rte_bitset.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 116 116 100.0 %
Date: 2026-04-01 20:02:27 Functions: 15 15 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 76 84 90.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2023 Ericsson AB
       3                 :            :  */
       4                 :            : 
       5                 :            : #ifndef _RTE_BITSET_H_
       6                 :            : #define _RTE_BITSET_H_
       7                 :            : 
       8                 :            : /**
       9                 :            :  * @file
      10                 :            :  * RTE Bitset
      11                 :            :  *
      12                 :            :  * This file provides functions and macros for querying and
      13                 :            :  * manipulating sets of bits kept in arrays of @c uint64_t-sized
      14                 :            :  * elements.
      15                 :            :  *
      16                 :            :  * The bits in a bitset are numbered from 0 to @c size - 1, with the
      17                 :            :  * lowest index being the least significant bit.
      18                 :            :  *
      19                 :            :  * The bitset array must be properly aligned.
      20                 :            :  *
      21                 :            :  * For optimal performance, the @c size parameter, required by
      22                 :            :  * many of the API's functions, should be a compile-time constant.
      23                 :            :  *
      24                 :            :  * For large bitsets, the rte_bitmap.h API may be more appropriate.
      25                 :            :  *
      26                 :            :  * @warning
      27                 :            :  * All functions modifying a bitset may overwrite any unused bits of
      28                 :            :  * the last word. Such unused bits are ignored by all functions reading
      29                 :            :  * bits.
      30                 :            :  */
      31                 :            : 
      32                 :            : #include <limits.h>
      33                 :            : #include <stdbool.h>
      34                 :            : #include <stddef.h>
      35                 :            : #include <stdint.h>
      36                 :            : 
      37                 :            : #include <rte_bitops.h>
      38                 :            : #include <rte_branch_prediction.h>
      39                 :            : #include <rte_common.h>
      40                 :            : #include <rte_compat.h>
      41                 :            : #include <rte_debug.h>
      42                 :            : #include <rte_memcpy.h>
      43                 :            : 
      44                 :            : #ifdef __cplusplus
      45                 :            : extern "C" {
      46                 :            : #endif
      47                 :            : 
      48                 :            : /**
      49                 :            :  * The size (in bytes) of each element in the array used to represent
      50                 :            :  * a bitset.
      51                 :            :  */
      52                 :            : #define RTE_BITSET_WORD_SIZE (sizeof(uint64_t))
      53                 :            : 
      54                 :            : /**
      55                 :            :  * The size (in bits) of each element in the array used to represent
      56                 :            :  * a bitset.
      57                 :            :  */
      58                 :            : #define RTE_BITSET_WORD_BITS (RTE_BITSET_WORD_SIZE * CHAR_BIT)
      59                 :            : 
      60                 :            : /**
      61                 :            :  * Computes the number of words required to store @c size bits.
      62                 :            :  */
      63                 :            : #define RTE_BITSET_NUM_WORDS(size) \
      64                 :            :         ((size + RTE_BITSET_WORD_BITS - 1) / RTE_BITSET_WORD_BITS)
      65                 :            : 
      66                 :            : /**
      67                 :            :  * Computes the amount of memory (in bytes) required to fit a bitset
      68                 :            :  * holding @c size bits.
      69                 :            :  */
      70                 :            : #define RTE_BITSET_SIZE(size) \
      71                 :            :         ((size_t)(RTE_BITSET_NUM_WORDS(size) * RTE_BITSET_WORD_SIZE))
      72                 :            : 
      73                 :            : #define __RTE_BITSET_WORD_IDX(bit_num) ((bit_num) / RTE_BITSET_WORD_BITS)
      74                 :            : #define __RTE_BITSET_BIT_OFFSET(bit_num) ((bit_num) % RTE_BITSET_WORD_BITS)
      75                 :            : #define __RTE_BITSET_UNUSED(size) \
      76                 :            :         ((RTE_BITSET_NUM_WORDS(size) * RTE_BITSET_WORD_BITS) - (size))
      77                 :            : #define __RTE_BITSET_USED_MASK(size) \
      78                 :            :         (UINT64_MAX >> __RTE_BITSET_UNUSED(size))
      79                 :            : 
      80                 :            : #define __RTE_BITSET_DELEGATE_N(fun, bitset, bit_num, ...) \
      81                 :            :         fun(&(bitset)[__RTE_BITSET_WORD_IDX(bit_num)], __RTE_BITSET_BIT_OFFSET(bit_num), \
      82                 :            :                 __VA_ARGS__)
      83                 :            : 
      84                 :            : /* MSVC doesn't have ##__VA_ARGS__, so argument-less -> special case */
      85                 :            : #define __RTE_BITSET_DELEGATE(fun, bitset, bit_num) \
      86                 :            :         fun(&(bitset)[__RTE_BITSET_WORD_IDX(bit_num)], __RTE_BITSET_BIT_OFFSET(bit_num))
      87                 :            : 
      88                 :            : /**
      89                 :            :  * @warning
      90                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
      91                 :            :  *
      92                 :            :  * Declare a bitset.
      93                 :            :  *
      94                 :            :  * Declare (e.g., as a struct field) or define (e.g., as a stack
      95                 :            :  * variable) a bitset of the specified size.
      96                 :            :  *
      97                 :            :  * @param size
      98                 :            :  *   The number of bits the bitset must be able to represent. Must be
      99                 :            :  *   a compile-time constant.
     100                 :            :  * @param name
     101                 :            :  *   The field or variable name of the resulting definition.
     102                 :            :  */
     103                 :            : #define RTE_BITSET_DECLARE(name, size) \
     104                 :            :         uint64_t name[RTE_BITSET_NUM_WORDS(size)]
     105                 :            : 
     106                 :            : #define __RTE_BITSET_FOREACH_LEFT(var, size, start_bit, len) \
     107                 :            :         ((len) - 1 - ((var) >= (start_bit) ? (var) - (start_bit) : (size) - (start_bit) + (var)))
     108                 :            : 
     109                 :            : #define __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, flags) \
     110                 :            :         for ((var) = __rte_bitset_find(bitset, size, start_bit, len, flags); \
     111                 :            :                         (var) != -1; \
     112                 :            :                         (var) = __RTE_BITSET_FOREACH_LEFT(var, size, start_bit, len) > 0 ? \
     113                 :            :                                 __rte_bitset_find(bitset, size, ((var) + 1) % (size), \
     114                 :            :                                 __RTE_BITSET_FOREACH_LEFT(var, size, start_bit, len), flags) : -1)
     115                 :            : 
     116                 :            : /**
     117                 :            :  * @warning
     118                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     119                 :            :  *
     120                 :            :  * Iterate over all bits set.
     121                 :            :  *
     122                 :            :  * This macro iterates over all bits set (i.e., all ones) in the
     123                 :            :  * bitset, in the forward direction (i.e., starting with the least
     124                 :            :  * significant '1').
     125                 :            :  *
     126                 :            :  * @param var
     127                 :            :  *   An iterator variable of type @c ssize_t. For each successive
     128                 :            :  *   iteration, this variable will hold the bit index of a set bit.
     129                 :            :  * @param bitset
     130                 :            :  *   A <tt>const uint64_t *</tt> pointer to the bitset array.
     131                 :            :  * @param size
     132                 :            :  *   The size of the bitset (in bits).
     133                 :            :  */
     134                 :            : #define RTE_BITSET_FOREACH_SET(var, bitset, size) \
     135                 :            :         __RTE_BITSET_FOREACH(var, bitset, size, 0, size, 0)
     136                 :            : 
     137                 :            : /**
     138                 :            :  * @warning
     139                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     140                 :            :  *
     141                 :            :  * Iterate over all bits cleared.
     142                 :            :  *
     143                 :            :  * This macro iterates over all bits cleared in the bitset, in the
     144                 :            :  * forward direction (i.e., starting with the lowest-indexed set bit).
     145                 :            :  *
     146                 :            :  * @param var
     147                 :            :  *   An iterator variable of type @c ssize_t. For each successive iteration,
     148                 :            :  *   this variable will hold the bit index of a cleared bit.
     149                 :            :  * @param bitset
     150                 :            :  *   A <tt>const uint64_t *</tt> pointer to the bitset array.
     151                 :            :  * @param size
     152                 :            :  *   The size of the bitset (in bits).
     153                 :            :  */
     154                 :            : #define RTE_BITSET_FOREACH_CLEAR(var, bitset, size) \
     155                 :            :         __RTE_BITSET_FOREACH(var, bitset, size, 0, size, __RTE_BITSET_FIND_FLAG_FIND_CLEAR)
     156                 :            : 
     157                 :            : /**
     158                 :            :  * @warning
     159                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     160                 :            :  *
     161                 :            :  * Iterate over all bits set within a range.
     162                 :            :  *
     163                 :            :  * This macro iterates over all bits set (i.e., all ones) in the
     164                 :            :  * specified range, in the forward direction (i.e., starting with the
     165                 :            :  * least significant '1').
     166                 :            :  *
     167                 :            :  * @param var
     168                 :            :  *   An iterator variable of type @c ssize_t. For each successive iteration,
     169                 :            :  *   this variable will hold the bit index of a set bit.
     170                 :            :  * @param bitset
     171                 :            :  *   A <tt>const uint64_t *</tt> pointer to the bitset array.
     172                 :            :  * @param size
     173                 :            :  *   The size of the bitset (in bits).
     174                 :            :  * @param start_bit
     175                 :            :  *   The index of the first bit to check. Must be less than @c size.
     176                 :            :  * @param len
     177                 :            :  *   The length (in bits) of the range. @c start_bit + @c len must be less
     178                 :            :  *   than or equal to @c size.
     179                 :            :  */
     180                 :            : #define RTE_BITSET_FOREACH_SET_RANGE(var, bitset, size, start_bit, len) \
     181                 :            :         __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, 0)
     182                 :            : 
     183                 :            : /**
     184                 :            :  * @warning
     185                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     186                 :            :  *
     187                 :            :  * Iterate over all cleared bits within a range.
     188                 :            :  *
     189                 :            :  * This macro iterates over all bits cleared (i.e., all zeroes) in the
     190                 :            :  * specified range, in the forward direction (i.e., starting with the
     191                 :            :  * least significant '0').
     192                 :            :  *
     193                 :            :  * @param var
     194                 :            :  *   An iterator variable of type @c ssize_t. For each successive iteration,
     195                 :            :  *   this variable will hold the bit index of a set bit.
     196                 :            :  * @param bitset
     197                 :            :  *   A <tt>const uint64_t *</tt> pointer to the bitset array.
     198                 :            :  * @param size
     199                 :            :  *   The size of the bitset (in bits).
     200                 :            :  * @param start_bit
     201                 :            :  *   The index of the first bit to check. Must be less than @c size.
     202                 :            :  * @param len
     203                 :            :  *   The length (in bits) of the range. @c start_bit + @c len must be less
     204                 :            :  *   than or equal to @c size.
     205                 :            :  */
     206                 :            : #define RTE_BITSET_FOREACH_CLEAR_RANGE(var, bitset, size, start_bit, len) \
     207                 :            :         __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, __RTE_BITSET_FIND_FLAG_FIND_CLEAR)
     208                 :            : 
     209                 :            : #define RTE_BITSET_FOREACH_SET_WRAP(var, bitset, size, start_bit, len) \
     210                 :            :         __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, __RTE_BITSET_FIND_FLAG_WRAP)
     211                 :            : 
     212                 :            : #define RTE_BITSET_FOREACH_CLEAR_WRAP(var, bitset, size, start_bit, len) \
     213                 :            :         __RTE_BITSET_FOREACH(var, bitset, size, start_bit, len, \
     214                 :            :                 __RTE_BITSET_FIND_FLAG_WRAP | __RTE_BITSET_FIND_FLAG_FIND_CLEAR)
     215                 :            : 
     216                 :            : /**
     217                 :            :  * @warning
     218                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     219                 :            :  *
     220                 :            :  * Initializes a bitset.
     221                 :            :  *
     222                 :            :  * All bits are cleared.
     223                 :            :  *
     224                 :            :  * In case all words in the bitset array are already set to zero by
     225                 :            :  * other means (e.g., at the time of memory allocation), this function
     226                 :            :  * need not be called.
     227                 :            :  *
     228                 :            :  * @param bitset
     229                 :            :  *   A pointer to the array of bitset 64-bit words.
     230                 :            :  * @param size
     231                 :            :  *   The size of the bitset (in bits).
     232                 :            :  */
     233                 :            : __rte_experimental
     234                 :            : static inline void
     235                 :            : rte_bitset_init(uint64_t *bitset, size_t size)
     236                 :            : {
     237                 :     140005 :         memset(bitset, 0, RTE_BITSET_SIZE(size));
     238                 :            : }
     239                 :            : 
     240                 :            : /**
     241                 :            :  * @warning
     242                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     243                 :            :  *
     244                 :            :  * Test if a bit is set.
     245                 :            :  *
     246                 :            :  * @param bitset
     247                 :            :  *   A pointer to the array of words making up the bitset.
     248                 :            :  * @param bit_num
     249                 :            :  *   Index of the bit to test. Index 0 is the least significant bit.
     250                 :            :  * @return
     251                 :            :  *   Returns true if the bit is '1', and false if the bit is '0'.
     252                 :            :  */
     253                 :            : __rte_experimental
     254                 :            : static inline bool
     255                 :   15043510 : rte_bitset_test(const uint64_t *bitset, size_t bit_num)
     256                 :            : {
     257                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     258   [ +  +  +  +  :   15923851 :         return __RTE_BITSET_DELEGATE(rte_bit_test, bitset, bit_num);
                   -  + ]
     259                 :            : #else
     260                 :            :         RTE_SET_USED(bitset);
     261                 :            :         RTE_SET_USED(bit_num);
     262                 :            :         RTE_VERIFY(false);
     263                 :            : #endif
     264                 :            : }
     265                 :            : 
     266                 :            : /**
     267                 :            :  * @warning
     268                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     269                 :            :  *
     270                 :            :  * Set a bit in the bitset.
     271                 :            :  *
     272                 :            :  * Bits are numbered from 0 to (size - 1) (inclusive).
     273                 :            :  *
     274                 :            :  * The operation is not guaranteed to be atomic.
     275                 :            :  *
     276                 :            :  * @param bitset
     277                 :            :  *   A pointer to the array of words making up the bitset.
     278                 :            :  * @param bit_num
     279                 :            :  *   The index of the bit to be set.
     280                 :            :  */
     281                 :            : __rte_experimental
     282                 :            : static inline void
     283                 :    2510814 : rte_bitset_set(uint64_t *bitset, size_t bit_num)
     284                 :            : {
     285                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     286   [ +  +  +  - ]:   17172314 :         __RTE_BITSET_DELEGATE(rte_bit_set, bitset, bit_num);
     287                 :            : #else
     288                 :            :         RTE_SET_USED(bitset);
     289                 :            :         RTE_SET_USED(bit_num);
     290                 :            :         RTE_VERIFY(false);
     291                 :            : #endif
     292                 :    8760264 : }
     293                 :            : 
     294                 :            : /**
     295                 :            :  * @warning
     296                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     297                 :            :  *
     298                 :            :  * Clear a bit in the bitset.
     299                 :            :  *
     300                 :            :  * Bits are numbered 0 to (size - 1) (inclusive).
     301                 :            :  *
     302                 :            :  * The operation is not guaranteed to be atomic.
     303                 :            :  *
     304                 :            :  * @param bitset
     305                 :            :  *   A pointer to the array of words making up the bitset.
     306                 :            :  * @param bit_num
     307                 :            :  *   The index of the bit to be cleared.
     308                 :            :  */
     309                 :            : __rte_experimental
     310                 :            : static inline void
     311                 :    2508946 : rte_bitset_clear(uint64_t *bitset, size_t bit_num)
     312                 :            : {
     313                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     314                 :    8781099 :         __RTE_BITSET_DELEGATE(rte_bit_clear, bitset, bit_num);
     315                 :            : #else
     316                 :            :         RTE_SET_USED(bitset);
     317                 :            :         RTE_SET_USED(bit_num);
     318                 :            :         RTE_VERIFY(false);
     319                 :            : #endif
     320                 :    8758842 : }
     321                 :            : 
     322                 :            : /**
     323                 :            :  * @warning
     324                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     325                 :            :  *
     326                 :            :  * Set or clear a bit in the bitset.
     327                 :            :  *
     328                 :            :  * Bits are numbered 0 to (size - 1) (inclusive).
     329                 :            :  *
     330                 :            :  * The operation is not guaranteed to be atomic.
     331                 :            :  *
     332                 :            :  * @param bitset
     333                 :            :  *   A pointer to the array of words making up the bitset.
     334                 :            :  * @param bit_num
     335                 :            :  *   The index of the bit to be set or cleared.
     336                 :            :  * @param bit_value
     337                 :            :  *   Control if the bit should be set or cleared.
     338                 :            :  */
     339                 :            : __rte_experimental
     340                 :            : static inline void
     341                 :    5011875 : rte_bitset_assign(uint64_t *bitset, size_t bit_num, bool bit_value)
     342                 :            : {
     343                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     344   [ +  +  +  +  :   26786022 :         __RTE_BITSET_DELEGATE_N(rte_bit_assign, bitset, bit_num, bit_value);
          +  +  +  +  +  
                +  +  + ]
     345                 :            : #else
     346                 :            :         RTE_SET_USED(bitset);
     347                 :            :         RTE_SET_USED(bit_num);
     348                 :            :         RTE_SET_USED(bit_value);
     349                 :            :         RTE_VERIFY(false);
     350                 :            : #endif
     351                 :    5011875 : }
     352                 :            : 
     353                 :            : /**
     354                 :            :  * @warning
     355                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     356                 :            :  *
     357                 :            :  * Change the value of a bit in the bitset.
     358                 :            :  *
     359                 :            :  * Bits are numbered 0 to (size - 1) (inclusive).
     360                 :            :  *
     361                 :            :  * The operation is not guaranteed to be atomic.
     362                 :            :  *
     363                 :            :  * @param bitset
     364                 :            :  *   A pointer to the array of words making up the bitset.
     365                 :            :  * @param bit_num
     366                 :            :  *   The index of the bit to be flipped.
     367                 :            :  */
     368                 :            : __rte_experimental
     369                 :            : static inline void
     370                 :    5011875 : rte_bitset_flip(uint64_t *bitset, size_t bit_num)
     371                 :            : {
     372                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     373         [ +  + ]:    5011875 :         __RTE_BITSET_DELEGATE(rte_bit_flip, bitset, bit_num);
     374                 :            : #else
     375                 :            :         RTE_SET_USED(bitset);
     376                 :            :         RTE_SET_USED(bit_num);
     377                 :            :         RTE_VERIFY(false);
     378                 :            : #endif
     379                 :    5011875 : }
     380                 :            : 
     381                 :            : /**
     382                 :            :  * @warning
     383                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     384                 :            :  *
     385                 :            :  * Atomically test if a bit is set.
     386                 :            :  *
     387                 :            :  * Atomically test if a bit in a bitset is set with the specified
     388                 :            :  * memory ordering.
     389                 :            :  *
     390                 :            :  * @param bitset
     391                 :            :  *   A pointer to the array of words making up the bitset.
     392                 :            :  * @param bit_num
     393                 :            :  *   Index of the bit to test. Index 0 is the least significant bit.
     394                 :            :  * @param memory_order
     395                 :            :  *   The memory order to use.
     396                 :            :  * @return
     397                 :            :  *   Returns true if the bit is '1', and false if the bit is '0'.
     398                 :            :  */
     399                 :            : __rte_experimental
     400                 :            : static inline bool
     401                 :            : rte_bitset_atomic_test(const uint64_t *bitset, size_t bit_num, int memory_order)
     402                 :            : {
     403                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     404                 :   15093525 :         return __RTE_BITSET_DELEGATE_N(rte_bit_atomic_test, bitset, bit_num, memory_order);
     405                 :            : #else
     406                 :            :         RTE_SET_USED(bitset);
     407                 :            :         RTE_SET_USED(bit_num);
     408                 :            :         RTE_SET_USED(memory_order);
     409                 :            :         RTE_VERIFY(false);
     410                 :            : #endif
     411                 :            : }
     412                 :            : 
     413                 :            : /**
     414                 :            :  * @warning
     415                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     416                 :            :  *
     417                 :            :  * Atomically set a bit in the bitset.
     418                 :            :  *
     419                 :            :  * Set a bit in a bitset as an atomic operation, with the specified
     420                 :            :  * memory ordering.
     421                 :            :  *
     422                 :            :  * rte_bitset_atomic_set() is multi-thread safe, provided all threads
     423                 :            :  * acting in parallel on the same bitset does so through
     424                 :            :  * @c rte_bitset_atomic_*() functions.
     425                 :            :  *
     426                 :            :  * Bits are numbered from 0 to (size - 1) (inclusive).
     427                 :            :  *
     428                 :            :  * @param bitset
     429                 :            :  *   A pointer to the array of words making up the bitset.
     430                 :            :  * @param bit_num
     431                 :            :  *   The index of the bit to be set.
     432                 :            :  * @param memory_order
     433                 :            :  *   The memory order to use.
     434                 :            :  */
     435                 :            : __rte_experimental
     436                 :            : static inline void
     437                 :            : rte_bitset_atomic_set(uint64_t *bitset, size_t bit_num, int memory_order)
     438                 :            : {
     439                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     440                 :    2509895 :         __RTE_BITSET_DELEGATE_N(rte_bit_atomic_set, bitset, bit_num, memory_order);
     441                 :            : #else
     442                 :            :         RTE_SET_USED(bitset);
     443                 :            :         RTE_SET_USED(bit_num);
     444                 :            :         RTE_SET_USED(memory_order);
     445                 :            :         RTE_VERIFY(false);
     446                 :            : #endif
     447                 :            : }
     448                 :            : 
     449                 :            : /**
     450                 :            :  * @warning
     451                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     452                 :            :  *
     453                 :            :  * Atomically clear a bit in the bitset.
     454                 :            :  *
     455                 :            :  * Clear a bit in a bitset as an atomic operation, with the specified
     456                 :            :  * memory ordering.
     457                 :            :  *
     458                 :            :  * rte_bitset_atomic_clear() is multi-thread safe, provided all
     459                 :            :  * threads acting in parallel on the same bitset does so through @c
     460                 :            :  * rte_bitset_atomic_*() functions.
     461                 :            :  *
     462                 :            :  * Bits are numbered from 0 to (size - 1) (inclusive).
     463                 :            :  *
     464                 :            :  * @param bitset
     465                 :            :  *   A pointer to the array of words making up the bitset.
     466                 :            :  * @param bit_num
     467                 :            :  *   The index of the bit to be cleared.
     468                 :            :  * @param memory_order
     469                 :            :  *   The memory order to use.
     470                 :            :  */
     471                 :            : __rte_experimental
     472                 :            : static inline void
     473                 :            : rte_bitset_atomic_clear(uint64_t *bitset, size_t bit_num, int memory_order)
     474                 :            : {
     475                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     476                 :    2512902 :         __RTE_BITSET_DELEGATE_N(rte_bit_atomic_clear, bitset, bit_num, memory_order);
     477                 :            : #else
     478                 :            :         RTE_SET_USED(bitset);
     479                 :            :         RTE_SET_USED(bit_num);
     480                 :            :         RTE_SET_USED(memory_order);
     481                 :            :         RTE_VERIFY(false);
     482                 :            : #endif
     483                 :            : }
     484                 :            : 
     485                 :            : /**
     486                 :            :  * @warning
     487                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     488                 :            :  *
     489                 :            :  * Atomically set or clear a bit in the bitset.
     490                 :            :  *
     491                 :            :  * Assign a value to a bit in a bitset as an atomic operation, with
     492                 :            :  * the specified memory ordering.
     493                 :            :  *
     494                 :            :  * rte_bitset_atomic_assign() is multi-thread safe, provided all
     495                 :            :  * threads acting in parallel on the same bitset does so through
     496                 :            :  * @c rte_bitset_atomic_*() functions.
     497                 :            :  *
     498                 :            :  * Bits are numbered from 0 to (size - 1) (inclusive).
     499                 :            :  *
     500                 :            :  * @param bitset
     501                 :            :  *   A pointer to the array of words making up the bitset.
     502                 :            :  * @param bit_num
     503                 :            :  *   The index of the bit to be set or cleared.
     504                 :            :  * @param bit_value
     505                 :            :  *   Control if the bit should be set or cleared.
     506                 :            :  * @param memory_order
     507                 :            :  *   The memory order to use.
     508                 :            :  */
     509                 :            : __rte_experimental
     510                 :            : static inline void
     511                 :            : rte_bitset_atomic_assign(uint64_t *bitset, size_t bit_num, bool bit_value, int memory_order)
     512                 :            : {
     513                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     514                 :    5035364 :         __RTE_BITSET_DELEGATE_N(rte_bit_atomic_assign, bitset, bit_num, bit_value, memory_order);
     515                 :            : #else
     516                 :            :         RTE_SET_USED(bitset);
     517                 :            :         RTE_SET_USED(bit_num);
     518                 :            :         RTE_SET_USED(bit_value);
     519                 :            :         RTE_SET_USED(memory_order);
     520                 :            :         RTE_VERIFY(false);
     521                 :            : #endif
     522                 :            : }
     523                 :            : 
     524                 :            : /**
     525                 :            :  * @warning
     526                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     527                 :            :  *
     528                 :            :  * Atomically change the value of a bit in the bitset.
     529                 :            :  *
     530                 :            :  * Flip a bit in a bitset as an atomic operation, with the specified
     531                 :            :  * memory ordering.
     532                 :            :  *
     533                 :            :  * rte_bitset_atomic_flip() is multi-thread safe, provided all threads
     534                 :            :  * acting in parallel on the same bitset does so through
     535                 :            :  * @c rte_bitset_atomic_*() functions.
     536                 :            :  *
     537                 :            :  * Bits are numbered from 0 to (size - 1) (inclusive).
     538                 :            :  *
     539                 :            :  * @param bitset
     540                 :            :  *   A pointer to the array of words making up the bitset.
     541                 :            :  * @param bit_num
     542                 :            :  *   The index of the bit to be flipped.
     543                 :            :  * @param memory_order
     544                 :            :  *   The memory order to use.
     545                 :            :  */
     546                 :            : __rte_experimental
     547                 :            : static inline void
     548                 :            : rte_bitset_atomic_flip(uint64_t *bitset, size_t bit_num, int memory_order)
     549                 :            : {
     550                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     551                 :    5035364 :         __RTE_BITSET_DELEGATE_N(rte_bit_atomic_flip, bitset, bit_num, memory_order);
     552                 :            : #else
     553                 :            :         RTE_SET_USED(bitset);
     554                 :            :         RTE_SET_USED(bit_num);
     555                 :            :         RTE_SET_USED(memory_order);
     556                 :            :         RTE_VERIFY(false);
     557                 :            : #endif
     558                 :            : }
     559                 :            : 
     560                 :            : /**
     561                 :            :  * @warning
     562                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     563                 :            :  *
     564                 :            :  * Set all bits in the bitset.
     565                 :            :  *
     566                 :            :  * @param bitset
     567                 :            :  *   A pointer to the array of words making up the bitset.
     568                 :            :  * @param size
     569                 :            :  *   The size of the bitset (in bits).
     570                 :            :  */
     571                 :            : __rte_experimental
     572                 :            : static inline void
     573                 :            : rte_bitset_set_all(uint64_t *bitset, size_t size)
     574                 :            : {
     575                 :            :         memset(bitset, 0xFF, RTE_BITSET_SIZE(size));
     576                 :            : }
     577                 :            : 
     578                 :            : /**
     579                 :            :  * @warning
     580                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     581                 :            :  *
     582                 :            :  * Clear all bits in the bitset.
     583                 :            :  *
     584                 :            :  * @param bitset
     585                 :            :  *   A pointer to the array of words making up the bitset.
     586                 :            :  * @param size
     587                 :            :  *   The size of the bitset (in bits).
     588                 :            :  */
     589                 :            : __rte_experimental
     590                 :            : static inline void
     591                 :            : rte_bitset_clear_all(uint64_t *bitset, size_t size)
     592                 :            : {
     593                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     594                 :            :         rte_bitset_init(bitset, size);
     595                 :            : #else
     596                 :            :         RTE_SET_USED(bitset);
     597                 :            :         RTE_SET_USED(size);
     598                 :            :         RTE_VERIFY(false);
     599                 :            : #endif
     600                 :            : }
     601                 :            : 
     602                 :            : /**
     603                 :            :  * @warning
     604                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     605                 :            :  *
     606                 :            :  * Count all set bits (also known as the @e weight).
     607                 :            :  *
     608                 :            :  * @param bitset
     609                 :            :  *   A pointer to the array of words making up the bitset.
     610                 :            :  * @param size
     611                 :            :  *   The size of the bitset (in bits).
     612                 :            :  * @return
     613                 :            :  *   Returns the number of '1' bits in the bitset.
     614                 :            :  */
     615                 :            : __rte_experimental
     616                 :            : static inline size_t
     617                 :      60069 : rte_bitset_count_set(const uint64_t *bitset, size_t size)
     618                 :            : {
     619                 :            :         size_t i;
     620                 :            :         size_t total = 0;
     621                 :            : 
     622                 :            :         /*
     623                 :            :          * Unused bits in a rte_bitset are always '0', and thus are
     624                 :            :          * not included in this count.
     625                 :            :          */
     626         [ +  + ]:     501808 :         for (i = 0; i < RTE_BITSET_NUM_WORDS(size) - 1; i++)
     627                 :     441739 :                 total += rte_popcount64(bitset[i]);
     628                 :            : 
     629         [ -  + ]:          1 :         total += rte_popcount64(bitset[i] & __RTE_BITSET_USED_MASK(size));
     630                 :            : 
     631                 :      60069 :         return total;
     632                 :            : }
     633                 :            : 
     634                 :            : /**
     635                 :            :  * @warning
     636                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     637                 :            :  *
     638                 :            :  * Count all cleared bits.
     639                 :            :  *
     640                 :            :  * @param bitset
     641                 :            :  *   A pointer to the array of words making up the bitset.
     642                 :            :  * @param size
     643                 :            :  *   The size of the bitset (in bits).
     644                 :            :  * @return
     645                 :            :  *   Returns the number of '0' bits in the bitset.
     646                 :            :  */
     647                 :            : __rte_experimental
     648                 :            : static inline size_t
     649                 :            : rte_bitset_count_clear(const uint64_t *bitset, size_t size)
     650                 :            : {
     651                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     652                 :         18 :         return size - rte_bitset_count_set(bitset, size);
     653                 :            : #else
     654                 :            :         RTE_SET_USED(bitset);
     655                 :            :         RTE_SET_USED(size);
     656                 :            :         RTE_VERIFY(false);
     657                 :            : #endif
     658                 :            : }
     659                 :            : 
     660                 :            : #define __RTE_BITSET_FIND_FLAG_FIND_CLEAR (1U << 0)
     661                 :            : #define __RTE_BITSET_FIND_FLAG_WRAP (1U << 1)
     662                 :            : 
     663                 :            : __rte_experimental
     664                 :            : static inline ssize_t
     665                 :    5832411 : __rte_bitset_find_nowrap(const uint64_t *bitset, size_t __rte_unused size, size_t start_bit,
     666                 :            :                 size_t len, bool find_clear)
     667                 :            : {
     668                 :            :         size_t word_idx;
     669                 :            :         size_t offset;
     670                 :    5832411 :         size_t end_bit = start_bit + len;
     671                 :            : 
     672                 :            :         RTE_ASSERT(end_bit <= size);
     673                 :            : 
     674         [ +  + ]:    5832411 :         if (unlikely(len == 0))
     675                 :            :                 return -1;
     676                 :            : 
     677                 :    5819692 :         word_idx = __RTE_BITSET_WORD_IDX(start_bit);
     678                 :    5819692 :         offset = __RTE_BITSET_BIT_OFFSET(start_bit);
     679                 :            : 
     680         [ +  + ]:    5929427 :         while (word_idx <= __RTE_BITSET_WORD_IDX(end_bit - 1)) {
     681                 :            :                 uint64_t word;
     682                 :            : 
     683                 :    5901682 :                 word = bitset[word_idx];
     684         [ +  + ]:    5901682 :                 if (find_clear)
     685                 :    2918065 :                         word = ~word;
     686                 :            : 
     687                 :    5901682 :                 word >>= offset;
     688                 :            : 
     689         [ +  + ]:    5901682 :                 if (word != 0) {
     690                 :    5791947 :                         size_t ffs = start_bit + rte_bsf64(word);
     691                 :            : 
     692                 :            :                         /*
     693                 :            :                          * Check if set bit were among the last,
     694                 :            :                          * unused bits, in the last word.
     695                 :            :                          */
     696         [ +  + ]:    5791947 :                         if (unlikely(ffs >= end_bit))
     697                 :            :                                 return -1;
     698                 :            : 
     699                 :    5752119 :                         return ffs;
     700                 :            :                 }
     701                 :            : 
     702                 :     109735 :                 start_bit += (RTE_BITSET_WORD_BITS - offset);
     703                 :     109735 :                 word_idx++;
     704                 :            :                 offset = 0;
     705                 :            :         }
     706                 :            : 
     707                 :            :         return -1;
     708                 :            : 
     709                 :            : }
     710                 :            : 
     711                 :            : __rte_experimental
     712                 :            : static inline ssize_t
     713                 :    5816939 : __rte_bitset_find(const uint64_t *bitset, size_t size, size_t start_bit, size_t len,
     714                 :            :                 unsigned int flags)
     715                 :            : {
     716                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     717                 :    5816939 :         bool find_clear = flags & __RTE_BITSET_FIND_FLAG_FIND_CLEAR;
     718                 :    5816939 :         bool may_wrap = flags & __RTE_BITSET_FIND_FLAG_WRAP;
     719                 :    5816939 :         bool does_wrap = (start_bit + len) > size;
     720                 :            :         ssize_t rc;
     721                 :            : 
     722                 :            :         RTE_ASSERT(len <= size);
     723                 :            :         if (!may_wrap)
     724                 :            :                 RTE_ASSERT(!does_wrap);
     725                 :            : 
     726         [ +  + ]:    5816939 :         if (may_wrap && does_wrap) {
     727                 :    1835974 :                 size_t len0 = size - start_bit;
     728                 :    1835974 :                 size_t len1 = len - len0;
     729                 :            : 
     730                 :    1835974 :                 rc = __rte_bitset_find_nowrap(bitset, size, start_bit, len0, find_clear);
     731         [ +  + ]:    1835974 :                 if (rc < 0)
     732                 :      15472 :                         rc =  __rte_bitset_find_nowrap(bitset, size, 0, len1, find_clear);
     733                 :            :         } else
     734                 :    3980965 :                 rc = __rte_bitset_find_nowrap(bitset, size, start_bit, len, find_clear);
     735                 :            : 
     736                 :    5816939 :         return rc;
     737                 :            : #else
     738                 :            :         RTE_SET_USED(bitset);
     739                 :            :         RTE_SET_USED(size);
     740                 :            :         RTE_SET_USED(start_bit);
     741                 :            :         RTE_SET_USED(len);
     742                 :            :         RTE_SET_USED(flags);
     743                 :            :         RTE_VERIFY(false);
     744                 :            : #endif
     745                 :            : }
     746                 :            : 
     747                 :            : /**
     748                 :            :  * @warning
     749                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     750                 :            :  *
     751                 :            :  * Find first bit set.
     752                 :            :  *
     753                 :            :  * Scans the bitset in the forward direction (i.e., starting at the
     754                 :            :  * least significant bit), and returns the index of the first '1'.
     755                 :            :  *
     756                 :            :  * @param bitset
     757                 :            :  *   A pointer to the array of words making up the bitset.
     758                 :            :  * @param size
     759                 :            :  *   The size of the bitset (in bits).
     760                 :            :  * @return
     761                 :            :  *   Returns the index of the least significant '1', or -1 if all
     762                 :            :  *   bits are '0'.
     763                 :            :  */
     764                 :            : __rte_experimental
     765                 :            : static inline ssize_t
     766                 :            : rte_bitset_find_first_set(const uint64_t *bitset, size_t size)
     767                 :            : {
     768                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     769                 :        253 :         return __rte_bitset_find(bitset, size, 0, size, 0);
     770                 :            : #else
     771                 :            :         RTE_SET_USED(bitset);
     772                 :            :         RTE_SET_USED(size);
     773                 :            :         RTE_VERIFY(false);
     774                 :            : #endif
     775                 :            : }
     776                 :            : 
     777                 :            : /**
     778                 :            :  * @warning
     779                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     780                 :            :  *
     781                 :            :  * Find first bit set at offset.
     782                 :            :  *
     783                 :            :  * Scans the bitset in the forward direction (i.e., starting at the
     784                 :            :  * least significant bit), starting at an offset @c start_bit into the
     785                 :            :  * bitset, and returns the index of the first '1' encountered.
     786                 :            :  *
     787                 :            :  * @param bitset
     788                 :            :  *   A pointer to the array of words making up the bitset.
     789                 :            :  * @param size
     790                 :            :  *   The size of the bitset (in bits).
     791                 :            :  * @param start_bit
     792                 :            :  *   The index of the first bit to check. Must be less than @c size.
     793                 :            :  * @param len
     794                 :            :  *   The number of bits to scan. @c start_bit + @c len must be less
     795                 :            :  *   than or equal to @c size.
     796                 :            :  * @return
     797                 :            :  *   Returns the index of the least significant '1', or -1 if all
     798                 :            :  *   bits are '0'.
     799                 :            :  */
     800                 :            : __rte_experimental
     801                 :            : static inline ssize_t
     802                 :            : rte_bitset_find_set(const uint64_t *bitset, size_t size, size_t start_bit, size_t len)
     803                 :            : {
     804                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     805                 :     252160 :         return __rte_bitset_find(bitset, size, start_bit, len, 0);
     806                 :            : #else
     807                 :            :         RTE_SET_USED(bitset);
     808                 :            :         RTE_SET_USED(size);
     809                 :            :         RTE_SET_USED(start_bit);
     810                 :            :         RTE_SET_USED(len);
     811                 :            :         RTE_VERIFY(false);
     812                 :            : #endif
     813                 :            : }
     814                 :            : 
     815                 :            : /**
     816                 :            :  * @warning
     817                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     818                 :            :  *
     819                 :            :  * Find first bit set at offset, with wrap-around.
     820                 :            :  *
     821                 :            :  * Scans the bitset in the forward direction (i.e., starting at the
     822                 :            :  * least significant bit), starting at an offset @c start_bit into the
     823                 :            :  * bitset. If no '1' is encountered before the end of the bitset, the search
     824                 :            :  * will continue at index 0.
     825                 :            :  *
     826                 :            :  * @param bitset
     827                 :            :  *   A pointer to the array of words making up the bitset.
     828                 :            :  * @param size
     829                 :            :  *   The size of the bitset (in bits).
     830                 :            :  * @param start_bit
     831                 :            :  *   The index of the first bit to check. Must be less than @c size.
     832                 :            :  * @param len
     833                 :            :  *   The number of bits to scan. @c start_bit + @c len must be less
     834                 :            :  *   than or equal to @c size.
     835                 :            :  * @return
     836                 :            :  *   Returns the index of the least significant '1', or -1 if all
     837                 :            :  *   bits are '0'.
     838                 :            :  */
     839                 :            : __rte_experimental
     840                 :            : static inline ssize_t
     841                 :            : rte_bitset_find_set_wrap(const uint64_t *bitset, size_t size, size_t start_bit, size_t len)
     842                 :            : {
     843                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     844                 :     747587 :         return __rte_bitset_find(bitset, size, start_bit, len, __RTE_BITSET_FIND_FLAG_WRAP);
     845                 :            : #else
     846                 :            :         RTE_SET_USED(bitset);
     847                 :            :         RTE_SET_USED(size);
     848                 :            :         RTE_SET_USED(start_bit);
     849                 :            :         RTE_SET_USED(len);
     850                 :            :         RTE_VERIFY(false);
     851                 :            : #endif
     852                 :            : }
     853                 :            : 
     854                 :            : /**
     855                 :            :  * @warning
     856                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     857                 :            :  *
     858                 :            :  * Find first cleared bit.
     859                 :            :  *
     860                 :            :  * Scans the bitset in the forward direction (i.e., starting at the
     861                 :            :  * least significant bit), and returns the index of the first '0'.
     862                 :            :  *
     863                 :            :  * @param bitset
     864                 :            :  *   A pointer to the array of words making up the bitset.
     865                 :            :  * @param size
     866                 :            :  *   The size of the bitset (in bits).
     867                 :            :  * @return
     868                 :            :  *   Returns the index of the least significant '0', or -1 if all
     869                 :            :  *   bits are '1'.
     870                 :            :  */
     871                 :            : __rte_experimental
     872                 :            : static inline ssize_t
     873                 :            : rte_bitset_find_first_clear(const uint64_t *bitset, size_t size)
     874                 :            : {
     875                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     876                 :        240 :         return __rte_bitset_find(bitset, size, 0, size, __RTE_BITSET_FIND_FLAG_FIND_CLEAR);
     877                 :            : #else
     878                 :            :         RTE_SET_USED(bitset);
     879                 :            :         RTE_SET_USED(size);
     880                 :            :         RTE_VERIFY(false);
     881                 :            : #endif
     882                 :            : }
     883                 :            : 
     884                 :            : /**
     885                 :            :  * @warning
     886                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     887                 :            :  *
     888                 :            :  * Find first cleared bit at offset.
     889                 :            :  *
     890                 :            :  * Scans the bitset in the forward direction (i.e., starting at the
     891                 :            :  * least significant bit), starting at an offset @c start_bit into the
     892                 :            :  * bitset, and returns the index of the first '0' encountered.
     893                 :            :  *
     894                 :            :  * @param bitset
     895                 :            :  *   A pointer to the array of words making up the bitset.
     896                 :            :  * @param size
     897                 :            :  *   The size of the bitset (in bits).
     898                 :            :  * @param start_bit
     899                 :            :  *   The index of the first bit to check. Must be less than @c size.
     900                 :            :  * @param len
     901                 :            :  *   The number of bits to scan. @c start_bit + @c len must be less
     902                 :            :  *   than or equal to @c size.
     903                 :            :  * @return
     904                 :            :  *   Returns the index of the least significant '0', or -1 if all
     905                 :            :  *   bits are '1'.
     906                 :            :  */
     907                 :            : __rte_experimental
     908                 :            : static inline ssize_t
     909                 :            : rte_bitset_find_clear(const uint64_t *bitset, size_t size, size_t start_bit, size_t len)
     910                 :            : {
     911                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     912                 :     253552 :         return __rte_bitset_find(bitset, size, start_bit, len, __RTE_BITSET_FIND_FLAG_FIND_CLEAR);
     913                 :            : #else
     914                 :            :         RTE_SET_USED(bitset);
     915                 :            :         RTE_SET_USED(size);
     916                 :            :         RTE_SET_USED(start_bit);
     917                 :            :         RTE_SET_USED(len);
     918                 :            :         RTE_VERIFY(false);
     919                 :            : #endif
     920                 :            : }
     921                 :            : 
     922                 :            : /**
     923                 :            :  * @warning
     924                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     925                 :            :  *
     926                 :            :  * Find first cleared bit at offset, with wrap-around.
     927                 :            :  *
     928                 :            :  * Scans the bitset in the forward direction (i.e., starting at the
     929                 :            :  * least significant bit), starting at an offset @c start_bit into the
     930                 :            :  * bitset. If no '0' is encountered before the end of the bitset, the
     931                 :            :  * search will continue at index 0.
     932                 :            :  *
     933                 :            :  * @param bitset
     934                 :            :  *   A pointer to the array of words making up the bitset.
     935                 :            :  * @param size
     936                 :            :  *   The size of the bitset (in bits).
     937                 :            :  * @param start_bit
     938                 :            :  *   The index of the first bit to check. Must be less than @c size.
     939                 :            :  * @param len
     940                 :            :  *   The number of bits to scan. @c start_bit + @c len must be less
     941                 :            :  *   than or equal to @c size.
     942                 :            :  * @return
     943                 :            :  *   Returns the index of the least significant '0', or -1 if all
     944                 :            :  *   bits are '1'.
     945                 :            :  */
     946                 :            : __rte_experimental
     947                 :            : static inline ssize_t
     948                 :            : rte_bitset_find_clear_wrap(const uint64_t *bitset, size_t size, size_t start_bit, size_t len)
     949                 :            : {
     950                 :            : #ifdef ALLOW_EXPERIMENTAL_API
     951                 :     746208 :         return __rte_bitset_find(bitset, size, start_bit, len,
     952                 :            :                 __RTE_BITSET_FIND_FLAG_FIND_CLEAR | __RTE_BITSET_FIND_FLAG_WRAP);
     953                 :            : #else
     954                 :            :         RTE_SET_USED(bitset);
     955                 :            :         RTE_SET_USED(size);
     956                 :            :         RTE_SET_USED(start_bit);
     957                 :            :         RTE_SET_USED(len);
     958                 :            :         RTE_VERIFY(false);
     959                 :            : #endif
     960                 :            : }
     961                 :            : 
     962                 :            : /**
     963                 :            :  * @warning
     964                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     965                 :            :  *
     966                 :            :  * Copy bitset.
     967                 :            :  *
     968                 :            :  * Copy the bits of the @c src_bitset to the @c dst_bitset.
     969                 :            :  *
     970                 :            :  * The bitsets may not overlap and must be of equal size.
     971                 :            :  *
     972                 :            :  * @param dst_bitset
     973                 :            :  *   A pointer to the array of words making up the bitset.
     974                 :            :  * @param src_bitset
     975                 :            :  *   A pointer to the array of words making up the bitset.
     976                 :            :  * @param size
     977                 :            :  *   The size of the bitsets (in bits).
     978                 :            :  */
     979                 :            : __rte_experimental
     980                 :            : static inline void
     981                 :   10047239 : rte_bitset_copy(uint64_t *__rte_restrict dst_bitset, const uint64_t *__rte_restrict src_bitset,
     982                 :            :                 size_t size)
     983                 :            : {
     984         [ -  + ]:   10047239 :         rte_memcpy(dst_bitset, src_bitset, RTE_BITSET_SIZE(size));
     985                 :          1 : }
     986                 :            : 
     987                 :            : /**
     988                 :            :  * @warning
     989                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     990                 :            :  *
     991                 :            :  * Bitwise or two bitsets.
     992                 :            :  *
     993                 :            :  * Perform a bitwise OR operation on all bits in the two equal-size
     994                 :            :  * bitsets @c src_bitset0 and @c src_bitset1, and store the results in
     995                 :            :  * @c dst_bitset.
     996                 :            :  *
     997                 :            :  * @param dst_bitset
     998                 :            :  *   A pointer to the destination bitset.
     999                 :            :  * @param src_bitset0
    1000                 :            :  *   A pointer to the first source bitset.
    1001                 :            :  * @param src_bitset1
    1002                 :            :  *   A pointer to the second source bitset.
    1003                 :            :  * @param size
    1004                 :            :  *   The size of the bitsets (in bits).
    1005                 :            :  */
    1006                 :            : __rte_experimental
    1007                 :            : static inline void
    1008                 :          1 : rte_bitset_or(uint64_t *dst_bitset, const uint64_t *src_bitset0, const uint64_t *src_bitset1,
    1009                 :            :                 size_t size)
    1010                 :            : {
    1011                 :            :         size_t i;
    1012                 :            : 
    1013         [ +  + ]:          2 :         for (i = 0; i < RTE_BITSET_NUM_WORDS(size); i++)
    1014                 :          1 :                 dst_bitset[i] = src_bitset0[i] | src_bitset1[i];
    1015                 :          1 : }
    1016                 :            : 
    1017                 :            : /**
    1018                 :            :  * @warning
    1019                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
    1020                 :            :  *
    1021                 :            :  * Bitwise and two bitsets.
    1022                 :            :  *
    1023                 :            :  * Perform a bitwise AND operation on all bits in the two equal-size
    1024                 :            :  * bitsets @c src_bitset0 and @c src_bitset1, and store the result in
    1025                 :            :  * @c dst_bitset.
    1026                 :            :  *
    1027                 :            :  * @param dst_bitset
    1028                 :            :  *   A pointer to the destination bitset.
    1029                 :            :  * @param src_bitset0
    1030                 :            :  *   A pointer to the first source bitset.
    1031                 :            :  * @param src_bitset1
    1032                 :            :  *   A pointer to the second source bitset.
    1033                 :            :  * @param size
    1034                 :            :  *   The size of the bitsets (in bits).
    1035                 :            :  */
    1036                 :            : __rte_experimental
    1037                 :            : static inline void
    1038                 :          1 : rte_bitset_and(uint64_t *dst_bitset, const uint64_t *src_bitset0, const uint64_t *src_bitset1,
    1039                 :            :                 size_t size)
    1040                 :            : {
    1041                 :            :         size_t i;
    1042                 :            : 
    1043         [ +  + ]:          2 :         for (i = 0; i < RTE_BITSET_NUM_WORDS(size); i++)
    1044                 :          1 :                 dst_bitset[i] = src_bitset0[i] & src_bitset1[i];
    1045                 :          1 : }
    1046                 :            : 
    1047                 :            : /**
    1048                 :            :  * @warning
    1049                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
    1050                 :            :  *
    1051                 :            :  * Bitwise xor two bitsets.
    1052                 :            :  *
    1053                 :            :  * Perform a bitwise XOR operation on all bits in the two equal-size
    1054                 :            :  * bitsets @c src_bitset0 and @c src_bitset1, and store the result in
    1055                 :            :  * @c dst_bitset.
    1056                 :            :  *
    1057                 :            :  * @param dst_bitset
    1058                 :            :  *   A pointer to the destination bitset.
    1059                 :            :  * @param src_bitset0
    1060                 :            :  *   A pointer to the first source bitset.
    1061                 :            :  * @param src_bitset1
    1062                 :            :  *   A pointer to the second source bitset.
    1063                 :            :  * @param size
    1064                 :            :  *   The size of the bitsets (in bits).
    1065                 :            :  */
    1066                 :            : __rte_experimental
    1067                 :            : static inline void
    1068                 :          1 : rte_bitset_xor(uint64_t *dst_bitset, const uint64_t *src_bitset0, const uint64_t *src_bitset1,
    1069                 :            :                 size_t size)
    1070                 :            : {
    1071                 :            :         size_t i;
    1072                 :            : 
    1073         [ +  + ]:          3 :         for (i = 0; i < RTE_BITSET_NUM_WORDS(size); i++)
    1074                 :          2 :                 dst_bitset[i] = src_bitset0[i] ^ src_bitset1[i];
    1075                 :          1 : }
    1076                 :            : 
    1077                 :            : /**
    1078                 :            :  * @warning
    1079                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
    1080                 :            :  *
    1081                 :            :  * Compute the bitwise complement of a bitset.
    1082                 :            :  *
    1083                 :            :  * Flip every bit in the @c src_bitset, and store the result in @c
    1084                 :            :  * dst_bitset.
    1085                 :            :  *
    1086                 :            :  * @param dst_bitset
    1087                 :            :  *   A pointer to the destination bitset.
    1088                 :            :  * @param src_bitset
    1089                 :            :  *   A pointer to the source bitset.
    1090                 :            :  * @param size
    1091                 :            :  *   The size of the bitsets (in bits).
    1092                 :            :  */
    1093                 :            : __rte_experimental
    1094                 :            : static inline void
    1095                 :            : rte_bitset_complement(uint64_t *dst_bitset, const uint64_t *src_bitset, size_t size)
    1096                 :            : {
    1097                 :            :         size_t i;
    1098                 :            : 
    1099         [ +  + ]:      93380 :         for (i = 0; i < RTE_BITSET_NUM_WORDS(size); i++)
    1100                 :      83380 :                 dst_bitset[i] = ~src_bitset[i];
    1101                 :            : }
    1102                 :            : 
    1103                 :            : /**
    1104                 :            :  * @warning
    1105                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
    1106                 :            :  *
    1107                 :            :  * Shift bitset left.
    1108                 :            :  *
    1109                 :            :  * Perform a logical shift left of (multiply) @c src_bitset, and store
    1110                 :            :  * the result in @c dst_bitset.
    1111                 :            :  *
    1112                 :            :  * @param dst_bitset
    1113                 :            :  *   A pointer to the destination bitset.
    1114                 :            :  * @param src_bitset
    1115                 :            :  *   A pointer to the source bitset.
    1116                 :            :  * @param size
    1117                 :            :  *   The size of the bitsets (in bits).
    1118                 :            :  * @param shift_bits
    1119                 :            :  *   The number of bits to shift the bitset.
    1120                 :            :  */
    1121                 :            : __rte_experimental
    1122                 :            : static inline void
    1123                 :      10000 : rte_bitset_shift_left(uint64_t *dst_bitset, const uint64_t *src_bitset, size_t size,
    1124                 :            :                 size_t shift_bits)
    1125                 :            : {
    1126                 :      10000 :         const int src_word_offset = shift_bits / RTE_BITSET_WORD_BITS;
    1127                 :      10000 :         const int src_bit_offset = shift_bits % RTE_BITSET_WORD_BITS;
    1128                 :            :         unsigned int dst_idx;
    1129                 :            : 
    1130         [ +  + ]:      54105 :         for (dst_idx = 0; dst_idx < RTE_BITSET_NUM_WORDS(size); dst_idx++) {
    1131                 :      44105 :                 int src_high_idx = dst_idx - src_word_offset;
    1132                 :            :                 uint64_t low_bits = 0;
    1133                 :            :                 uint64_t high_bits = 0;
    1134                 :            : 
    1135         [ +  + ]:      44105 :                 if (src_high_idx >= 0) {
    1136                 :      20751 :                         int src_low_idx = src_high_idx - 1;
    1137                 :            : 
    1138                 :      20751 :                         high_bits = src_bitset[src_high_idx] << src_bit_offset;
    1139                 :            : 
    1140         [ +  + ]:      20751 :                         if (src_bit_offset > 0 && src_low_idx >= 0)
    1141                 :      12571 :                                 low_bits = src_bitset[src_low_idx] >>
    1142                 :      12571 :                                         (RTE_BITSET_WORD_BITS - src_bit_offset);
    1143                 :            :                 }
    1144                 :      44105 :                 dst_bitset[dst_idx] = low_bits | high_bits;
    1145                 :            :         }
    1146                 :      10000 : }
    1147                 :            : 
    1148                 :            : /**
    1149                 :            :  * @warning
    1150                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
    1151                 :            :  *
    1152                 :            :  * Shift bitset right.
    1153                 :            :  *
    1154                 :            :  * Perform a logical shift right of (divide) @c src_bitset, and store
    1155                 :            :  * the result in @c dst_bitset.
    1156                 :            :  *
    1157                 :            :  * @param dst_bitset
    1158                 :            :  *   A pointer to the destination bitset.
    1159                 :            :  * @param src_bitset
    1160                 :            :  *   A pointer to the source bitset.
    1161                 :            :  * @param size
    1162                 :            :  *   The size of the bitsets (in bits).
    1163                 :            :  * @param shift_bits
    1164                 :            :  *   The number of bits to shift the bitset.
    1165                 :            :  */
    1166                 :            : __rte_experimental
    1167                 :            : static inline void
    1168                 :      10000 : rte_bitset_shift_right(uint64_t *dst_bitset, const uint64_t *src_bitset, size_t size,
    1169                 :            :                 size_t shift_bits)
    1170                 :            : {
    1171                 :      10000 :         const int num_words = RTE_BITSET_NUM_WORDS(size);
    1172                 :      10000 :         const uint64_t used_mask = __RTE_BITSET_USED_MASK(size);
    1173                 :      10000 :         const int src_word_offset = shift_bits / RTE_BITSET_WORD_BITS;
    1174                 :      10000 :         const int src_bit_offset = shift_bits % RTE_BITSET_WORD_BITS;
    1175                 :            :         int dst_idx;
    1176                 :            : 
    1177         [ +  + ]:      54356 :         for (dst_idx = 0; dst_idx < num_words; dst_idx++) {
    1178                 :      44356 :                 int src_low_idx = src_word_offset + dst_idx;
    1179                 :      44356 :                 int src_high_idx = src_low_idx + 1;
    1180                 :            :                 uint64_t src_low_word_bits = 0;
    1181                 :            :                 uint64_t src_high_word_bits = 0;
    1182                 :            : 
    1183         [ +  + ]:      44356 :                 if (src_low_idx < num_words) {
    1184                 :      20663 :                         src_low_word_bits = src_bitset[src_low_idx];
    1185                 :            : 
    1186         [ +  + ]:      20663 :                         if (src_low_idx == (num_words - 1))
    1187                 :       7879 :                                 src_low_word_bits &= used_mask;
    1188                 :            : 
    1189                 :      20663 :                         src_low_word_bits >>= src_bit_offset;
    1190                 :            : 
    1191         [ +  + ]:      20663 :                         if (src_bit_offset > 0 && src_high_idx < num_words) {
    1192                 :      12637 :                                 src_high_word_bits = src_bitset[src_high_idx];
    1193                 :            : 
    1194         [ +  + ]:      12637 :                                 if (src_high_idx == (num_words - 1))
    1195                 :       4952 :                                         src_high_word_bits &= used_mask;
    1196                 :            : 
    1197                 :      12637 :                                 src_high_word_bits <<= (RTE_BITSET_WORD_BITS - src_bit_offset);
    1198                 :            :                         }
    1199                 :            :                 }
    1200                 :      44356 :                 dst_bitset[dst_idx] = src_low_word_bits | src_high_word_bits;
    1201                 :            :         }
    1202                 :      10000 : }
    1203                 :            : 
    1204                 :            : /**
    1205                 :            :  * @warning
    1206                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
    1207                 :            :  *
    1208                 :            :  * Compare two bitsets.
    1209                 :            :  *
    1210                 :            :  * Compare two bitsets for equality.
    1211                 :            :  *
    1212                 :            :  * @param bitset_a
    1213                 :            :  *   A pointer to the destination bitset.
    1214                 :            :  * @param bitset_b
    1215                 :            :  *   A pointer to the source bitset.
    1216                 :            :  * @param size
    1217                 :            :  *   The size of the bitsets (in bits).
    1218                 :            :  */
    1219                 :            : __rte_experimental
    1220                 :            : static inline bool
    1221                 :   10067239 : rte_bitset_equal(const uint64_t *bitset_a, const uint64_t *bitset_b, size_t size)
    1222                 :            : {
    1223                 :            :         size_t i;
    1224                 :            :         uint64_t last_a, last_b;
    1225                 :            : 
    1226   [ +  +  +  +  :  109580173 :         for (i = 0; i < RTE_BITSET_NUM_WORDS(size) - 1; i++)
             +  +  +  + ]
    1227   [ +  -  +  -  :   99512931 :                 if (bitset_a[i] != bitset_b[i])
             +  -  +  - ]
    1228                 :            :                         return false;
    1229                 :            : 
    1230                 :   10067242 :         last_a = bitset_a[i] << __RTE_BITSET_UNUSED(size);
    1231                 :   10067242 :         last_b = bitset_b[i] << __RTE_BITSET_UNUSED(size);
    1232                 :            : 
    1233                 :   10067242 :         return last_a == last_b;
    1234                 :            : }
    1235                 :            : 
    1236                 :            : /**
    1237                 :            :  * @warning
    1238                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
    1239                 :            :  *
    1240                 :            :  * Converts a bitset to a string.
    1241                 :            :  *
    1242                 :            :  * This function prints a string representation of the bitstring to
    1243                 :            :  * the supplied buffer.
    1244                 :            :  *
    1245                 :            :  * Each bit is represented either by '0' or '1' in the output, with
    1246                 :            :  * the first (left-most) character in the output being the most
    1247                 :            :  * significant bit. The resulting string is NUL terminated.
    1248                 :            :  *
    1249                 :            :  * @param bitset
    1250                 :            :  *   A pointer to the array of bitset 64-bit words.
    1251                 :            :  * @param size
    1252                 :            :  *   The number of bits the bitset represent.
    1253                 :            :  * @param buf
    1254                 :            :  *   A buffer to hold the output.
    1255                 :            :  * @param capacity
    1256                 :            :  *   The size of the buffer. Must be @c size + 1 or larger.
    1257                 :            :  * @return
    1258                 :            :  *   Returns the number of bytes written (i.e., @c size + 1), or -EINVAL
    1259                 :            :  *   in case the buffer capacity was too small.
    1260                 :            :  */
    1261                 :            : __rte_experimental
    1262                 :            : ssize_t
    1263                 :            : rte_bitset_to_str(const uint64_t *bitset, size_t size, char *buf, size_t capacity);
    1264                 :            : 
    1265                 :            : #ifdef __cplusplus
    1266                 :            : }
    1267                 :            : #endif
    1268                 :            : 
    1269                 :            : #endif /* _RTE_BITSET_H_ */

Generated by: LCOV version 1.14