DPDK  24.07.0
Macros | Functions
rte_bitops.h File Reference
#include <stdint.h>
#include <rte_debug.h>

Go to the source code of this file.

Macros

#define RTE_BIT64(nr)   (UINT64_C(1) << (nr))
 
#define RTE_BIT32(nr)   (UINT32_C(1) << (nr))
 
#define RTE_SHIFT_VAL32(val, nr)   (UINT32_C(val) << (nr))
 
#define RTE_SHIFT_VAL64(val, nr)   (UINT64_C(val) << (nr))
 
#define RTE_GENMASK32(high, low)   (((~UINT32_C(0)) << (low)) & (~UINT32_C(0) >> (31u - (high))))
 
#define RTE_GENMASK64(high, low)   (((~UINT64_C(0)) << (low)) & (~UINT64_C(0) >> (63u - (high))))
 
#define RTE_FIELD_GET32(mask, reg)   ((typeof(mask))(((reg) & (mask)) >> rte_ctz32(mask)))
 
#define RTE_FIELD_GET64(mask, reg)   ((typeof(mask))(((reg) & (mask)) >> rte_ctz64(mask)))
 
#define RTE_IS_POWER_OF_2(n)   ((n) && !(((n) - 1) & (n)))
 

Functions

static uint32_t rte_bit_relaxed_get32 (unsigned int nr, volatile uint32_t *addr)
 
static void rte_bit_relaxed_set32 (unsigned int nr, volatile uint32_t *addr)
 
static void rte_bit_relaxed_clear32 (unsigned int nr, volatile uint32_t *addr)
 
static uint32_t rte_bit_relaxed_test_and_set32 (unsigned int nr, volatile uint32_t *addr)
 
static uint32_t rte_bit_relaxed_test_and_clear32 (unsigned int nr, volatile uint32_t *addr)
 
static uint64_t rte_bit_relaxed_get64 (unsigned int nr, volatile uint64_t *addr)
 
static void rte_bit_relaxed_set64 (unsigned int nr, volatile uint64_t *addr)
 
static void rte_bit_relaxed_clear64 (unsigned int nr, volatile uint64_t *addr)
 
static uint64_t rte_bit_relaxed_test_and_set64 (unsigned int nr, volatile uint64_t *addr)
 
static uint64_t rte_bit_relaxed_test_and_clear64 (unsigned int nr, volatile uint64_t *addr)
 
static unsigned int rte_clz32 (uint32_t v)
 
static unsigned int rte_clz64 (uint64_t v)
 
static unsigned int rte_ctz32 (uint32_t v)
 
static unsigned int rte_ctz64 (uint64_t v)
 
static unsigned int rte_popcount32 (uint32_t v)
 
static unsigned int rte_popcount64 (uint64_t v)
 
static uint32_t rte_combine32ms1b (uint32_t x)
 
static uint64_t rte_combine64ms1b (uint64_t v)
 
static uint32_t rte_bsf32 (uint32_t v)
 
static int rte_bsf32_safe (uint32_t v, uint32_t *pos)
 
static uint32_t rte_bsf64 (uint64_t v)
 
static int rte_bsf64_safe (uint64_t v, uint32_t *pos)
 
static uint32_t rte_fls_u32 (uint32_t x)
 
static uint32_t rte_fls_u64 (uint64_t x)
 
static int rte_is_power_of_2 (uint32_t n)
 
static uint32_t rte_align32pow2 (uint32_t x)
 
static uint32_t rte_align32prevpow2 (uint32_t x)
 
static uint64_t rte_align64pow2 (uint64_t v)
 
static uint64_t rte_align64prevpow2 (uint64_t v)
 
static uint32_t rte_log2_u32 (uint32_t v)
 
static uint32_t rte_log2_u64 (uint64_t v)
 

Detailed Description

Bit Operations

This file defines a family of APIs for bit operations without enforcing memory ordering.

Definition in file rte_bitops.h.

Macro Definition Documentation

◆ RTE_BIT64

#define RTE_BIT64 (   nr)    (UINT64_C(1) << (nr))

Get the uint64_t value for a specified bit set.

Parameters
nrThe bit number in range of 0 to 63.
Examples:
examples/ipsec-secgw/ipsec_worker.c.

Definition at line 32 of file rte_bitops.h.

◆ RTE_BIT32

#define RTE_BIT32 (   nr)    (UINT32_C(1) << (nr))

Get the uint32_t value for a specified bit set.

Parameters
nrThe bit number in range of 0 to 31.

Definition at line 40 of file rte_bitops.h.

◆ RTE_SHIFT_VAL32

#define RTE_SHIFT_VAL32 (   val,
  nr 
)    (UINT32_C(val) << (nr))

