Skip to contentSkip to conversation

add portable version of __builtin_add_overflow

PATCH Superseded Andre Muezerie 7 participants 53 messages Jan 2, 2025 – Jun 9, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover add portable version of __builtin_add_overflow

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

Andre Muezerie (5):
maintainers: add portable version of __builtin_add_overflow lib/eal: add portable version of __builtin_add_overflow doc/api: add portable version of __builtin_add_overflow drivers/net: use portable version of __builtin_add_overflow app/test: add tests for portable versions of __builtin_add_overflow

 MAINTAINERS                    |   1 +
 app/test/meson.build           |   1 +
 app/test/test_math.c           | 125 +++++++++++++++++++++++++++++++++
 doc/api/doxy-api-index.md      |   1 +
 drivers/net/ice/base/ice_nvm.c |   9 ++-
 lib/eal/include/meson.build    |   1 +
 lib/eal/include/rte_math.h     |  42 +++++++++++
 7 files changed, 175 insertions(+), 5 deletions(-)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h

--
2.47.0.vfs.0.3

#1 lore raw

PATCH 1/5 maintainers: add portable version of __builtin_add_overflow

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index 60bdcce543..4b03b6752e 100644
@@ -180,6 +180,7 @@ F: app/test/test_devargs.c
 F: app/test/test_eal*
 F: app/test/test_errno.c
 F: app/test/test_lcores.c
+F: app/test/test_math.c
 F: app/test/test_memcpy*
 F: app/test/test_per_lcore.c
 F: app/test/test_pflock.c
Signature · 1 line
2.47.0.vfs.0.3
#2 lore raw

PATCH 2/5 lib/eal: add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces __rte_add_overflow_u8, __rte_add_overflow_u16 and __rte_add_overflow_u32.

---
 lib/eal/include/meson.build |  1 +
 lib/eal/include/rte_math.h  | 42 +++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 lib/eal/include/rte_math.h
lib/eal/include/meson.build +1 -0
index d903577caa..041a4105b5 100644
@@ -31,6 +31,7 @@ headers += files(
         'rte_lcore_var.h',
         'rte_lock_annotations.h',
         'rte_malloc.h',
+        'rte_math.h',
         'rte_mcslock.h',
         'rte_memory.h',
         'rte_memzone.h',
lib/eal/include/rte_math.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.47.0.vfs.0.3
#3 lore raw

RE: [PATCH 2/5] lib/eal: add portable version of __builtin_add_overflow

Quoted 8 lines

From: Andre Muezerie [mailto:[email protected]]
Sent: Thursday, 2 January 2025 23.33

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces __rte_add_overflow_u8, __rte_add_overflow_u16 and __rte_add_overflow_u32.

Instead of the proposed three type-specific macros, add one generic rte_add_overflow(a, b, res) macro, like rte_bit_test() [1] using "_Generic".

[1]: https://elixir.bootlin.com/dpdk/v24.11.1/source/lib/eal/include/rte_bitops.h#L130

You may still need the type-specific macros as internal helpers for the MSVC implementation.

Furthermore, a 64 bit variant might be useful.

PS: DPDK naming convention typically uses just "8"/"16"/"32" postfix to the function name, not "_u8"/"_u16"/"_u32".

#4 lore raw

Re: [PATCH 2/5] lib/eal: add portable version of __builtin_add_overflow

Quoted 11 lines · Morten Brørup
Quoted 8 lines

From: Andre Muezerie [mailto:[email protected]]
Sent: Thursday, 2 January 2025 23.33

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces __rte_add_overflow_u8, __rte_add_overflow_u16 and __rte_add_overflow_u32.

Instead of the proposed three type-specific macros, add one generic rte_add_overflow(a, b, res) macro, like rte_bit_test() [1] using "_Generic".

Quoted 7 lines

[1]: https://elixir.bootlin.com/dpdk/v24.11.1/source/lib/eal/include/rte_bitops.h#L130

You may still need the type-specific macros as internal helpers for the MSVC implementation.

Furthermore, a 64 bit variant might be useful.

PS: DPDK naming convention typically uses just "8"/"16"/"32" postfix to the function name, not "_u8"/"_u16"/"_u32".

These are great suggestions. I'll incorporate them in the v2 of this series.

#5 lore raw

PATCH 3/5 doc/api: add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 doc/api/doxy-api-index.md | 1 +
 1 file changed, 1 insertion(+)
doc/api/doxy-api-index.md +1 -0
index f0193502bc..c95a86448d 100644
@@ -226,6 +226,7 @@ The public API headers are grouped by topics:
   [checksum](@ref rte_cksum.h),
   [config file](@ref rte_cfgfile.h),
   [key/value args](@ref rte_kvargs.h),
+  [math](@ref rte_math.h),
   [argument parsing](@ref rte_argparse.h),
   [ptr_compress](@ref rte_ptr_compress.h),
   [string](@ref rte_string_fns.h),
Signature · 1 line
2.47.0.vfs.0.3
#6 lore raw

PATCH 4/5 drivers/net: use portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. It should be replaced with a portable version that can also be used with other compilers.

---
 drivers/net/ice/base/ice_nvm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/ice/base/ice_nvm.c +4 -5
index 56c6c96a95..1fc64b502c 100644
@@ -3,6 +3,7 @@
  */
 
 #include "ice_common.h"
+#include <rte_math.h>
 
 #define GL_MNG_DEF_DEVID 0x000B611C
 
@@ -469,8 +470,6 @@ int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
 	return status;
 }
 
