From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 3D80A45FCC; Thu, 2 Jan 2025 23:33:23 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 001A840611; Thu, 2 Jan 2025 23:33:01 +0100 (CET) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 1013A402B4 for ; Thu, 2 Jan 2025 23:32:55 +0100 (CET) Received: by linux.microsoft.com (Postfix, from userid 1213) id 2F6E12041A8F; Thu, 2 Jan 2025 14:32:54 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2F6E12041A8F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1735857174; bh=GdcjnffHyYVd7uNdULtAJFYPeDPn8hxA3u3vMUR2S2M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SW80HgJnSbY4KB+J0pM2bnEP1AdS2+IY0t3My8xjpT9IWf/NkXZdUYER/+HPlbEC+ uajSbO1Q/at4/R+vOKsZAow+Z/Vs3DsngprABDFMhkEd+BGdoCEJB8BIDte2e0VheZ dX7/+Zd5OEmJKcs6vWZ0iLMQnjGt11sYaVaB54xg= From: Andre Muezerie To: Cc: dev@dpdk.org, Andre Muezerie Subject: [PATCH 5/5] app/test: add tests for portable versions of __builtin_add_overflow Date: Thu, 2 Jan 2025 14:32:48 -0800 Message-Id: <1735857169-19131-6-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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-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 adds tests for these new portable functions, to confirm they behave the same way across different compilers. Signed-off-by: Andre Muezerie --- app/test/meson.build | 1 + app/test/test_math.c | 125 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 app/test/test_math.c diff --git a/app/test/meson.build b/app/test/meson.build index 22b3291fa6..ab23f8dc79 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -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'], diff --git a/app/test/test_math.c b/app/test/test_math.c new file mode 100644 index 0000000000..55fb11f22c --- /dev/null +++ b/app/test/test_math.c @@ -0,0 +1,125 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (C) 2025 Microsoft Corporation + */ + +#include +#include + +#include "test.h" + +/* Check condition and return if true. */ +#define TEST_MATH_RETURN_IF_ERROR(X) \ +do { \ + if (X) { \ + return -1; \ + } \ +} while (0) + +RTE_LOG_REGISTER(math_logtype_test, test.math, INFO); + +static int +verify_add_overflow_u8(uint8_t a, uint8_t b, + uint8_t expected_res, uint8_t expected_overflow) +{ + uint8_t res; + uint8_t overflow = __rte_add_overflow_u8(a, b, &res); + RTE_TEST_ASSERT_EQUAL(res, expected_res, + "ERROR: __rte_add_overflow_u8(0x%x, 0x%x) returned result 0x%x," + " but 0x%x was expected.", a, b, res, expected_res); + RTE_TEST_ASSERT_EQUAL(overflow, expected_overflow, + "ERROR: __rte_add_overflow_u8(0x%x, 0x%x) returned overflow 0x%x," + " but 0x%x was expected.", a, b, overflow, expected_overflow); + + return 0; +} + +static int +verify_add_overflow_u16(uint16_t a, uint16_t b, + uint16_t expected_res, uint16_t expected_overflow) +{ + uint16_t res; + uint8_t overflow = __rte_add_overflow_u16(a, b, &res); + RTE_TEST_ASSERT_EQUAL(res, expected_res, + "ERROR: __rte_add_overflow_u16(0x%x, 0x%x) returned result 0x%x," + " but 0x%x was expected.", a, b, res, expected_res); + RTE_TEST_ASSERT_EQUAL(overflow, expected_overflow, + "ERROR: __rte_add_overflow_u16(0x%x, 0x%x) returned overflow 0x%x," + " but 0x%x was expected.", a, b, overflow, expected_overflow); + + return 0; +} + +static int +verify_add_overflow_u32(uint32_t a, uint32_t b, + uint32_t expected_res, uint32_t expected_overflow) +{ + uint32_t res; + uint8_t overflow = __rte_add_overflow_u32(a, b, &res); + RTE_TEST_ASSERT_EQUAL(res, expected_res, + "ERROR: __rte_add_overflow_u32(0x%x, 0x%x) returned result 0x%x," + " but 0x%x was expected.", a, b, res, expected_res); + RTE_TEST_ASSERT_EQUAL(overflow, expected_overflow, + "ERROR: __rte_add_overflow_u32(0x%x, 0x%x) returned overflow 0x%x," + " but 0x%x was expected.", a, b, overflow, expected_overflow); + + return 0; +} + +static int +test_add_overflow_u8(void) +{ + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u8(0, 0, 0, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u8(0, 1, 1, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u8(0, 0xFF, 0xFF, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u8(1, 0xFF, 0, 1)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u8(2, 0xFF, 1, 1)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u8(4, 0xFE, 2, 1)); + + return 0; +} + +static int +test_add_overflow_u16(void) +{ + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u16(0, 0, 0, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u16(0, 1, 1, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u16(0, 0xFFFF, 0xFFFF, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u16(1, 0xFFFF, 0, 1)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u16(2, 0xFFFF, 1, 1)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u16(4, 0xFFFE, 2, 1)); + + return 0; +} + +static int +test_add_overflow_u32(void) +{ + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u32(0, 0, 0, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u32(0, 1, 1, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u32(0, 0xFFFFFFFF, 0xFFFFFFFF, 0)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u32(1, 0xFFFFFFFF, 0, 1)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u32(2, 0xFFFFFFFF, 1, 1)); + TEST_MATH_RETURN_IF_ERROR(verify_add_overflow_u32(4, 0xFFFFFFFE, 2, 1)); + + return 0; +} + +static struct unit_test_suite math_test_suite = { + .suite_name = "math autotest", + .setup = NULL, + .teardown = NULL, + .unit_test_cases = { + TEST_CASE(test_add_overflow_u8), + TEST_CASE(test_add_overflow_u16), + TEST_CASE(test_add_overflow_u32), + TEST_CASES_END() + } +}; + +static int +test_math(void) +{ + return unit_test_suite_runner(&math_test_suite); +} + +REGISTER_FAST_TEST(math_autotest, true, true, test_math); -- 2.47.0.vfs.0.3