From patchwork Sun May 5 08:37:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 139867 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 1740E43FAE; Sun, 5 May 2024 10:48:06 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 17A4D402E2; Sun, 5 May 2024 10:48:01 +0200 (CEST) Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04on2087.outbound.protection.outlook.com [40.107.7.87]) by mails.dpdk.org (Postfix) with ESMTP id 79FA84028C for ; Sun, 5 May 2024 10:47:58 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JsKy/4ezw3mY+ntUoup1KAVkwUuOh/shFszaLuOUA+M04LCkNqh6DdcshoIh/c7/h/niL30W9SVO7kGf2wQNYS40fzf35SeQz7tq1SBo+A9MIxyKjtai2Cq7scEUkGqVHp/Em4KaF7EeOtAB7dAsYKjg7kIrUN1jrZQ6pW0JLrrF7QimNXQcW/Bfo3EOodpr3sdcVoTpU4CoK197O8FtM/DGdnUngU0taFzJqhrYpWEGnrUzglriEjjZh1QxZZdp1TCI78+7Dt1KICrQXlF6mt7Rf+kNoN8GhwtJAobzwOI9X4ZizkEolNcWN2r7EoZ0YznUT3kd2Q3HsUwqq0cxhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uGaYNBWczZiaiy2F567USLR166LHBzCdDI8GvTpYwsg=; b=CJT2wPJ4jEV+NdBXY3YN80/WG7TiPNnUKtiGrX0K+GPWhmrvHZNMLtAHD62oCNQUSdYjS/s5Q70+iIyxqt2KzB7FGMoVDeCCaV9OFL9PRGZBee8imiM1wBL/St+/OJvd/eULLDO2/dJEgGfXXo2ggevInnp1KAlnD3t7mobipPesMbv4Ylp0wIGeVv2NX6Qxp6QGFUugAZsjsEyXcD0hfqN4nlXl8Lzx5nCP6TQjSIcVHrPvPB8oThDwAfhUIJHzaTVvsgliyBt7zlvtAKoac7pDqvi4LgkyQcXSxqwBaZ/RpeQ/6hcEUl1aU+50tVhw9i/Dq0t17TDdDPqKQjmNhQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 192.176.1.74) smtp.rcpttodomain=dpdk.org smtp.mailfrom=ericsson.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ericsson.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uGaYNBWczZiaiy2F567USLR166LHBzCdDI8GvTpYwsg=; b=gYLBbLwIln9MlnMGoXwMzN+9H53zU8pfmsp7YMf5IHTDvhWpKINROxiHkwF46IzUShl1suzQ1i64qhXv7DwoA818x6Dedcuy4HKYWd0lg8fpfM19tXvRL7XvIElOEZ+9fsKY3G9i4rrdV+j/b/6y1zLd2I4J3LPKptg2+c/fnhKIwhFMDYQVjFydhGG3EDrdKafxO7I6oSqqAH+sz+bxN9P0uiVWJUn3L5o1IL0T+YmrS6mlY6DqZ3R0pHRBrwWLQxyMQv7C1NgHYB74KE7M0MPcgX2BzSHmIca7lwgwcj3eJ0NBeJaLc+0yOkHU8EMNUKviwU/ND+BpZEuOwuDUlg== Received: from AM0PR02CA0034.eurprd02.prod.outlook.com (2603:10a6:208:3e::47) by DBAPR07MB6567.eurprd07.prod.outlook.com (2603:10a6:10:188::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.39; Sun, 5 May 2024 08:47:56 +0000 Received: from AM3PEPF00009BA0.eurprd04.prod.outlook.com (2603:10a6:208:3e:cafe::b2) by AM0PR02CA0034.outlook.office365.com (2603:10a6:208:3e::47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.40 via Frontend Transport; Sun, 5 May 2024 08:47:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 192.176.1.74) smtp.mailfrom=ericsson.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ericsson.com; Received-SPF: Pass (protection.outlook.com: domain of ericsson.com designates 192.176.1.74 as permitted sender) receiver=protection.outlook.com; client-ip=192.176.1.74; helo=oa.msg.ericsson.com; pr=C Received: from oa.msg.ericsson.com (192.176.1.74) by AM3PEPF00009BA0.mail.protection.outlook.com (10.167.16.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.18 via Frontend Transport; Sun, 5 May 2024 08:47:56 +0000 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.68) with Microsoft SMTP Server id 15.2.1544.9; Sun, 5 May 2024 10:47:56 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id 5D89038007A; Sun, 5 May 2024 10:47:56 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Morten_Br=C3=B8rup?= , =?utf-8?q?Mattia?= =?utf-8?q?s_R=C3=B6nnblom?= Subject: [RFC v7 1/6] eal: extend bit manipulation functionality Date: Sun, 5 May 2024 10:37:32 +0200 Message-ID: <20240505083737.118649-2-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240505083737.118649-1-mattias.ronnblom@ericsson.com> References: <20240502055706.112443-2-mattias.ronnblom@ericsson.com> <20240505083737.118649-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM3PEPF00009BA0:EE_|DBAPR07MB6567:EE_ X-MS-Office365-Filtering-Correlation-Id: 07cde067-3f1a-4643-ab32-08dc6ce00f81 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|376005|36860700004|82310400017|1800799015; X-Microsoft-Antispam-Message-Info: =?utf-8?q?jxXltuKR1fgD+uoBJZHid3BeweSJgPV?= =?utf-8?q?q2xrv8qbMIBAwU8shwKaCeE4JLPFsGvTpM2cFYA5uxP6z1YRnuQPH9IZ4MAngd6JJ?= =?utf-8?q?HaQCImxna0DlFY8U1LjObHUIloM9HUPrx0zSvKSux46atmg8TY0uflWUsBtCEFdY/?= =?utf-8?q?gWIfQ52rKuFqQ205BzMviq5FjCESOmle3oJYbK+NKaHUp7fB2rHRlg54d38zd0OVl?= =?utf-8?q?DtZWpVJj79YumoKjycFb1Rhm46L12j5A85Z3r68Jeoa/Fbt2E9rQgsj9aiUj8Orwn?= =?utf-8?q?0UJ/vBsNclFXjsP7dOULmwVEo0fGWQeUDlgOpM0MCI1yIRC7Nb16UlmwBqlG5e9bo?= =?utf-8?q?iLHkyIpbVhA5lzbQVu9yXkLFvz1Nj9zDrzGOWzEQxKuLax3o8mRYDEZFMbvEnaVDq?= =?utf-8?q?FCJj0hYdWAugPS9NRQ2BTIT0y/MOLHd/pxPVPBkvCW4PQCUF/3zFgY4mTgPORwjoZ?= =?utf-8?q?OvZ2TnrgFBGUoEEWRZJv8etLomOTQRGoAOdgbMHeDSmAvu4INVkkkjF5rbfk3CZuU?= =?utf-8?q?z8NzhZzwqPBOaLwnVf5vQSPHiASF1uBRox2xfg4uSMmryFlr11J3qC+4+mj0umA5+?= =?utf-8?q?kSpc6+cbgacwivWQuKGusMm3TQ3eCnCCCE+W7szH9/i3vh8QcSEDz5aTFnlj4/Qan?= =?utf-8?q?bGLq7ilm+vWJ+caWvW0Bh/UlkORjNu3PkwCJnyCsBM7qt96oZbXYHSnGrY3Ab5DH7?= =?utf-8?q?Kb3ljnhJ71zxyAjNNlW5zVlD0NbyVP/qGpGjCBV0abL5dCTO9pbRpSS+J6VIh8P+i?= =?utf-8?q?e/mM+mC7IV7UEQkBEStCvMxSYIxGPhYSR3zP4XbkneSDNaiZe6PFxoDVl2/xCRvIZ?= =?utf-8?q?9xEMAaQKGmV0uxL8ENTrjWZOJrXI9VQ2GM2E10Q4OJp50yxuHgo1WLOHgQ2/9sohx?= =?utf-8?q?sl+AIc1vG+SU3oF8aYcoxxsY6NnKIsOVNqxgeEvmIuExtjuKwQPChDbj6VwTffl7k?= =?utf-8?q?4Vo+TMnnCmTZQ3xHAPVInE73nxfdgW+cNiMx14Qpzj+BTfnUvMYSUHwdpxMiztzih?= =?utf-8?q?gKT7Hg84H3pzMfuM0r5vu0nYWLaIVcCkl3Gc9D6DwaYnhyG5bGTv1+kIP6vMiXyAl?= =?utf-8?q?Ok75jfVoNTPxJciQHX1dqQ477jxqVE8WQxWZWcz+qwUYJZ3O2MXvKy/c8i3XjLXyr?= =?utf-8?q?KVDtAbJE/7bHVR56qrC84/QnmXXkRGSWVncr99wMU6zyBX9ixKvVMZIu3f6lLOofz?= =?utf-8?q?hkcdh2kyzSwAsME0aAV9/vHtIeD9v8OnpWEqKWDldqKx2MFTEk7HDVzcmEp91I1p7?= =?utf-8?q?1si1LEIyB8s5fMIhTApRBSeemktGvXruqKpHZggpvv2xy7dV8WPU/P6lYyCZadcZt?= =?utf-8?q?nQALLM76OFk0?= X-Forefront-Antispam-Report: CIP:192.176.1.74; CTRY:SE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:oa.msg.ericsson.com; PTR:office365.se.ericsson.net; CAT:NONE; SFS:(13230031)(376005)(36860700004)(82310400017)(1800799015); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2024 08:47:56.7773 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 07cde067-3f1a-4643-ab32-08dc6ce00f81 X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=92e84ceb-fbfd-47ab-be52-080c6b87953f; Ip=[192.176.1.74]; Helo=[oa.msg.ericsson.com] X-MS-Exchange-CrossTenant-AuthSource: AM3PEPF00009BA0.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR07MB6567 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 Add functionality to test and modify the value of individual bits in 32-bit or 64-bit words. These functions have no implications on memory ordering, atomicity and does not use volatile and thus does not prevent any compiler optimizations. RFC v6: * Have rte_bit_test() accept const-marked bitsets. RFC v4: * Add rte_bit_flip() which, believe it or not, flips the value of a bit. * Mark macro-generated private functions as experimental. * Use macros to generate *assign*() functions. RFC v3: * Work around lack of C++ support for _Generic (Tyler Retzlaff). * Fix ','-related checkpatch warnings. Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup Acked-by: Tyler Retzlaff --- lib/eal/include/rte_bitops.h | 259 ++++++++++++++++++++++++++++++++++- 1 file changed, 257 insertions(+), 2 deletions(-) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 449565eeae..3297133e22 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -2,6 +2,7 @@ * Copyright(c) 2020 Arm Limited * Copyright(c) 2010-2019 Intel Corporation * Copyright(c) 2023 Microsoft Corporation + * Copyright(c) 2024 Ericsson AB */ #ifndef _RTE_BITOPS_H_ @@ -11,12 +12,14 @@ * @file * Bit Operations * - * This file defines a family of APIs for bit operations - * without enforcing memory ordering. + * This file provides functionality for low-level, single-word + * arithmetic and bit-level operations, such as counting or + * setting individual bits. */ #include +#include #include #ifdef __cplusplus @@ -105,6 +108,196 @@ extern "C" { #define RTE_FIELD_GET64(mask, reg) \ ((typeof(mask))(((reg) & (mask)) >> rte_ctz64(mask))) +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Test bit in word. + * + * Generic selection macro to test the value of a bit in a 32-bit or + * 64-bit word. The type of operation depends on the type of the @c + * addr parameter. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ +#define rte_bit_test(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_test32, \ + const uint32_t *: __rte_bit_test32, \ + uint64_t *: __rte_bit_test64, \ + const uint64_t *: __rte_bit_test64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Set bit in word. + * + * Generic selection macro to set a bit in a 32-bit or 64-bit + * word. The type of operation depends on the type of the @c addr + * parameter. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ +#define rte_bit_set(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_set32, \ + uint64_t *: __rte_bit_set64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Clear bit in word. + * + * Generic selection macro to clear a bit in a 32-bit or 64-bit + * word. The type of operation depends on the type of the @c addr + * parameter. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ +#define rte_bit_clear(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_clear32, \ + uint64_t *: __rte_bit_clear64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Assign a value to a bit in word. + * + * Generic selection macro to assign a value to a bit in a 32-bit or 64-bit + * word. The type of operation depends on the type of the @c addr parameter. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param value + * The new value of the bit - true for '1', or false for '0'. + */ +#define rte_bit_assign(addr, nr, value) \ + _Generic((addr), \ + uint32_t *: __rte_bit_assign32, \ + uint64_t *: __rte_bit_assign64)(addr, nr, value) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Flip a bit in word. + * + * Generic selection macro to change the value of a bit to '0' if '1' + * or '1' if '0' in a 32-bit or 64-bit word. The type of operation + * depends on the type of the @c addr parameter. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ +#define rte_bit_flip(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_flip32, \ + uint64_t *: __rte_bit_flip64)(addr, nr) + +#define __RTE_GEN_BIT_TEST(family, fun, qualifier, size) \ + __rte_experimental \ + static inline bool \ + __rte_bit_ ## family ## fun ## size(const qualifier uint ## size ## _t *addr, \ + unsigned int nr) \ + { \ + RTE_ASSERT(nr < size); \ + \ + uint ## size ## _t mask = (uint ## size ## _t)1 << nr; \ + return *addr & mask; \ + } + +#define __RTE_GEN_BIT_SET(family, fun, qualifier, size) \ + __rte_experimental \ + static inline void \ + __rte_bit_ ## family ## fun ## size(qualifier uint ## size ## _t *addr, \ + unsigned int nr) \ + { \ + RTE_ASSERT(nr < size); \ + \ + uint ## size ## _t mask = (uint ## size ## _t)1 << nr; \ + *addr |= mask; \ + } \ + +#define __RTE_GEN_BIT_CLEAR(family, fun, qualifier, size) \ + __rte_experimental \ + static inline void \ + __rte_bit_ ## family ## fun ## size(qualifier uint ## size ## _t *addr, \ + unsigned int nr) \ + { \ + RTE_ASSERT(nr < size); \ + \ + uint ## size ## _t mask = ~((uint ## size ## _t)1 << nr); \ + (*addr) &= mask; \ + } \ + +#define __RTE_GEN_BIT_ASSIGN(family, fun, qualifier, size) \ + __rte_experimental \ + static inline void \ + __rte_bit_ ## family ## fun ## size(qualifier uint ## size ## _t *addr, \ + unsigned int nr, bool value) \ + { \ + if (value) \ + __rte_bit_ ## family ## set ## size(addr, nr); \ + else \ + __rte_bit_ ## family ## clear ## size(addr, nr); \ + } + +#define __RTE_GEN_BIT_FLIP(family, fun, qualifier, size) \ + __rte_experimental \ + static inline void \ + __rte_bit_ ## family ## fun ## size(qualifier uint ## size ## _t *addr, \ + unsigned int nr) \ + { \ + bool value; \ + \ + value = __rte_bit_ ## family ## test ## size(addr, nr); \ + __rte_bit_ ## family ## assign ## size(addr, nr, !value); \ + } + +__RTE_GEN_BIT_TEST(, test,, 32) +__RTE_GEN_BIT_SET(, set,, 32) +__RTE_GEN_BIT_CLEAR(, clear,, 32) +__RTE_GEN_BIT_ASSIGN(, assign,, 32) +__RTE_GEN_BIT_FLIP(, flip,, 32) + +__RTE_GEN_BIT_TEST(, test,, 64) +__RTE_GEN_BIT_SET(, set,, 64) +__RTE_GEN_BIT_CLEAR(, clear,, 64) +__RTE_GEN_BIT_ASSIGN(, assign,, 64) +__RTE_GEN_BIT_FLIP(, flip,, 64) + /*------------------------ 32-bit relaxed operations ------------------------*/ /** @@ -787,6 +980,68 @@ rte_log2_u64(uint64_t v) #ifdef __cplusplus } + +/* + * Since C++ doesn't support generic selection (i.e., _Generic), + * function overloading is used instead. Such functions must be + * defined outside 'extern "C"' to be accepted by the compiler. + */ + +#undef rte_bit_test +#undef rte_bit_set +#undef rte_bit_clear +#undef rte_bit_assign +#undef rte_bit_flip + +#define __RTE_BIT_OVERLOAD_SZ_2(fun, qualifier, size, arg1_type, arg1_name) \ + static inline void \ + rte_bit_ ## fun(qualifier uint ## size ## _t *addr, \ + arg1_type arg1_name) \ + { \ + __rte_bit_ ## fun ## size(addr, arg1_name); \ + } + +#define __RTE_BIT_OVERLOAD_2(fun, qualifier, arg1_type, arg1_name) \ + __RTE_BIT_OVERLOAD_SZ_2(fun, qualifier, 32, arg1_type, arg1_name) \ + __RTE_BIT_OVERLOAD_SZ_2(fun, qualifier, 64, arg1_type, arg1_name) + +#define __RTE_BIT_OVERLOAD_SZ_2R(fun, qualifier, size, ret_type, arg1_type, \ + arg1_name) \ + static inline ret_type \ + rte_bit_ ## fun(qualifier uint ## size ## _t *addr, \ + arg1_type arg1_name) \ + { \ + return __rte_bit_ ## fun ## size(addr, arg1_name); \ + } + +#define __RTE_BIT_OVERLOAD_2R(fun, qualifier, ret_type, arg1_type, arg1_name) \ + __RTE_BIT_OVERLOAD_SZ_2R(fun, qualifier, 32, ret_type, arg1_type, \ + arg1_name) \ + __RTE_BIT_OVERLOAD_SZ_2R(fun, qualifier, 64, ret_type, arg1_type, \ + arg1_name) + +#define __RTE_BIT_OVERLOAD_SZ_3(fun, qualifier, size, arg1_type, arg1_name, \ + arg2_type, arg2_name) \ + static inline void \ + rte_bit_ ## fun(uint ## size ## _t *addr, arg1_type arg1_name, \ + arg2_type arg2_name) \ + { \ + __rte_bit_ ## fun ## size(addr, arg1_name, arg2_name); \ + } + +#define __RTE_BIT_OVERLOAD_3(fun, qualifier, arg1_type, arg1_name, arg2_type, \ + arg2_name) \ + __RTE_BIT_OVERLOAD_SZ_3(fun, qualifier, 32, arg1_type, arg1_name, \ + arg2_type, arg2_name) \ + __RTE_BIT_OVERLOAD_SZ_3(fun, qualifier, 64, arg1_type, arg1_name, \ + arg2_type, arg2_name) + +__RTE_BIT_OVERLOAD_2R(test, const, bool, unsigned int, nr) +__RTE_BIT_OVERLOAD_2(set,, unsigned int, nr) +__RTE_BIT_OVERLOAD_2(clear,, unsigned int, nr) +__RTE_BIT_OVERLOAD_3(assign,, unsigned int, nr, bool, value) +__RTE_BIT_OVERLOAD_2(flip,, unsigned int, nr) + #endif #endif /* _RTE_BITOPS_H_ */ From patchwork Sun May 5 08:37:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 139868 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id D51B143FAE; Sun, 5 May 2024 10:48:15 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9183D402EF; Sun, 5 May 2024 10:48:03 +0200 (CEST) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on2043.outbound.protection.outlook.com [40.107.6.43]) by mails.dpdk.org (Postfix) with ESMTP id 88AAD4025D for ; Sun, 5 May 2024 10:47:59 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CLwfoVz2j00zzT0fwWjk4EB27Xju7NbgYRBKQraoii1POE2jPGD+AohT5IZRlPsSKj5NTPcxK81vDCxLZkkSp6sasJ6lT93jBP+B38RGrDupOiqT0cW2CnZZ07+hTwXfOyC8ekatQsYkhBaEHaOPR1UtHeGeNFkM89loM0g9Gisi+FRbUxVWbEuB3xBoYYGoTpNmKW7LT+OcIWghUz7ciJS1/yIz2qVw6CE7PJi9UcBVFUENyZ4kbwIxzmSaHudjMGbUSbnF/NARkMPE3TyCZ+UqqGcXdEfkk5DgybkiAi9oYR1OxCS7RTq3bNQrDi300NUsArGroP0K0sNAKACo9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=d8z68B8fzU8h0PRajH8DnhYus8GmVFVNaHI8wvhWqbw=; b=iscgQAO6bLJyxRjf1AG7CVdFENOtSByCeWMvuFDyxsOLmFBSgF5endm1r57q1cZAZvGC16BUPTA1NAfeIBrPcJHOm0dzjnAB3QzOuq6O54k5/E2oHeuFsIxBgmCpCg+OKyv93DFED/m1iZIhhUy1oWl03p90pryS1tjRflqhvSdvEgNiET2p7Uc4uoi7eRz4kYCzlhcs8LKx7WRB3qhyj0Wnhz0XvlNg0poxphaidwGY/tmxutmio7Tfzp3J6n+f8RDMrRLT2jrLoHk6Qidoxk4IpStf6RTgotJ4pyL9ihlQTQ2v39Ge9VwE3/mLBJu1yuIqV68UvYKKmpdpyKl03A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 192.176.1.74) smtp.rcpttodomain=dpdk.org smtp.mailfrom=ericsson.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ericsson.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=d8z68B8fzU8h0PRajH8DnhYus8GmVFVNaHI8wvhWqbw=; b=YtY0hpTF3XyUXvweruLbBht1drIbDfJQE1tIpZppIhHFS3XVsgETc05Syr06CcS+WiswoLtXJ116wr34lIZxQErnjJJDb1Kt5IdvI6nfqANEr24spUCAYIlob89iBgUJAfqNsNLcjI+ywi+T8S/0RkOgttOWLF8iqS2F2sYBEiG3J3lPkS8oZG8A41Wt8UYL6U1mlL82eBmDipsjLglE20kWB1LUXMum0zKZkutI3bKvuV0pSjddoKN/mMO/nmHAtQ24VDzoywkKxm104NYFtNK4eHXgsy0L2+uVcNksx3d36wcZDZuQOpI4g1hmOaVxwmcx9+kRHe5mqw6jwaiszQ== Received: from DB7PR05CA0058.eurprd05.prod.outlook.com (2603:10a6:10:2e::35) by DUZPR07MB9815.eurprd07.prod.outlook.com (2603:10a6:10:4d8::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.39; Sun, 5 May 2024 08:47:57 +0000 Received: from DU2PEPF0001E9C1.eurprd03.prod.outlook.com (2603:10a6:10:2e:cafe::ba) by DB7PR05CA0058.outlook.office365.com (2603:10a6:10:2e::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.40 via Frontend Transport; Sun, 5 May 2024 08:47:57 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 192.176.1.74) smtp.mailfrom=ericsson.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ericsson.com; Received-SPF: Pass (protection.outlook.com: domain of ericsson.com designates 192.176.1.74 as permitted sender) receiver=protection.outlook.com; client-ip=192.176.1.74; helo=oa.msg.ericsson.com; pr=C Received: from oa.msg.ericsson.com (192.176.1.74) by DU2PEPF0001E9C1.mail.protection.outlook.com (10.167.8.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Sun, 5 May 2024 08:47:57 +0000 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.64) with Microsoft SMTP Server id 15.2.1544.9; Sun, 5 May 2024 10:47:57 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id F092C38007A; Sun, 5 May 2024 10:47:56 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Morten_Br=C3=B8rup?= , =?utf-8?q?Mattia?= =?utf-8?q?s_R=C3=B6nnblom?= Subject: [RFC v7 2/6] eal: add unit tests for bit operations Date: Sun, 5 May 2024 10:37:33 +0200 Message-ID: <20240505083737.118649-3-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240505083737.118649-1-mattias.ronnblom@ericsson.com> References: <20240502055706.112443-2-mattias.ronnblom@ericsson.com> <20240505083737.118649-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF0001E9C1:EE_|DUZPR07MB9815:EE_ X-MS-Office365-Filtering-Correlation-Id: cb861e23-09c7-4ecf-fb7a-08dc6ce00fed X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|82310400017|36860700004|1800799015|376005; X-Microsoft-Antispam-Message-Info: =?utf-8?q?2f+QmG8MoRubmLJHEDwYhTplE8iH+Ab?= =?utf-8?q?rV5ISFCt9cnBwCMc1OBSIUnf0IPEIVAhfuwWqfjo8fU3Hz87pYBSdk8aq2MFw0v3M?= =?utf-8?q?cBi2jKuM997yjurCAvfy22xRfjq7vWa9bV11fOZWMhGtxNi8WZnqGr/+nfF3smlkJ?= =?utf-8?q?skKaLjxRaJsNsToR5es1kc/BKKqIWigNZlvlrETFdhG7/saUC2wK7rA6M0H4EEAGz?= =?utf-8?q?2GaNYQN41ii3kwdiGH9dLujjGJZKMBXRho+Q0EXSFa3sjM4dHBoO6bjIg0TmZUYv0?= =?utf-8?q?9yCZmYhBOLgclDGwKuDbn9tB9xsqrc6Lq00BcsNz1RjC8DwrRvEJoinJztSXq+3HN?= =?utf-8?q?zFNYNRS6KiM/PyjMrDcLGBBLhDpTq8lQJ7014FF2AZgXTFEMpNB6jOSAXchmukWIV?= =?utf-8?q?2lfqGOJOJakdHoWCKJBGgG/cWvGA0pZRmIkMVR94xRzQZpToLEfSGV4fAH9ALiSA8?= =?utf-8?q?CqYOX9GD44y0XyMf+U4wRZ167WIN393LIGzo1hB6zqQmZG/VneYwAk+UDLB7/bDNS?= =?utf-8?q?MKjKMt4MY153YX2j7nfOLXPuddFJ6w+SmyeZ4LexG8B+o/vRqK9pUV0mhmUacxweF?= =?utf-8?q?sqqxpGOwbHvVDSovZWP8Zvxu2NMMKlliGGfSBZ+cTlGhpnrRIIRvXFhRhLQA90FCU?= =?utf-8?q?bI7ktM9vBKj6I5XfqAVA8wIsiXPvdZSvv82wKl0ePjBWfW2pF8OlYd3vPyegNGe0g?= =?utf-8?q?zo1/cZu1wJ4+KQMpckVPlRwdeHj5Ezx3N1AWc8tNg6hni3RK2w90Na2qjwvLLpNM+?= =?utf-8?q?R1zxQ+XezLCvBiZ4PQ1XqC7icY9vW3sYOad6ShnSEkACQ4mEH8JmJe4UGGhhKDTM8?= =?utf-8?q?VKnNRc0uw+/wu77yTRCfPELyoDFoBT1pUfcLVdf0bktzgdxcSEBYZPXIW1WFOTjrd?= =?utf-8?q?upIRkmIfrP+5RWG62OazQmvfSgkYiENQdWQaaZjn1s7SwdCkr3DXJliwrKgihBEcO?= =?utf-8?q?qlE/2jEfaUZV0OHxx05olYXvrRhyUC9hfcCV7DdFmPsYs26ZMrZ5IFFL0hz/mb/Jx?= =?utf-8?q?a4I5G/YN9A3ieXycuZcVfzfQe+vxwtat9FWrD69XszhBODaxKp7b1MAQ0t6qkC1Dz?= =?utf-8?q?Oi/IK4PTLKgfoSF/laNCP8xKFvejKG6sIQFwvaqszrzB4HkaaYdTZwXzW/jH81i6N?= =?utf-8?q?5fAHVaESiB7e3gg1YVVshbCCH2mho/u865WJ9OTQE5hUuyImZqrz05RU36jn1lEAh?= =?utf-8?q?Duz6RLhDeGA98k9/WbwzU996fz7JdSENAVxB9Pb6IiDfH6sLUlFNd3+3W3U8VlP0/?= =?utf-8?q?Kn4aweFErt3kX1HXo/TSn0GIeCXO9CWMd+4W34P+l6XUwL5OPxmzqYrM=3D?= X-Forefront-Antispam-Report: CIP:192.176.1.74; CTRY:SE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:oa.msg.ericsson.com; PTR:office365.se.ericsson.net; CAT:NONE; SFS:(13230031)(82310400017)(36860700004)(1800799015)(376005); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2024 08:47:57.4715 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cb861e23-09c7-4ecf-fb7a-08dc6ce00fed X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=92e84ceb-fbfd-47ab-be52-080c6b87953f; Ip=[192.176.1.74]; Helo=[oa.msg.ericsson.com] X-MS-Exchange-CrossTenant-AuthSource: DU2PEPF0001E9C1.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DUZPR07MB9815 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 Extend bitops tests to cover the rte_bit_[test|set|clear|assign|flip]() functions. The tests are converted to use the test suite runner framework. RFC v6: * Test rte_bit_*test() usage through const pointers. RFC v4: * Remove redundant line continuations. Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup Acked-by: Tyler Retzlaff --- app/test/test_bitops.c | 85 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 15 deletions(-) diff --git a/app/test/test_bitops.c b/app/test/test_bitops.c index 0d4ccfb468..322f58c066 100644 --- a/app/test/test_bitops.c +++ b/app/test/test_bitops.c @@ -1,13 +1,68 @@ /* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2019 Arm Limited + * Copyright(c) 2024 Ericsson AB */ +#include + #include #include +#include #include "test.h" -uint32_t val32; -uint64_t val64; +#define GEN_TEST_BIT_ACCESS(test_name, set_fun, clear_fun, assign_fun, \ + flip_fun, test_fun, size) \ + static int \ + test_name(void) \ + { \ + uint ## size ## _t reference = (uint ## size ## _t)rte_rand(); \ + unsigned int bit_nr; \ + uint ## size ## _t word = (uint ## size ## _t)rte_rand(); \ + \ + for (bit_nr = 0; bit_nr < size; bit_nr++) { \ + bool reference_bit = (reference >> bit_nr) & 1; \ + bool assign = rte_rand() & 1; \ + if (assign) \ + assign_fun(&word, bit_nr, reference_bit); \ + else { \ + if (reference_bit) \ + set_fun(&word, bit_nr); \ + else \ + clear_fun(&word, bit_nr); \ + \ + } \ + TEST_ASSERT(test_fun(&word, bit_nr) == reference_bit, \ + "Bit %d had unexpected value", bit_nr); \ + flip_fun(&word, bit_nr); \ + TEST_ASSERT(test_fun(&word, bit_nr) != reference_bit, \ + "Bit %d had unflipped value", bit_nr); \ + flip_fun(&word, bit_nr); \ + \ + const uint ## size ## _t *const_ptr = &word; \ + TEST_ASSERT(test_fun(const_ptr, bit_nr) == \ + reference_bit, \ + "Bit %d had unexpected value", bit_nr); \ + } \ + \ + for (bit_nr = 0; bit_nr < size; bit_nr++) { \ + bool reference_bit = (reference >> bit_nr) & 1; \ + TEST_ASSERT(test_fun(&word, bit_nr) == reference_bit, \ + "Bit %d had unexpected value", bit_nr); \ + } \ + \ + TEST_ASSERT(reference == word, "Word had unexpected value"); \ + \ + return TEST_SUCCESS; \ + } + +GEN_TEST_BIT_ACCESS(test_bit_access32, rte_bit_set, rte_bit_clear, + rte_bit_assign, rte_bit_flip, rte_bit_test, 32) + +GEN_TEST_BIT_ACCESS(test_bit_access64, rte_bit_set, rte_bit_clear, + rte_bit_assign, rte_bit_flip, rte_bit_test, 64) + +static uint32_t val32; +static uint64_t val64; #define MAX_BITS_32 32 #define MAX_BITS_64 64 @@ -117,22 +172,22 @@ test_bit_relaxed_test_set_clear(void) return TEST_SUCCESS; } +static struct unit_test_suite test_suite = { + .suite_name = "Bitops test suite", + .unit_test_cases = { + TEST_CASE(test_bit_access32), + TEST_CASE(test_bit_access64), + TEST_CASE(test_bit_relaxed_set), + TEST_CASE(test_bit_relaxed_clear), + TEST_CASE(test_bit_relaxed_test_set_clear), + TEST_CASES_END() + } +}; + static int test_bitops(void) { - val32 = 0; - val64 = 0; - - if (test_bit_relaxed_set() < 0) - return TEST_FAILED; - - if (test_bit_relaxed_clear() < 0) - return TEST_FAILED; - - if (test_bit_relaxed_test_set_clear() < 0) - return TEST_FAILED; - - return TEST_SUCCESS; + return unit_test_suite_runner(&test_suite); } REGISTER_FAST_TEST(bitops_autotest, true, true, test_bitops); From patchwork Sun May 5 08:37:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 139869 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 30DB343FAE; Sun, 5 May 2024 10:48:22 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1EDD4402E7; Sun, 5 May 2024 10:48:05 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2047.outbound.protection.outlook.com [40.107.20.47]) by mails.dpdk.org (Postfix) with ESMTP id AFE064028C for ; Sun, 5 May 2024 10:47:59 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JxY07GRHYrtMsU+kENXt/L6b5oVqff8jnexTIL7aceb/HDjMsyollIPLFoh8h7GV52d9hcMY4RVQww6hHB6ZXss14p0oBN4cBLttiLETJnqEHVmVQc3q5glRW3eeJzhNNPvkqA9CY0Ib7ePE5c2CuD3ybkqejsceajs/oH/6dwLGcSiHQ9Bs/6WsMdJvl8dHce4ZCuGxui6C/hKt8wKWoTqf+Y/EnFritOTtfznhOqhpfrBnDnq+T+RK18MspFvngl1W9ePLuobxdx+3+frgxShE7M9+3zjGsHDcFOykz8gUHE/4VXQquPsf3un8CtE7aEi6b3z+8YWTF+xU6O83RQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2NtFc/85D5XqMWuHibOU2UGt9NdiuGqxaSS34YzRHUc=; b=cjvMoYsAvskAQyyrfCh/jHsWNQnUDrIth1xNXbdT06VKRkwQQ04WXfk+HoVrFI07FFbBnoDn37tlddkG6bcl17a+Dc9D7guw9UyW9fuJkblG8lx1CkSyFqWjgWL2IY3yIR6KXtj1IaRvd2SM29DsI//4TFkto4F+YMEFyT/l3gKi+HkzqF6xZZyR0ZTfE3UJu5Ukwr6jVT292u8pQQmOhvrr21klSzE2FDKbvphmbyoZJphhozFcblg/VAlWW8UiT7iXjRtoX4cnI6y46M6JhlQBjzlfq3gBOhHXttrvAZnmdEluQTGI8tp3JfVy68AkgVVt/iZ1ROtQ+zRRrsiC1Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 192.176.1.74) smtp.rcpttodomain=dpdk.org smtp.mailfrom=ericsson.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ericsson.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2NtFc/85D5XqMWuHibOU2UGt9NdiuGqxaSS34YzRHUc=; b=mTGL1TPLYV6/KV7F/Xm0/+QC1sdsSdcA0uGzGlHtvG894M6wbFRUdFCNDGNNSuSfIlfbXRTqskGu3izZ3OulTP4STBTApO4dZtxAaq+Lg8V49ckAoqRtStoJ0NX9XwqcH2WX62hvQxPhDSix/3BjZWsi+LhSc86X1RcCfXgBU6HbYTYKXUsbXe8fv8mpv3Vi7HSC/e0poH+U5X3mpVaojhF5lyTLiV9KTlx/fxDRKzePLpPTXi55nwOY7/DpwyKg7XMLeCf65POydy4sEMc6DFdHhYhiyegbIgqEg8GsqQN25LF0n9E7FZvw0FGd5exIPgJUKI+4Y/agq1Vf1x1lzw== Received: from DB7PR05CA0042.eurprd05.prod.outlook.com (2603:10a6:10:2e::19) by PAXPR07MB8793.eurprd07.prod.outlook.com (2603:10a6:102:244::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.40; Sun, 5 May 2024 08:47:58 +0000 Received: from DU2PEPF0001E9C1.eurprd03.prod.outlook.com (2603:10a6:10:2e:cafe::42) by DB7PR05CA0042.outlook.office365.com (2603:10a6:10:2e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.39 via Frontend Transport; Sun, 5 May 2024 08:47:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 192.176.1.74) smtp.mailfrom=ericsson.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ericsson.com; Received-SPF: Pass (protection.outlook.com: domain of ericsson.com designates 192.176.1.74 as permitted sender) receiver=protection.outlook.com; client-ip=192.176.1.74; helo=oa.msg.ericsson.com; pr=C Received: from oa.msg.ericsson.com (192.176.1.74) by DU2PEPF0001E9C1.mail.protection.outlook.com (10.167.8.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Sun, 5 May 2024 08:47:58 +0000 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.64) with Microsoft SMTP Server id 15.2.1544.9; Sun, 5 May 2024 10:47:57 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id 7E85038007A; Sun, 5 May 2024 10:47:57 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Morten_Br=C3=B8rup?= , =?utf-8?q?Mattia?= =?utf-8?q?s_R=C3=B6nnblom?= Subject: [RFC v7 3/6] eal: add exactly-once bit access functions Date: Sun, 5 May 2024 10:37:34 +0200 Message-ID: <20240505083737.118649-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240505083737.118649-1-mattias.ronnblom@ericsson.com> References: <20240502055706.112443-2-mattias.ronnblom@ericsson.com> <20240505083737.118649-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF0001E9C1:EE_|PAXPR07MB8793:EE_ X-MS-Office365-Filtering-Correlation-Id: cf9d5499-03d0-42a5-bc26-08dc6ce0104f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|376005|36860700004|82310400017|1800799015; X-Microsoft-Antispam-Message-Info: =?utf-8?q?BiBF+zXfZaCAZV3hSUNijRZyEHNvsUO?= =?utf-8?q?Eo7UVXWyL1JGUmTWix1YRvbLM7GcoAVBEB2YuT4bnu/wZS04qQIT2KGbW/Omm1nK5?= =?utf-8?q?QXslJYWuiY2B8yi5jyTlmuhClr0a3UyI00F00XNN65bTxTeJtr/esSgspznwX4lM9?= =?utf-8?q?S6rcZgzrMXfokGLMZffrys+ZGzrdeNhPR/WJZUQzt1cJU+xBoySShp76jrVh7TUGd?= =?utf-8?q?wi/qHzJZq6CZGnuBENTKxrDd3ABE1JSKGTvDmsybBymNK8l5DZSmA6o2ghQhDoHDc?= =?utf-8?q?bd/8w6pIXU8q0JiQ5Hexx9rpS6611QnTF6koFkQbJHlRR3CY3tcVMGqH2cLQTVQTW?= =?utf-8?q?ReUifPQmg1aVo52QJ+GRmEfvlw9L6DPnEJKUtp+CLb0I9DmaAGTx7zgi9C5HvoNla?= =?utf-8?q?JrXTu2AYXS7LUgXr/petkeLYQoIyueMh1joBLOmVhjIQGKnAHm2aTIi5gyWFyz9LZ?= =?utf-8?q?c+JUmvd7rDCnbYUfMGiEhrJXENDPEYnFm2a/uqWAO1RI9vvtx8UcGHsxdNvq9aDkH?= =?utf-8?q?CONy8P/MSOK76bKdrYYPh5ArEjp69VAYAhSLp2XV4/9SAtByV8eH1NlFu3FgxoqZr?= =?utf-8?q?ndU3XG2Ab1xWzDeYVsOTVduXe0OT2UKij5/jWneTTjyikPxBmHIIqOCnRxKm2Lrj1?= =?utf-8?q?FfqTSIYcoPefTz7apumBx3dFEt2axpeVUdXlk1a6CpryT3P667D4J6E5ZdkJXh9Ut?= =?utf-8?q?6pREdbEmU5SS+zwZAJoY6dDDMLn/7H8IRaiXhj//duwkAx5D/mbxdGhhiD12qr7Oc?= =?utf-8?q?x6tUampJM+DYAMxSRGOX21UEZ5dVeIDcu6EJlTKwE7xIAusQHmp7U1NEyDIonIWbi?= =?utf-8?q?rhpsBxj0a2Ql/umzEXfzAK0NE4n8myJ3XzSQ7S9qIQljZAzNkk4JqFdqeZYd2+6uT?= =?utf-8?q?5BmBkx8QT5m0Mt8brvWVSGFQ92JJI22YwMAqzog33+A2CpULEbMEVunW+/a3ORTKy?= =?utf-8?q?NPBpCIS7EQ0uf/5gLo0yYv30hID92ExzRVau292nUbBX0wSfCYZA16JaNHZV+JqjK?= =?utf-8?q?bgWIEFg02QjHJ9WfhC8JioObpmCSm96usCGUOy2M3oDQRdsxv1nt01i2Qgq/9gfZI?= =?utf-8?q?hFLLKrPwggXew0Bb7SOnWlFQr5m0EVwbqayp3yB8adNEUUqyjx4iWck+fatzrjPoB?= =?utf-8?q?+BLFUTO3lrpnawpkly3/CuLxyCFtM+mAg/cG0Y7GlcDPU9Hcytn57STGE7FqxF60b?= =?utf-8?q?CpVSiPYSlvPMBbxkBXZU4lXxKNRBk/rXPQpkq+eWCnE0/kaXDfZLVKd2ydM3cZdvn?= =?utf-8?q?exlo6AOuf0okjE4HIAdnz93aXhrBc5NF+t5r2T4k3YG82WfX1PY7ieWDl+U8WCNfv?= =?utf-8?q?dCbQXsLHAjQx?= X-Forefront-Antispam-Report: CIP:192.176.1.74; CTRY:SE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:oa.msg.ericsson.com; PTR:office365.se.ericsson.net; CAT:NONE; SFS:(13230031)(376005)(36860700004)(82310400017)(1800799015); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2024 08:47:58.1121 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cf9d5499-03d0-42a5-bc26-08dc6ce0104f X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=92e84ceb-fbfd-47ab-be52-080c6b87953f; Ip=[192.176.1.74]; Helo=[oa.msg.ericsson.com] X-MS-Exchange-CrossTenant-AuthSource: DU2PEPF0001E9C1.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR07MB8793 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 Add test/set/clear/assign/flip functions which prevents certain compiler optimizations and guarantees that program-level memory loads and/or stores will actually occur. These functions are useful when interacting with memory-mapped hardware devices. The "once" family of functions does not promise atomicity and provides no memory ordering guarantees beyond the C11 relaxed memory model. RFC v7: * Fix various minor issues in documentation. RFC v6: * Have rte_bit_once_test() accept const-marked bitsets. RFC v3: * Work around lack of C++ support for _Generic (Tyler Retzlaff). Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup Acked-by: Tyler Retzlaff --- lib/eal/include/rte_bitops.h | 201 +++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 3297133e22..3644aa115c 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -226,6 +226,183 @@ extern "C" { uint32_t *: __rte_bit_flip32, \ uint64_t *: __rte_bit_flip64)(addr, nr) +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Generic selection macro to test exactly once the value of a bit in + * a 32-bit or 64-bit word. The type of operation depends on the type + * of the @c addr parameter. + * + * rte_bit_once_test() is guaranteed to result in exactly one memory + * load (e.g., it may not be eliminate or merged by the compiler). + * + * \code{.c} + * rte_bit_once_set(addr, 17); + * if (rte_bit_once_test(addr, 17)) { + * ... + * } + * \endcode + * + * In the above example, rte_bit_once_set() may not be removed by + * the compiler, which would be allowed in case rte_bit_set() and + * rte_bit_test() was used. + * + * \code{.c} + * while (rte_bit_once_test(addr, 17); + * ; + * \endcode + * + * In case rte_bit_test(addr, 17) was used instead, the resulting + * object code could (and in many cases would be) replaced with + * the equivalent to + * \code{.c} + * if (rte_bit_test(addr, 17)) { + * for (;;) // spin forever + * ; + * } + * \endcode + * + * rte_bit_once_test() does not give any guarantees in regards to + * memory ordering or atomicity. + * + * The regular bit set operations (e.g., rte_bit_test()) should be + * preferred over the "once" family of operations (e.g., + * rte_bit_once_test()) if possible, since the latter may prevent + * optimizations crucial for run-time performance. + * + * @param addr + * A pointer to the word to query. + * @param nr + * The index of the bit. + * @return + * Returns true if the bit is set, and false otherwise. + */ + +#define rte_bit_once_test(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_test32, \ + const uint32_t *: __rte_bit_once_test32, \ + uint64_t *: __rte_bit_once_test64, \ + const uint64_t *: __rte_bit_once_test64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Set bit in word exactly once. + * + * Generic selection macro to set bit specified by @c nr in the word + * pointed to by @c addr to '1' exactly once. + * + * rte_bit_once_set() is guaranteed to result in exactly one memory + * load and exactly one memory store, *or* an atomic bit set + * operation. + * + * See rte_bit_test_once32() for more information and uses cases for + * the "once" class of functions. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ + +#define rte_bit_once_set(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_set32, \ + uint64_t *: __rte_bit_once_set64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Clear bit in word exactly once. + * + * Generic selection macro to set bit specified by @c nr in the word + * pointed to by @c addr to '0' exactly once. + * + * rte_bit_once_clear() is guaranteed to result in exactly one memory load + * and exactly one memory store, *or* an atomic bit clear operation. + * + * See rte_bit_test_once() for more information and uses cases for + * the "once" class of functions. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ +#define rte_bit_once_clear(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_clear32, \ + uint64_t *: __rte_bit_once_clear64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Assign a value to bit in a word exactly once. + * + * Generic selection macro to set bit specified by @c nr in the word + * pointed to by @c addr to the value indicated by @c value exactly + * once. + * + * rte_bit_once_assign() is guaranteed to result in exactly one memory + * load and exactly one memory store, *or* an atomic bit clear + * operation. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param value + * The new value of the bit - true for '1', or false for '0'. + */ +#define rte_bit_once_assign(addr, nr, value) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_assign32, \ + uint64_t *: __rte_bit_once_assign64)(addr, nr, value) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Flip bit in word, reading and writing exactly once. + * + * Generic selection macro to change the value of a bit to '0' if '1' + * or '1' if '0' in a 32-bit or 64-bit word. The type of operation + * depends on the type of the @c addr parameter. + * + * rte_bit_once_flip() is guaranteed to result in exactly one memory + * load and exactly one memory store, *or* an atomic bit flip + * operation. + * + * See rte_bit_test_once() for more information and uses cases for the + * "once" class of functions. + * + * This macro does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + */ +#define rte_bit_once_flip(addr, nr) \ + _Generic((addr), \ + uint32_t *: __rte_bit_once_flip32, \ + uint64_t *: __rte_bit_once_flip64)(addr, nr) + #define __RTE_GEN_BIT_TEST(family, fun, qualifier, size) \ __rte_experimental \ static inline bool \ @@ -298,6 +475,18 @@ __RTE_GEN_BIT_CLEAR(, clear,, 64) __RTE_GEN_BIT_ASSIGN(, assign,, 64) __RTE_GEN_BIT_FLIP(, flip,, 64) +__RTE_GEN_BIT_TEST(once_, test, volatile, 32) +__RTE_GEN_BIT_SET(once_, set, volatile, 32) +__RTE_GEN_BIT_CLEAR(once_, clear, volatile, 32) +__RTE_GEN_BIT_ASSIGN(once_, assign, volatile, 32) +__RTE_GEN_BIT_FLIP(once_, flip, volatile, 32) + +__RTE_GEN_BIT_TEST(once_, test, volatile, 64) +__RTE_GEN_BIT_SET(once_, set, volatile, 64) +__RTE_GEN_BIT_CLEAR(once_, clear, volatile, 64) +__RTE_GEN_BIT_ASSIGN(once_, assign, volatile, 64) +__RTE_GEN_BIT_FLIP(once_, flip, volatile, 64) + /*------------------------ 32-bit relaxed operations ------------------------*/ /** @@ -993,6 +1182,12 @@ rte_log2_u64(uint64_t v) #undef rte_bit_assign #undef rte_bit_flip +#undef rte_bit_once_test +#undef rte_bit_once_set +#undef rte_bit_once_clear +#undef rte_bit_once_assign +#undef rte_bit_once_flip + #define __RTE_BIT_OVERLOAD_SZ_2(fun, qualifier, size, arg1_type, arg1_name) \ static inline void \ rte_bit_ ## fun(qualifier uint ## size ## _t *addr, \ @@ -1042,6 +1237,12 @@ __RTE_BIT_OVERLOAD_2(clear,, unsigned int, nr) __RTE_BIT_OVERLOAD_3(assign,, unsigned int, nr, bool, value) __RTE_BIT_OVERLOAD_2(flip,, unsigned int, nr) +__RTE_BIT_OVERLOAD_2R(once_test, const volatile, bool, unsigned int, nr) +__RTE_BIT_OVERLOAD_2(once_set, volatile, unsigned int, nr) +__RTE_BIT_OVERLOAD_2(once_clear, volatile, unsigned int, nr) +__RTE_BIT_OVERLOAD_3(once_assign, volatile, unsigned int, nr, bool, value) +__RTE_BIT_OVERLOAD_2(once_flip, volatile, unsigned int, nr) + #endif #endif /* _RTE_BITOPS_H_ */ From patchwork Sun May 5 08:37:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 139871 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 3592443FAE; Sun, 5 May 2024 10:48:36 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 968794067C; Sun, 5 May 2024 10:48:07 +0200 (CEST) Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-ve1eur01on2075.outbound.protection.outlook.com [40.107.14.75]) by mails.dpdk.org (Postfix) with ESMTP id 776C9402E7 for ; Sun, 5 May 2024 10:48:01 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Pem+SZYjCHOR/eQsTIjTRY8g1mhGYZnxqN3SGWSjKanvb9aGIrEV+EHrNVZrknn7IyNSyaqZ/t3Ei31e3CDwJUO/gQjkUkl4FhHk2ZoOj7Mk72Bv+a+QLzRcmMbGQxh85AP9Qtbey4vNZp6oYAjqm6exmm2ItCimeL3lGUm1uJgpTl2qglhmmLUpjEJiYD3Phw0JCNrnIqJkTgNmXZvVtvtErGsHcv6KC7yry2e6F076cvyKzuKdlRG5yZ1C9vdNiJUOvbOWDW+BUpay0mHLN3W7aXFR/+rjXrujdgdd83WHxJKtEYkx6UHvl8616/ymw1ijAqPu7ck9J/38Y1HUJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3sGw/xeCUCWn//mCtmdfD0/OHX4OH+QhKpNnji9Ybjc=; b=nqODnsqO2QWoHzjCkedbsu0L3zKGSoiI/fCoeDk0R0gke4Vozu8U+9elowNm/JcidlCFaJdNuceLDnctjAOzaeX4Ow+8odItSb93BbP3YOPfv9J7LBJkbTjWPBM8ftR1mz0lxmJnfNHGDCMzxH1dAXYhyNItcfI27w4tzBa3kaLAJwheNa9iTfIKkATnNewX7yF6u0YSO6Pxr0FGvkgYoWNeBgmb5s7mJ2CPBSGujavoa5DDcpRUT6fdGpiA2WTSh6oYBcr229b/IpsiVbjYqGdobNNGS2LPsb0MUc6SxzvI80sFyTrVeNZG/YtT7YNdG3yqukijKVjTMiVFKdw0OA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 192.176.1.74) smtp.rcpttodomain=dpdk.org smtp.mailfrom=ericsson.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ericsson.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3sGw/xeCUCWn//mCtmdfD0/OHX4OH+QhKpNnji9Ybjc=; b=k3TYxuHplcgsv6Afjz9wNsHHjhAN2SsPdqhQiHTVZNRX/rv01KrOQbOYx1PBD40EjLAa3IuupO00dmJQgv7AQpJE7yhXJFhf1f9SS3y1df8bXa9Leouvnp97ERuCmaKRTZXYyhV9fdxYpjKIT8iA375d58o4lxQ5ev0Ne8yUAbNi4o9ykNTg8VCvgdwvJ4qvh5oAYB4KPQj14Sq+2tfJmo38Kgx8E3mur4Rl96oIkTilQGnlWJSKVrTvh0O15IvkWXRhXVsbcnwopRpSaZ8g9BW3i0aseJIQalkCoGZLCZhZHD7JkEyJ8WRg0NMJebcpaaEB+lFUhiMQQedAfg/BOA== Received: from AM0PR01CA0167.eurprd01.prod.exchangelabs.com (2603:10a6:208:aa::36) by DBBPR07MB7417.eurprd07.prod.outlook.com (2603:10a6:10:1e8::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.39; Sun, 5 May 2024 08:47:58 +0000 Received: from AMS0EPF000001AA.eurprd05.prod.outlook.com (2603:10a6:208:aa:cafe::c8) by AM0PR01CA0167.outlook.office365.com (2603:10a6:208:aa::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.40 via Frontend Transport; Sun, 5 May 2024 08:47:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 192.176.1.74) smtp.mailfrom=ericsson.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ericsson.com; Received-SPF: Pass (protection.outlook.com: domain of ericsson.com designates 192.176.1.74 as permitted sender) receiver=protection.outlook.com; client-ip=192.176.1.74; helo=oa.msg.ericsson.com; pr=C Received: from oa.msg.ericsson.com (192.176.1.74) by AMS0EPF000001AA.mail.protection.outlook.com (10.167.16.150) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.18 via Frontend Transport; Sun, 5 May 2024 08:47:58 +0000 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.61) with Microsoft SMTP Server id 15.2.1544.9; Sun, 5 May 2024 10:47:58 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id 0D52238007A; Sun, 5 May 2024 10:47:58 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Morten_Br=C3=B8rup?= , =?utf-8?q?Mattia?= =?utf-8?q?s_R=C3=B6nnblom?= Subject: [RFC v7 4/6] eal: add unit tests for exactly-once bit access functions Date: Sun, 5 May 2024 10:37:35 +0200 Message-ID: <20240505083737.118649-5-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240505083737.118649-1-mattias.ronnblom@ericsson.com> References: <20240502055706.112443-2-mattias.ronnblom@ericsson.com> <20240505083737.118649-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AMS0EPF000001AA:EE_|DBBPR07MB7417:EE_ X-MS-Office365-Filtering-Correlation-Id: 92b89e72-5eff-4479-d809-08dc6ce01089 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|1800799015|376005|36860700004|82310400017; X-Microsoft-Antispam-Message-Info: =?utf-8?q?i090hJgxrkXBsI9D8825LSpgU56uYgU?= =?utf-8?q?zTS3GFKzbs9VqPjYWJfa1aoJQq+Zyu0o788dHHXfqRIUklWOCtWLBHvnYFEbDw+cY?= =?utf-8?q?jHCg6KCuo+ClrRvho/bgUosVGa6o0WpgxCEREb4s6Wl0Of58RpG6avvRSZjzveJ08?= =?utf-8?q?JDYkPPE+4WaJQv8lXzP1I9wn0xp9x6UX+tgc4A70jto9W6i2ILDDr6oiRGwJnwFDJ?= =?utf-8?q?LU4r4nlbnfkOMt84LEmDDyV0ltGKIOdxuuARbIDsSKFbLgF90supSaOdZwvCikLck?= =?utf-8?q?z2BiTpV2sDkmdI1hVl6sEutAtDQnUQcBVX7OgMQxKrSrXkuD/koKV5zB+VJ9DYfBf?= =?utf-8?q?4g824lQMv+rnmcE+7aupyMdT5JnWvazth2SlwTu2CGMQhhnB/wurve2PD99KGQPru?= =?utf-8?q?sXrXHrioAVdmzsFdiJMAgeO+3CFMhZCm8LJKCNLQZCPxRlzfXsWt2EQGNyYwyTF6E?= =?utf-8?q?cZQWU05br+pnbgzYREsFXfsopMR8DGnh4M8K/nWsgaBh/UdfGEGUsS5Xgka3C+/U2?= =?utf-8?q?7aon+v225r8HEghbhV2AwsHN4gqujJ2W1vW6WlXWyYxlo96hd51QNpIThU43hmat5?= =?utf-8?q?J9EClYy2G8pswFT4GkJdRzLnF2GSNjuXZRZ0v81Gvs2ACljPm78dQrxLFSjRD4gR8?= =?utf-8?q?nL9jDLsBWCE4hOJzKN9FX8X1XRWR1vh/bubjFOJDBrbc5OCj9DO6SbhIsDuc+aaWy?= =?utf-8?q?X20ja9WTY2h/wQ9lNFCn2p9/j+NMoiZXv/7lw9+wFqnyENG9LIdmDFdMtajrfYQjV?= =?utf-8?q?dOEIejtLi56+pUnbKHG6fPZmowjBVrkzR1dBT1zdfAfeXTaxIwKPwSOS6OfY5Mr1f?= =?utf-8?q?Xseur8hziGLr1lBUS9FbWhVnkpj+Q/nv4LCbx8sEClEJYSndQ2S8Ycmp6eylz5vVs?= =?utf-8?q?z2hFJHNBSUgH4zl6upLrrPp7w59hiXKpLc9aFfNNZnMDFw44YPDg8YqmFCQusxw9F?= =?utf-8?q?iCHEs05C0eZLP1H650ihLs1wXRdGPMyPz9N95ghXn/VLAldL8vkaWR/zqdTxaci1S?= =?utf-8?q?pVXQmj9YgbJXkXYBLHvPDGJMsrB8fVCgywv6Vs0AimRV2vyPQfAnfSwLF6VYjLhek?= =?utf-8?q?urshgRoo0NTlNNyhzDVg3wnN1xIIs+yokRy2Jx8mvauX99v7g7gwIm16Ev9xNxdyi?= =?utf-8?q?BECMiVdTdDPwVw0zyruPxm/+hCBTJ3E5GPd1YZLFtDsuGrnLKAz2puDqHYlYcAvFb?= =?utf-8?q?l7o/BXfo7jI2EU89bQ9m0f2/1o8xvGCchpc1gmYqByeJD6TY+GlqjqM62F06Jh9Iz?= =?utf-8?q?C+6PLDfQKvlq7fO1gD1YU8TPPUdDPjAA34Mz8RLTWS6au3e9K7L6bI9c=3D?= X-Forefront-Antispam-Report: CIP:192.176.1.74; CTRY:SE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:oa.msg.ericsson.com; PTR:office365.se.ericsson.net; CAT:NONE; SFS:(13230031)(1800799015)(376005)(36860700004)(82310400017); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2024 08:47:58.5242 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 92b89e72-5eff-4479-d809-08dc6ce01089 X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=92e84ceb-fbfd-47ab-be52-080c6b87953f; Ip=[192.176.1.74]; Helo=[oa.msg.ericsson.com] X-MS-Exchange-CrossTenant-AuthSource: AMS0EPF000001AA.eurprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR07MB7417 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 Extend bitops tests to cover the rte_bit_once_*() family of functions. RFC v5: * Atomic bit op implementation moved from this patch to the proper patch in the series. (Morten Brørup) RFC v4: * Remove redundant continuations. Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup Acked-by: Tyler Retzlaff --- app/test/test_bitops.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/test/test_bitops.c b/app/test/test_bitops.c index 322f58c066..9bffc4da14 100644 --- a/app/test/test_bitops.c +++ b/app/test/test_bitops.c @@ -61,6 +61,14 @@ GEN_TEST_BIT_ACCESS(test_bit_access32, rte_bit_set, rte_bit_clear, GEN_TEST_BIT_ACCESS(test_bit_access64, rte_bit_set, rte_bit_clear, rte_bit_assign, rte_bit_flip, rte_bit_test, 64) +GEN_TEST_BIT_ACCESS(test_bit_once_access32, rte_bit_once_set, + rte_bit_once_clear, rte_bit_once_assign, + rte_bit_once_flip, rte_bit_once_test, 32) + +GEN_TEST_BIT_ACCESS(test_bit_once_access64, rte_bit_once_set, + rte_bit_once_clear, rte_bit_once_assign, + rte_bit_once_flip, rte_bit_once_test, 64) + static uint32_t val32; static uint64_t val64; @@ -177,6 +185,8 @@ static struct unit_test_suite test_suite = { .unit_test_cases = { TEST_CASE(test_bit_access32), TEST_CASE(test_bit_access64), + TEST_CASE(test_bit_once_access32), + TEST_CASE(test_bit_once_access64), TEST_CASE(test_bit_relaxed_set), TEST_CASE(test_bit_relaxed_clear), TEST_CASE(test_bit_relaxed_test_set_clear), From patchwork Sun May 5 08:37:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 139870 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 23FCE43FAE; Sun, 5 May 2024 10:48:28 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 537E840395; Sun, 5 May 2024 10:48:06 +0200 (CEST) Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2057.outbound.protection.outlook.com [40.107.22.57]) by mails.dpdk.org (Postfix) with ESMTP id 37478402E4 for ; Sun, 5 May 2024 10:48:01 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UO9fVczkjoI3KCeYGta8lT8AKjwJD2FSjmXYTdyj3JOL2KypJHYljccqJHjJb6uqCHXT9eir1VtMBd+VtORkikqQGCwZ2x6JZN96NowbRNFNHWYDOh0PK4m4s5BpLe6OWN7LQ7RP3Rdyl6ddYGt3nYiLZtOIGCxbyv++1bXJtSIshn0pi9p3I5Vr2UwX05ZBmC0CxJncOUyBaYXtA+MY5eE+n4K7FN6ARjlGRrCvjZcvit8XQ64YsnOU7eThoPn7lkagXrt1l/r/kUzT3rgc1Gi1JpFmvvHiovC5OSrIA3MLfdgLXuV1vC641JujpgslsO21JpuSF4PqDCDz1hWCUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=pWRlgC0WG3OPI0HrFEOZKsI752eHZBpRWP+cxmiblQc=; b=K7fudXbDOgjvaFRT3p7osy+lwVvLrwbgj03RnlZwsOtLoxEfj5ieP6aaQpb5xUT7DVrZsmx43LieXic5yIzrvXvt2b8QV+VnvwEs7txto5J9gn5FO77bj/UpUrKWD0cvJtkHGoQnEs2R0QfgDnqsMN9NVa93w0yIeFsRI6Hd2j9Bpuhm5Ol9ncUZcVJNlK/w6G3vbpb/azPFXNiMgRK0925RSQ+8NowZPBW1FeoUg3uEakrkrirQ+A9rvo688r268dKAE1tCc5IXqs3BGh1N0Y5rnp7C1U/q3gMR2Nd3paJGL0FvqFDdNpgs4Xc7kLlLZSa/dmABzk4KJT9xoduKDA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 192.176.1.74) smtp.rcpttodomain=dpdk.org smtp.mailfrom=ericsson.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ericsson.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pWRlgC0WG3OPI0HrFEOZKsI752eHZBpRWP+cxmiblQc=; b=HRZrtDG9NtJ0chBcqtzN26SIse1F1zi8nYxjPtzCxg5jMRIMv6oQXDRL0M/EtYjlEpSTyb2/bbKXcsUDFVZRKIxw2OedhsK1pyUCha8mZ/UZKIL4ndAwkPNFfRK7vys0BPqiwLXvXRe7pfY3TvJtXLbG6dt6iYfHqQiTOjoASquKT9hSiQf0smeP3DlovkOww8yMoLk0UJ7XJAERXIzhOCKkb4CbGrQbVy13Zp5Usy/C7nEEToN2afHttfYv5qhmJuJbzf7/WPG523vEgJFX+kZjse3HmsbZrxXFUGAKiQw/zMLXgLPA5T6G6p2BeHyimXV2dBTQ/26QyZmHdxmKqA== Received: from DU7P195CA0005.EURP195.PROD.OUTLOOK.COM (2603:10a6:10:54d::24) by PAWPR07MB9853.eurprd07.prod.outlook.com (2603:10a6:102:38d::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.39; Sun, 5 May 2024 08:47:59 +0000 Received: from DB3PEPF0000885C.eurprd02.prod.outlook.com (2603:10a6:10:54d:cafe::ac) by DU7P195CA0005.outlook.office365.com (2603:10a6:10:54d::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.39 via Frontend Transport; Sun, 5 May 2024 08:47:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 192.176.1.74) smtp.mailfrom=ericsson.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ericsson.com; Received-SPF: Pass (protection.outlook.com: domain of ericsson.com designates 192.176.1.74 as permitted sender) receiver=protection.outlook.com; client-ip=192.176.1.74; helo=oa.msg.ericsson.com; pr=C Received: from oa.msg.ericsson.com (192.176.1.74) by DB3PEPF0000885C.mail.protection.outlook.com (10.167.242.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.18 via Frontend Transport; Sun, 5 May 2024 08:47:58 +0000 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.67) with Microsoft SMTP Server id 15.2.1544.9; Sun, 5 May 2024 10:47:58 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id 94B9138007A; Sun, 5 May 2024 10:47:58 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Morten_Br=C3=B8rup?= , =?utf-8?q?Mattia?= =?utf-8?q?s_R=C3=B6nnblom?= Subject: [RFC v7 5/6] eal: add atomic bit operations Date: Sun, 5 May 2024 10:37:36 +0200 Message-ID: <20240505083737.118649-6-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240505083737.118649-1-mattias.ronnblom@ericsson.com> References: <20240502055706.112443-2-mattias.ronnblom@ericsson.com> <20240505083737.118649-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB3PEPF0000885C:EE_|PAWPR07MB9853:EE_ X-MS-Office365-Filtering-Correlation-Id: 779eae21-991b-4122-f90a-08dc6ce010d5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|82310400017|36860700004|1800799015|376005; X-Microsoft-Antispam-Message-Info: =?utf-8?q?vNn7XEQhFBqKy40SLNjKDx9HsXWTfRe?= =?utf-8?q?WiGgWdqPKhOyP6O3wPaVYyfC/ZfzH5E4OKX9bIh2P1YCX0DekJZPL9XlM3X0W0tiF?= =?utf-8?q?ep/C79wMmI9Jvhe+vRpNIyu+KXfF3BAqsoa5JAAm+O1GR1ydRwtqm1KHzoFZcrrxJ?= =?utf-8?q?U0ilQ8CwaKgN8sKJecVaOCQ7jpgwcm2inhr8q+B8kWDxChKTub5Lvjm/wABjXZ9uj?= =?utf-8?q?5yiIdRX5TVW0xsH84S/19nLBgxmFf99X5xRH2JZcNzAxLJNKUQaMbI7xN3t6fd9un?= =?utf-8?q?m/wT+YPcB3OmvEGqia9DwABe2eMggGNijkNbzmtFXnFxO9691hglxbDVgm0KsIQiI?= =?utf-8?q?MrQajnhPbjd1FbJZIIzdr4Fx4A0w0xT+HqFW8SylNRJaY6SEGppw5/BCyJXnRyQDT?= =?utf-8?q?4harXRp3yDBowhsHpGzpHXpHtj7nudoa7L9EWmo6bXt9Ue2ds4UHUrh2HLGh2zT7c?= =?utf-8?q?y0QNpY13yS8OKaY1KIq84O0b36MiHJd5zATQ3SNkzuuBkGDGDKdIFG6zsAsBw2YdF?= =?utf-8?q?57MTwFGyID5X0pG2yEiFe9DAThwMMgvsqGRtAcLtmscXBspNh6WH+DfLlmvpZv4t5?= =?utf-8?q?jd1VbKD2iXBLYmMZqCUS84wklVgyTq/HbjwgjuJ7eT90GfsMRS4Gb9C2LA80HPYQw?= =?utf-8?q?vX5Nfx7A9bXlvP8alabi6UvfXvOl98uhWl9up4fwvzMX6iS7DQ/IrNdW9tNfMaKMJ?= =?utf-8?q?+2UpFKz3O2l42UGE8R2f3JtchcMh1d2CxK7jK/IGbpB3tT/zTVcZiBGI3B6ZOfZUF?= =?utf-8?q?Pfxf7OAzHEu5X+ma196cOZeOr84oo3Qk4s+OhVrkoURepO+K5wELIVT2/WywY+h1p?= =?utf-8?q?KWnej8K8Fc25lisH5W3Qv0YOvUj913L1qV/0u7mHbYTo2Vva+4OohdjoFIDndH1Pg?= =?utf-8?q?ZrrhkNu+Ct/tuFVXwphUsVboIWU5AFefAegsffuqn4qCeTGuWA15/ll9DCs8Uq4Aw?= =?utf-8?q?hXtRyEt/xLo6tDt8j5htmp+p3YA1Gd38g0ZaQVDMD2itP/svEVW3cnuxCL/PT1are?= =?utf-8?q?MVBPXmuBD9FE0zW+HPeScyzxKao7+zujAF505Jkas8FYLBTN7dnWvzAlKT5HLKw5y?= =?utf-8?q?Sewp7x9v+t87vyDnQ9wB47RslQW8H/45s3xR1poC9cv8pTVeiGvtMZjyLXAuX2YNN?= =?utf-8?q?tOtCG3IQBXOVYHFX0JmM5uhy5PsxqFALidtCn1K1ax2bQD8Q065uzCYwZaPkfdMu+?= =?utf-8?q?Fxg+h7Gaw1Zcp3E/n93TkRq6GJSQTkWXCf/+6v3xdJf+wPOwpNFxXBXFDrC4gZPQs?= =?utf-8?q?NOxoQRQbtkn47StdzUd7DjIOhGZ0ZzRyKzWjckQUbdEMCWQbCxwX47aXmQmNotiUR?= =?utf-8?q?sPWyu1dFxiOR?= X-Forefront-Antispam-Report: CIP:192.176.1.74; CTRY:SE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:oa.msg.ericsson.com; PTR:office365.se.ericsson.net; CAT:NONE; SFS:(13230031)(82310400017)(36860700004)(1800799015)(376005); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2024 08:47:58.9762 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 779eae21-991b-4122-f90a-08dc6ce010d5 X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=92e84ceb-fbfd-47ab-be52-080c6b87953f; Ip=[192.176.1.74]; Helo=[oa.msg.ericsson.com] X-MS-Exchange-CrossTenant-AuthSource: DB3PEPF0000885C.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR07MB9853 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 Add atomic bit test/set/clear/assign/flip and test-and-set/clear/assign/flip functions. All atomic bit functions allow (and indeed, require) the caller to specify a memory order. RFC v7: * Replace compare-exchange-based rte_bitset_atomic_test_and_*() and flip() with implementations that use the previous value as returned by the atomic fetch function. * Reword documentation to match the non-atomic macro variants. * Remove pointer to for memory model documentation, since there is no documentation for that API. RFC v6: * Have rte_bit_atomic_test() accept const-marked bitsets. RFC v4: * Add atomic bit flip. * Mark macro-generated private functions experimental. RFC v3: * Work around lack of C++ support for _Generic (Tyler Retzlaff). RFC v2: o Add rte_bit_atomic_test_and_assign() (for consistency). o Fix bugs in rte_bit_atomic_test_and_[set|clear](). o Use to support MSVC. Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup Acked-by: Tyler Retzlaff --- lib/eal/include/rte_bitops.h | 413 +++++++++++++++++++++++++++++++++++ 1 file changed, 413 insertions(+) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 3644aa115c..673b888c1a 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -21,6 +21,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -403,6 +404,204 @@ extern "C" { uint32_t *: __rte_bit_once_flip32, \ uint64_t *: __rte_bit_once_flip64)(addr, nr) +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Test if a particular bit in a word is set with a particular memory + * order. + * + * Test a bit with the resulting memory load ordered as per the + * specified memory order. + * + * @param addr + * A pointer to the word to query. + * @param nr + * The index of the bit. + * @param memory_order + * The memory order to use. + * @return + * Returns true if the bit is set, and false otherwise. + */ +#define rte_bit_atomic_test(addr, nr, memory_order) \ + _Generic((addr), \ + uint32_t *: __rte_bit_atomic_test32, \ + const uint32_t *: __rte_bit_atomic_test32, \ + uint64_t *: __rte_bit_atomic_test64, \ + const uint64_t *: __rte_bit_atomic_test64)(addr, nr, \ + memory_order) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Atomically set bit in word. + * + * Generic selection macro to atomically set bit specified by @c nr in + * the word pointed to by @c addr to '1', with the memory ordering as + * specified by @c memory_order. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param memory_order + * The memory order to use. + */ +#define rte_bit_atomic_set(addr, nr, memory_order) \ + _Generic((addr), \ + uint32_t *: __rte_bit_atomic_set32, \ + uint64_t *: __rte_bit_atomic_set64)(addr, nr, memory_order) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Atomically clear bit in word. + * + * Generic selection macro to atomically set bit specified by @c nr in + * the word pointed to by @c addr to '0', with the memory ordering as + * specified by @c memory_order. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param memory_order + * The memory order to use. + */ +#define rte_bit_atomic_clear(addr, nr, memory_order) \ + _Generic((addr), \ + uint32_t *: __rte_bit_atomic_clear32, \ + uint64_t *: __rte_bit_atomic_clear64)(addr, nr, memory_order) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Atomically assign a value to bit in word. + * + * Generic selection macro to atomically set bit specified by @c nr in the + * word pointed to by @c addr to the value indicated by @c value, with + * the memory ordering as specified with @c memory_order. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param value + * The new value of the bit - true for '1', or false for '0'. + * @param memory_order + * The memory order to use. + */ +#define rte_bit_atomic_assign(addr, nr, value, memory_order) \ + _Generic((addr), \ + uint32_t *: __rte_bit_atomic_assign32, \ + uint64_t *: __rte_bit_atomic_assign64)(addr, nr, value, \ + memory_order) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Atomically flip bit in word. + * + * Generic selection macro to atomically negate the value of the bit + * specified by @c nr in the word pointed to by @c addr to the value + * indicated by @c value, with the memory ordering as specified with + * @c memory_order. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param memory_order + * The memory order to use. + */ +#define rte_bit_atomic_flip(addr, nr, memory_order) \ + _Generic((addr), \ + uint32_t *: __rte_bit_atomic_flip32, \ + uint64_t *: __rte_bit_atomic_flip64)(addr, nr, memory_order) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Atomically test and set a bit in word. + * + * Generic selection macro to atomically test and set bit specified by + * @c nr in the word pointed to by @c addr to '1', with the memory + * ordering as specified with @c memory_order. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param memory_order + * The memory order to use. + * @return + * Returns true if the bit was set, and false otherwise. + */ +#define rte_bit_atomic_test_and_set(addr, nr, memory_order) \ + _Generic((addr), \ + uint32_t *: __rte_bit_atomic_test_and_set32, \ + uint64_t *: __rte_bit_atomic_test_and_set64)(addr, nr, \ + memory_order) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Atomically test and clear a bit in word. + * + * Generic selection macro to atomically test and clear bit specified + * by @c nr in the word pointed to by @c addr to '0', with the memory + * ordering as specified with @c memory_order. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param memory_order + * The memory order to use. + * @return + * Returns true if the bit was set, and false otherwise. + */ +#define rte_bit_atomic_test_and_clear(addr, nr, memory_order) \ + _Generic((addr), \ + uint32_t *: __rte_bit_atomic_test_and_clear32, \ + uint64_t *: __rte_bit_atomic_test_and_clear64)(addr, nr, \ + memory_order) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Atomically test and assign a bit in word. + * + * Generic selection macro to atomically test and assign bit specified + * by @c nr in the word pointed to by @c addr the value specified by + * @c value, with the memory ordering as specified with @c + * memory_order. + * + * @param addr + * A pointer to the word to modify. + * @param nr + * The index of the bit. + * @param value + * The new value of the bit - true for '1', or false for '0'. + * @param memory_order + * The memory order to use. + * @return + * Returns true if the bit was set, and false otherwise. + */ +#define rte_bit_atomic_test_and_assign(addr, nr, value, memory_order) \ + _Generic((addr), \ + uint32_t *: __rte_bit_atomic_test_and_assign32, \ + uint64_t *: __rte_bit_atomic_test_and_assign64)(addr, nr, \ + value, \ + memory_order) + #define __RTE_GEN_BIT_TEST(family, fun, qualifier, size) \ __rte_experimental \ static inline bool \ @@ -487,6 +686,145 @@ __RTE_GEN_BIT_CLEAR(once_, clear, volatile, 64) __RTE_GEN_BIT_ASSIGN(once_, assign, volatile, 64) __RTE_GEN_BIT_FLIP(once_, flip, volatile, 64) +#define __RTE_GEN_BIT_ATOMIC_TEST(size) \ + __rte_experimental \ + static inline bool \ + __rte_bit_atomic_test ## size(const uint ## size ## _t *addr, \ + unsigned int nr, int memory_order) \ + { \ + RTE_ASSERT(nr < size); \ + \ + const RTE_ATOMIC(uint ## size ## _t) *a_addr = \ + (const RTE_ATOMIC(uint ## size ## _t) *)addr; \ + uint ## size ## _t mask = (uint ## size ## _t)1 << nr; \ + return rte_atomic_load_explicit(a_addr, memory_order) & mask; \ + } + +#define __RTE_GEN_BIT_ATOMIC_SET(size) \ + __rte_experimental \ + static inline void \ + __rte_bit_atomic_set ## size(uint ## size ## _t *addr, \ + unsigned int nr, int memory_order) \ + { \ + RTE_ASSERT(nr < size); \ + \ + RTE_ATOMIC(uint ## size ## _t) *a_addr = \ + (RTE_ATOMIC(uint ## size ## _t) *)addr; \ + uint ## size ## _t mask = (uint ## size ## _t)1 << nr; \ + rte_atomic_fetch_or_explicit(a_addr, mask, memory_order); \ + } + +#define __RTE_GEN_BIT_ATOMIC_CLEAR(size) \ + __rte_experimental \ + static inline void \ + __rte_bit_atomic_clear ## size(uint ## size ## _t *addr, \ + unsigned int nr, int memory_order) \ + { \ + RTE_ASSERT(nr < size); \ + \ + RTE_ATOMIC(uint ## size ## _t) *a_addr = \ + (RTE_ATOMIC(uint ## size ## _t) *)addr; \ + uint ## size ## _t mask = (uint ## size ## _t)1 << nr; \ + rte_atomic_fetch_and_explicit(a_addr, ~mask, memory_order); \ + } + +#define __RTE_GEN_BIT_ATOMIC_FLIP(size) \ + __rte_experimental \ + static inline void \ + __rte_bit_atomic_flip ## size(uint ## size ## _t *addr, \ + unsigned int nr, int memory_order) \ + { \ + RTE_ASSERT(nr < size); \ + \ + RTE_ATOMIC(uint ## size ## _t) *a_addr = \ + (RTE_ATOMIC(uint ## size ## _t) *)addr; \ + uint ## size ## _t mask = (uint ## size ## _t)1 << nr; \ + rte_atomic_fetch_xor_explicit(a_addr, mask, memory_order); \ + } + +#define __RTE_GEN_BIT_ATOMIC_ASSIGN(size) \ + __rte_experimental \ + static inline void \ + __rte_bit_atomic_assign ## size(uint ## size ## _t *addr, \ + unsigned int nr, bool value, \ + int memory_order) \ + { \ + if (value) \ + __rte_bit_atomic_set ## size(addr, nr, memory_order); \ + else \ + __rte_bit_atomic_clear ## size(addr, nr, \ + memory_order); \ + } + +#define __RTE_GEN_BIT_ATOMIC_TEST_AND_SET(size) \ + __rte_experimental \ + static inline bool \ + __rte_bit_atomic_test_and_set ## size(uint ## size ## _t *addr, \ + unsigned int nr, \ + int memory_order) \ + { \ + RTE_ASSERT(nr < size); \ + \ + RTE_ATOMIC(uint ## size ## _t) *a_addr = \ + (RTE_ATOMIC(uint ## size ## _t) *)addr; \ + uint ## size ## _t mask = (uint ## size ## _t)1 << nr; \ + uint ## size ## _t prev; \ + \ + prev = rte_atomic_fetch_or_explicit(a_addr, mask, \ + memory_order); \ + \ + return prev & mask; \ + } + +#define __RTE_GEN_BIT_ATOMIC_TEST_AND_CLEAR(size) \ + __rte_experimental \ + static inline bool \ + __rte_bit_atomic_test_and_clear ## size(uint ## size ## _t *addr, \ + unsigned int nr, \ + int memory_order) \ + { \ + RTE_ASSERT(nr < size); \ + \ + RTE_ATOMIC(uint ## size ## _t) *a_addr = \ + (RTE_ATOMIC(uint ## size ## _t) *)addr; \ + uint ## size ## _t mask = (uint ## size ## _t)1 << nr; \ + uint ## size ## _t prev; \ + \ + prev = rte_atomic_fetch_and_explicit(a_addr, ~mask, \ + memory_order); \ + \ + return prev & mask; \ + } + +#define __RTE_GEN_BIT_ATOMIC_TEST_AND_ASSIGN(size) \ + __rte_experimental \ + static inline bool \ + __rte_bit_atomic_test_and_assign ## size(uint ## size ## _t *addr, \ + unsigned int nr, \ + bool value, \ + int memory_order) \ + { \ + if (value) \ + return __rte_bit_atomic_test_and_set ## size(addr, nr, \ + memory_order); \ + else \ + return __rte_bit_atomic_test_and_clear ## size(addr, nr, \ + memory_order); \ + } + +#define __RTE_GEN_BIT_ATOMIC_OPS(size) \ + __RTE_GEN_BIT_ATOMIC_TEST(size) \ + __RTE_GEN_BIT_ATOMIC_SET(size) \ + __RTE_GEN_BIT_ATOMIC_CLEAR(size) \ + __RTE_GEN_BIT_ATOMIC_ASSIGN(size) \ + __RTE_GEN_BIT_ATOMIC_TEST_AND_SET(size) \ + __RTE_GEN_BIT_ATOMIC_TEST_AND_CLEAR(size) \ + __RTE_GEN_BIT_ATOMIC_TEST_AND_ASSIGN(size) \ + __RTE_GEN_BIT_ATOMIC_FLIP(size) + +__RTE_GEN_BIT_ATOMIC_OPS(32) +__RTE_GEN_BIT_ATOMIC_OPS(64) + /*------------------------ 32-bit relaxed operations ------------------------*/ /** @@ -1188,6 +1526,14 @@ rte_log2_u64(uint64_t v) #undef rte_bit_once_assign #undef rte_bit_once_flip +#undef rte_bit_atomic_test +#undef rte_bit_atomic_set +#undef rte_bit_atomic_clear +#undef rte_bit_atomic_assign +#undef rte_bit_atomic_test_and_set +#undef rte_bit_atomic_test_and_clear +#undef rte_bit_atomic_test_and_assign + #define __RTE_BIT_OVERLOAD_SZ_2(fun, qualifier, size, arg1_type, arg1_name) \ static inline void \ rte_bit_ ## fun(qualifier uint ## size ## _t *addr, \ @@ -1231,6 +1577,59 @@ rte_log2_u64(uint64_t v) __RTE_BIT_OVERLOAD_SZ_3(fun, qualifier, 64, arg1_type, arg1_name, \ arg2_type, arg2_name) +#define __RTE_BIT_OVERLOAD_SZ_3R(fun, qualifier, size, ret_type, arg1_type, \ + arg1_name, arg2_type, arg2_name) \ + static inline ret_type \ + rte_bit_ ## fun(uint ## size ## _t *addr, arg1_type arg1_name, \ + arg2_type arg2_name) \ + { \ + return __rte_bit_ ## fun ## size(addr, arg1_name, arg2_name); \ + } + +#define __RTE_BIT_OVERLOAD_3R(fun, qualifier, ret_type, arg1_type, arg1_name, \ + arg2_type, arg2_name) \ + __RTE_BIT_OVERLOAD_SZ_3R(fun, qualifier, 32, ret_type, arg1_type, \ + arg1_name, arg2_type, arg2_name) \ + __RTE_BIT_OVERLOAD_SZ_3R(fun, qualifier, 64, ret_type, arg1_type, \ + arg1_name, arg2_type, arg2_name) + +#define __RTE_BIT_OVERLOAD_SZ_4(fun, qualifier, size, arg1_type, arg1_name, \ + arg2_type, arg2_name, arg3_type, arg3_name) \ + static inline void \ + rte_bit_ ## fun(uint ## size ## _t *addr, arg1_type arg1_name, \ + arg2_type arg2_name, arg3_type arg3_name) \ + { \ + __rte_bit_ ## fun ## size(addr, arg1_name, arg2_name, \ + arg3_name); \ + } + +#define __RTE_BIT_OVERLOAD_4(fun, qualifier, arg1_type, arg1_name, arg2_type, \ + arg2_name, arg3_type, arg3_name) \ + __RTE_BIT_OVERLOAD_SZ_4(fun, qualifier, 32, arg1_type, arg1_name, \ + arg2_type, arg2_name, arg3_type, arg3_name) \ + __RTE_BIT_OVERLOAD_SZ_4(fun, qualifier, 64, arg1_type, arg1_name, \ + arg2_type, arg2_name, arg3_type, arg3_name) + +#define __RTE_BIT_OVERLOAD_SZ_4R(fun, qualifier, size, ret_type, arg1_type, \ + arg1_name, arg2_type, arg2_name, arg3_type, \ + arg3_name) \ + static inline ret_type \ + rte_bit_ ## fun(uint ## size ## _t *addr, arg1_type arg1_name, \ + arg2_type arg2_name, arg3_type arg3_name) \ + { \ + return __rte_bit_ ## fun ## size(addr, arg1_name, arg2_name, \ + arg3_name); \ + } + +#define __RTE_BIT_OVERLOAD_4R(fun, qualifier, ret_type, arg1_type, arg1_name, \ + arg2_type, arg2_name, arg3_type, arg3_name) \ + __RTE_BIT_OVERLOAD_SZ_4R(fun, qualifier, 32, ret_type, arg1_type, \ + arg1_name, arg2_type, arg2_name, arg3_type, \ + arg3_name) \ + __RTE_BIT_OVERLOAD_SZ_4R(fun, qualifier, 64, ret_type, arg1_type, \ + arg1_name, arg2_type, arg2_name, arg3_type, \ + arg3_name) + __RTE_BIT_OVERLOAD_2R(test, const, bool, unsigned int, nr) __RTE_BIT_OVERLOAD_2(set,, unsigned int, nr) __RTE_BIT_OVERLOAD_2(clear,, unsigned int, nr) @@ -1243,6 +1642,20 @@ __RTE_BIT_OVERLOAD_2(once_clear, volatile, unsigned int, nr) __RTE_BIT_OVERLOAD_3(once_assign, volatile, unsigned int, nr, bool, value) __RTE_BIT_OVERLOAD_2(once_flip, volatile, unsigned int, nr) +__RTE_BIT_OVERLOAD_3R(atomic_test, const, bool, unsigned int, nr, + int, memory_order) +__RTE_BIT_OVERLOAD_3(atomic_set,, unsigned int, nr, int, memory_order) +__RTE_BIT_OVERLOAD_3(atomic_clear,, unsigned int, nr, int, memory_order) +__RTE_BIT_OVERLOAD_4(atomic_assign,, unsigned int, nr, bool, value, + int, memory_order) +__RTE_BIT_OVERLOAD_3(atomic_flip,, unsigned int, nr, int, memory_order) +__RTE_BIT_OVERLOAD_3R(atomic_test_and_set,, bool, unsigned int, nr, + int, memory_order) +__RTE_BIT_OVERLOAD_3R(atomic_test_and_clear,, bool, unsigned int, nr, + int, memory_order) +__RTE_BIT_OVERLOAD_4R(atomic_test_and_assign,, bool, unsigned int, nr, + bool, value, int, memory_order) + #endif #endif /* _RTE_BITOPS_H_ */ From patchwork Sun May 5 08:37:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Mattias_R=C3=B6nnblom?= X-Patchwork-Id: 139872 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9A0A143FAE; Sun, 5 May 2024 10:48:42 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 030E940685; Sun, 5 May 2024 10:48:09 +0200 (CEST) Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-he1eur01on2085.outbound.protection.outlook.com [40.107.13.85]) by mails.dpdk.org (Postfix) with ESMTP id EB7A2402E7 for ; Sun, 5 May 2024 10:48:01 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V6eP1NvJlxAW9/h5to43iwkvXc5FwCJIdmqL6dxYi08vtF+0rLQIP92LxXgaycBeu9I2SiHRIReDf2h1zPGIEdq6GYonMt4dLA9SKDNasAP7Oh3Fv94U0Ced3KC5eJroQwqQAPrt0Txu/Yxq+sbViTpd0lf2pDauhJEzHZz98q4J5TcW3g+yzek+5gtOvvTCn9TIIElXSQ6FLxFzqKfcOGoU/ExJedCt219yNz3LNPygpGkDwdJ32eyq5HiaSraEU0NiMiRIH5R9ENTByCDDCxUWnF3FNOkYAs7xdiYg8J+e/5x8eKICMU8JBsaKhqagcLlEWWGoe1W8KWXd7kqfSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9OUelsYpMkkuM3XvGHV53aJb4v8YWs53DjmkXfumHoc=; b=A+gbuZs4Sw/U4l9ILTJ1HYElyriZ07mqsVo2PN+FwIBbcIhY+db2HSvJyfNsnOBaVGWBRXMauKZEatrvVGmKYKFnvh3qZjJLIdapr8R/AG2BnTJxEbzugqucJFoo38HAN/5i6yEpccJCEjoELPuUoRX4JC0p55u2qp30aH3uWLlRU7DdSWYi228nIIiSLjEDir2wfu8WhQeIyj/vCwckoSYXUChQKUVMl5hSJV3EDmujQGmE1w2zSCvKZSsbKBhBKzTwEK+jL5afYkBnjjTFWPZsuyAFXuhjp/fQzpNLBo5qgQTtTv1jvYo79KmfHHu5a/RiMzW3hdbNQ3pZ3uDsrg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 192.176.1.74) smtp.rcpttodomain=dpdk.org smtp.mailfrom=ericsson.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ericsson.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9OUelsYpMkkuM3XvGHV53aJb4v8YWs53DjmkXfumHoc=; b=s0lgSk171Nc4RWDWE8H0gi4nMrep/7cfBozUSOO4UznmNFTH0wN2z8ZOjdj2/b6V/7NeuHbzKDblrQ+DhinSPChTrjDHyv1V5oS/WlPQtt57XXlIQKAw5DOhWgX+tgJuv7aBmjM1t96nNXJIY2HjKsuL8O1B5RbjF3mpjvR42xz/F1fPDPX/eIrW4hO0gPQeyXc9f5D9c4u/XfT0XBXV/mnHVpLH8l1RXI6r037c3wgY79DWEhXFEIviTadYHPkGR7u095DxrWAtdO/ko5spS2SpynEIHsAIm2NzY+xezZASyvJydslkx0gxLfy5wNT4z7VqoN5NBWyCcAaAUb9bNw== Received: from DB3PR08CA0028.eurprd08.prod.outlook.com (2603:10a6:8::41) by AM7PR07MB7009.eurprd07.prod.outlook.com (2603:10a6:20b:1b0::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.39; Sun, 5 May 2024 08:48:00 +0000 Received: from DU2PEPF0001E9C4.eurprd03.prod.outlook.com (2603:10a6:8:0:cafe::2b) by DB3PR08CA0028.outlook.office365.com (2603:10a6:8::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.40 via Frontend Transport; Sun, 5 May 2024 08:48:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 192.176.1.74) smtp.mailfrom=ericsson.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=ericsson.com; Received-SPF: Pass (protection.outlook.com: domain of ericsson.com designates 192.176.1.74 as permitted sender) receiver=protection.outlook.com; client-ip=192.176.1.74; helo=oa.msg.ericsson.com; pr=C Received: from oa.msg.ericsson.com (192.176.1.74) by DU2PEPF0001E9C4.mail.protection.outlook.com (10.167.8.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.18 via Frontend Transport; Sun, 5 May 2024 08:47:59 +0000 Received: from seliicinfr00049.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.64) with Microsoft SMTP Server id 15.2.1544.9; Sun, 5 May 2024 10:47:59 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00049.seli.gic.ericsson.se (Postfix) with ESMTP id 220FD38007A; Sun, 5 May 2024 10:47:59 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Morten_Br=C3=B8rup?= , =?utf-8?q?Mattia?= =?utf-8?q?s_R=C3=B6nnblom?= Subject: [RFC v7 6/6] eal: add unit tests for atomic bit access functions Date: Sun, 5 May 2024 10:37:37 +0200 Message-ID: <20240505083737.118649-7-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240505083737.118649-1-mattias.ronnblom@ericsson.com> References: <20240502055706.112443-2-mattias.ronnblom@ericsson.com> <20240505083737.118649-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF0001E9C4:EE_|AM7PR07MB7009:EE_ X-MS-Office365-Filtering-Correlation-Id: 3d80fdf8-c30c-45c0-d2db-08dc6ce01143 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|82310400017|36860700004|1800799015|376005; X-Microsoft-Antispam-Message-Info: =?utf-8?q?vklzcXrI4UFVGuGMyEI2ODSddqmnSdU?= =?utf-8?q?CZEPUGhHZEzzhU0nsnL2Bahsk1fJtPCbOC6MGgMM9Fsb1cpbVKu9smHnChLE8DwZ5?= =?utf-8?q?EGMAc19nSJcHIUwro57vHo1DqIzx6qxTel802VuTZBEbofKAS+/0MiKTf7T+iZLjY?= =?utf-8?q?m81yoJ7wzoYGaEf4rRmbU2dcJiqFNvwU0VVMbM0mTgN+DDemCSaHloY/0GbmZH71Y?= =?utf-8?q?7+BLqBHHDdVhMHURnnU7UOtBjRanGUF6EmvEFECikk/WC6ZuGIq2YZVCTDQSBFEzn?= =?utf-8?q?j25KZheD3dXqY5l2KGeBJXANA5R6LKinegn+9MEMYoNeHP4rzbH/0k16jwM9RMRjd?= =?utf-8?q?goz+6PY1ibn1qe36s/Ak2Y46R7/bTz4Ctwp7hR9adNE7bi/BZZ437bBpO5LI5DZMB?= =?utf-8?q?UJPuDF9zfJ6K81oSAnEXEK57gK/2QOkT0wX2B58YLHEUy88Bk4Uooeh6aM8XUsnuC?= =?utf-8?q?LYHcLP4t0bHlMMCllTxaepBWR6YvWr96vwCsx98aZgnd0wUU1RbBaRs4/pPxi5QlS?= =?utf-8?q?sUhVKXr5NmD/hyeu1jT4InJkkCQYHbi90T8tBJmgN8Jo5QjBsldMuzv63OF/QPq9v?= =?utf-8?q?SRh/yB846um8gVxXjdSGXtHvMH2ufHagU4rGnS1uxCgaEqH5ypijsbLXXZleXJrLS?= =?utf-8?q?LaNalRivW41AGviWMgS4OLGXbTwCAP2VnfCz4rymxYo28a6zhMK/WL7LdaYjOxQWw?= =?utf-8?q?dLoaY5FkgWgLWN5KJuSh0BKw3X6V0CjPJTpaO8t+UqgQhgme5NgvFNwxTvjcm4L6z?= =?utf-8?q?Hk1f9IHg+i2NUERC5RPZ4RQ1Cn2yGdn2gLIl/9u0QobJJ+txLgjYQkaVieIjUpr01?= =?utf-8?q?J63/mrcN23qr2gOeMokEKnU9DLeQkJ4/5B3S9S2AEz0cJkLbGaxlXqWrAwulD16ST?= =?utf-8?q?9Zw2js/F60/kTH6uOUkITts3+Ms5eT18T1oESu70xqrtHHomKH4+rsvVMLcD3wGI7?= =?utf-8?q?+kfHJQohBMokSXoGUhrlgZ7TeYGcqsizTjEd23g9roVyOoZBwKvTiBECjWCjdbSK5?= =?utf-8?q?foghtfCU50btpyCB3lLVWMJAEyeSUf7LKvocuqARz//yUwaJcx9TI+3eBpH3YFN6y?= =?utf-8?q?qOn1rGNE7r1AxJY5QBHacK5xXS1jZbIB8wRPVxvfCvtFcv5vPnWT+RXOFOctaVPfY?= =?utf-8?q?nd3Ej9Qx6Yrb7NljlUFFhHghlcezABbQqXw20AdY9IW/FlH9odGJ85SIrDlhvDT5/?= =?utf-8?q?hOUSAC1SkuFNt23fOP/Y/bE7411CIh7wQjTbaigggMtz77cDCDq82MEk3FxL2/ycS?= =?utf-8?q?qEOsBN+sGoetBW9w9IbwAttlt3s2aLnoTo9WWwcr51T5v3ZCU8dcLN5e+ZzlvQpoG?= =?utf-8?q?PKk2jg6K1ind?= X-Forefront-Antispam-Report: CIP:192.176.1.74; CTRY:SE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:oa.msg.ericsson.com; PTR:office365.se.ericsson.net; CAT:NONE; SFS:(13230031)(82310400017)(36860700004)(1800799015)(376005); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2024 08:47:59.6961 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3d80fdf8-c30c-45c0-d2db-08dc6ce01143 X-MS-Exchange-CrossTenant-Id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=92e84ceb-fbfd-47ab-be52-080c6b87953f; Ip=[192.176.1.74]; Helo=[oa.msg.ericsson.com] X-MS-Exchange-CrossTenant-AuthSource: DU2PEPF0001E9C4.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR07MB7009 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 Extend bitops tests to cover the rte_bit_atomic_*() family of functions. RFC v4: * Add atomicity test for atomic bit flip. RFC v3: * Rename variable 'main' to make ICC happy. Signed-off-by: Mattias Rönnblom Acked-by: Morten Brørup Acked-by: Tyler Retzlaff --- app/test/test_bitops.c | 315 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 314 insertions(+), 1 deletion(-) diff --git a/app/test/test_bitops.c b/app/test/test_bitops.c index 9bffc4da14..c86d7e1f77 100644 --- a/app/test/test_bitops.c +++ b/app/test/test_bitops.c @@ -3,10 +3,13 @@ * Copyright(c) 2024 Ericsson AB */ +#include #include -#include #include +#include +#include +#include #include #include "test.h" @@ -69,6 +72,304 @@ GEN_TEST_BIT_ACCESS(test_bit_once_access64, rte_bit_once_set, rte_bit_once_clear, rte_bit_once_assign, rte_bit_once_flip, rte_bit_once_test, 64) +#define bit_atomic_set(addr, nr) \ + rte_bit_atomic_set(addr, nr, rte_memory_order_relaxed) + +#define bit_atomic_clear(addr, nr) \ + rte_bit_atomic_clear(addr, nr, rte_memory_order_relaxed) + +#define bit_atomic_assign(addr, nr, value) \ + rte_bit_atomic_assign(addr, nr, value, rte_memory_order_relaxed) + +#define bit_atomic_flip(addr, nr) \ + rte_bit_atomic_flip(addr, nr, rte_memory_order_relaxed) + +#define bit_atomic_test(addr, nr) \ + rte_bit_atomic_test(addr, nr, rte_memory_order_relaxed) + +GEN_TEST_BIT_ACCESS(test_bit_atomic_access32, bit_atomic_set, + bit_atomic_clear, bit_atomic_assign, + bit_atomic_flip, bit_atomic_test, 32) + +GEN_TEST_BIT_ACCESS(test_bit_atomic_access64, bit_atomic_set, + bit_atomic_clear, bit_atomic_assign, + bit_atomic_flip, bit_atomic_test, 64) + +#define PARALLEL_TEST_RUNTIME 0.25 + +#define GEN_TEST_BIT_PARALLEL_ASSIGN(size) \ + \ + struct parallel_access_lcore ## size \ + { \ + unsigned int bit; \ + uint ## size ##_t *word; \ + bool failed; \ + }; \ + \ + static int \ + run_parallel_assign ## size(void *arg) \ + { \ + struct parallel_access_lcore ## size *lcore = arg; \ + uint64_t deadline = rte_get_timer_cycles() + \ + PARALLEL_TEST_RUNTIME * rte_get_timer_hz(); \ + bool value = false; \ + \ + do { \ + bool new_value = rte_rand() & 1; \ + bool use_test_and_modify = rte_rand() & 1; \ + bool use_assign = rte_rand() & 1; \ + \ + if (rte_bit_atomic_test(lcore->word, lcore->bit, \ + rte_memory_order_relaxed) != value) { \ + lcore->failed = true; \ + break; \ + } \ + \ + if (use_test_and_modify) { \ + bool old_value; \ + if (use_assign) \ + old_value = rte_bit_atomic_test_and_assign( \ + lcore->word, lcore->bit, new_value, \ + rte_memory_order_relaxed); \ + else { \ + old_value = new_value ? \ + rte_bit_atomic_test_and_set( \ + lcore->word, lcore->bit, \ + rte_memory_order_relaxed) : \ + rte_bit_atomic_test_and_clear( \ + lcore->word, lcore->bit, \ + rte_memory_order_relaxed); \ + } \ + if (old_value != value) { \ + lcore->failed = true; \ + break; \ + } \ + } else { \ + if (use_assign) \ + rte_bit_atomic_assign(lcore->word, lcore->bit, \ + new_value, \ + rte_memory_order_relaxed); \ + else { \ + if (new_value) \ + rte_bit_atomic_set( \ + lcore->word, lcore->bit, \ + rte_memory_order_relaxed); \ + else \ + rte_bit_atomic_clear( \ + lcore->word, lcore->bit, \ + rte_memory_order_relaxed); \ + } \ + } \ + \ + value = new_value; \ + } while (rte_get_timer_cycles() < deadline); \ + \ + return 0; \ + } \ + \ + static int \ + test_bit_atomic_parallel_assign ## size(void) \ + { \ + unsigned int worker_lcore_id; \ + uint ## size ## _t word = 0; \ + struct parallel_access_lcore ## size lmain = { \ + .word = &word \ + }; \ + struct parallel_access_lcore ## size lworker = { \ + .word = &word \ + }; \ + \ + if (rte_lcore_count() < 2) { \ + printf("Need multiple cores to run parallel test.\n"); \ + return TEST_SKIPPED; \ + } \ + \ + worker_lcore_id = rte_get_next_lcore(-1, 1, 0); \ + \ + lmain.bit = rte_rand_max(size); \ + do { \ + lworker.bit = rte_rand_max(size); \ + } while (lworker.bit == lmain.bit); \ + \ + int rc = rte_eal_remote_launch(run_parallel_assign ## size, \ + &lworker, worker_lcore_id); \ + TEST_ASSERT(rc == 0, "Worker thread launch failed"); \ + \ + run_parallel_assign ## size(&lmain); \ + \ + rte_eal_mp_wait_lcore(); \ + \ + TEST_ASSERT(!lmain.failed, "Main lcore atomic access failed"); \ + TEST_ASSERT(!lworker.failed, "Worker lcore atomic access " \ + "failed"); \ + \ + return TEST_SUCCESS; \ + } + +GEN_TEST_BIT_PARALLEL_ASSIGN(32) +GEN_TEST_BIT_PARALLEL_ASSIGN(64) + +#define GEN_TEST_BIT_PARALLEL_TEST_AND_MODIFY(size) \ + \ + struct parallel_test_and_set_lcore ## size \ + { \ + uint ## size ##_t *word; \ + unsigned int bit; \ + uint64_t flips; \ + }; \ + \ + static int \ + run_parallel_test_and_modify ## size(void *arg) \ + { \ + struct parallel_test_and_set_lcore ## size *lcore = arg; \ + uint64_t deadline = rte_get_timer_cycles() + \ + PARALLEL_TEST_RUNTIME * rte_get_timer_hz(); \ + do { \ + bool old_value; \ + bool new_value = rte_rand() & 1; \ + bool use_assign = rte_rand() & 1; \ + \ + if (use_assign) \ + old_value = rte_bit_atomic_test_and_assign( \ + lcore->word, lcore->bit, new_value, \ + rte_memory_order_relaxed); \ + else \ + old_value = new_value ? \ + rte_bit_atomic_test_and_set( \ + lcore->word, lcore->bit, \ + rte_memory_order_relaxed) : \ + rte_bit_atomic_test_and_clear( \ + lcore->word, lcore->bit, \ + rte_memory_order_relaxed); \ + if (old_value != new_value) \ + lcore->flips++; \ + } while (rte_get_timer_cycles() < deadline); \ + \ + return 0; \ + } \ + \ + static int \ + test_bit_atomic_parallel_test_and_modify ## size(void) \ + { \ + unsigned int worker_lcore_id; \ + uint ## size ## _t word = 0; \ + unsigned int bit = rte_rand_max(size); \ + struct parallel_test_and_set_lcore ## size lmain = { \ + .word = &word, \ + .bit = bit \ + }; \ + struct parallel_test_and_set_lcore ## size lworker = { \ + .word = &word, \ + .bit = bit \ + }; \ + \ + if (rte_lcore_count() < 2) { \ + printf("Need multiple cores to run parallel test.\n"); \ + return TEST_SKIPPED; \ + } \ + \ + worker_lcore_id = rte_get_next_lcore(-1, 1, 0); \ + \ + int rc = rte_eal_remote_launch(run_parallel_test_and_modify ## size, \ + &lworker, worker_lcore_id); \ + TEST_ASSERT(rc == 0, "Worker thread launch failed"); \ + \ + run_parallel_test_and_modify ## size(&lmain); \ + \ + rte_eal_mp_wait_lcore(); \ + \ + uint64_t total_flips = lmain.flips + lworker.flips; \ + bool expected_value = total_flips % 2; \ + \ + TEST_ASSERT(expected_value == rte_bit_test(&word, bit), \ + "After %"PRId64" flips, the bit value " \ + "should be %d", total_flips, expected_value); \ + \ + uint64_t expected_word = 0; \ + rte_bit_assign(&expected_word, bit, expected_value); \ + \ + TEST_ASSERT(expected_word == word, "Untouched bits have " \ + "changed value"); \ + \ + return TEST_SUCCESS; \ + } + +GEN_TEST_BIT_PARALLEL_TEST_AND_MODIFY(32) +GEN_TEST_BIT_PARALLEL_TEST_AND_MODIFY(64) + +#define GEN_TEST_BIT_PARALLEL_FLIP(size) \ + \ + struct parallel_flip_lcore ## size \ + { \ + uint ## size ##_t *word; \ + unsigned int bit; \ + uint64_t flips; \ + }; \ + \ + static int \ + run_parallel_flip ## size(void *arg) \ + { \ + struct parallel_flip_lcore ## size *lcore = arg; \ + uint64_t deadline = rte_get_timer_cycles() + \ + PARALLEL_TEST_RUNTIME * rte_get_timer_hz(); \ + do { \ + rte_bit_atomic_flip(lcore->word, lcore->bit, \ + rte_memory_order_relaxed); \ + lcore->flips++; \ + } while (rte_get_timer_cycles() < deadline); \ + \ + return 0; \ + } \ + \ + static int \ + test_bit_atomic_parallel_flip ## size(void) \ + { \ + unsigned int worker_lcore_id; \ + uint ## size ## _t word = 0; \ + unsigned int bit = rte_rand_max(size); \ + struct parallel_flip_lcore ## size lmain = { \ + .word = &word, \ + .bit = bit \ + }; \ + struct parallel_flip_lcore ## size lworker = { \ + .word = &word, \ + .bit = bit \ + }; \ + \ + if (rte_lcore_count() < 2) { \ + printf("Need multiple cores to run parallel test.\n"); \ + return TEST_SKIPPED; \ + } \ + \ + worker_lcore_id = rte_get_next_lcore(-1, 1, 0); \ + \ + int rc = rte_eal_remote_launch(run_parallel_flip ## size, \ + &lworker, worker_lcore_id); \ + TEST_ASSERT(rc == 0, "Worker thread launch failed"); \ + \ + run_parallel_flip ## size(&lmain); \ + \ + rte_eal_mp_wait_lcore(); \ + \ + uint64_t total_flips = lmain.flips + lworker.flips; \ + bool expected_value = total_flips % 2; \ + \ + TEST_ASSERT(expected_value == rte_bit_test(&word, bit), \ + "After %"PRId64" flips, the bit value " \ + "should be %d", total_flips, expected_value); \ + \ + uint64_t expected_word = 0; \ + rte_bit_assign(&expected_word, bit, expected_value); \ + \ + TEST_ASSERT(expected_word == word, "Untouched bits have " \ + "changed value"); \ + \ + return TEST_SUCCESS; \ + } + +GEN_TEST_BIT_PARALLEL_FLIP(32) +GEN_TEST_BIT_PARALLEL_FLIP(64) + static uint32_t val32; static uint64_t val64; @@ -187,6 +488,18 @@ static struct unit_test_suite test_suite = { TEST_CASE(test_bit_access64), TEST_CASE(test_bit_once_access32), TEST_CASE(test_bit_once_access64), + TEST_CASE(test_bit_access32), + TEST_CASE(test_bit_access64), + TEST_CASE(test_bit_once_access32), + TEST_CASE(test_bit_once_access64), + TEST_CASE(test_bit_atomic_access32), + TEST_CASE(test_bit_atomic_access64), + TEST_CASE(test_bit_atomic_parallel_assign32), + TEST_CASE(test_bit_atomic_parallel_assign64), + TEST_CASE(test_bit_atomic_parallel_test_and_modify32), + TEST_CASE(test_bit_atomic_parallel_test_and_modify64), + TEST_CASE(test_bit_atomic_parallel_flip32), + TEST_CASE(test_bit_atomic_parallel_flip64), TEST_CASE(test_bit_relaxed_set), TEST_CASE(test_bit_relaxed_clear), TEST_CASE(test_bit_relaxed_test_set_clear),