-#define check_add_overflow __builtin_add_overflow
-
 /**
  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
  * @hw: pointer to hardware structure
@@ -500,7 +499,7 @@ ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
 		return status;
 	}
 
-	if (check_add_overflow(pfa_ptr, (u16)(pfa_len - 1), &max_tlv)) {
+	if (__rte_add_overflow_u16(pfa_ptr, (u16)(pfa_len - 1), &max_tlv)) {
 		ice_debug(hw, ICE_DBG_INIT, "PFA starts at offset %u. PFA length of %u caused 16-bit arithmetic overflow.\n",
 				  pfa_ptr, pfa_len);
 		return ICE_ERR_INVAL_SIZE;
@@ -541,8 +540,8 @@ ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
 			return ICE_ERR_INVAL_SIZE;
 		}
 
-		if (check_add_overflow(next_tlv, (u16)2, &next_tlv) ||
-		    check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
+		if (__rte_add_overflow_u16(next_tlv, (u16)2, &next_tlv) ||
+		    __rte_add_overflow_u16(next_tlv, tlv_len, &next_tlv)) {
 			ice_debug(hw, ICE_DBG_INIT, "TLV of type %u and length 0x%04x caused 16-bit arithmetic overflow. The PFA starts at 0x%04x and has length of 0x%04x\n",
 					  tlv_sub_module_type, tlv_len, pfa_ptr, pfa_len);
 			return ICE_ERR_INVAL_SIZE;
Signature · 1 line
2.47.0.vfs.0.3
#7 lore raw

PATCH 5/5 app/test: add tests for portable versions of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch adds tests for these new portable functions, to confirm they behave the same way across different compilers.

---
 app/test/meson.build |   1 +
 app/test/test_math.c | 125 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+)
 create mode 100644 app/test/test_math.c
app/test/meson.build +1 -0
index 22b3291fa6..ab23f8dc79 100644
@@ -118,6 +118,7 @@ source_file_deps = {
     'test_lpm_perf.c': ['net', 'lpm'],
     'test_malloc.c': [],
     'test_malloc_perf.c': [],
+    'test_math.c': [],
     'test_mbuf.c': ['net'],
     'test_mcslock.c': [],
     'test_member.c': ['member', 'net'],
app/test/test_math.c new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
2.47.0.vfs.0.3
#8 lore raw
In reply to Andre Muezerie
Quoted 21 lines · On Thu, 2 Jan 2025 14:32:43 -0800 Andre Muezerie…

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

Andre Muezerie (5):
maintainers: add portable version of __builtin_add_overflow lib/eal: add portable version of __builtin_add_overflow doc/api: add portable version of __builtin_add_overflow drivers/net: use portable version of __builtin_add_overflow app/test: add tests for portable versions of __builtin_add_overflow

 MAINTAINERS                    |   1 +
 app/test/meson.build           |   1 +
 app/test/test_math.c           | 125 +++++++++++++++++++++++++++++++++
 doc/api/doxy-api-index.md      |   1 +
 drivers/net/ice/base/ice_nvm.c |   9 ++-
 lib/eal/include/meson.build    |   1 +
 lib/eal/include/rte_math.h     |  42 +++++++++++
 7 files changed, 175 insertions(+), 5 deletions(-)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h
Quoted 3 lines

--
2.47.0.vfs.0.3

You should add _builtin_add_overflow into the checkpatch naughty list. Or maybe all the _builtin_XXX functions?

#9 lore raw
Quoted 24 lines · Stephen Hemminger
Quoted 21 lines · On Thu, 2 Jan 2025 14:32:43 -0800 Andre Muezerie…

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

Andre Muezerie (5):
maintainers: add portable version of __builtin_add_overflow lib/eal: add portable version of __builtin_add_overflow doc/api: add portable version of __builtin_add_overflow drivers/net: use portable version of __builtin_add_overflow app/test: add tests for portable versions of __builtin_add_overflow

 MAINTAINERS                    |   1 +
 app/test/meson.build           |   1 +
 app/test/test_math.c           | 125 +++++++++++++++++++++++++++++++++
 doc/api/doxy-api-index.md      |   1 +
 drivers/net/ice/base/ice_nvm.c |   9 ++-
 lib/eal/include/meson.build    |   1 +
 lib/eal/include/rte_math.h     |  42 +++++++++++
 7 files changed, 175 insertions(+), 5 deletions(-)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h
Quoted 6 lines
Quoted 3 lines

--
2.47.0.vfs.0.3

You should add _builtin_add_overflow into the checkpatch naughty list. Or maybe all the _builtin_XXX functions?

Absolutely! Let me add that for a v2 series.

#10 lore raw
Quoted 25 lines · Andre Muezerie
Quoted 24 lines · Stephen Hemminger
Quoted 21 lines · On Thu, 2 Jan 2025 14:32:43 -0800 Andre Muezerie…

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

Andre Muezerie (5):
maintainers: add portable version of __builtin_add_overflow lib/eal: add portable version of __builtin_add_overflow doc/api: add portable version of __builtin_add_overflow drivers/net: use portable version of __builtin_add_overflow app/test: add tests for portable versions of __builtin_add_overflow

 MAINTAINERS                    |   1 +
 app/test/meson.build           |   1 +
 app/test/test_math.c           | 125 +++++++++++++++++++++++++++++++++
 doc/api/doxy-api-index.md      |   1 +
 drivers/net/ice/base/ice_nvm.c |   9 ++-
 lib/eal/include/meson.build    |   1 +
 lib/eal/include/rte_math.h     |  42 +++++++++++
 7 files changed, 175 insertions(+), 5 deletions(-)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

--
2.47.0.vfs.0.3

You should add _builtin_add_overflow into the checkpatch naughty list. Or maybe all the _builtin_XXX functions?

Absolutely! Let me add that for a v2 series.

Turns out such check was already added recently (MESSAGE='Using __builtin helpers, prefer EAL macros'), so further changes needed at this point.

#11 lore raw

v2 cover add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

Andre Muezerie (5):
maintainers: add portable version of __builtin_add_overflow lib/eal: add portable version of __builtin_add_overflow doc/api: add portable version of __builtin_add_overflow drivers/net: use portable version of __builtin_add_overflow app/test: add tests for portable version of __builtin_add_overflow

 MAINTAINERS                    |   1 +
 app/test/meson.build           |   1 +
 app/test/test_math.c           | 170 +++++++++++++++++++++++++++++++++
 doc/api/doxy-api-index.md      |   1 +
 drivers/net/ice/base/ice_nvm.c |   9 +-
 lib/eal/include/meson.build    |   1 +
 lib/eal/include/rte_math.h     |  46 +++++++++
 7 files changed, 224 insertions(+), 5 deletions(-)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h

--
2.47.0.vfs.0.3

#12 lore raw

PATCH v2 1/5 maintainers: add portable version of __builtin_add_overflow

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index 0f940ad713..fb64ba893b 100644
@@ -179,6 +179,7 @@ F: app/test/test_devargs.c
 F: app/test/test_eal*
 F: app/test/test_errno.c
 F: app/test/test_lcores.c
+F: app/test/test_math.c
 F: app/test/test_memcpy*
 F: app/test/test_per_lcore.c
 F: app/test/test_pflock.c
Signature · 1 line
2.47.0.vfs.0.3
#13 lore raw

PATCH v2 2/5 lib/eal: add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces rte_add_overflow.

---
 lib/eal/include/meson.build |  1 +
 lib/eal/include/rte_math.h  | 46 +++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 lib/eal/include/rte_math.h
lib/eal/include/meson.build +1 -0
index d903577caa..041a4105b5 100644
@@ -31,6 +31,7 @@ headers += files(
         'rte_lcore_var.h',
         'rte_lock_annotations.h',
         'rte_malloc.h',
+        'rte_math.h',
         'rte_mcslock.h',
         'rte_memory.h',
         'rte_memzone.h',
lib/eal/include/rte_math.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.47.0.vfs.0.3
#14 lore raw

Re: [PATCH v2 2/5] lib/eal: add portable version of __builtin_add_overflow

Quoted 60 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces rte_add_overflow.

---
 lib/eal/include/meson.build |  1 +
 lib/eal/include/rte_math.h  | 46 +++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 lib/eal/include/rte_math.h
lib/eal/include/meson.build +1 -0
index d903577caa..041a4105b5 100644
@@ -31,6 +31,7 @@ headers += files(
         'rte_lcore_var.h',
         'rte_lock_annotations.h',
         'rte_malloc.h',
+        'rte_math.h',
         'rte_mcslock.h',
         'rte_memory.h',
         'rte_memzone.h',
lib/eal/include/rte_math.h new file +30 -0
new file mode 100644
index 0000000000..2f4581f81b
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Microsoft Corporation
+ */
+
+#ifndef _RTE_MATH_H_
+#define _RTE_MATH_H_
+
+/**
+ * @file
+ *
+ * Math function definitions for DPDK.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Function that allows performing simple arithmetic operations together with
+ * checking whether the operation overflowed.
+ * Example of usage:
+ *     uint8_t overflow;
+ *     uint16_t a, b, result;
+ *     a = 1;
+ *     b = 2;
+ *     overflow = rte_add_overflow(a, b, &result);
+ */
+#ifdef RTE_TOOLCHAIN_MSVC
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : _addcarry_u8, \

lib/eal/include/rte_math.h line 40

