[v8,1/3] eal: move bit count functions to bitops header

Message ID 1680567118-6435-2-git-send-email-roretzla@linux.microsoft.com (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series eal: provide abstracted bit counting functions |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Tyler Retzlaff April 4, 2023, 12:11 a.m. UTC
  Move the following inline functions from rte_common.h to rte_bitops.h

  rte_combine32ms1b
  rte_combine64ms1b
  rte_bsf32
  rte_bsf32_safe
  rte_bsf64
  rte_bsf64_safe
  rte_fls_u32
  rte_fls_u64
  rte_is_power_of_2
  rte_align32pow2
  rte_align32prevpow2
  rte_align64pow2
  rte_align64prevpow2
  rte_log2_u32
  rte_log2_u64

Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Ferruh Yigit <ferruh.yigit@amd.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
---
 app/test/test_common.c                   |   1 +
 lib/eal/common/rte_reciprocal.c          |   1 +
 lib/eal/include/rte_bitops.h             | 292 ++++++++++++++++++++++++++++++
 lib/eal/include/rte_common.h             | 293 -------------------------------
 lib/pipeline/rte_swx_pipeline_internal.h |   1 +
 5 files changed, 295 insertions(+), 293 deletions(-)
  

Patch

diff --git a/app/test/test_common.c b/app/test/test_common.c
index f89e1eb..cf4a2c7 100644
--- a/app/test/test_common.c
+++ b/app/test/test_common.c
@@ -7,6 +7,7 @@ 
 #include <string.h>
 #include <math.h>
 #include <rte_common.h>
+#include <rte_bitops.h>
 #include <rte_hexdump.h>
 #include <rte_pause.h>
 
diff --git a/lib/eal/common/rte_reciprocal.c b/lib/eal/common/rte_reciprocal.c
index 42dfa44..d47dc47 100644
--- a/lib/eal/common/rte_reciprocal.c
+++ b/lib/eal/common/rte_reciprocal.c
@@ -8,6 +8,7 @@ 
 #include <stdint.h>
 
 #include <rte_common.h>
+#include <rte_bitops.h>
 
 #include "rte_reciprocal.h"
 
diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h
index f50dbe4..531479e 100644
--- a/lib/eal/include/rte_bitops.h
+++ b/lib/eal/include/rte_bitops.h
@@ -275,6 +275,298 @@ 
 	return val & mask;
 }
 
