Skip to contentSkip to conversation

provide rte_ffs32, rte_ffs64 and __rte_x86_movdiri for MSVC

PATCH In review Tyler Retzlaff 3 participants 16 messages Mar 20, 2024 – Jan 29, 2025

lore
Share & tools
lore
#0 lore raw

cover provide rte_ffs32, rte_ffs64 and __rte_x86_movdiri for MSVC

MSVC does not support inline assembly so use movdiri intrinsic and provide abstracted rte_ffs{32,64} inline functions instead of directly using GCC built-ins.

Tyler Retzlaff (2):
eal: provide movdiri for MSVC
eal: add rte ffs32 and rte ffs64 inline functions

 lib/eal/include/rte_bitops.h | 34 ++++++++++++++++++++++++++++++++++
 lib/eal/x86/include/rte_io.h |  4 ++++
 2 files changed, 38 insertions(+)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/2 eal: provide movdiri for MSVC

MSVC does not support inline assembly instead it provides compiler intrinsics. Provide conditional compile for MSVC for movdiri using the _directstoreu_u32 intrinsic.

---
 lib/eal/x86/include/rte_io.h | 4 ++++
 1 file changed, 4 insertions(+)
lib/eal/x86/include/rte_io.h +4 -0
index 0e1fefd..2c72601 100644
@@ -22,11 +22,15 @@
 static __rte_always_inline void
 __rte_x86_movdiri(uint32_t value, volatile void *addr)
 {
+#ifdef RTE_TOOLCHAIN_MSVC
+	_directstoreu_u32((void *)(uintptr_t)addr, value);
+#else
 	asm volatile(
 		/* MOVDIRI */
 		".byte 0x40, 0x0f, 0x38, 0xf9, 0x02"
 		:
 		: "a" (value), "d" (addr));
+#endif
 }
 
 __rte_experimental
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/2 eal: add rte ffs32 and rte ffs64 inline functions

In reply to Tyler Retzlaff

provide toolchain abstraction for __builtin_ffs{,l,ll} gcc built-in intrinsics.

---
 lib/eal/include/rte_bitops.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
lib/eal/include/rte_bitops.h +34 -0
index 449565e..e157a45 100644
@@ -405,6 +405,28 @@
 	return (unsigned int)__popcnt64(v);
 }
 
+static inline unsigned int
+rte_ffs32(uint32_t v)
+{
+	unsigned long rv;
+
+	if (0 == _BitScanForward(&rv, v))
+		return 0;
+
+	return (unsigned int)rv + 1;
+}
+
+static inline unsigned int
+rte_ffs64(uint64_t v)
+{
+	unsigned long rv;
+
+	if (0 == _BitScanForward64(&rv, v))
+		return 0;
+
+	return (unsigned int)rv + 1;
+}
+
 #else
 
 /**
@@ -491,6 +513,18 @@
 	return (unsigned int)__builtin_popcountll(v);
 }
 
+static inline unsigned int
+rte_ffs32(uint32_t v)
+{
+	return (unsigned int)__builtin_ffs(v);
+}
+
+static inline unsigned int
+rte_ffs64(uint64_t v)
+{
+	return (unsigned int)__builtin_ffsll(v);
+}
+
 #endif
 
 /**
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH 2/2] eal: add rte ffs32 and rte ffs64 inline functions

Quoted 5 lines · Tyler Retzlaff

provide toolchain abstraction for __builtin_ffs{,l,ll} gcc built-in intrinsics.

Please add a unit test and an entry in the release notes.

Quoted 9 lines
---
 lib/eal/include/rte_bitops.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
lib/eal/include/rte_bitops.h +0 -0
index 449565e..e157a45 100644

lib/eal/include/rte_bitops.h line 413

Quoted 9 lines
lib/eal/include/rte_bitops.h +6 -0
@@ -405,6 +405,28 @@
        return (unsigned int)__popcnt64(v);
 }
 
+static inline unsigned int
+rte_ffs32(uint32_t v)
+{
+       unsigned long rv;
+
+       if (0 == _BitScanForward(&rv, v))

_BitScanForward(&rv, v) == 0 please.

Signature · 1 line
David Marchand
#4 lore raw

Re: [PATCH 2/2] eal: add rte ffs32 and rte ffs64 inline functions

Quoted 22 lines · David Marchand
Quoted 5 lines · Tyler Retzlaff

provide toolchain abstraction for __builtin_ffs{,l,ll} gcc built-in intrinsics.

Please add a unit test and an entry in the release notes.

Quoted 12 lines
---
 lib/eal/include/rte_bitops.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
lib/eal/include/rte_bitops.h +0 -0
index 449565e..e157a45 100644
@@ -405,6 +405,28 @@
        return (unsigned int)__popcnt64(v);
 }
 
Quoted 13 lines

lib/eal/include/rte_bitops.h line 413

Quoted 6 lines
lib/eal/include/rte_bitops.h +6 -0
+static inline unsigned int
+rte_ffs32(uint32_t v)
+{
+       unsigned long rv;
+
+       if (0 == _BitScanForward(&rv, v))

_BitScanForward(&rv, v) == 0 please.

Signature · 1 line
David Marchand

Thanks David. I'll make the change suggested and add some unit tests. --
Andre Muezerie

#5 lore raw

v2 cover provide rte_ffs32, rte_ffs64 and __rte_x86_movdiri

In reply to Tyler Retzlaff

MSVC does not support inline assembly so use movdiri intrinsic and provide abstracted rte_ffs{32,64} inline functions instead of directly using GCC built-ins.

v2:
* Moved constants to the right side of the comparison
* Added tests for rte_ffs32 and rte_ffs64 functions

Andre Muezerie (1):
app/test: add test for rte_ffs32 and rte_ffs64 functions.

Tyler Retzlaff (2):
eal: provide movdiri for MSVC
eal: add rte ffs32 and rte ffs64 inline functions

 app/test/test_bitops.c       | 38 ++++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_bitops.h | 34 ++++++++++++++++++++++++++++++++
 lib/eal/x86/include/rte_io.h |  4 ++++
 3 files changed, 76 insertions(+)

--
2.47.0.vfs.0.3

#6 lore raw

PATCH v2 1/3 eal: provide movdiri for MSVC

From: Tyler Retzlaff <[email protected]>

MSVC does not support inline assembly. Instead it provides compiler intrinsics. Provide conditional compile for MSVC for movdiri using the _directstoreu_u32 intrinsic.

---
 lib/eal/x86/include/rte_io.h | 4 ++++
 1 file changed, 4 insertions(+)
lib/eal/x86/include/rte_io.h +4 -0
index f92a2e5295..7087e728ce 100644
@@ -22,11 +22,15 @@ extern "C" {
 static __rte_always_inline void
 __rte_x86_movdiri(uint32_t value, volatile void *addr)
 {
+#ifdef RTE_TOOLCHAIN_MSVC
+	_directstoreu_u32((void *)(uintptr_t)addr, value);
+#else
 	asm volatile(
 		/* MOVDIRI */
 		".byte 0x0f, 0x38, 0xf9, 0x02"
 		:
 		: "a" (value), "d" (addr));
+#endif
 }
 
 __rte_experimental
Signature · 1 line
2.47.0.vfs.0.3
#7 lore raw

PATCH v2 2/3 eal: add rte ffs32 and rte ffs64 inline functions

In reply to Andre Muezerie

From: Tyler Retzlaff <[email protected]>

Provide toolchain abstraction for __builtin_ffs{,l,ll} gcc built-in intrinsics.

---
 lib/eal/include/rte_bitops.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
lib/eal/include/rte_bitops.h +34 -0
index deb1fd43f2..14074850ec 100644
@@ -958,6 +958,28 @@ rte_popcount64(uint64_t v)
 	return (unsigned int)__popcnt64(v);
 }
 