Quoted 10 lines
lib/eal/include/rte_math.h new file +10 -0
+	uint16_t : _addcarry_u16, \
+	uint32_t : _addcarry_u32, \
+	uint64_t : _addcarry_u64)(0, a, b, res)
+#else
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : __builtin_add_overflow, \
+	uint16_t : __builtin_add_overflow, \
+	uint32_t : __builtin_add_overflow, \
+	uint64_t : __builtin_add_overflow)(a, b, res)
+#endif

For the gcc version, can you just simplify to the one-line below?

#define rte_add_overflow __builtin_add_overflow

/Bruce

#15 lore raw

RE: [PATCH v2 2/5] lib/eal: add portable version of __builtin_add_overflow

Quoted 36 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Monday, 6 January 2025 12.07

Quoted 7 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces rte_add_overflow.

+/*
+ * Function that allows performing simple arithmetic operations

together with

Quoted 11 lines

+ * checking whether the operation overflowed.
+ * Example of usage:

+ *     uint8_t overflow;
+ *     uint16_t a, b, result;
+ *     a = 1;
+ *     b = 2;
+ *     overflow = rte_add_overflow(a, b, &result);

+ */

lib/eal/include/rte_math.h new file +3 -0
+#ifdef RTE_TOOLCHAIN_MSVC
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : _addcarry_u8, \

lib/eal/include/rte_math.h line 40

Quoted 10 lines
lib/eal/include/rte_math.h new file +10 -0
+	uint16_t : _addcarry_u16, \
+	uint32_t : _addcarry_u32, \
+	uint64_t : _addcarry_u64)(0, a, b, res)
+#else
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : __builtin_add_overflow, \
+	uint16_t : __builtin_add_overflow, \
+	uint32_t : __builtin_add_overflow, \
+	uint64_t : __builtin_add_overflow)(a, b, res)
+#endif

For the gcc version, can you just simplify to the one-line below?

Quoted 1 line

#define rte_add_overflow __builtin_add_overflow

Yes, but then GCC compilation would not fail if "a" has some other type than the four types explicitly supported. I prefer keeping the method used this v2 patch.

#16 lore raw

Re: [PATCH v2 2/5] lib/eal: add portable version of __builtin_add_overflow

Quoted 36 lines · Morten Brørup
Quoted 36 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Monday, 6 January 2025 12.07

Quoted 7 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces rte_add_overflow.

+/*
+ * Function that allows performing simple arithmetic operations

together with

Quoted 11 lines

+ * checking whether the operation overflowed.
+ * Example of usage:

+ *     uint8_t overflow;
+ *     uint16_t a, b, result;
+ *     a = 1;
+ *     b = 2;
+ *     overflow = rte_add_overflow(a, b, &result);

+ */

lib/eal/include/rte_math.h new file +3 -0
+#ifdef RTE_TOOLCHAIN_MSVC
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : _addcarry_u8, \

lib/eal/include/rte_math.h line 40

Quoted 10 lines
lib/eal/include/rte_math.h new file +10 -0
+	uint16_t : _addcarry_u16, \
+	uint32_t : _addcarry_u32, \
+	uint64_t : _addcarry_u64)(0, a, b, res)
+#else
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : __builtin_add_overflow, \
+	uint16_t : __builtin_add_overflow, \
+	uint32_t : __builtin_add_overflow, \
+	uint64_t : __builtin_add_overflow)(a, b, res)
+#endif

For the gcc version, can you just simplify to the one-line below?

Quoted 5 lines
Quoted 1 line

#define rte_add_overflow __builtin_add_overflow

Yes, but then GCC compilation would not fail if "a" has some other type than the four types explicitly supported. I prefer keeping the method used this v2 patch.

Is that really a problem? Should our DPDK macro not support all the types that the GCC builtin supports?

/Bruce

#17 lore raw

RE: [PATCH v2 2/5] lib/eal: add portable version of __builtin_add_overflow

Quoted 43 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Monday, 6 January 2025 12.34

Quoted 5 lines · Morten Brørup
Quoted 5 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Monday, 6 January 2025 12.07

Quoted 1 line · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a

portable

Quoted 33 lines
Quoted 32 lines
Quoted 6 lines

version that can also be used with other compilers.

This patch introduces rte_add_overflow.

+/*
+ * Function that allows performing simple arithmetic operations

together with

Quoted 11 lines

+ * checking whether the operation overflowed.
+ * Example of usage:

+ *     uint8_t overflow;
+ *     uint16_t a, b, result;
+ *     a = 1;
+ *     b = 2;
+ *     overflow = rte_add_overflow(a, b, &result);

+ */

lib/eal/include/rte_math.h new file +3 -0
+#ifdef RTE_TOOLCHAIN_MSVC
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : _addcarry_u8, \

lib/eal/include/rte_math.h line 40

Quoted 10 lines
lib/eal/include/rte_math.h new file +10 -0
+	uint16_t : _addcarry_u16, \
+	uint32_t : _addcarry_u32, \
+	uint64_t : _addcarry_u64)(0, a, b, res)
+#else
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : __builtin_add_overflow, \
+	uint16_t : __builtin_add_overflow, \
+	uint32_t : __builtin_add_overflow, \
+	uint64_t : __builtin_add_overflow)(a, b, res)
+#endif

For the gcc version, can you just simplify to the one-line below?

#define rte_add_overflow __builtin_add_overflow

Quoted 7 lines
Quoted 1 line

Yes, but then GCC compilation would not fail if "a" has some other

type than the four types explicitly supported.

Quoted 2 lines

I prefer keeping the method used this v2 patch.

Is that really a problem? Should our DPDK macro not support all the types
that the GCC builtin supports?

The DPDK macro should support all the types that both MSVC and GCC supports.
Using _Generic() for GCC is an improvement for the CI to catch MSVC incompatible code when building for GCC.

Only these four unsigned types are supported by the x86_64 intrinsics.
I don't think we need support for more types; but if the need should arise, it can be added later.

#18 lore raw

Re: [PATCH v2 2/5] lib/eal: add portable version of __builtin_add_overflow

Quoted 51 lines · Morten Brørup
Quoted 43 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Monday, 6 January 2025 12.34

Quoted 5 lines · Morten Brørup
Quoted 5 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Monday, 6 January 2025 12.07

Quoted 1 line · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a

portable

Quoted 33 lines
Quoted 32 lines
Quoted 6 lines

version that can also be used with other compilers.

This patch introduces rte_add_overflow.

+/*
+ * Function that allows performing simple arithmetic operations

together with

Quoted 11 lines

+ * checking whether the operation overflowed.
+ * Example of usage:

+ *     uint8_t overflow;
+ *     uint16_t a, b, result;
+ *     a = 1;
+ *     b = 2;
+ *     overflow = rte_add_overflow(a, b, &result);

+ */

lib/eal/include/rte_math.h new file +3 -0
+#ifdef RTE_TOOLCHAIN_MSVC
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : _addcarry_u8, \

lib/eal/include/rte_math.h line 40

Quoted 10 lines
lib/eal/include/rte_math.h new file +10 -0
+	uint16_t : _addcarry_u16, \
+	uint32_t : _addcarry_u32, \
+	uint64_t : _addcarry_u64)(0, a, b, res)
+#else
+#define rte_add_overflow(a, b, res) _Generic((a), \
+	uint8_t : __builtin_add_overflow, \
+	uint16_t : __builtin_add_overflow, \
+	uint32_t : __builtin_add_overflow, \
+	uint64_t : __builtin_add_overflow)(a, b, res)
+#endif

For the gcc version, can you just simplify to the one-line below?

#define rte_add_overflow __builtin_add_overflow

Quoted 7 lines
Quoted 1 line