Get the uint32_t shifted value.

Parameters
valThe value to be shifted.
nrThe shift number in range of 0 to (32 - width of val).

Definition at line 50 of file rte_bitops.h.

◆ RTE_SHIFT_VAL64

#define RTE_SHIFT_VAL64 (   val,
  nr 
)    (UINT64_C(val) << (nr))

Get the uint64_t shifted value.

Parameters
valThe value to be shifted.
nrThe shift number in range of 0 to (64 - width of val).

Definition at line 60 of file rte_bitops.h.

◆ RTE_GENMASK32

#define RTE_GENMASK32 (   high,
  low 
)    (((~UINT32_C(0)) << (low)) & (~UINT32_C(0) >> (31u - (high))))

Generate a contiguous 32-bit mask starting at bit position low and ending at position high.

Parameters
highHigh bit position.
lowLow bit position.

Definition at line 71 of file rte_bitops.h.

◆ RTE_GENMASK64

#define RTE_GENMASK64 (   high,
  low 
)    (((~UINT64_C(0)) << (low)) & (~UINT64_C(0) >> (63u - (high))))

Generate a contiguous 64-bit mask starting at bit position low and ending at position high.

Parameters
highHigh bit position.
lowLow bit position.

Definition at line 83 of file rte_bitops.h.

◆ RTE_FIELD_GET32

#define RTE_FIELD_GET32 (   mask,
  reg 
)    ((typeof(mask))(((reg) & (mask)) >> rte_ctz32(mask)))

Extract a 32-bit field element.

Parameters
maskShifted mask.
regValue of entire bitfield.

Definition at line 94 of file rte_bitops.h.

◆ RTE_FIELD_GET64

#define RTE_FIELD_GET64 (   mask,
  reg 
)    ((typeof(mask))(((reg) & (mask)) >> rte_ctz64(mask)))

Extract a 64-bit field element.

Parameters
maskShifted mask.
regValue of entire bitfield.

Definition at line 105 of file rte_bitops.h.

◆ RTE_IS_POWER_OF_2

#define RTE_IS_POWER_OF_2 (   n)    ((n) && !(((n) - 1) & (n)))

Macro to return 1 if n is a power of 2, 0 otherwise

Definition at line 663 of file rte_bitops.h.

Function Documentation

◆ rte_bit_relaxed_get32()

static uint32_t rte_bit_relaxed_get32 ( unsigned int  nr,
volatile uint32_t *  addr 
)
inlinestatic

Get the target bit from a 32-bit value without memory ordering.

Parameters
nrThe target bit to get.
addrThe address holding the bit.
Returns
The target bit.

Definition at line 121 of file rte_bitops.h.

◆ rte_bit_relaxed_set32()

static void rte_bit_relaxed_set32 ( unsigned int  nr,
volatile uint32_t *  addr 
)
inlinestatic

Set the target bit in a 32-bit value to 1 without memory ordering.

Parameters
nrThe target bit to set.
addrThe address holding the bit.

Definition at line 138 of file rte_bitops.h.

◆ rte_bit_relaxed_clear32()

static void rte_bit_relaxed_clear32 ( unsigned int  nr,
volatile uint32_t *  addr 
)
inlinestatic

Clear the target bit in a 32-bit value to 0 without memory ordering.

Parameters
nrThe target bit to clear.
addrThe address holding the bit.

Definition at line 155 of file rte_bitops.h.

◆ rte_bit_relaxed_test_and_set32()

static uint32_t rte_bit_relaxed_test_and_set32 ( unsigned int  nr,
volatile uint32_t *  addr 
)
inlinestatic

Return the original bit from a 32-bit value, then set it to 1 without memory ordering.

Parameters
nrThe target bit to get and set.
addrThe address holding the bit.
Returns
The original bit.

Definition at line 175 of file rte_bitops.h.

◆ rte_bit_relaxed_test_and_clear32()

static uint32_t rte_bit_relaxed_test_and_clear32 ( unsigned int  nr,
volatile uint32_t *  addr 
)
inlinestatic

Return the original bit from a 32-bit value, then clear it to 0 without memory ordering.

Parameters
nrThe target bit to get and clear.
addrThe address holding the bit.
Returns
The original bit.

Definition at line 197 of file rte_bitops.h.

◆ rte_bit_relaxed_get64()

static uint64_t rte_bit_relaxed_get64 ( unsigned int  nr,
volatile uint64_t *  addr 
)
inlinestatic

Get the target bit from a 64-bit value without memory ordering.

Parameters
nrThe target bit to get.
addrThe address holding the bit.
Returns
The target bit.

Definition at line 220 of file rte_bitops.h.