+static inline unsigned int
+rte_ffs32(uint32_t v)
+{
+	unsigned long rv;
+
+	if (_BitScanForward(&rv, v) == 0)
+		return 0;
+
+	return (unsigned int)rv + 1;
+}
+
+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 +1066,18 @@ rte_popcount64(uint64_t v)
 	return (unsigned int)__builtin_popcountll(v);
 }
 
+static inline unsigned int
+rte_ffs32(uint32_t v)
+{
+	return (unsigned int)__builtin_ffs(v);
+}
+
+static inline unsigned int
+rte_ffs64(uint64_t v)
+{
+	return (unsigned int)__builtin_ffsll(v);
+}
+
 #endif
 
 /**
Signature · 1 line
2.47.0.vfs.0.3
#8 lore raw

PATCH v2 3/3 app/test: add test for rte_ffs32 and rte_ffs64 functions.

In reply to Andre Muezerie

Add tests for new rte_ffs32 and rte_ffs64 functions.

---
 app/test/test_bitops.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
app/test/test_bitops.c +38 -0
index 78a7df6bb1..dd374d7883 100644
@@ -406,6 +406,42 @@ test_bit_relaxed_test_set_clear(void)
 	return TEST_SUCCESS;
 }
 
+static int
+test_bit_scan_forward(void)
+{
+	unsigned int bit_nr;
+
+	TEST_ASSERT((bit_nr = rte_ffs32(0)) == 0,
+		"rte_ffs32 returned unexpected %d", bit_nr);
+
+	for (int i = 0; i < 32; ++i) {
+		uint32_t n = RTE_BIT32(i);
+
+		TEST_ASSERT((bit_nr = rte_ffs32(n)) == (unsigned int)(i+1),
+			"rte_ffs32 returned unexpected %d", bit_nr);
+	}
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_bit_scan_forward64(void)
+{
+	unsigned int bit_nr;
+
+	TEST_ASSERT((bit_nr = rte_ffs64(0)) == 0,
+		"rte_ffs64 returned unexpected %d", bit_nr);
+
+	for (int i = 0; i < 64; ++i) {
+		uint64_t n = RTE_BIT64(i);
+
+		TEST_ASSERT((bit_nr = rte_ffs64(n)) == (unsigned int)(i+1),
+			"rte_ffs64 returned unexpected %d", bit_nr);
+	}
+
+	return TEST_SUCCESS;
+}
+
 static struct unit_test_suite test_suite = {
 	.suite_name = "Bitops test suite",
 	.unit_test_cases = {
@@ -428,6 +464,8 @@ static struct unit_test_suite test_suite = {
 		TEST_CASE(test_bit_relaxed_set),
 		TEST_CASE(test_bit_relaxed_clear),
 		TEST_CASE(test_bit_relaxed_test_set_clear),
+		TEST_CASE(test_bit_scan_forward),
+		TEST_CASE(test_bit_scan_forward64),
 		TEST_CASES_END()
 	}
 };
Signature · 1 line
2.47.0.vfs.0.3
#9 lore raw

Re: [PATCH v2 0/3] provide rte_ffs32, rte_ffs64 and __rte_x86_movdiri

In reply to Andre Muezerie
Quoted 12 lines · Andre Muezerie

MSVC does not support inline assembly so use movdiri intrinsic and provide abstracted rte_ffs{32,64} inline functions instead of directly using GCC built-ins.

v2:
* Moved constants to the right side of the comparison
* Added tests for rte_ffs32 and rte_ffs64 functions

Andre Muezerie (1):
app/test: add test for rte_ffs32 and rte_ffs64 functions.

Quoted 9 lines

Tyler Retzlaff (2):
eal: provide movdiri for MSVC
eal: add rte ffs32 and rte ffs64 inline functions

 app/test/test_bitops.c       | 38 ++++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_bitops.h | 34 ++++++++++++++++++++++++++++++++
 lib/eal/x86/include/rte_io.h |  4 ++++
 3 files changed, 76 insertions(+)

I see nothing wrong with adding those wrappers to ease MSVC support. Just, those two ffs helpers should be marked experimental.

And the unit tests for counting/searching bits had been separated in a dedicated app/test/test_bitcount.c.

Signature · 1 line
David Marchand
#10 lore raw

Re: [PATCH v2 0/3] provide rte_ffs32, rte_ffs64 and __rte_x86_movdiri

Quoted 24 lines · David Marchand
Quoted 12 lines · Andre Muezerie

MSVC does not support inline assembly so use movdiri intrinsic and provide abstracted rte_ffs{32,64} inline functions instead of directly using GCC built-ins.

v2:
* Moved constants to the right side of the comparison
* Added tests for rte_ffs32 and rte_ffs64 functions

Andre Muezerie (1):
app/test: add test for rte_ffs32 and rte_ffs64 functions.

Quoted 9 lines

Tyler Retzlaff (2):
eal: provide movdiri for MSVC
eal: add rte ffs32 and rte ffs64 inline functions

 app/test/test_bitops.c       | 38 ++++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_bitops.h | 34 ++++++++++++++++++++++++++++++++
 lib/eal/x86/include/rte_io.h |  4 ++++
 3 files changed, 76 insertions(+)
Quoted 9 lines

I see nothing wrong with adding those wrappers to ease MSVC support. Just, those two ffs helpers should be marked experimental.

And the unit tests for counting/searching bits had been separated in a dedicated app/test/test_bitcount.c.

Signature · 1 line
David Marchand

Thanks for the review David. I sent a v3 series addressing these issues.

#11 lore raw

v3 cover provide rte_ffs32, rte_ffs64 and __rte_x86_movdiri

In reply to Tyler Retzlaff

MSVC does not support inline assembly so use movdiri intrinsic and provide abstracted rte_ffs{32,64} inline functions instead of directly using GCC built-ins.

v3:
* Added __rte_experimental to the new functions.
* Added comments to the new functions.
* Moved tests to test_bitcount.c

v2:
* Moved constants to the right side of the comparison
* Added tests for rte_ffs32 and rte_ffs64 functions

Andre Muezerie (1):
app/test: add test for rte_ffs32 and rte_ffs64 functions.

Tyler Retzlaff (2):
eal: provide movdiri for MSVC
eal: add rte ffs32 and rte ffs64 inline functions

 app/test/test_bitcount.c     | 38 ++++++++++++++++++
 lib/eal/include/rte_bitops.h | 74 ++++++++++++++++++++++++++++++++++++
 lib/eal/x86/include/rte_io.h |  4 ++
 3 files changed, 116 insertions(+)

--
2.47.2.vfs.0.1

#12 lore raw

PATCH v3 1/3 eal: provide movdiri for MSVC

From: Tyler Retzlaff <[email protected]>

MSVC does not support inline assembly. Instead it provides compiler intrinsics. Provide conditional compile for MSVC for movdiri using the _directstoreu_u32 intrinsic.

---
 lib/eal/x86/include/rte_io.h | 4 ++++
 1 file changed, 4 insertions(+)
lib/eal/x86/include/rte_io.h +4 -0
index f92a2e5295..7087e728ce 100644
@@ -22,11 +22,15 @@ extern "C" {
 static __rte_always_inline void
 __rte_x86_movdiri(uint32_t value, volatile void *addr)
 {
+#ifdef RTE_TOOLCHAIN_MSVC
+	_directstoreu_u32((void *)(uintptr_t)addr, value);
+#else
 	asm volatile(
 		/* MOVDIRI */
 		".byte 0x0f, 0x38, 0xf9, 0x02"
 		:
 		: "a" (value), "d" (addr));