Yes, but then GCC compilation would not fail if "a" has some other

type than the four types explicitly supported.

Quoted 2 lines

I prefer keeping the method used this v2 patch.

Is that really a problem? Should our DPDK macro not support all the types
that the GCC builtin supports?

Quoted 5 lines

The DPDK macro should support all the types that both MSVC and GCC supports.
Using _Generic() for GCC is an improvement for the CI to catch MSVC incompatible code when building for GCC.

Only these four unsigned types are supported by the x86_64 intrinsics.
I don't think we need support for more types; but if the need should arise, it can be added later.

Great. Let me know if any further action is needed on this patch.

#19 lore raw

PATCH v2 3/5 doc/api: add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 doc/api/doxy-api-index.md | 1 +
 1 file changed, 1 insertion(+)
doc/api/doxy-api-index.md +1 -0
index f0193502bc..c95a86448d 100644
@@ -226,6 +226,7 @@ The public API headers are grouped by topics:
   [checksum](@ref rte_cksum.h),
   [config file](@ref rte_cfgfile.h),
   [key/value args](@ref rte_kvargs.h),
+  [math](@ref rte_math.h),
   [argument parsing](@ref rte_argparse.h),
   [ptr_compress](@ref rte_ptr_compress.h),
   [string](@ref rte_string_fns.h),
Signature · 1 line
2.47.0.vfs.0.3
#20 lore raw

PATCH v2 4/5 drivers/net: use portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. It should be replaced with a portable version that can also be used with other compilers.

---
 drivers/net/ice/base/ice_nvm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/ice/base/ice_nvm.c +4 -5
index 56c6c96a95..1002a6b59f 100644
@@ -3,6 +3,7 @@
  */
 
 #include "ice_common.h"
+#include <rte_math.h>
 
 #define GL_MNG_DEF_DEVID 0x000B611C
 
@@ -469,8 +470,6 @@ int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
 	return status;
 }
 
-#define check_add_overflow __builtin_add_overflow
-
 /**
  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
  * @hw: pointer to hardware structure
@@ -500,7 +499,7 @@ ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
 		return status;
 	}
 
-	if (check_add_overflow(pfa_ptr, (u16)(pfa_len - 1), &max_tlv)) {
+	if (rte_add_overflow(pfa_ptr, (u16)(pfa_len - 1), &max_tlv)) {
 		ice_debug(hw, ICE_DBG_INIT, "PFA starts at offset %u. PFA length of %u caused 16-bit arithmetic overflow.\n",
 				  pfa_ptr, pfa_len);
 		return ICE_ERR_INVAL_SIZE;
@@ -541,8 +540,8 @@ ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
 			return ICE_ERR_INVAL_SIZE;
 		}
 
-		if (check_add_overflow(next_tlv, (u16)2, &next_tlv) ||
-		    check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
+		if (rte_add_overflow(next_tlv, (u16)2, &next_tlv) ||
+		    rte_add_overflow(next_tlv, tlv_len, &next_tlv)) {
 			ice_debug(hw, ICE_DBG_INIT, "TLV of type %u and length 0x%04x caused 16-bit arithmetic overflow. The PFA starts at 0x%04x and has length of 0x%04x\n",
 					  tlv_sub_module_type, tlv_len, pfa_ptr, pfa_len);
 			return ICE_ERR_INVAL_SIZE;
Signature · 1 line
2.47.0.vfs.0.3
#21 lore raw

PATCH v2 5/5 app/test: add tests for portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch adds tests for these new portable functions, to confirm they behave the same way across different compilers.

---
 app/test/meson.build |   1 +
 app/test/test_math.c | 170 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+)
 create mode 100644 app/test/test_math.c
app/test/meson.build +1 -0
index d5cb6a7f7a..49e13c5505 100644
@@ -118,6 +118,7 @@ source_file_deps = {
     'test_lpm_perf.c': ['net', 'lpm'],
     'test_malloc.c': [],
     'test_malloc_perf.c': [],
+    'test_math.c': [],
     'test_mbuf.c': ['net'],
     'test_mcslock.c': [],
     'test_member.c': ['member', 'net'],
app/test/test_math.c new file +170 -0

170 lines not on this page · raw message

Signature · 1 line
2.47.0.vfs.0.3
#22 lore raw

v3 cover add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

v3:
- Rebase on top of latest main.

Andre Muezerie (5):
maintainers: add portable version of __builtin_add_overflow
eal: add portable version of __builtin_add_overflow doc/api: add portable version of __builtin_add_overflow net/intel: use portable version of __builtin_add_overflow app/test: add tests for portable version of __builtin_add_overflow

 MAINTAINERS                          |   1 +
 app/test/meson.build                 |   1 +
 app/test/test_math.c                 | 170 +++++++++++++++++++++++++++
 doc/api/doxy-api-index.md            |   1 +
 drivers/net/intel/ice/base/ice_nvm.c |   9 +-
 lib/eal/include/meson.build          |   1 +
 lib/eal/include/rte_math.h           |  46 ++++++++
 7 files changed, 224 insertions(+), 5 deletions(-)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h

--
2.48.1.vfs.0.0

#23 lore raw

PATCH v3 1/5 maintainers: add portable version of __builtin_add_overflow

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index 312e6fcee5..31c25a6fff 100644
@@ -179,6 +179,7 @@ F: app/test/test_devargs.c
 F: app/test/test_eal*
 F: app/test/test_errno.c
 F: app/test/test_lcores.c
+F: app/test/test_math.c
 F: app/test/test_memcpy*
 F: app/test/test_per_lcore.c
 F: app/test/test_pflock.c
Signature · 1 line
2.48.1.vfs.0.0
#24 lore raw

PATCH v3 2/5 eal: add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces rte_add_overflow.

---
 lib/eal/include/meson.build |  1 +
 lib/eal/include/rte_math.h  | 46 +++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 lib/eal/include/rte_math.h
lib/eal/include/meson.build +1 -0
index d903577caa..041a4105b5 100644
@@ -31,6 +31,7 @@ headers += files(
         'rte_lcore_var.h',
         'rte_lock_annotations.h',
         'rte_malloc.h',
+        'rte_math.h',
         'rte_mcslock.h',
         'rte_memory.h',
         'rte_memzone.h',
lib/eal/include/rte_math.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.48.1.vfs.0.0
#25 lore raw

PATCH v3 3/5 doc/api: add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 doc/api/doxy-api-index.md | 1 +
 1 file changed, 1 insertion(+)
doc/api/doxy-api-index.md +1 -0
index b2fc24b3e4..48922054ed 100644
@@ -227,6 +227,7 @@ The public API headers are grouped by topics:
   [checksum](@ref rte_cksum.h),
   [config file](@ref rte_cfgfile.h),
   [key/value args](@ref rte_kvargs.h),
+  [math](@ref rte_math.h),
   [argument parsing](@ref rte_argparse.h),
   [ptr_compress](@ref rte_ptr_compress.h),
   [string](@ref rte_string_fns.h),
Signature · 1 line
2.48.1.vfs.0.0
#26 lore raw

PATCH v3 4/5 net/intel: use portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. It should be replaced with a portable version that can also be used with other compilers.

---
 drivers/net/intel/ice/base/ice_nvm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/intel/ice/base/ice_nvm.c +4 -5
index 56c6c96a95..1002a6b59f 100644
@@ -3,6 +3,7 @@
  */
 
 #include "ice_common.h"
+#include <rte_math.h>
 
 #define GL_MNG_DEF_DEVID 0x000B611C
 