◆ rte_bit_relaxed_set64()

static void rte_bit_relaxed_set64 ( unsigned int  nr,
volatile uint64_t *  addr 
)
inlinestatic

Set the target bit in a 64-bit value to 1 without memory ordering.

Parameters
nrThe target bit to set.
addrThe address holding the bit.

Definition at line 237 of file rte_bitops.h.

◆ rte_bit_relaxed_clear64()

static void rte_bit_relaxed_clear64 ( unsigned int  nr,
volatile uint64_t *  addr 
)
inlinestatic

Clear the target bit in a 64-bit value to 0 without memory ordering.

Parameters
nrThe target bit to clear.
addrThe address holding the bit.

Definition at line 254 of file rte_bitops.h.

◆ rte_bit_relaxed_test_and_set64()

static uint64_t rte_bit_relaxed_test_and_set64 ( unsigned int  nr,
volatile uint64_t *  addr 
)
inlinestatic

Return the original bit from a 64-bit value, then set it to 1 without memory ordering.

Parameters
nrThe target bit to get and set.
addrThe address holding the bit.
Returns
The original bit.

Definition at line 274 of file rte_bitops.h.

◆ rte_bit_relaxed_test_and_clear64()

static uint64_t rte_bit_relaxed_test_and_clear64 ( unsigned int  nr,
volatile uint64_t *  addr 
)
inlinestatic

Return the original bit from a 64-bit value, then clear it to 0 without memory ordering.

Parameters
nrThe target bit to get and clear.
addrThe address holding the bit.
Returns
The original bit.

Definition at line 296 of file rte_bitops.h.

◆ rte_clz32()

static unsigned int rte_clz32 ( uint32_t  v)
inlinestatic

Get the count of leading 0-bits in v.

Parameters
vThe value.
Returns
The count of leading zero bits.

Definition at line 419 of file rte_bitops.h.

◆ rte_clz64()

static unsigned int rte_clz64 ( uint64_t  v)
inlinestatic

Get the count of leading 0-bits in v.

Parameters
vThe value.
Returns
The count of leading zero bits.

Definition at line 433 of file rte_bitops.h.

◆ rte_ctz32()

static unsigned int rte_ctz32 ( uint32_t  v)
inlinestatic

Get the count of trailing 0-bits in v.

Parameters
vThe value.
Returns
The count of trailing zero bits.

Definition at line 447 of file rte_bitops.h.

◆ rte_ctz64()

static unsigned int rte_ctz64 ( uint64_t  v)
inlinestatic

Get the count of trailing 0-bits in v.

Parameters
vThe value.
Returns
The count of trailing zero bits.

Definition at line 461 of file rte_bitops.h.

◆ rte_popcount32()

static unsigned int rte_popcount32 ( uint32_t  v)
inlinestatic

Get the count of 1-bits in v.

Parameters
vThe value.
Returns
The count of 1-bits.
Examples:
examples/bbdev_app/main.c, and examples/fips_validation/main.c.

Definition at line 475 of file rte_bitops.h.

◆ rte_popcount64()

static unsigned int rte_popcount64 ( uint64_t  v)
inlinestatic

Get the count of 1-bits in v.

Parameters
vThe value.
Returns
The count of 1-bits.
Examples:
examples/eventdev_pipeline/main.c.

Definition at line 489 of file rte_bitops.h.

◆ rte_combine32ms1b()

static uint32_t rte_combine32ms1b ( uint32_t  x)
inlinestatic

Combines 32b inputs most significant set bits into the least significant bits to construct a value with the same MSBs as x but all 1's under it.

Parameters
xThe integer whose MSBs need to be combined with its LSBs
Returns
The combined value.

Definition at line 507 of file rte_bitops.h.

◆ rte_combine64ms1b()

static uint64_t rte_combine64ms1b ( uint64_t  v)
inlinestatic

Combines 64b inputs most significant set bits into the least significant bits to construct a value with the same MSBs as x but all 1's under it.

Parameters
vThe integer whose MSBs need to be combined with its LSBs
Returns
The combined value.

Definition at line 529 of file rte_bitops.h.

◆ rte_bsf32()

static uint32_t rte_bsf32 ( uint32_t  v)
inlinestatic

Searches the input parameter for the least significant set bit (starting from zero). If a least significant 1 bit is found, its bit index is returned. If the content of the input parameter is zero, then the content of the return value is undefined.

Parameters
vinput parameter, should not be zero.
Returns
least significant set bit in the input parameter.

Definition at line 553 of file rte_bitops.h.

◆ rte_bsf32_safe()

static int rte_bsf32_safe ( uint32_t  v,
uint32_t *  pos 
)
inlinestatic

Searches the input parameter for the least significant set bit (starting from zero). Safe version (checks for input parameter being zero).

Warning
pos must be a valid pointer. It is not checked!
Parameters
vThe input parameter.
posIf v was not 0, this value will contain position of least significant bit within the input parameter.
Returns
Returns 0 if v was 0, otherwise returns 1.

Definition at line 573 of file rte_bitops.h.

◆ rte_bsf64()

static uint32_t rte_bsf64 ( uint64_t  v)
inlinestatic

Searches the input parameter for the least significant set bit (starting from zero). If a least significant 1 bit is found, its bit index is returned. If the content of the input parameter is zero, then the content of the return value is undefined.

Parameters
vinput parameter, should not be zero.
Returns
least significant set bit in the input parameter.

Definition at line 594 of file rte_bitops.h.

◆ rte_bsf64_safe()

static int rte_bsf64_safe ( uint64_t  v,
uint32_t *  pos 
)
inlinestatic

Searches the input parameter for the least significant set bit (starting from zero). Safe version (checks for input parameter being zero).

Warning
pos must be a valid pointer. It is not checked!
Parameters
vThe input parameter.
posIf v was not 0, this value will contain position of least significant bit within the input parameter.
Returns
Returns 0 if v was 0, otherwise returns 1.

Definition at line 614 of file rte_bitops.h.

◆ rte_fls_u32()

static uint32_t rte_fls_u32 ( uint32_t  x)
inlinestatic

Return the last (most-significant) bit set.

Note
The last (most significant) bit is at position 32.
rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
Parameters
xThe input parameter.
Returns
The last (most-significant) bit set, or 0 if the input is 0.

Definition at line 635 of file rte_bitops.h.

◆ rte_fls_u64()

static uint32_t rte_fls_u64 ( uint64_t  x)
inlinestatic

Return the last (most-significant) bit set.

Note
The last (most significant) bit is at position 64.
rte_fls_u64(0) = 0, rte_fls_u64(1) = 1, rte_fls_u64(0x8000000000000000) = 64
Parameters
xThe input parameter.
Returns
The last (most-significant) bit set, or 0 if the input is 0.

Definition at line 653 of file rte_bitops.h.

◆ rte_is_power_of_2()

static int rte_is_power_of_2 ( uint32_t  n)
inlinestatic

Returns true if n is a power of 2

Parameters
nNumber to check
Returns
1 if true, 0 otherwise
Examples:
examples/ipsec-secgw/event_helper.c.

Definition at line 672 of file rte_bitops.h.

◆ rte_align32pow2()

static uint32_t rte_align32pow2 ( uint32_t  x)
inlinestatic

Aligns input parameter to the next power of 2

Parameters
xThe integer value to align
Returns
Input parameter aligned to the next power of 2
Examples:
examples/bbdev_app/main.c, and examples/ipsec-secgw/sad.c.

Definition at line 687 of file rte_bitops.h.

◆ rte_align32prevpow2()

static uint32_t rte_align32prevpow2 ( uint32_t  x)
inlinestatic

Aligns input parameter to the previous power of 2

Parameters
xThe integer value to align
Returns
Input parameter aligned to the previous power of 2

Definition at line 705 of file rte_bitops.h.

◆ rte_align64pow2()

static uint64_t rte_align64pow2 ( uint64_t  v)
inlinestatic

Aligns 64b input parameter to the next power of 2

Parameters
vThe 64b value to align
Returns
Input parameter aligned to the next power of 2

Definition at line 722 of file rte_bitops.h.

◆ rte_align64prevpow2()

static uint64_t rte_align64prevpow2 ( uint64_t  v)
inlinestatic

Aligns 64b input parameter to the previous power of 2

Parameters
vThe 64b value to align
Returns
Input parameter aligned to the previous power of 2

Definition at line 740 of file rte_bitops.h.

◆ rte_log2_u32()

static uint32_t rte_log2_u32 ( uint32_t  v)
inlinestatic

Return the rounded-up log2 of a integer.

Note
Contrary to the logarithm mathematical operation, rte_log2_u32(0) == 0 and not -inf.
Parameters
vThe input parameter.
Returns
The rounded-up log2 of the input, or 0 if the input is 0.

Definition at line 759 of file rte_bitops.h.

◆ rte_log2_u64()

static uint32_t rte_log2_u64 ( uint64_t  v)
inlinestatic

Return the rounded-up log2 of a 64-bit integer.

Note
Contrary to the logarithm mathematical operation, rte_log2_u64(0) == 0 and not -inf.
Parameters
vThe input parameter.
Returns
The rounded-up log2 of the input, or 0 if the input is 0.

Definition at line 779 of file rte_bitops.h.