__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.
Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
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
@@ -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),
@@ -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',
new file mode 100644
@@ -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, \
+ 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
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif