[PATCH v3 2/3] eal: add rte ffs32 and rte ffs64 inline functions
Andre Muezerie
andremue at linux.microsoft.com
Fri Jan 24 17:14:03 CET 2025
From: Tyler Retzlaff <roretzla at linux.microsoft.com>
Provide toolchain abstraction for __builtin_ffs{,l,ll} gcc built-in
intrinsics.
Signed-off-by: Tyler Retzlaff <roretzla at linux.microsoft.com>
---
lib/eal/include/rte_bitops.h | 74 ++++++++++++++++++++++++++++++++++++
1 file changed, 74 insertions(+)
diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h
index deb1fd43f2..0862fd4008 100644
--- a/lib/eal/include/rte_bitops.h
+++ b/lib/eal/include/rte_bitops.h
@@ -958,6 +958,48 @@ rte_popcount64(uint64_t v)
return (unsigned int)__popcnt64(v);
}
+/**
+ * Search v from least significant bit (LSB) to the most
+ * significant bit (MSB) for a set bit (1).
+ *
+ * @param v
+ * The value.
+ * @return
+ * Bit index + 1 if a set bit is found, zero otherwise.
+ */
+__rte_experimental
+static inline unsigned int
+rte_ffs32(uint32_t v)
+{
+ unsigned long rv;
+
+ if (_BitScanForward(&rv, v) == 0)
+ return 0;
+
+ return (unsigned int)rv + 1;
+}
+
+/**
+ * Search v from least significant bit (LSB) to the most
+ * significant bit (MSB) for a set bit (1).
+ *
+ * @param v
+ * The value.
+ * @return
+ * Bit index + 1 if a set bit is found, zero otherwise.
+ */
+__rte_experimental
+static inline unsigned int
+rte_ffs64(uint64_t v)
+{
+ unsigned long rv;
+
+ if (_BitScanForward64(&rv, v) == 0)
+ return 0;
+
+ return (unsigned int)rv + 1;
+}
+
#else
/**
@@ -1044,6 +1086,38 @@ rte_popcount64(uint64_t v)
return (unsigned int)__builtin_popcountll(v);
}
+/**
+ * Search v from least significant bit (LSB) to the most
+ * significant bit (MSB) for a set bit (1).
+ *
+ * @param v
+ * The value.
+ * @return
+ * Bit index + 1 if a set bit is found, zero otherwise.
+ */
+__rte_experimental
+static inline unsigned int
+rte_ffs32(uint32_t v)
+{
+ return (unsigned int)__builtin_ffs(v);
+}
+
+/**
+ * Search v from least significant bit (LSB) to the most
+ * significant bit (MSB) for a set bit (1).
+ *
+ * @param v
+ * The value.
+ * @return
+ * Bit index + 1 if a set bit is found, zero otherwise.
+ */
+__rte_experimental
+static inline unsigned int
+rte_ffs64(uint64_t v)
+{
+ return (unsigned int)__builtin_ffsll(v);
+}
+
#endif
/**
--
2.47.2.vfs.0.1
More information about the dev
mailing list