+#endif
 }
 
 __rte_experimental
Signature · 1 line
2.47.2.vfs.0.1
#13 lore raw

PATCH v3 2/3 eal: add rte ffs32 and rte ffs64 inline functions

In reply to Andre Muezerie

From: Tyler Retzlaff <[email protected]>

Provide toolchain abstraction for __builtin_ffs{,l,ll} gcc built-in intrinsics.

---
 lib/eal/include/rte_bitops.h | 74 ++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
lib/eal/include/rte_bitops.h +74 -0
index deb1fd43f2..0862fd4008 100644
@@ -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
 
 /**
Signature · 1 line
2.47.2.vfs.0.1
#14 lore raw

PATCH v3 3/3 app/test: add test for rte_ffs32 and rte_ffs64 functions.

In reply to Andre Muezerie

Add tests for new rte_ffs32 and rte_ffs64 functions.

---
 app/test/test_bitcount.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
app/test/test_bitcount.c +38 -0
index 83c68feb7b..8eb4a460f2 100644
@@ -12,6 +12,42 @@
 
 RTE_LOG_REGISTER(bitcount_logtype_test, test.bitcount, INFO);
 
+static int
+test_bit_scan_forward(void)
+{
+	unsigned int bit_nr;
+
+	TEST_ASSERT((bit_nr = rte_ffs32(0)) == 0,
+		"rte_ffs32 returned unexpected %d", bit_nr);
+
+	for (int i = 0; i < 32; ++i) {
+		uint32_t n = RTE_BIT32(i);
+
+		TEST_ASSERT((bit_nr = rte_ffs32(n)) == (unsigned int)(i+1),
+			"rte_ffs32 returned unexpected %d", bit_nr);
+	}
+
+	return TEST_SUCCESS;
+}
+
+static int
+test_bit_scan_forward64(void)
+{
+	unsigned int bit_nr;
+
+	TEST_ASSERT((bit_nr = rte_ffs64(0)) == 0,
+		"rte_ffs64 returned unexpected %d", bit_nr);
+
+	for (int i = 0; i < 64; ++i) {
+		uint64_t n = RTE_BIT64(i);
+
+		TEST_ASSERT((bit_nr = rte_ffs64(n)) == (unsigned int)(i+1),
+			"rte_ffs64 returned unexpected %d", bit_nr);
+	}
+
+	return TEST_SUCCESS;
+}
+
 static int
 test_clz32(void)
 {
@@ -117,6 +153,8 @@ static struct unit_test_suite bitcount_test_suite = {
 	.setup = NULL,
 	.teardown = NULL,
 	.unit_test_cases = {
+		TEST_CASE(test_bit_scan_forward),
+		TEST_CASE(test_bit_scan_forward64),
 		TEST_CASE(test_clz32),
 		TEST_CASE(test_clz64),
 		TEST_CASE(test_ctz32),
Signature · 1 line
2.47.2.vfs.0.1
#15 lore raw

Re: [PATCH v3 0/3] provide rte_ffs32, rte_ffs64 and __rte_x86_movdiri

In reply to Andre Muezerie
Quoted 17 lines · Andre Muezerie

MSVC does not support inline assembly so use movdiri intrinsic and provide abstracted rte_ffs{32,64} inline functions instead of directly using GCC built-ins.

v3:
* Added __rte_experimental to the new functions.
* Added comments to the new functions.
* Moved tests to test_bitcount.c

v2:
* Moved constants to the right side of the comparison
* Added tests for rte_ffs32 and rte_ffs64 functions

Andre Muezerie (1):
app/test: add test for rte_ffs32 and rte_ffs64 functions.

Quoted 8 lines

Tyler Retzlaff (2):
eal: provide movdiri for MSVC
eal: add rte ffs32 and rte ffs64 inline functions

 app/test/test_bitcount.c     | 38 ++++++++++++++++++
 lib/eal/include/rte_bitops.h | 74 ++++++++++++++++++++++++++++++++++++
 lib/eal/x86/include/rte_io.h |  4 ++
 3 files changed, 116 insertions(+)

Series applied, thanks Tyler and André.

Signature · 1 line
David Marchand