@@ -469,8 +470,6 @@ int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
 	return status;
 }
 
-#define check_add_overflow __builtin_add_overflow
-
 /**
  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
  * @hw: pointer to hardware structure
@@ -500,7 +499,7 @@ ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
 		return status;
 	}
 
-	if (check_add_overflow(pfa_ptr, (u16)(pfa_len - 1), &max_tlv)) {
+	if (rte_add_overflow(pfa_ptr, (u16)(pfa_len - 1), &max_tlv)) {
 		ice_debug(hw, ICE_DBG_INIT, "PFA starts at offset %u. PFA length of %u caused 16-bit arithmetic overflow.\n",
 				  pfa_ptr, pfa_len);
 		return ICE_ERR_INVAL_SIZE;
@@ -541,8 +540,8 @@ ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
 			return ICE_ERR_INVAL_SIZE;
 		}
 
-		if (check_add_overflow(next_tlv, (u16)2, &next_tlv) ||
-		    check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
+		if (rte_add_overflow(next_tlv, (u16)2, &next_tlv) ||
+		    rte_add_overflow(next_tlv, tlv_len, &next_tlv)) {
 			ice_debug(hw, ICE_DBG_INIT, "TLV of type %u and length 0x%04x caused 16-bit arithmetic overflow. The PFA starts at 0x%04x and has length of 0x%04x\n",
 					  tlv_sub_module_type, tlv_len, pfa_ptr, pfa_len);
 			return ICE_ERR_INVAL_SIZE;
Signature · 1 line
2.48.1.vfs.0.0
#27 lore raw

Re: [PATCH v3 4/5] net/intel: use portable version of __builtin_add_overflow

Quoted 21 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. It should be replaced with a portable version that can also be used with other compilers.

---
 drivers/net/intel/ice/base/ice_nvm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/intel/ice/base/ice_nvm.c +1 -0
index 56c6c96a95..1002a6b59f 100644
@@ -3,6 +3,7 @@
  */
 
 #include "ice_common.h"
+#include <rte_math.h>
 
 #define GL_MNG_DEF_DEVID 0x000B611C
 

drivers/net/intel/ice/base/ice_nvm.c int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data) · line 473

Quoted 5 lines
drivers/net/intel/ice/base/ice_nvm.c +0 -2
@@ -469,8 +470,6 @@ int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
 	return status;
 }
 
-#define check_add_overflow __builtin_add_overflow
-

Rather than modifying the base code, can you instead add a #define to the osdep.h file in the base directory to alias the new function to __builtin_add_overflow for MSVC. The other files (other than osdep.h) in the base directory, come from a common/shared source that is not DPDK specific, so we try to avoid modifying them where possible.

/Bruce

#28 lore raw

Re: [PATCH v3 4/5] net/intel: use portable version of __builtin_add_overflow

Quoted 34 lines · Bruce Richardson
Quoted 21 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. It should be replaced with a portable version that can also be used with other compilers.

---
 drivers/net/intel/ice/base/ice_nvm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/net/intel/ice/base/ice_nvm.c +1 -0
index 56c6c96a95..1002a6b59f 100644
@@ -3,6 +3,7 @@
  */
 
 #include "ice_common.h"
+#include <rte_math.h>
 
 #define GL_MNG_DEF_DEVID 0x000B611C
 

drivers/net/intel/ice/base/ice_nvm.c int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data) · line 473

Quoted 5 lines
drivers/net/intel/ice/base/ice_nvm.c +0 -2
@@ -469,8 +470,6 @@ int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
 	return status;
 }
 
-#define check_add_overflow __builtin_add_overflow
-

Rather than modifying the base code, can you instead add a #define to the osdep.h file in the base directory to alias the new function to __builtin_add_overflow for MSVC. The other files (other than osdep.h) in the base directory, come from a common/shared source that is not DPDK specific, so we try to avoid modifying them where possible.

Quoted 1 line

/Bruce

The problem is that the file below defines __builtin_add_overflow itself, so it does not look like we can completely avoid changing this file. I can still make the change you asked for, but the patch will have to remove that define from the file under base.

drivers\net\intel\ice\base\ice_nvm.c
--
Andre Muezerie

#29 lore raw

PATCH v3 5/5 app/test: add tests for portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch adds tests for these new portable functions, to confirm they behave the same way across different compilers.

---
 app/test/meson.build |   1 +
 app/test/test_math.c | 170 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+)
 create mode 100644 app/test/test_math.c
app/test/meson.build +1 -0
index b6285a6b45..05541c2d0f 100644
@@ -118,6 +118,7 @@ source_file_deps = {
     'test_lpm_perf.c': ['net', 'lpm'],
     'test_malloc.c': [],
     'test_malloc_perf.c': [],
+    'test_math.c': [],
     'test_mbuf.c': ['net'],
     'test_mcslock.c': [],
     'test_member.c': ['member', 'net'],
app/test/test_math.c new file +170 -0

170 lines not on this page · raw message

Signature · 1 line
2.48.1.vfs.0.0
#30 lore raw

Re: [PATCH v3 5/5] app/test: add tests for portable version of __builtin_add_overflow

Recheck-request: iol-intel-Performance

There was an infra failure with the Arm Grace server about 12 hours ago which caused this failure - sending a retest request.

#31 lore raw

v4 cover add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

v4:
- Added define in ice_osdep.h to use portable version of __builtin_add_overflow when using MSVC.
- Undid all changes from drivers/net/intel/ice/base/ice_nvm.c.

v3:
- Rebase on top of latest main.

Andre Muezerie (5):
maintainers: add portable version of __builtin_add_overflow
eal: add portable version of __builtin_add_overflow doc/api: add portable version of __builtin_add_overflow net/intel: use portable version of __builtin_add_overflow app/test: add tests for portable version of __builtin_add_overflow

 MAINTAINERS                            |   1 +
 app/test/meson.build                   |   1 +
 app/test/test_math.c                   | 170 +++++++++++++++++++++++++
 doc/api/doxy-api-index.md              |   1 +
 drivers/net/intel/ice/base/ice_osdep.h |   5 +
 lib/eal/include/meson.build            |   1 +
 lib/eal/include/rte_math.h             |  46 +++++++
 7 files changed, 225 insertions(+)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h

--
2.48.1.vfs.0.1

#32 lore raw

PATCH v4 1/5 maintainers: add portable version of __builtin_add_overflow

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index 82f6e2f917..a60fd0f976 100644
@@ -179,6 +179,7 @@ F: app/test/test_devargs.c
 F: app/test/test_eal*
 F: app/test/test_errno.c
 F: app/test/test_lcores.c
+F: app/test/test_math.c
 F: app/test/test_memcpy*
 F: app/test/test_per_lcore.c
 F: app/test/test_pflock.c
Signature · 1 line
2.48.1.vfs.0.1
#33 lore raw

Re: [PATCH v4 1/5] maintainers: add portable version of __builtin_add_overflow

Quoted 14 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +0 -0
index 82f6e2f917..a60fd0f976 100644

MAINTAINERS F: app/test/test_devargs.c · line 185

Quoted 7 lines
MAINTAINERS +1 -0
@@ -179,6 +179,7 @@ F: app/test/test_devargs.c
 F: app/test/test_eal*
 F: app/test/test_errno.c
 F: app/test/test_lcores.c
+F: app/test/test_math.c
 F: app/test/test_memcpy*
 F: app/test/test_per_lcore.c
 F: app/test/test_pflock.c

This patch should be squashed with the last patch actually adding the file.

/Bruce

#34 lore raw

PATCH v4 2/5 eal: add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces rte_add_overflow.

---
 lib/eal/include/meson.build |  1 +
 lib/eal/include/rte_math.h  | 46 +++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 lib/eal/include/rte_math.h
lib/eal/include/meson.build +1 -0
index d903577caa..041a4105b5 100644
@@ -31,6 +31,7 @@ headers += files(
         'rte_lcore_var.h',
         'rte_lock_annotations.h',
         'rte_malloc.h',
+        'rte_math.h',
         'rte_mcslock.h',
         'rte_memory.h',
         'rte_memzone.h',
lib/eal/include/rte_math.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.48.1.vfs.0.1
#35 lore raw

PATCH v4 3/5 doc/api: add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 doc/api/doxy-api-index.md | 1 +
 1 file changed, 1 insertion(+)
doc/api/doxy-api-index.md +1 -0
index b2fc24b3e4..48922054ed 100644
@@ -227,6 +227,7 @@ The public API headers are grouped by topics:
   [checksum](@ref rte_cksum.h),
   [config file](@ref rte_cfgfile.h),
   [key/value args](@ref rte_kvargs.h),
+  [math](@ref rte_math.h),
   [argument parsing](@ref rte_argparse.h),
   [ptr_compress](@ref rte_ptr_compress.h),
   [string](@ref rte_string_fns.h),
Signature · 1 line
2.48.1.vfs.0.1
#36 lore raw

Re: [PATCH v4 3/5] doc/api: add portable version of __builtin_add_overflow

Quoted 14 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

---
 doc/api/doxy-api-index.md | 1 +
 1 file changed, 1 insertion(+)
doc/api/doxy-api-index.md +0 -0
index b2fc24b3e4..48922054ed 100644

doc/api/doxy-api-index.md The public API headers are grouped by topics: · line 233

Quoted 7 lines
doc/api/doxy-api-index.md +1 -0
@@ -227,6 +227,7 @@ The public API headers are grouped by topics:
   [checksum](@ref rte_cksum.h),
   [config file](@ref rte_cfgfile.h),
   [key/value args](@ref rte_kvargs.h),
+  [math](@ref rte_math.h),
   [argument parsing](@ref rte_argparse.h),
   [ptr_compress](@ref rte_ptr_compress.h),
   [string](@ref rte_string_fns.h),

Please squash with patch 2, to add the docs along with the new header.

#37 lore raw

PATCH v4 4/5 net/intel: use portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. It should be replaced with a portable version that can also be used with other compilers.

---
 drivers/net/intel/ice/base/ice_osdep.h | 5 +++++
 1 file changed, 5 insertions(+)
drivers/net/intel/ice/base/ice_osdep.h +5 -0
index 7b96fcde03..e6c5512430 100644
@@ -14,6 +14,7 @@
 #include <stdbool.h>
 
 #include <rte_common.h>
+#include <rte_math.h>
 #include <rte_memcpy.h>
 #include <rte_malloc.h>
 #include <rte_memzone.h>
@@ -128,6 +129,10 @@ writeq(uint64_t value, volatile void *addr)
 #define wr64(a, reg, value) writeq((value), (a)->hw_addr + (reg))
 #define rd64(a, reg)        readq((a)->hw_addr + (reg))
 
+#ifdef RTE_TOOLCHAIN_MSVC
+#define __builtin_add_overflow rte_add_overflow
+#endif
+
 #endif /* __INTEL_NET_BASE_OSDEP__ */
 
 #ifndef __always_unused
Signature · 1 line
2.48.1.vfs.0.1
#38 lore raw

PATCH v4 5/5 app/test: add tests for portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch adds tests for these new portable functions, to confirm they behave the same way across different compilers.

---
 app/test/meson.build |   1 +
 app/test/test_math.c | 170 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+)
 create mode 100644 app/test/test_math.c
app/test/meson.build +1 -0
index b6285a6b45..05541c2d0f 100644
@@ -118,6 +118,7 @@ source_file_deps = {
     'test_lpm_perf.c': ['net', 'lpm'],
     'test_malloc.c': [],
     'test_malloc_perf.c': [],
+    'test_math.c': [],
     'test_mbuf.c': ['net'],
     'test_mcslock.c': [],
     'test_member.c': ['member', 'net'],
app/test/test_math.c new file +170 -0

170 lines not on this page · raw message

Signature · 1 line
2.48.1.vfs.0.1
#39 lore raw

v5 cover add portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

v5:
- Combined patches 1 with 5 and 2 with 3.

v4:
- Added define in ice_osdep.h to use portable version of __builtin_add_overflow when using MSVC.
- Undid all changes from drivers/net/intel/ice/base/ice_nvm.c.

v3:
- Rebase on top of latest main.

Andre Muezerie (3):
eal: add portable version of __builtin_add_overflow net/intel: use portable version of __builtin_add_overflow app/test: add tests for portable version of __builtin_add_overflow

 MAINTAINERS                            |   1 +
 app/test/meson.build                   |   1 +
 app/test/test_math.c                   | 170 +++++++++++++++++++++++++
 doc/api/doxy-api-index.md              |   1 +
 drivers/net/intel/ice/base/ice_osdep.h |   5 +
 lib/eal/include/meson.build            |   1 +
 lib/eal/include/rte_math.h             |  46 +++++++
 7 files changed, 225 insertions(+)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h

--
2.48.1.vfs.0.1

#40 lore raw

PATCH v5 1/3 eal: add portable version of __builtin_add_overflow

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch introduces rte_add_overflow.

---
 doc/api/doxy-api-index.md   |  1 +
 lib/eal/include/meson.build |  1 +
 lib/eal/include/rte_math.h  | 46 +++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+)
 create mode 100644 lib/eal/include/rte_math.h
doc/api/doxy-api-index.md +1 -0
index b2fc24b3e4..48922054ed 100644
@@ -227,6 +227,7 @@ The public API headers are grouped by topics:
   [checksum](@ref rte_cksum.h),
   [config file](@ref rte_cfgfile.h),
   [key/value args](@ref rte_kvargs.h),
+  [math](@ref rte_math.h),
   [argument parsing](@ref rte_argparse.h),
   [ptr_compress](@ref rte_ptr_compress.h),
   [string](@ref rte_string_fns.h),
lib/eal/include/meson.build +1 -0
index d903577caa..041a4105b5 100644
@@ -31,6 +31,7 @@ headers += files(
         'rte_lcore_var.h',
         'rte_lock_annotations.h',
         'rte_malloc.h',
+        'rte_math.h',
         'rte_mcslock.h',
         'rte_memory.h',
         'rte_memzone.h',
lib/eal/include/rte_math.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.48.1.vfs.0.1
#41 lore raw

PATCH v5 2/3 net/intel: use portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. It should be replaced with a portable version that can also be used with other compilers.

---
 drivers/net/intel/ice/base/ice_osdep.h | 5 +++++
 1 file changed, 5 insertions(+)
drivers/net/intel/ice/base/ice_osdep.h +5 -0
index 7b96fcde03..e6c5512430 100644
@@ -14,6 +14,7 @@
 #include <stdbool.h>
 
 #include <rte_common.h>
+#include <rte_math.h>
 #include <rte_memcpy.h>
 #include <rte_malloc.h>
 #include <rte_memzone.h>
@@ -128,6 +129,10 @@ writeq(uint64_t value, volatile void *addr)
 #define wr64(a, reg, value) writeq((value), (a)->hw_addr + (reg))
 #define rd64(a, reg)        readq((a)->hw_addr + (reg))
 
+#ifdef RTE_TOOLCHAIN_MSVC
+#define __builtin_add_overflow rte_add_overflow
+#endif
+
 #endif /* __INTEL_NET_BASE_OSDEP__ */
 
 #ifndef __always_unused
Signature · 1 line
2.48.1.vfs.0.1
#43 lore raw

PATCH v5 3/3 app/test: add tests for portable version of __builtin_add_overflow

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

This patch adds tests for these new portable functions, to confirm they behave the same way across different compilers.

---
 MAINTAINERS          |   1 +
 app/test/meson.build |   1 +
 app/test/test_math.c | 170 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 172 insertions(+)
 create mode 100644 app/test/test_math.c
MAINTAINERS +1 -0
index 82f6e2f917..a60fd0f976 100644
@@ -179,6 +179,7 @@ F: app/test/test_devargs.c
 F: app/test/test_eal*
 F: app/test/test_errno.c
 F: app/test/test_lcores.c
+F: app/test/test_math.c
 F: app/test/test_memcpy*
 F: app/test/test_per_lcore.c
 F: app/test/test_pflock.c
app/test/meson.build +1 -0
index b6285a6b45..05541c2d0f 100644
@@ -118,6 +118,7 @@ source_file_deps = {
     'test_lpm_perf.c': ['net', 'lpm'],
     'test_malloc.c': [],
     'test_malloc_perf.c': [],
+    'test_math.c': [],
     'test_mbuf.c': ['net'],
     'test_mcslock.c': [],
     'test_member.c': ['member', 'net'],
app/test/test_math.c new file +170 -0

170 lines not on this page · raw message

Signature · 1 line
2.48.1.vfs.0.1
#44 lore raw
In reply to Andre Muezerie
Quoted 20 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

v5:
- Combined patches 1 with 5 and 2 with 3.

v4:
- Added define in ice_osdep.h to use portable version of __builtin_add_overflow when using MSVC.
- Undid all changes from drivers/net/intel/ice/base/ice_nvm.c.

v3:
- Rebase on top of latest main.

Andre Muezerie (3):
eal: add portable version of __builtin_add_overflow net/intel: use portable version of __builtin_add_overflow app/test: add tests for portable version of __builtin_add_overflow

Quoted 10 lines
 MAINTAINERS                            |   1 +
 app/test/meson.build                   |   1 +
 app/test/test_math.c                   | 170 +++++++++++++++++++++++++
 doc/api/doxy-api-index.md              |   1 +
 drivers/net/intel/ice/base/ice_osdep.h |   5 +
 lib/eal/include/meson.build            |   1 +
 lib/eal/include/rte_math.h             |  46 +++++++
 7 files changed, 225 insertions(+)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h

I am not a fan of adding such public API, an internal API would be enough. Do you plan to add more helpers for math operations?

For the current helper, the only user is a driver (base code). Can't we just wrap a __builtin_add_overflow (under #ifdef msvc) in the osdep.h header?

Signature · 1 line
David Marchand
#45 lore raw
Quoted 37 lines

From: David Marchand [mailto:[email protected]]
Sent: Wednesday, 4 June 2025 12.08

Quoted 15 lines · Andre Muezerie

__builtin_add_overflow is gcc specific. There's a need for a portable version that can also be used with other compilers.

v5:
- Combined patches 1 with 5 and 2 with 3.

v4:
- Added define in ice_osdep.h to use portable version of __builtin_add_overflow when using MSVC.
- Undid all changes from drivers/net/intel/ice/base/ice_nvm.c.

v3:
- Rebase on top of latest main.

Quoted 8 lines

Andre Muezerie (3):
eal: add portable version of __builtin_add_overflow net/intel: use portable version of __builtin_add_overflow app/test: add tests for portable version of __builtin_add_overflow

 MAINTAINERS                            |   1 +
 app/test/meson.build                   |   1 +
 app/test/test_math.c                   | 170

+++++++++++++++++++++++++

Quoted 7 lines
 doc/api/doxy-api-index.md              |   1 +
 drivers/net/intel/ice/base/ice_osdep.h |   5 +
 lib/eal/include/meson.build            |   1 +
 lib/eal/include/rte_math.h             |  46 +++++++
 7 files changed, 225 insertions(+)
 create mode 100644 app/test/test_math.c
 create mode 100644 lib/eal/include/rte_math.h
Quoted 7 lines

I am not a fan of adding such public API, an internal API would be enough.
Do you plan to add more helpers for math operations?

For the current helper, the only user is a driver (base code). Can't we just wrap a __builtin_add_overflow (under #ifdef msvc) in the osdep.h header?

We already have public APIs for bit operations in rte_bitops.h.
This math API follows the same principle; and math operations - just like bit operations - might be useful for DPDK applications, so let's keep it public.

The only issue I have with these (incl. the bit operations) are that they are in the EAL library, although they have absolutely nothing to do with hardware or O/S abstraction, so they really should be in a "utils" library. But that's another story, so let's not burden Andre with that.

#46 lore raw
Quoted 10 lines · Morten Brørup
Quoted 7 lines

I am not a fan of adding such public API, an internal API would be enough.
Do you plan to add more helpers for math operations?

For the current helper, the only user is a driver (base code). Can't we just wrap a __builtin_add_overflow (under #ifdef msvc) in the osdep.h header?

We already have public APIs for bit operations in rte_bitops.h.
This math API follows the same principle; and math operations - just like bit operations - might be useful for DPDK applications, so let's keep it public.

This comparison is poor.

There are many users of bitops in dpdk, and *public* headers needed it.

Here, we have one single function in a driver implementation. And this code is unused (__builtin_add_overflow -> check_add_overflow -> ice_get_pfa_module_tlv -> ice_get_link_default_override -> ice_cfg_phy_fec, with no intree user).

Quoted 3 lines

The only issue I have with these (incl. the bit operations) are that they are in the EAL library, although they have absolutely nothing to do with hardware or O/S abstraction, so they really should be in a "utils" library. But that's another story, so let's not burden Andre with that.

Orthogonal to the question.

Signature · 1 line
David Marchand
#47 lore raw
Quoted 14 lines

From: David Marchand [mailto:[email protected]]
Sent: Wednesday, 4 June 2025 12.41

Quoted 6 lines · Morten Brørup
Quoted 6 lines

I am not a fan of adding such public API, an internal API would be enough.
Do you plan to add more helpers for math operations?

For the current helper, the only user is a driver (base code). Can't we just wrap a __builtin_add_overflow (under #ifdef msvc) in

the

Quoted 2 lines
Quoted 1 line

osdep.h header?

Quoted 8 lines
Quoted 2 lines

We already have public APIs for bit operations in rte_bitops.h. This math API follows the same principle; and math operations - just

like bit operations - might be useful for DPDK applications, so let's keep it public.

This comparison is poor.

There are many users of bitops in dpdk, and *public* headers needed it.

I don't think the number of uses of a generic function should determine if it should be public or private. The important thing is avoiding copy-pasting.

Quoted 6 lines

Here, we have one single function in a driver implementation. And this code is unused (__builtin_add_overflow -> check_add_overflow -> ice_get_pfa_module_tlv -> ice_get_link_default_override -> ice_cfg_phy_fec, with no intree user).

I'm mainly saying that Andre is doing nothing wrong here; it's a matter of setting the bar for making generic functions part of DPDK's public API.

In this particular case, I don't have a strong opinion on how public the new function is. Putting it in some generic private header is also perfectly acceptable for me. Just don't put it directly in the driver; that would lead to copy-paste into other drivers.

Quoted 9 lines
Quoted 3 lines

The only issue I have with these (incl. the bit operations) are that

they are in the EAL library, although they have absolutely nothing to do with hardware or O/S abstraction, so they really should be in a "utils" library.

Quoted 1 line

But that's another story, so let's not burden Andre with that.

Orthogonal to the question.

Partly, yes.
But if we had a generic "utils" library, there would be less resistance to adding the new function there than there is to adding it to the EAL API.

#48 lore raw

04/06/2025 13:04, Morten Brørup:

Quoted 32 lines
Quoted 14 lines

From: David Marchand [mailto:[email protected]]
Sent: Wednesday, 4 June 2025 12.41

Quoted 6 lines · Morten Brørup
Quoted 6 lines

I am not a fan of adding such public API, an internal API would be enough.
Do you plan to add more helpers for math operations?

For the current helper, the only user is a driver (base code). Can't we just wrap a __builtin_add_overflow (under #ifdef msvc) in

the

Quoted 2 lines
Quoted 1 line

osdep.h header?

Quoted 8 lines
Quoted 2 lines

We already have public APIs for bit operations in rte_bitops.h. This math API follows the same principle; and math operations - just

like bit operations - might be useful for DPDK applications, so let's keep it public.

This comparison is poor.

There are many users of bitops in dpdk, and *public* headers needed it.

I don't think the number of uses of a generic function should determine if it should be public or private. The important thing is avoiding copy-pasting.

Quoted 5 lines

Here, we have one single function in a driver implementation. And this code is unused (__builtin_add_overflow -> check_add_overflow -> ice_get_pfa_module_tlv -> ice_get_link_default_override -> ice_cfg_phy_fec, with no intree user).

Quoted 6 lines

I'm mainly saying that Andre is doing nothing wrong here;
it's a matter of setting the bar for making generic functions part of DPDK's public API.

In this particular case, I don't have a strong opinion on how public the new function is. Putting it in some generic private header is also perfectly acceptable for me. Just don't put it directly in the driver; that would lead to copy-paste into other drivers.

We can move it as a public API later if there is a real need. I don't think it is good to rush on adding new API in general.

Quoted 9 lines
Quoted 7 lines
Quoted 1 line

The only issue I have with these (incl. the bit operations) are that

they are in the EAL library, although they have absolutely nothing to do with hardware or O/S abstraction, so they really should be in a "utils" library.

Quoted 1 line

But that's another story, so let's not burden Andre with that.

Orthogonal to the question.

Partly, yes.

EAL is also good to abstract compiler differences. I agree such basic stuff should be in EAL if we would decide to add it.

Quoted 2 lines

But if we had a generic "utils" library, there would be less resistance to adding the new function there than there is to adding it to the EAL API.

It would be the same.
An API is supposed to be maintained forever
and give guidance on what to use.

As a conclusion, I agree with David, it is safe to keep it private for the unused base function for now.

#49 lore raw

v6 cover define __builtin_add_overflow for MSVC

In reply to Andre Muezerie

__builtin_add_overflow is gcc specific. A macro needs to be defined for code using this to be compiled with MSVC.
Since only one driver is using this, this patch adds the macro to that driver only. It can be moved to some common place if/when needed.

v6:
- Moved definition of __builtin_add_overflow to the only driver that needs it, making it private to that driver.

v5:
- Combined patches 1 with 5 and 2 with 3.

v4:
- Added define in ice_osdep.h to use portable version of __builtin_add_overflow when using MSVC.
- Undid all changes from drivers/net/intel/ice/base/ice_nvm.c.

v3:
- Rebase on top of latest main.

Andre Muezerie (1):
net/intel: define __builtin_add_overflow for MSVC

 drivers/net/intel/ice/base/ice_osdep.h | 9 +++++++++
 1 file changed, 9 insertions(+)

--
2.49.0.vfs.0.3

#50 lore raw

PATCH v6 1/1 net/intel: define __builtin_add_overflow for MSVC

__builtin_add_overflow is gcc specific. A macro needs to be defined for code using this to be compiled with MSVC.
Since only one driver is using this, this patch adds the macro to that driver only. It can be moved to some common place if/when needed.

---
 drivers/net/intel/ice/base/ice_osdep.h | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/intel/ice/base/ice_osdep.h +9 -0
index ad6cde9896..4f635f00a4 100644
@@ -128,6 +128,15 @@ writeq(uint64_t value, volatile void *addr)
 #define wr64(a, reg, value) writeq((value), (a)->hw_addr + (reg))
 #define rd64(a, reg)        readq((a)->hw_addr + (reg))
 
+#ifdef RTE_TOOLCHAIN_MSVC
+#define __builtin_add_overflow(a, b, res) \
+	_Generic((a), \
+		uint8_t : _addcarry_u8, \
+		uint16_t : _addcarry_u16, \
+		uint32_t : _addcarry_u32, \
+		uint64_t : _addcarry_u64)(0, a, b, res)
+#endif
+
 #endif /* __INTEL_NET_BASE_OSDEP__ */
 
 #ifndef __always_unused
Signature · 1 line
2.49.0.vfs.0.3
Acked-by on v6 1/1
#51 lore raw

RE: [PATCH v6 1/1] net/intel: define __builtin_add_overflow for MSVC

+TO: David and Thomas, were part of the discussion +CC: Stephen

Quoted 8 lines

From: Andre Muezerie [mailto:[email protected]]
Sent: Thursday, 5 June 2025 16.47

__builtin_add_overflow is gcc specific. A macro needs to be defined for code using this to be compiled with MSVC.
Since only one driver is using this, this patch adds the macro to that driver only. It can be moved to some common place if/when needed.

This seems to be consensus over adding it to rte_math.h, so:

Quoted 30 lines
---
 drivers/net/intel/ice/base/ice_osdep.h | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/intel/ice/base/ice_osdep.h +9 -1
index ad6cde9896..4f635f00a4 100644
@@ -128,6 +128,15 @@ writeq(uint64_t value, volatile void *addr)
 #define wr64(a, reg, value) writeq((value), (a)->hw_addr + (reg))
 #define rd64(a, reg)        readq((a)->hw_addr + (reg))
 
+#ifdef RTE_TOOLCHAIN_MSVC
+#define __builtin_add_overflow(a, b, res) \
+	_Generic((a), \
+		uint8_t : _addcarry_u8, \
+		uint16_t : _addcarry_u16, \
+		uint32_t : _addcarry_u32, \
+		uint64_t : _addcarry_u64)(0, a, b, res)
+#endif
+
 #endif /* __INTEL_NET_BASE_OSDEP__ */
 
 #ifndef __always_unused
--

2.49.0.vfs.0.3

#52 lore raw

Re: [PATCH v6 1/1] net/intel: define __builtin_add_overflow for MSVC

Quoted 6 lines · Morten Brørup
(quoted diff) +2 -0
+TO: David and Thomas, were part of the discussion
+CC: Stephen
Quoted 4 lines

From: Andre Muezerie [mailto:[email protected]]
Sent: Thursday, 5 June 2025 16.47

Quoted 9 lines
Quoted 5 lines

__builtin_add_overflow is gcc specific. A macro needs to be defined for code using this to be compiled with MSVC.
Since only one driver is using this, this patch adds the macro to that driver only. It can be moved to some common place if/when needed.

This seems to be consensus over adding it to rte_math.h, so:

Applied to dpdk-next-net-intel.

Thanks,
/Bruce