+/**
+ * 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.
+ *
+ * @param x
+ *    The integer whose MSBs need to be combined with its LSBs
+ * @return
+ *    The combined value.
+ */
+static inline uint32_t
+rte_combine32ms1b(uint32_t x)
+{
+	x |= x >> 1;
+	x |= x >> 2;
+	x |= x >> 4;
+	x |= x >> 8;
+	x |= x >> 16;
+
+	return x;
+}
+
+/**
+ * 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.
+ *
+ * @param v
+ *    The integer whose MSBs need to be combined with its LSBs
+ * @return
+ *    The combined value.
+ */
+static inline uint64_t
+rte_combine64ms1b(uint64_t v)
+{
+	v |= v >> 1;
+	v |= v >> 2;
+	v |= v >> 4;
+	v |= v >> 8;
+	v |= v >> 16;
+	v |= v >> 32;
+
+	return v;
+}
+
+/**
+ * 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.
+ * @param v
+ *     input parameter, should not be zero.
+ * @return
+ *     least significant set bit in the input parameter.
+ */
+static inline uint32_t
+rte_bsf32(uint32_t v)
+{
+	return (uint32_t)__builtin_ctz(v);
+}
+
+/**
+ * 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!
+ *
+ * @param v
+ *     The input parameter.
+ * @param pos
+ *     If ``v`` was not 0, this value will contain position of least significant
+ *     bit within the input parameter.
+ * @return
+ *     Returns 0 if ``v`` was 0, otherwise returns 1.
+ */
+static inline int
+rte_bsf32_safe(uint32_t v, uint32_t *pos)
+{
+	if (v == 0)
+		return 0;
+
+	*pos = rte_bsf32(v);
+	return 1;
+}
+
+/**
+ * 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.
+ * @param v
+ *     input parameter, should not be zero.
+ * @return
+ *     least significant set bit in the input parameter.
+ */
+static inline uint32_t
+rte_bsf64(uint64_t v)
+{
+	return (uint32_t)__builtin_ctzll(v);
+}
+
+/**
+ * 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!
+ *
+ * @param v
+ *     The input parameter.
+ * @param pos
+ *     If ``v`` was not 0, this value will contain position of least significant
+ *     bit within the input parameter.
+ * @return
+ *     Returns 0 if ``v`` was 0, otherwise returns 1.
+ */
+static inline int
+rte_bsf64_safe(uint64_t v, uint32_t *pos)
+{
+	if (v == 0)
+		return 0;
+
+	*pos = rte_bsf64(v);
+	return 1;
+}
+
+/**
+ * Return the last (most-significant) bit set.
+ *
+ * @note The last (most significant) bit is at position 32.
+ * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
+ *
+ * @param x
+ *     The input parameter.
+ * @return
+ *     The last (most-significant) bit set, or 0 if the input is 0.
+ */
+static inline uint32_t
+rte_fls_u32(uint32_t x)
+{
+	return (x == 0) ? 0 : 32 - __builtin_clz(x);
+}
+
+/**
+ * Return the last (most-significant) bit set.
+ *
+ * @note The last (most significant) bit is at position 64.
+ * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
+ *       rte_fls_u64(0x8000000000000000) = 64
+ *
+ * @param x
+ *     The input parameter.
+ * @return
+ *     The last (most-significant) bit set, or 0 if the input is 0.
+ */
+static inline uint32_t
+rte_fls_u64(uint64_t x)
+{
+	return (x == 0) ? 0 : 64 - __builtin_clzll(x);
+}
+
+/*********** Macros to work with powers of 2 ********/
+
+/**
+ * Macro to return 1 if n is a power of 2, 0 otherwise
+ */
+#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
+
+/**
+ * Returns true if n is a power of 2
+ * @param n
+ *     Number to check
+ * @return 1 if true, 0 otherwise
+ */
+static inline int
+rte_is_power_of_2(uint32_t n)
+{
+	return n && !(n & (n - 1));
+}
+
+/**
+ * Aligns input parameter to the next power of 2
+ *
+ * @param x
+ *   The integer value to align
+ *
+ * @return
+ *   Input parameter aligned to the next power of 2
+ */
+static inline uint32_t
+rte_align32pow2(uint32_t x)
+{
+	x--;
+	x = rte_combine32ms1b(x);
+
+	return x + 1;
+}
+
+/**
+ * Aligns input parameter to the previous power of 2
+ *
+ * @param x
+ *   The integer value to align
+ *
+ * @return
+ *   Input parameter aligned to the previous power of 2
+ */
+static inline uint32_t
+rte_align32prevpow2(uint32_t x)
+{
+	x = rte_combine32ms1b(x);
+
+	return x - (x >> 1);
+}
+
+/**
+ * Aligns 64b input parameter to the next power of 2
+ *
+ * @param v
+ *   The 64b value to align
+ *
+ * @return
+ *   Input parameter aligned to the next power of 2
+ */
+static inline uint64_t
+rte_align64pow2(uint64_t v)
+{
+	v--;
+	v = rte_combine64ms1b(v);
+
+	return v + 1;
+}
+
+/**
+ * Aligns 64b input parameter to the previous power of 2
+ *
+ * @param v
+ *   The 64b value to align
+ *
+ * @return
+ *   Input parameter aligned to the previous power of 2
+ */
+static inline uint64_t
+rte_align64prevpow2(uint64_t v)
+{
+	v = rte_combine64ms1b(v);
+
+	return v - (v >> 1);
+}
+
+/**
+ * Return the rounded-up log2 of a integer.
+ *
+ * @note Contrary to the logarithm mathematical operation,
+ * rte_log2_u32(0) == 0 and not -inf.
+ *
+ * @param v
+ *     The input parameter.
+ * @return
+ *     The rounded-up log2 of the input, or 0 if the input is 0.
+ */
+static inline uint32_t
+rte_log2_u32(uint32_t v)
+{
+	if (v == 0)
+		return 0;
+	v = rte_align32pow2(v);
+	return rte_bsf32(v);
+}
+
+/**
+ * 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.
+ *
+ * @param v
+ *     The input parameter.
+ * @return
+ *     The rounded-up log2 of the input, or 0 if the input is 0.
+ */
+static inline uint32_t
+rte_log2_u64(uint64_t v)
+{
+	if (v == 0)
+		return 0;
+	v = rte_align64pow2(v);
+	/* we checked for v being 0 already, so no undefined behavior */
+	return rte_bsf64(v);
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index 15765b4..c5ad69c 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -471,140 +471,6 @@  static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 /** Marker for 8B alignment in a structure. */
 __extension__ typedef uint64_t RTE_MARKER64[0];
 
-/**
- * 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.
- *
- * @param x
- *    The integer whose MSBs need to be combined with its LSBs
- * @return
- *    The combined value.
- */
-static inline uint32_t
-rte_combine32ms1b(uint32_t x)
-{
-	x |= x >> 1;
-	x |= x >> 2;
-	x |= x >> 4;
-	x |= x >> 8;
-	x |= x >> 16;
-
-	return x;
-}
-
-/**
- * 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.
- *
- * @param v
- *    The integer whose MSBs need to be combined with its LSBs
- * @return
- *    The combined value.
- */
-static inline uint64_t
-rte_combine64ms1b(uint64_t v)
-{
-	v |= v >> 1;
-	v |= v >> 2;
-	v |= v >> 4;
-	v |= v >> 8;
-	v |= v >> 16;
-	v |= v >> 32;
-
-	return v;
-}
-
-/*********** Macros to work with powers of 2 ********/
-
-/**
- * Macro to return 1 if n is a power of 2, 0 otherwise
- */
-#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
-
-/**
- * Returns true if n is a power of 2
- * @param n
- *     Number to check
- * @return 1 if true, 0 otherwise
- */
-static inline int
-rte_is_power_of_2(uint32_t n)
-{
-	return n && !(n & (n - 1));
-}
-
-/**
- * Aligns input parameter to the next power of 2
- *
- * @param x
- *   The integer value to align
- *
- * @return
- *   Input parameter aligned to the next power of 2
- */
-static inline uint32_t
-rte_align32pow2(uint32_t x)
-{
-	x--;
-	x = rte_combine32ms1b(x);
-
-	return x + 1;
-}
-
-/**
- * Aligns input parameter to the previous power of 2
- *
- * @param x
- *   The integer value to align
- *
- * @return
- *   Input parameter aligned to the previous power of 2
- */
-static inline uint32_t
-rte_align32prevpow2(uint32_t x)
-{
-	x = rte_combine32ms1b(x);
-
-	return x - (x >> 1);
-}
-
-/**
- * Aligns 64b input parameter to the next power of 2
- *
- * @param v
- *   The 64b value to align
- *
- * @return
- *   Input parameter aligned to the next power of 2
- */
-static inline uint64_t
-rte_align64pow2(uint64_t v)
-{
-	v--;
-	v = rte_combine64ms1b(v);
-
-	return v + 1;
-}
-
-/**
- * Aligns 64b input parameter to the previous power of 2
- *
- * @param v
- *   The 64b value to align
- *
- * @return
- *   Input parameter aligned to the previous power of 2
- */
-static inline uint64_t
-rte_align64prevpow2(uint64_t v)
-{
-	v = rte_combine64ms1b(v);
-
-	return v - (v >> 1);
-}
-
 /*********** Macros for calculating min and max **********/
 
 /**
@@ -629,165 +495,6 @@  static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 
 /*********** Other general functions / macros ********/
 
-/**
- * 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.
- * @param v
- *     input parameter, should not be zero.
- * @return
- *     least significant set bit in the input parameter.
- */
-static inline uint32_t
-rte_bsf32(uint32_t v)
-{
-	return (uint32_t)__builtin_ctz(v);
-}
-
-/**
- * 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!
- *
- * @param v
- *     The input parameter.
- * @param pos
- *     If ``v`` was not 0, this value will contain position of least significant
- *     bit within the input parameter.
- * @return
- *     Returns 0 if ``v`` was 0, otherwise returns 1.
- */
-static inline int
-rte_bsf32_safe(uint32_t v, uint32_t *pos)
-{
-	if (v == 0)
-		return 0;
-
-	*pos = rte_bsf32(v);
-	return 1;
-}
-
-/**
- * Return the rounded-up log2 of a integer.
- *
- * @note Contrary to the logarithm mathematical operation,
- * rte_log2_u32(0) == 0 and not -inf.
- *
- * @param v
- *     The input parameter.
- * @return
- *     The rounded-up log2 of the input, or 0 if the input is 0.
- */
-static inline uint32_t
-rte_log2_u32(uint32_t v)
-{
-	if (v == 0)
-		return 0;
-	v = rte_align32pow2(v);
-	return rte_bsf32(v);
-}
-
-
-/**
- * Return the last (most-significant) bit set.
- *
- * @note The last (most significant) bit is at position 32.
- * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
- *
- * @param x
- *     The input parameter.
- * @return
- *     The last (most-significant) bit set, or 0 if the input is 0.
- */
-static inline uint32_t
-rte_fls_u32(uint32_t x)
-{
-	return (x == 0) ? 0 : 32 - __builtin_clz(x);
-}
-
-/**
- * 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.
- * @param v
- *     input parameter, should not be zero.
- * @return
- *     least significant set bit in the input parameter.
- */
-static inline uint32_t
-rte_bsf64(uint64_t v)
-{
-	return (uint32_t)__builtin_ctzll(v);
-}
-
-/**
- * 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!
- *
- * @param v
- *     The input parameter.
- * @param pos
- *     If ``v`` was not 0, this value will contain position of least significant
- *     bit within the input parameter.
- * @return
- *     Returns 0 if ``v`` was 0, otherwise returns 1.
- */
-static inline int
-rte_bsf64_safe(uint64_t v, uint32_t *pos)
-{
-	if (v == 0)
-		return 0;
-
-	*pos = rte_bsf64(v);
-	return 1;
-}
-
-/**
- * Return the last (most-significant) bit set.
- *
- * @note The last (most significant) bit is at position 64.
- * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
- *       rte_fls_u64(0x8000000000000000) = 64
- *
- * @param x
- *     The input parameter.
- * @return
- *     The last (most-significant) bit set, or 0 if the input is 0.
- */
-static inline uint32_t
-rte_fls_u64(uint64_t x)
-{
-	return (x == 0) ? 0 : 64 - __builtin_clzll(x);
-}
-
-/**
- * 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.
- *
- * @param v
- *     The input parameter.
- * @return
- *     The rounded-up log2 of the input, or 0 if the input is 0.
- */
-static inline uint32_t
-rte_log2_u64(uint64_t v)
-{
-	if (v == 0)
-		return 0;
-	v = rte_align64pow2(v);
-	/* we checked for v being 0 already, so no undefined behavior */
-	return rte_bsf64(v);
-}
-
 #ifndef offsetof
 /** Return the offset of a field in a structure. */
 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
diff --git a/lib/pipeline/rte_swx_pipeline_internal.h b/lib/pipeline/rte_swx_pipeline_internal.h
index 2f24e1a..a67b6e9 100644
--- a/lib/pipeline/rte_swx_pipeline_internal.h
+++ b/lib/pipeline/rte_swx_pipeline_internal.h
@@ -8,6 +8,7 @@ 
 #include <string.h>
 #include <sys/queue.h>
 
+#include <rte_bitops.h>
 #include <rte_byteorder.h>
 #include <rte_common.h>
 #include <rte_cycles.h>