From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id A5A8345FCC;
	Thu,  2 Jan 2025 23:33:06 +0100 (CET)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id BF9F9402EE;
	Thu,  2 Jan 2025 23:32:57 +0100 (CET)
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
 by mails.dpdk.org (Postfix) with ESMTP id A08B4402B4
 for <dev@dpdk.org>; Thu,  2 Jan 2025 23:32:54 +0100 (CET)
Received: by linux.microsoft.com (Postfix, from userid 1213)
 id BA1462041A8C; Thu,  2 Jan 2025 14:32:53 -0800 (PST)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com BA1462041A8C
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
 s=default; t=1735857173;
 bh=gmfn01yYKHMKCxBb9bK1wBcJD7amHnInhfITshKYGjM=;
 h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
 b=gkuZilGNDp/l/JlJO7bukC0qY00YItLu16gko3z3iz3YKz3e0ujGonUWeNhj0Ubi+
 gye0Ll4awsAPb1uN295rsTTtTXPvQD+56PlOyZLw/5C9w/aYWPmTUGGk1rkZGWPPYs
 eHKEAF9hQ4Yb5qzb68O0HVDSc3gu4jhiPJWLL1uY=
From: Andre Muezerie <andremue@linux.microsoft.com>
To: Tyler Retzlaff <roretzla@linux.microsoft.com>
Cc: dev@dpdk.org,
	Andre Muezerie <andremue@linux.microsoft.com>
Subject: [PATCH 2/5] lib/eal: add portable version of __builtin_add_overflow
Date: Thu,  2 Jan 2025 14:32:45 -0800
Message-Id: <1735857169-19131-3-git-send-email-andremue@linux.microsoft.com>
X-Mailer: git-send-email 1.8.3.1
In-Reply-To: <1735857169-19131-1-git-send-email-andremue@linux.microsoft.com>
References: <1735857169-19131-1-git-send-email-andremue@linux.microsoft.com>
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

__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.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 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

diff --git a/lib/eal/include/meson.build b/lib/eal/include/meson.build
index d903577caa..041a4105b5 100644
--- a/lib/eal/include/meson.build
+++ b/lib/eal/include/meson.build
@@ -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',
diff --git a/lib/eal/include/rte_math.h b/lib/eal/include/rte_math.h
new file mode 100644
index 0000000000..df2f3d4d34
--- /dev/null
+++ b/lib/eal/include/rte_math.h
@@ -0,0 +1,42 @@
+/* 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
+
+/*
+ * Functions that allow performing simple arithmetic operations together with
+ * checking whether the operations overflowed.
+ * Example of usage:
+ *     uint8_t overflow;
+ *     uint16_t a, b, result;
+ *     a = 1;
+ *     b = 2;
+ *     overflow = __rte_add_overflow_u16(a, b, &result);
+ */
+#ifdef RTE_TOOLCHAIN_MSVC
+#define __rte_add_overflow_u8(a, b, res) _addcarry_u8(0, a, b, res)
+#define __rte_add_overflow_u16(a, b, res) _addcarry_u16(0, a, b, res)
+#define __rte_add_overflow_u32(a, b, res) _addcarry_u32(0, a, b, res)
+#else
+#define __rte_add_overflow_u8(a, b, res) __builtin_add_overflow(a, b, res)
+#define __rte_add_overflow_u16(a, b, res) __builtin_add_overflow(a, b, res)
+#define __rte_add_overflow_u32(a, b, res) __builtin_add_overflow(a, b, res)
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
-- 
2.47.0.vfs.0.3