From patchwork Thu Apr 25 08:58:48 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: 139676 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 3A98843F04; Thu, 25 Apr 2024 11:10:29 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C951E436D7; Thu, 25 Apr 2024 11:10:06 +0200 (CEST) Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-ve1eur01on2053.outbound.protection.outlook.com [40.107.14.53]) by mails.dpdk.org (Postfix) with ESMTP id 14E2B436CA for ; Thu, 25 Apr 2024 11:10:02 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fb3Ia1NEds0aUZtpL4CecTfidWrMGAqc0qF1XF6HwsnDWS9vK+Ns3juIAtXOabLX40Vdo8Hu+zS2lytTSEVWMPvR49crDdQArh1NTv7oBFWj/InkS3Ycitz0f5cXB9+h5BQUZ9/rMwlsR9Vjpf1ORd3IhLGi00yTyjq2uXqNsbyxVFC0F7reJ+fVj/lVlaI/cORHrpMeeWR9kz0DbOtSCWMcJpV8k1859oQ30bHu1MxNFeIJ6qyxtoQ7o21z1trtJu8m4PLMVbxVzEXhFiPqU8phDhsl+ag9HWv2J83jz+gDwdzX5kF/Vj9wLigNITjo66SCLBn+8Nqjg0+pg+CaEw== 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=MkX7rlJaLUWKCpLft5kVGlkg8JQFoluRS01LeOWajQQ=; b=nglyaNIXDY+1vrLlkQMCDngPRRcxqYGjJ/zvgYo/TKrHwCdoBGQ5JAmOJ51wDohm4LuRYKy9CsDh2fEy0NyVh15WIT9eC8x1xKQ8jFYTrih44NYyBT1Ibwh/5LzJe4gZ3NVnYYxQwew7gQrww7qci+OJGQBXInsbKNpU++N9HsVZSCwtnCbZaB9U9V5QSa1rFtTwiSh65DOgYzDnaD821aWnla9qxxb+8izIJ9WQpHHXOJuMSGJ4rjtc/rAbHWGY7Z4WScKL14jKmySE32KCcCIPnDUp6vWhQtEdDT1+YxOjHoV9EcaeKmldOlb8ucCxbUQFzSKs2bz+Y03WKXWZ2A== 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=MkX7rlJaLUWKCpLft5kVGlkg8JQFoluRS01LeOWajQQ=; b=cQDmYLzSS/FbKBCQBj46gkThQJwY9y53wL8FeFze1gMShhzq7tqfibbzeF6euXXaPe4YyWCFeatngS6Y1iyAcYGgR2HmKVERcfgb8EbyQOF+SW/QzNJEikVsk7klkHj5CkwdbFxf0t/Dh/zkU5PIOIa3klLh465M0LzakioCk4dWa5Q9CAyPpqfAN2ysN5TqSjzEAg2oI5jGiGWTXSJa9E8qHAK5qwkh5pDMnVW1CwVayie9ibseJapKSiwDneKP4ajc8zF2h17EMgpIu1ODUIIK8EqDk0ff+amXWLrNU+hXxZQDP6h4NPbC11Sec4lRZDFDVS5kK+eWgaryo2A7yA== Received: from AM7PR02CA0011.eurprd02.prod.outlook.com (2603:10a6:20b:100::21) by AM8PR07MB8041.eurprd07.prod.outlook.com (2603:10a6:20b:3d8::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.22; Thu, 25 Apr 2024 09:09:57 +0000 Received: from AM3PEPF0000A793.eurprd04.prod.outlook.com (2603:10a6:20b:100:cafe::26) by AM7PR02CA0011.outlook.office365.com (2603:10a6:20b:100::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7452.29 via Frontend Transport; Thu, 25 Apr 2024 09:09: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 AM3PEPF0000A793.mail.protection.outlook.com (10.167.16.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Thu, 25 Apr 2024 09:09:57 +0000 Received: from seliicinfr00050.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.1258.12; Thu, 25 Apr 2024 11:09:55 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id A8FA41C006A; Thu, 25 Apr 2024 11:09:55 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Mattias_R=C3=B6nnb?= =?utf-8?q?lom?= Subject: [RFC v2 1/6] eal: extend bit manipulation functionality Date: Thu, 25 Apr 2024 10:58:48 +0200 Message-ID: <20240425085853.97888-2-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240425085853.97888-1-mattias.ronnblom@ericsson.com> References: <20240302135328.531940-2-mattias.ronnblom@ericsson.com> <20240425085853.97888-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM3PEPF0000A793:EE_|AM8PR07MB8041:EE_ X-MS-Office365-Filtering-Correlation-Id: 748ad0e0-6a8f-4c10-8c36-08dc65077a8c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|376005|36860700004|1800799015|82310400014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?PfLu+INMSSy8XL5V4LOxvz9uLVhtMrc?= =?utf-8?q?dDREtVGeTit2FJa7VmMvCSybj4aJDOWni7Jh1uDZYYwGP5MoQxAuRdTwjasQVdSpD?= =?utf-8?q?Kzyn/h8TH+HCiFCd6a7seTPBVz/l8zP96UsPGIoi5E/2Dp25lgtAHnCR9mZ9uxTmd?= =?utf-8?q?A09TrzbXCLkgDCPJGLrl3ljRMYb7AR4fS+vxNUYLctZmoiYMMsrVUnFdp4rsX3h0v?= =?utf-8?q?4y4f/epoNLk8QoKyLEsschaYTVVKS5Rz6mVokiWU96GHKBCFjgoYsoRnQisaCOFXA?= =?utf-8?q?UoAck8sQn8yfwqIgudsbjlxlNRVWym692tWVOgC/WJlaVlDzjymE1EC3+A8i7Awf8?= =?utf-8?q?yUDnCvNNMLctBHZtuW9bxiuJSGt8JRHI7pLSC+JyvuQFWlht7DUFo4+FZwjH1a6CV?= =?utf-8?q?nnpzI7mITDfkjROZCQyc6ZTu/4lcnpHELVqsz+3ROj2KCtFbqDL9tROoclZdbyHhz?= =?utf-8?q?bJFbLqYiMs2omWR3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ront-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)(1800799015)(82310400014); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2024 09:09:57.4350 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 748ad0e0-6a8f-4c10-8c36-08dc65077a8c 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: AM3PEPF0000A793.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8PR07MB8041 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, set, clear, and assign the value to 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. Signed-off-by: Mattias Rönnblom --- lib/eal/include/rte_bitops.h | 158 ++++++++++++++++++++++++++++++++++- 1 file changed, 156 insertions(+), 2 deletions(-) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 449565eeae..75a29fdfe0 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,157 @@ 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, \ + 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) + +#define __RTE_GEN_BIT_TEST(name, size, qualifier) \ + static inline bool \ + name(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(name, size, qualifier) \ + static inline void \ + name(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(name, size, qualifier) \ + static inline void \ + name(qualifier uint ## size ## _t *addr, unsigned int nr) \ + { \ + RTE_ASSERT(nr < size); \ + \ + uint ## size ## _t mask = ~((uint ## size ## _t)1 << nr); \ + (*addr) &= mask; \ + } \ + +__RTE_GEN_BIT_TEST(__rte_bit_test32, 32,) +__RTE_GEN_BIT_SET(__rte_bit_set32, 32,) +__RTE_GEN_BIT_CLEAR(__rte_bit_clear32, 32,) + +__RTE_GEN_BIT_TEST(__rte_bit_test64, 64,) +__RTE_GEN_BIT_SET(__rte_bit_set64, 64,) +__RTE_GEN_BIT_CLEAR(__rte_bit_clear64, 64,) + +__rte_experimental +static inline void +__rte_bit_assign32(uint32_t *addr, unsigned int nr, bool value) +{ + if (value) + __rte_bit_set32(addr, nr); + else + __rte_bit_clear32(addr, nr); +} + +__rte_experimental +static inline void +__rte_bit_assign64(uint64_t *addr, unsigned int nr, bool value) +{ + if (value) + __rte_bit_set64(addr, nr); + else + __rte_bit_clear64(addr, nr); +} + /*------------------------ 32-bit relaxed operations ------------------------*/ /** From patchwork Thu Apr 25 08:58:49 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: 139673 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 DE56C43F04; Thu, 25 Apr 2024 11:10:05 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2998E436CC; Thu, 25 Apr 2024 11:10:02 +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 1F63C436BF for ; Thu, 25 Apr 2024 11:10:00 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LOYSmHajmF45uwZwYYcEp0jKFqCu2PYPU1yydKSXGQz2NlAlaESu8+InoVJvseY/+2hOBengEK/JaRA7PbUrtObOKsoYCt9NI9F9DxGZOT4FyORpFCdco5dG2jsYEfCHUqMPXGSnXqSapGhegnoev1Vr7RoeIsy2bluJcFVSiqdS4NtRdj/oiVAGjr/C3zRXS1Vyn27ttxreQSvHETQI67pisgaw28jX8qDQ0/Ur5POb9ku4JSlptFqkx4EVmd6KXz4M+7oHzIGgcmfR8cF4ng1vGbIUQA9iRkLe7ILzj3AiBvuybB0P4PEkrVPCAAGHNi5lIWUdcnWZrrq1o/HOvQ== 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=UAXttjTnMNYVno4iLY1+608dHrWTK4YyrJUpoliiT/k=; b=BatapY27STw0FuKEsmj8YsDyl0yPZ5d6HKpOdST21Lx9TzE7AdROOZ42S5vem3eD5ihPuDG2ZKofZKlYwOucORGi01OHDQBmsp32UpzTKO3+vtpxXkxupFrEWDwIpYS5y3AeOzLophZHg9xkbjLvKFHqT4Dj3OhsHYQ77XVB/4yuwhtRiBdyupxgkEGF2KQcZN56DFbR3rH+fqulzu+IVn38Evm0CdYtrHJbyTeuYGiPXSHuZs7mV1BQmHi5JE2dlr3UpNVGa3kOIHZGZ/IE7cwGA7qr7C/iIgYhugdZe/UzV9OzKHcnKqzp+NmPm7TQXvttIq4ksm0Xk4HsEpWHOQ== 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=UAXttjTnMNYVno4iLY1+608dHrWTK4YyrJUpoliiT/k=; b=akb05kjnqKlI+48wBC28d7hFChjSSoVte2mLzFIP3OgNEjxX2GhEu/fVauHtaK6j1Her5kX0ZcLsIgLgw8jm+2Ma0sACkbmiCRphQpJyuqBl0fdUVmv4EHb9MgFZ40t1IFui/XDn99pCedrJVBe+DO4j19K/gXxVFiQxI+lSABh4GwGqr/8mIkz6EraprR0bKtX1XRBVjfmYpIo6pcX39Yksm2DHm8aoocrun6B7h25jU9Nt+VWfdnP58Bh80gmGY9+E1uEwnBwg8U/nCr6RmqjRAQwDuPCsKq6qdfKDD1A/Ftg1ATkW48dmI7RI7oT4Sw2Dr4p4k4e/JU7szabziw== Received: from DUZP191CA0048.EURP191.PROD.OUTLOOK.COM (2603:10a6:10:4f8::8) by DBBPR07MB7420.eurprd07.prod.outlook.com (2603:10a6:10:1e4::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.22; Thu, 25 Apr 2024 09:09:58 +0000 Received: from DU2PEPF0001E9C2.eurprd03.prod.outlook.com (2603:10a6:10:4f8:cafe::70) by DUZP191CA0048.outlook.office365.com (2603:10a6:10:4f8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.25 via Frontend Transport; Thu, 25 Apr 2024 09:09: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 DU2PEPF0001E9C2.mail.protection.outlook.com (10.167.8.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Thu, 25 Apr 2024 09:09:58 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.65) with Microsoft SMTP Server id 15.2.1258.12; Thu, 25 Apr 2024 11:09:57 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id 18A3A1C006A; Thu, 25 Apr 2024 11:09:57 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Mattias_R=C3=B6nnb?= =?utf-8?q?lom?= Subject: [RFC v2 2/6] eal: add unit tests for bit operations Date: Thu, 25 Apr 2024 10:58:49 +0200 Message-ID: <20240425085853.97888-3-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240425085853.97888-1-mattias.ronnblom@ericsson.com> References: <20240302135328.531940-2-mattias.ronnblom@ericsson.com> <20240425085853.97888-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF0001E9C2:EE_|DBBPR07MB7420:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f24ff4f-f1da-444b-1a40-08dc65077b1a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|36860700004|82310400014|376005|1800799015; X-Microsoft-Antispam-Message-Info: =?utf-8?q?xbz/QTitZdbTb9onFuws9c9ndsfW9AW?= =?utf-8?q?LjGk74OcXVrlug5/mleohILDoiQLXjyINDU0b+cm2Gz+AKA0x3+ZfZWjbCqkwhf/K?= =?utf-8?q?gejaWl1pbFt42uwJEg+g+4T+mXxxy0uoOMcbrwM8ZR++A1KtXKfs+57farVGN4bU8?= =?utf-8?q?4KkPtMqfhT1UDKPlip5ckADBtzVMO5tAfkUaydcFAQeKkDQnescHA1ZcHgvrlZzjg?= =?utf-8?q?H7v3vH5HNdU0iyiLVjidpxsK/t2Nx/SA5gCUCzvdrK4g8qrki9t8QOBVK4opgIZKP?= =?utf-8?q?IHbGOPQsAoIs6IEi1SG1XZOQB6et2cvDeE1gVdtNNTN/28Hsy9PLQvNFsl9PYsuwG?= =?utf-8?q?8dlT/Jlif/n/D04vWb4bmN35gA23y5xR9HcQ9kI/7zPamD8nXy8oxfYqGcVcmNXH6?= =?utf-8?q?pQiOhMc1WAAMEPOfYGCxF1Z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94QqF6Un3DkZ9ZThiLeYaXmkLKRoHkFdGXfRi7?= =?utf-8?q?JOR+d98kYAeF?= 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)(36860700004)(82310400014)(376005)(1800799015); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2024 09:09:58.3471 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0f24ff4f-f1da-444b-1a40-08dc65077b1a 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: DU2PEPF0001E9C2.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR07MB7420 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_[set|clear|assign|test]() family of functions. The tests are converted to use the test suite runner framework. Signed-off-by: Mattias Rönnblom --- app/test/test_bitops.c | 76 +++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/app/test/test_bitops.c b/app/test/test_bitops.c index 0d4ccfb468..f788b561a0 100644 --- a/app/test/test_bitops.c +++ b/app/test/test_bitops.c @@ -1,13 +1,59 @@ /* 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, \ + 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); \ + } \ + \ + 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_access_32, rte_bit_set, rte_bit_clear, \ + rte_bit_assign, rte_bit_test, 32) + +GEN_TEST_BIT_ACCESS(test_bit_access_64, rte_bit_set, rte_bit_clear, \ + rte_bit_assign, rte_bit_test, 64) + +static uint32_t val32; +static uint64_t val64; #define MAX_BITS_32 32 #define MAX_BITS_64 64 @@ -117,22 +163,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_access_32), + TEST_CASE(test_bit_access_64), + 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 Thu Apr 25 08:58:50 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: 139674 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 C3E6A43F04; Thu, 25 Apr 2024 11:10:12 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 61639436CE; Thu, 25 Apr 2024 11:10:03 +0200 (CEST) Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05on2080.outbound.protection.outlook.com [40.107.21.80]) by mails.dpdk.org (Postfix) with ESMTP id 313AB436C0 for ; Thu, 25 Apr 2024 11:10:00 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n22qQnuV3oAebAK7qtO2umi8Wwxzdkdnn5kYqtak3FNwVRzBhBQOk/gPgiveA7XcZFCGC8uZfmEOTY1J5jsf0d+7ReKqfWMkHsOoARZ0XFWi6WDiprEewP/oPbJab520RXwZU52bn6OboO17kbEfgAnFRK82y85XoICtFcP3lBM4IZBRSs8ieDpDdShLzX6nVcHVQH6NaNM+FiLIkSuTFCWZz3UNqKnUGP5VkTo3UIF85eATKSuhlSYrxaTEBI/H/khOjcuqadQ0OCQX59BnlPfdg+VXDsDxlq24k60tXaqUwyDPs/1qcEnD9Dkf8WeARLMI+6qwvUxy7JisMJa70g== 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=L1XCW1dzUR2ggKoBcDUrz5OYrvcTpPDIoxhI9dWGFtM=; b=MVC0sWmhGMQaXMdctQKd3RhGWb+SFRR00rmQQfTrHoWuCq4RipV+guYXDuayorAYnsZgrX4LyF3Bnq7cP0k7tIiERF0bRI41zNgLZAtHqkPZyywSU7xhjgLRwKFxfQrd3t91xwyjiCZ75g/LkIqcTm1FABAAImtadHA00IbZHc3ArqZaMRhIIP3IGG/krYhntKkL7N3oMof7VUtw0RHefjOdYLgl+FrU0Ot9pFl+KJaMtCJAh39fytW94PN6RUn7DMelXuKiwOHht1hnWWSuCWPXOaE7PqYwsGx/pQGox2XFDr/OnrS5lzmwgT/lffiByFifLox3Zr7SuWg2fZz3Qw== 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=L1XCW1dzUR2ggKoBcDUrz5OYrvcTpPDIoxhI9dWGFtM=; b=OG+WOvbKE91AO0eW7h+xZRf52qOOVU8Uxny3kyyq7iKajhHdl3Ruiuo5VMWKuUPKGT706af7qo4eAkkazHe/EuCAeQ+uxiFp6ugdhbGn8UwP8XpxAywjMOk5y8/LDAcnMol05dJo8NPdYXdUhmvPeaCO7rR8AinSgsHxc5oHW7ghrvVbASMxLjcnC50oDjRDVd0XGB0+uXPrKUoI8jZZidF9saCDjT6SJ91oOb5Xklbcxy127ZH08Pfm7EpkLEue3H9OCsLHpmWVaqWxOkz6woSqv+j0LgAvr3I1vTQPN79NVmF3xEEUwmgNwkgqtNY5sHkZ8OnaPxYJY0gBsqaBzA== Received: from DB7PR05CA0023.eurprd05.prod.outlook.com (2603:10a6:10:36::36) by AS8PR07MB7445.eurprd07.prod.outlook.com (2603:10a6:20b:2af::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.39; Thu, 25 Apr 2024 09:09:58 +0000 Received: from DB5PEPF00014B94.eurprd02.prod.outlook.com (2603:10a6:10:36::4) by DB7PR05CA0023.outlook.office365.com (2603:10a6:10:36::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.25 via Frontend Transport; Thu, 25 Apr 2024 09:09: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 DB5PEPF00014B94.mail.protection.outlook.com (10.167.8.232) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Thu, 25 Apr 2024 09:09:58 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.62) with Microsoft SMTP Server id 15.2.1544.9; Thu, 25 Apr 2024 11:09:57 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id C0E491C006A; Thu, 25 Apr 2024 11:09:57 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Mattias_R=C3=B6nnb?= =?utf-8?q?lom?= Subject: [RFC v2 3/6] eal: add exactly-once bit access functions Date: Thu, 25 Apr 2024 10:58:50 +0200 Message-ID: <20240425085853.97888-4-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240425085853.97888-1-mattias.ronnblom@ericsson.com> References: <20240302135328.531940-2-mattias.ronnblom@ericsson.com> <20240425085853.97888-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB5PEPF00014B94:EE_|AS8PR07MB7445:EE_ X-MS-Office365-Filtering-Correlation-Id: 0c34275c-8b5c-4219-85b1-08dc65077b1e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: =?utf-8?q?WlQZTznwYSckNGULqmVrqzvKikkzVml?= =?utf-8?q?sTVoZ0cvtbQtOvAXJ3Dq27KdgZCTyHONL6DeTiohtoXisNM5ibs3EX7p4TdvHqzJ6?= =?utf-8?q?qEknMRYuolcKrmD43Sn/sQGpYDoVWIVJU/+2zCrkW8PZRN97VcJvcrJjSmPNYAVL2?= =?utf-8?q?2i4DKA/Vu/nqLUP1Mx+r3oYFGeXyIjxLjBSUT/ADwKmMrntOwBRg1+OdSUr135hBJ?= =?utf-8?q?9FHOD99K1+Z/bR6J7l5rD81mGQtzA+LqTGLJ3mLfQtTpI+KtD0G7sdD6De9SttAFj?= =?utf-8?q?hegq6bII0A3GPSh6gmXEufGJkk0wLpg2tJRo1WH8qk0bnqQWvMIp17iV6mWdFte0B?= =?utf-8?q?PmgP6y7XYzgREv443FJ866gt42QvPMwgZ61w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d2ffVbLxk57wT0scdhyrXnfs?= =?utf-8?q?JtP/xAShw7Gvno3Ti9qTDjOwBFpTP4ndSPmFKJW6arhVT1jghFqlDxV8=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)(36860700004)(82310400014)(376005)(1800799015); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2024 09:09:58.3604 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0c34275c-8b5c-4219-85b1-08dc65077b1e 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: DB5PEPF00014B94.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR07MB7445 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 bit test/set/clear/assign 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. Signed-off-by: Mattias Rönnblom --- lib/eal/include/rte_bitops.h | 170 +++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 75a29fdfe0..a2746e657f 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -201,6 +201,147 @@ extern "C" { uint32_t *: __rte_bit_assign32, \ uint64_t *: __rte_bit_assign64)(addr, nr, value) +/** + * @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. + * + * This function 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, \ + uint64_t *: __rte_bit_once_test64)(addr, nr) + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Set bit in word exactly once. + * + * Set bit specified by @c nr in the word pointed to by @c addr to '1' + * exactly once. + * + * This function 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. + * + * Set bit specified by @c nr in the word pointed to by @c addr to '0' + * exactly once. + * + * This function is guaranteed to result in exactly one memory load + * and exactly one memory store, *or* an atomic bit clear 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_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. + * + * Set bit specified by @c nr in the word pointed to by @c addr to the + * value indicated by @c value exactly once. + * + * This function is guaranteed to result in exactly one memory load + * and exactly one memory store, *or* an atomic bit clear operation. + * + * This function 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) + #define __RTE_GEN_BIT_TEST(name, size, qualifier) \ static inline bool \ name(const qualifier uint ## size ## _t *addr, unsigned int nr) \ @@ -239,6 +380,14 @@ __RTE_GEN_BIT_TEST(__rte_bit_test64, 64,) __RTE_GEN_BIT_SET(__rte_bit_set64, 64,) __RTE_GEN_BIT_CLEAR(__rte_bit_clear64, 64,) +__RTE_GEN_BIT_TEST(__rte_bit_once_test32, 32, volatile) +__RTE_GEN_BIT_SET(__rte_bit_once_set32, 32, volatile) +__RTE_GEN_BIT_CLEAR(__rte_bit_once_clear32, 32, volatile) + +__RTE_GEN_BIT_TEST(__rte_bit_once_test64, 64, volatile) +__RTE_GEN_BIT_SET(__rte_bit_once_set64, 64, volatile) +__RTE_GEN_BIT_CLEAR(__rte_bit_once_clear64, 64, volatile) + __rte_experimental static inline void __rte_bit_assign32(uint32_t *addr, unsigned int nr, bool value) @@ -259,6 +408,27 @@ __rte_bit_assign64(uint64_t *addr, unsigned int nr, bool value) __rte_bit_clear64(addr, nr); } + +__rte_experimental +static inline void +__rte_bit_once_assign32(uint32_t *addr, unsigned int nr, bool value) +{ + if (value) + __rte_bit_once_set32(addr, nr); + else + __rte_bit_once_clear32(addr, nr); +} + +__rte_experimental +static inline void +__rte_bit_once_assign64(volatile uint64_t *addr, unsigned int nr, bool value) +{ + if (value) + __rte_bit_once_set64(addr, nr); + else + __rte_bit_once_clear64(addr, nr); +} + /*------------------------ 32-bit relaxed operations ------------------------*/ /** From patchwork Thu Apr 25 08:58:51 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: 139672 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 07D5743F04; Thu, 25 Apr 2024 11:10:01 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id EA4DA436BF; Thu, 25 Apr 2024 11:10:00 +0200 (CEST) Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on2060.outbound.protection.outlook.com [40.107.247.60]) by mails.dpdk.org (Postfix) with ESMTP id 19086436BE for ; Thu, 25 Apr 2024 11:10:00 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mf4CFp7KUxjnv3LUe15drJuofdq1VBE4xQ0ng0EyL3M5k/jJxtRhs2SyqZlkobWloFWxhTi+SnawoTAFNXWHmoZFEJ4eO9ynNrui+pex1Q5Yy+j62Q+qTigjzZrdiBq/NgNPh4fEwLPlnI+cBHoneca8mCfi7ERkbdct1GZCYFAxijIQVRFN5ITsWlfHYqAnjGPAd8PKTsqU2x2vgkJRIQZxnVvQeIi7gkkt3IJ/+MEOsvTZOBe4l/RGWB3qyl5efl/kKtovqwqWdE9DtdLeJpNIxGk2/D3F8uLcY/ZF8wHy3DLAqBrUsffwXgiQVRZ897NhAYpTcFhcwP+22spG7g== 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=7W+hBC8l/Qn64JH916BuvIZUxAEc7QpzZspDnV8Kzho=; b=ibS9BZRxnWDMghenYIHs9DYqit7rUJRFCJExSP6/tAq9qhx2etSmQJbO46tMNLw8dDiQt0Zzt42TyE2tIOLsBswh0uTFkFAkHE2Td34/XXt6jejeCgn6ElUdpXgkFbbwyGngu9hsmSeNKHU/TPnOITTbqKkAfR0+Oilve195RtEe3M8lbdKh49V8hXMIzitmgBsW7h6jjEnhhQnnPdBPdBic8GIWuTLg/C6ZAEldXI8l+c8HY64yCj69b+dDx1kJ7BbijOlS/tomStGa1hlXdyB/Ry+mIue9ozRZ0fQhF2BT8oLoBm9yScopkjbqTL5eFxSEL8V4iJS0p+2bz6juEw== 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=7W+hBC8l/Qn64JH916BuvIZUxAEc7QpzZspDnV8Kzho=; b=EY2jZ5KzIPKBmLbBsdA1Mb1DgVqCCq4N0eZyKchQFj2PRbCsgmU+QqusmolVLvNNZ9/ilnuUdWdZJoGtrN4Z6P99FRUCJoz0DvsErNx7aIU/jvR0LzBjZ8CIdqKO4TorHVRaiCNHpqk7wAJa6q7FCCr7khbpALwDLYc5E3chZYYjhWf+z9SE/8o7vufbm18benterL1wkHgBeGNoN4Y3th251HahqgeN/OmXXgN6tBJJQmSy/z1lw6eRXMhBCZrGrSaV5jBQGEF70QwOzQPElcxrfNmEcHgfKnWqzXZx692tDJeugmD/ozoAnGzYOdEztG6aoMDHHmLoVdoYUSXoOA== Received: from DB7PR05CA0052.eurprd05.prod.outlook.com (2603:10a6:10:2e::29) by AM9PR07MB7682.eurprd07.prod.outlook.com (2603:10a6:20b:2d3::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.22; Thu, 25 Apr 2024 09:09:59 +0000 Received: from DB1PEPF000509E2.eurprd03.prod.outlook.com (2603:10a6:10:2e:cafe::95) by DB7PR05CA0052.outlook.office365.com (2603:10a6:10:2e::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.25 via Frontend Transport; Thu, 25 Apr 2024 09:09: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 DB1PEPF000509E2.mail.protection.outlook.com (10.167.242.52) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Thu, 25 Apr 2024 09:09:58 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.63) with Microsoft SMTP Server id 15.2.1258.12; Thu, 25 Apr 2024 11:09:58 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id 529291C006A; Thu, 25 Apr 2024 11:09:58 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Mattias_R=C3=B6nnb?= =?utf-8?q?lom?= Subject: [RFC v2 4/6] eal: add unit tests for exactly-once bit access functions Date: Thu, 25 Apr 2024 10:58:51 +0200 Message-ID: <20240425085853.97888-5-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240425085853.97888-1-mattias.ronnblom@ericsson.com> References: <20240302135328.531940-2-mattias.ronnblom@ericsson.com> <20240425085853.97888-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB1PEPF000509E2:EE_|AM9PR07MB7682:EE_ X-MS-Office365-Filtering-Correlation-Id: 90b39979-094f-460f-d509-08dc65077b69 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|376005|1800799015|82310400014|36860700004; X-Microsoft-Antispam-Message-Info: =?utf-8?q?KjhB0ARHE1UDz0LgBQcUUvVNF3X8VW8?= =?utf-8?q?UY7jlz3PYpxoV7W73aj08UaedgxpPvsOL1+G3FjauJQ7Lu/hVcJbJea23bXUE8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4TyxV/AjNRyO5?= =?utf-8?q?bV8fffsofF7oJsNvMomXJQH2/ySRfllERiF5tD1Sr8VlEE1QCP/NTWaEVqtHg6BRH?= =?utf-8?q?UMKsTO0ecW5n0mr9YfMX1979ZbMN3U+JmvkaiHTIjvr05AN3Y4QtPeS11sV9eG3kw?= =?utf-8?q?mJMqJeJHHgbwz5zR/cSwZx9zze3KWA/PrEn3Sc4jWWhhlsG7/DSLn+cigt2I374Ww?= =?utf-8?q?Od28oXPkhEKFVFJhMwcD6TQHV1B4W/qWBPlaHS14HvkVWhKl5dTlKmBIuz3eaNAYu?= =?utf-8?q?7UJkytL1I5A7NRITiLG+PoDi/H4dsKuXoZAValD5VdDA1+ftTLTTr46ahrNx6sOts?= =?utf-8?q?z1IS6fHzuQxiXwWHb672S3BHyqLr8n3zyD4aKoFn2MSSlG4LBs6m7uMM=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)(376005)(1800799015)(82310400014)(36860700004); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2024 09:09:58.8540 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 90b39979-094f-460f-d509-08dc65077b69 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: DB1PEPF000509E2.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR07MB7682 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_[set|clear|assign|test]() family of functions. Signed-off-by: Mattias Rönnblom --- app/test/test_bitops.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/test/test_bitops.c b/app/test/test_bitops.c index f788b561a0..12c1027e36 100644 --- a/app/test/test_bitops.c +++ b/app/test/test_bitops.c @@ -46,12 +46,20 @@ return TEST_SUCCESS; \ } -GEN_TEST_BIT_ACCESS(test_bit_access_32, rte_bit_set, rte_bit_clear, \ +GEN_TEST_BIT_ACCESS(test_bit_access_32, rte_bit_set, rte_bit_clear, \ rte_bit_assign, rte_bit_test, 32) -GEN_TEST_BIT_ACCESS(test_bit_access_64, rte_bit_set, rte_bit_clear, \ +GEN_TEST_BIT_ACCESS(test_bit_access_64, rte_bit_set, rte_bit_clear, \ rte_bit_assign, rte_bit_test, 64) +GEN_TEST_BIT_ACCESS(test_bit_once_access_32, rte_bit_once_set, \ + rte_bit_once_clear, rte_bit_once_assign, \ + rte_bit_once_test, 32) + +GEN_TEST_BIT_ACCESS(test_bit_once_access_64, rte_bit_once_set, \ + rte_bit_once_clear, rte_bit_once_assign, \ + rte_bit_once_test, 64) + static uint32_t val32; static uint64_t val64; @@ -168,6 +176,8 @@ static struct unit_test_suite test_suite = { .unit_test_cases = { TEST_CASE(test_bit_access_32), TEST_CASE(test_bit_access_64), + TEST_CASE(test_bit_once_access_32), + TEST_CASE(test_bit_once_access_64), TEST_CASE(test_bit_relaxed_set), TEST_CASE(test_bit_relaxed_clear), TEST_CASE(test_bit_relaxed_test_set_clear), From patchwork Thu Apr 25 08:58:52 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: 139675 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 2202A43F04; Thu, 25 Apr 2024 11:10:22 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 443BE436DD; Thu, 25 Apr 2024 11:10:05 +0200 (CEST) Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2067.outbound.protection.outlook.com [40.107.104.67]) by mails.dpdk.org (Postfix) with ESMTP id 0BAEE436C8 for ; Thu, 25 Apr 2024 11:10:02 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dwYCK1/aIjUjCBbv9sggvwNApATG6KAWBLGcUDlVqoN0dw0kcUBraRs2sgU+A/vC97FX5YmyiRP+fn7fvM83TAQRcWICJsg8Mx4d5qC0J7RpTwsyl6vuRk4CVDTsYN55vm+CJ7IuLF2cDz+wkujN7wlh0FuVq3/0UMPjrcKsyrDH+tYaI1e2hfh1S3W/xQIc+8wq844/KWkWZ/xa420brwJap2VemKKsnzLW9terlWJ7lX6B+oj+xmDdyZmcdvy1Ei3aDBjOXhUxytwzBjYSwmSUjMPOVrPgcR3nz1ZdJRzSFTkJtIyLxvYb9lvI1scdxa/KI0acFdO0OlQ6gw/TrQ== 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=m127R3HU9pNG3bWTrW0IgoJ9+Gojo1qtktVZxd6EF04=; b=WqyyjqfglsJmRJPc3nQXD5vW9H6lpTQ+RlFiQtrCMpS3+ACKanXUH/HYG1hBjlR9VHK+QU+F/WmiUKG0liffLjQXOrdkuj2DLBarMN7mPHv6iTGsPR65qZubMnuTuM/qJ0drOlBOjiv1qWG2LalXuWcoqLsw+rhHsorQFm80wUTphqN7tDNVCkZWf00CF3Lpo2hJO7v4e9r7ykHw4zK+UmbcAZv6+3wTrwxdnp1596OBvkiMinMw5KkXsD9L1alMambUIg6/xtBgXRIkQbrjoM0snvZX3bAjSGLsh8rEns/pPFokgWMV/d5kiVuJ4LjxLL7gMrZaW0VbtH21baPjDA== 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=m127R3HU9pNG3bWTrW0IgoJ9+Gojo1qtktVZxd6EF04=; b=EXcjhBGE39dmj6/PA/QAGws6O2jMxFVoUcR9xDqrPJ/Ez3PDZPRSlYBAF+zvWoqD4JTibpo+Ed3Stig0IwAVSed+1GfsEFjNCY32vITAxMr9UeofAu+ziv21mpjGtCONOYT9ZP3+pD+O67a9OWjkNjS9FjAAaeB+Tm13Pp7S7nHk8wHy705EyD2zY9kLO1IJ+jw9lI4Nh/psMa2p7X0nl+duEbeCM09Il5G5L1+BKi9Bfhv4w5mjj49X84YH5WT+mN+zFggMVM1nHlPaA3HvPEFvkz/1gyoEw1NhQm39Ru17bkAeXU/wLaKkq6aE8rEe6/gNP5G1CLilBSnKVCCkmA== Received: from DUZP191CA0028.EURP191.PROD.OUTLOOK.COM (2603:10a6:10:4f8::24) by DBAPR07MB6807.eurprd07.prod.outlook.com (2603:10a6:10:19f::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Thu, 25 Apr 2024 09:09:59 +0000 Received: from DU2PEPF0001E9C2.eurprd03.prod.outlook.com (2603:10a6:10:4f8:cafe::52) by DUZP191CA0028.outlook.office365.com (2603:10a6:10:4f8::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.25 via Frontend Transport; Thu, 25 Apr 2024 09:09: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 DU2PEPF0001E9C2.mail.protection.outlook.com (10.167.8.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Thu, 25 Apr 2024 09:09:59 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.65) with Microsoft SMTP Server id 15.2.1258.12; Thu, 25 Apr 2024 11:09:59 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id E238F1C006A; Thu, 25 Apr 2024 11:09:58 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Mattias_R=C3=B6nnb?= =?utf-8?q?lom?= Subject: [RFC v2 5/6] eal: add atomic bit operations Date: Thu, 25 Apr 2024 10:58:52 +0200 Message-ID: <20240425085853.97888-6-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240425085853.97888-1-mattias.ronnblom@ericsson.com> References: <20240302135328.531940-2-mattias.ronnblom@ericsson.com> <20240425085853.97888-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF0001E9C2:EE_|DBAPR07MB6807:EE_ X-MS-Office365-Filtering-Correlation-Id: a656cc31-d8ad-4bb5-5e8f-08dc65077c01 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: =?utf-8?q?I+8TyHoetdndPMF8NL534sGUk3Y1jdX?= =?utf-8?q?H2C6RPhgWod0c6/DvDDV1v7bSrGmZTk51jN45Q0Eh2MXwHX8cEgQc6bna2r0QScmy?= =?utf-8?q?Xd/+ev2m9QZvJjmrOi22bo1wbBKM1k8yYaEDlfrm3B7JvVKHdyHi/RBDhIMBxxJ6h?= =?utf-8?q?JaEzOk4cUEHg93I5/6wVm4AppeiGFD/nqWh8qOPKiPvx96r3dGqqm7qEe3YCiVV/S?= =?utf-8?q?cwfliVnEp/fD/CkJhX+TaapRQ7a/REg7DpPP2Dw/evs/q2E3HIND92U3myJifmCcR?= =?utf-8?q?6QfRU3kaNC7pRcYr81IOTGuELn/uQg1Dd48tnPlzBWYnDBMHkL1yk/FVoNhL4QB3z?= =?utf-8?q?Dd+jndQ/+8UXNmpHCPDsYKvdauhF7lsY2wwp3X+Pc/06ALef7bFGhgNg9TYA5UlHH?= =?utf-8?q?Q9DO49Dzhi/rEcpftalIc2UHu+2CDlUQPMmgAJMOzf6TEQ48BgNTo4AdxJn+xjdQf?= =?utf-8?q?1iOv1n1/TkrWup+bgh1fDr2+QcNZWZTkJhL7tdWT93CcRIEkPdP1z+nJRiYM6iVeQ?= =?utf-8?q?4xTEohRpiOkI5eEuK4El3zowvn2OJU3GzQ3TKjtnh0AFiCXjuRpOWkoARX1uLYVbe?= =?utf-8?q?8ZM5QOsOtCuAlanzysqjS1Wg1s76RyOOPcG/clhKzMbXEm7hdmGAxOSkpVY8qQHJo?= =?utf-8?q?xRdb5flCI35x/FDDsyYMm0nf1/FsWjvHnUUpc+uUGMdsVaNWspBDz2F9pbtVm9/lT?= =?utf-8?q?vThGfqCtlfMK26lnIhbHIv122ppJwy86ldmNdYu1/7dAygPplBnWCwRL9QWFybgkp?= =?utf-8?q?l6R0efr4TRw2sNiFOJg58W1ecD7VeI7EnYv+pK8xJFGe1DRUxE70RAtQc8/w2J/kr?= =?utf-8?q?PpCamgJCw4UOlqEojaTsJZHNqbzcib+7M8a+hlaV+TYH6tJoBRYVQtRPxGk2LWADx?= =?utf-8?q?8a/VT/7qcos8dJx2+drZSyKms8fABUJVycuYS9Lu0Ry5/DjJ1Z5DKHehaEJCYt0su?= =?utf-8?q?7lZCTlg9B2n2MBMxDtvWcfbzpc32aGPZMxs5uSmJPjAdPvAEVtydabTZvbLiK2qd0?= =?utf-8?q?3edb3lt9MmhfMBia+SGuX5HHqC3NDULKTJHeF4oaNklxItdL8wsPe3sChlhlXiAcM?= =?utf-8?q?yDCHVa8pJVq+74d/LfudHvT9v6jPE2dh1la0GJBRhn43bDlaHZPkW/nvcQiwg/MHB?= =?utf-8?q?E8qBvtWsno5OBpNuuRy/4Okr1G9QADE/OChVJN2eDAfxBmT4+90/9chjWqs5PF9Z7?= =?utf-8?q?k871z+zsVhk2kXKhJ3LLnCX3GWav2Z3ZdlAOpzDzOtqgFhewBH6oo+7soaPkKbaeY?= =?utf-8?q?Icnx+Hu2/9yASSzKsx9BKhFhx4dEMvgnEERa5wipONyqyRNOpnsqsAVLoFcc0AyYH?= =?utf-8?q?RhJ7lU6B5Dw8?= 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)(82310400014)(1800799015); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2024 09:09:59.8471 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a656cc31-d8ad-4bb5-5e8f-08dc65077c01 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: DU2PEPF0001E9C2.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR07MB6807 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 and test-and-set/clear functions. All atomic bit functions allow (and indeed, require) the caller to specify a memory order. 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 --- lib/eal/include/rte_bitops.h | 297 +++++++++++++++++++++++++++++++++++ 1 file changed, 297 insertions(+) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index a2746e657f..8c38a1ac03 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" { @@ -342,6 +343,177 @@ extern "C" { 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. + * + * 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. See for details. + * @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, \ + 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. + * + * 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. See for details. + */ +#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. + * + * 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. See for details. + */ +#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. + * + * 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. See for details. + */ +#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 test and set a bit in word. + * + * 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. See for details. + * @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. + * + * 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. See for details. + * @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. + * + * 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. See for details. + * @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(name, size, qualifier) \ static inline bool \ name(const qualifier uint ## size ## _t *addr, unsigned int nr) \ @@ -429,6 +601,131 @@ __rte_bit_once_assign64(volatile uint64_t *addr, unsigned int nr, bool value) __rte_bit_once_clear64(addr, nr); } +#define __RTE_GEN_BIT_ATOMIC_TEST(size) \ + 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) \ + 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) \ + 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_ASSIGN(size) \ + 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_ASSIGN(size) \ + static inline bool \ + __rte_bit_atomic_test_and_assign ## size(uint ## size ## _t *addr, \ + unsigned int nr, \ + bool value, \ + int memory_order) \ + { \ + RTE_ASSERT(nr < size); \ + \ + RTE_ATOMIC(uint ## size ## _t) *a_addr = \ + (RTE_ATOMIC(uint ## size ## _t) *)addr; \ + uint ## size ## _t before; \ + uint ## size ## _t target; \ + \ + before = rte_atomic_load_explicit(a_addr, \ + rte_memory_order_relaxed); \ + \ + do { \ + target = before; \ + __rte_bit_assign ## size(&target, nr, value); \ + } while (!rte_atomic_compare_exchange_weak_explicit( \ + a_addr, &before, target, \ + rte_memory_order_relaxed, \ + memory_order)); \ + return __rte_bit_test ## size(&before, nr); \ + } + +#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_ASSIGN(size) + +__RTE_GEN_BIT_ATOMIC_OPS(32) +__RTE_GEN_BIT_ATOMIC_OPS(64) + +__rte_experimental +static inline bool +__rte_bit_atomic_test_and_set32(uint32_t *addr, unsigned int nr, + int memory_order) +{ + return __rte_bit_atomic_test_and_assign32(addr, nr, true, + memory_order); +} + +__rte_experimental +static inline bool +__rte_bit_atomic_test_and_clear32(uint32_t *addr, unsigned int nr, + int memory_order) +{ + return __rte_bit_atomic_test_and_assign32(addr, nr, false, + memory_order); +} + +__rte_experimental +static inline bool +__rte_bit_atomic_test_and_set64(uint64_t *addr, unsigned int nr, + int memory_order) +{ + return __rte_bit_atomic_test_and_assign64(addr, nr, true, + memory_order); +} + +__rte_experimental +static inline bool +__rte_bit_atomic_test_and_clear64(uint64_t *addr, unsigned int nr, + int memory_order) +{ + return __rte_bit_atomic_test_and_assign64(addr, nr, false, + memory_order); +} + /*------------------------ 32-bit relaxed operations ------------------------*/ /** From patchwork Thu Apr 25 08:58:53 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: 139677 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 4A62A43F04; Thu, 25 Apr 2024 11:10:36 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 581C7436E1; Thu, 25 Apr 2024 11:10:08 +0200 (CEST) Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on2082.outbound.protection.outlook.com [40.107.8.82]) by mails.dpdk.org (Postfix) with ESMTP id F3038436D2 for ; Thu, 25 Apr 2024 11:10:03 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SeaU6kgLdX4UIH4gWQJ2+t4S8SkifHs+8kmw16ZT86Vc0wtaRnbPTMTh8rUkv5HdOvsqMxbWYxPSjSqRaqXwDa1Kc/hS267p0zRd9r+afCtmT9vNrqtCBmZLLOgeVEny4Gwygq93Ahyj/9yjmCXS/k+egGhyF5SDFL4NoJtwayQJg1qR6xc5kgjg3TC8TUMorFCaI0X1GyRRU2KTtufsB6CwdVuazr0krfPD/gqyT/4gHwWFwxEovAz2OH8EbmAKtx9NYhl/GoKXA6Gf6GjWmZUiFgKxNJ4Pdv0+CH3YpgkD+B3NmR1e/wsuntFKVT0LFuI6HRdQ4eZAoo9MOVzaSw== 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=g5NvuycN3WYWkI5PKSiP4/AA/zbW0QK30xpyRr5rjCU=; b=EiLyrIYkTRm4D/ZlJn2wCeVsRu3+OMPTUtZMwdSKMXif47aqMDhXLgd8UAuMNRbh3LwRmLPKtB3i0B+0gCGSTxW0rpkOTp56KmTubViE7S1KFqkLDW2ccd70hkN7JGSjX2JV23cMKoiG2UyaImqDyF7mSGoEFOrEj1kmwLtTrgKCYo/aOrupGkhBUVUZVXr0mpLWwAFw4IKRrOb7UBfiDEDjDF000h+Ju/wYgIMBvXTpTOT02nJEEXTaOk09afG6OX4OORSpkMCkZi0wx5NkLeiSyXY3ex6w+C7yNqZpqqVdROkiReSB+GMlsiNVVD35lv+UaPHn2MnbZul6OyX+kg== 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=g5NvuycN3WYWkI5PKSiP4/AA/zbW0QK30xpyRr5rjCU=; b=GrvOKVAKIzHpD1vU3nURpVVlUmUHb57AnviKBgBFD/Ha3Dx9bhfJzOPnd/RRpN2YiiuNui91x8PKOkI0i7+3MTjb38oVAZMA4SNs9ujrX3Ss7gSk5021XyomHkLX6/Ub3JgQl1MoB5pTHFzUiFtoHtPv1XCghEQqJ4i1oHp3l5fNlfSYnwe+4Hctd7vb3MxFfq+iMqherlOcqk9Z9xy40DHndcBk/7+UsdOmfhwKaGwTyUWZbfLLFYjhjlKc6cqLpf6viTpiUOzfohz/8w551VOXCIuhSyKRaYSjGcE8fS+7iEQamBvcgOzjUIUAVticBJL45VG4+k8/DUtRdOY8Vg== Received: from DUZPR01CA0172.eurprd01.prod.exchangelabs.com (2603:10a6:10:4b3::29) by AS8PR07MB7959.eurprd07.prod.outlook.com (2603:10a6:20b:352::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Thu, 25 Apr 2024 09:10:02 +0000 Received: from DB1PEPF000509E7.eurprd03.prod.outlook.com (2603:10a6:10:4b3:cafe::bb) by DUZPR01CA0172.outlook.office365.com (2603:10a6:10:4b3::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.23 via Frontend Transport; Thu, 25 Apr 2024 09:10:02 +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 DB1PEPF000509E7.mail.protection.outlook.com (10.167.242.57) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Thu, 25 Apr 2024 09:10:01 +0000 Received: from seliicinfr00050.seli.gic.ericsson.se (153.88.142.248) by smtp-central.internal.ericsson.com (100.87.178.63) with Microsoft SMTP Server id 15.2.1258.12; Thu, 25 Apr 2024 11:09:59 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id 807E41C006A; Thu, 25 Apr 2024 11:09:59 +0200 (CEST) From: =?utf-8?q?Mattias_R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?utf-8?q?Mattias_R=C3=B6nnb?= =?utf-8?q?lom?= Subject: [RFC v2 6/6] eal: add unit tests for atomic bit access functions Date: Thu, 25 Apr 2024 10:58:53 +0200 Message-ID: <20240425085853.97888-7-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240425085853.97888-1-mattias.ronnblom@ericsson.com> References: <20240302135328.531940-2-mattias.ronnblom@ericsson.com> <20240425085853.97888-1-mattias.ronnblom@ericsson.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB1PEPF000509E7:EE_|AS8PR07MB7959:EE_ X-MS-Office365-Filtering-Correlation-Id: 4b0a77bc-1641-4586-0750-08dc65077d04 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: =?utf-8?q?g3/2zHT16FVO92OoOlyRUGHIdNZkJfb?= =?utf-8?q?TC87puTswdHjr8Af5mjgeuoBWtERKMF4o4ZtKWrl/ZZsKPtgKYHXvD9JUfrD3yHqF?= =?utf-8?q?b7+HBR6onzpv7QY9BsJC675SBe9nACAJgpFNd3Wna9+YZztfO7PzWpCFFB5MDQ2NZ?= =?utf-8?q?IWBrzuorKBT2GYiQIKJxPKMxrI3Vv9VWwdvf2195libhIMTFQ3GF2iVXRWuGfU37X?= =?utf-8?q?QWO/84Hr1C74C2vkXFE3R0LEmFevssRczzuW+qiZSEtTsfnkLie75rRARebplfVzy?= =?utf-8?q?I8E7D0mYFAGGcxbWAkngffKKF4ODoslVi2ue6m8Vjz27wyjluFutAbBptW+yrhovg?= =?utf-8?q?ySMBrP87Dtv6HUkW91D4hPugi14JiCFKBSc5DGO9N47urbDkVB9Ld+ieFt6nAM3eM?= =?utf-8?q?uAfcPVMZb8HRY8i6ZW8/sZ6koNzhpJY9xj1fgQJgtZTnEMewWQ7LMtVoBnS/Hdsp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eF5o33O?= =?utf-8?q?AGWQlZ/FkSVq?= 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)(82310400014)(1800799015)(376005)(36860700004); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2024 09:10:01.5422 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4b0a77bc-1641-4586-0750-08dc65077d04 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: DB1PEPF000509E7.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR07MB7959 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_[set|clear|assign|test|test_and_[set|clear|assign]]() family of functions. Signed-off-by: Mattias Rönnblom --- app/test/test_bitops.c | 233 ++++++++++++++++++++++++++++++++++- lib/eal/include/rte_bitops.h | 1 - 2 files changed, 232 insertions(+), 2 deletions(-) diff --git a/app/test/test_bitops.c b/app/test/test_bitops.c index 12c1027e36..a0967260aa 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" @@ -60,6 +63,228 @@ GEN_TEST_BIT_ACCESS(test_bit_once_access_64, rte_bit_once_set, \ rte_bit_once_clear, rte_bit_once_assign, \ 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_test(addr, nr) \ + rte_bit_atomic_test(addr, nr, rte_memory_order_relaxed) + +GEN_TEST_BIT_ACCESS(test_bit_atomic_access_32, bit_atomic_set, \ + bit_atomic_clear, bit_atomic_assign, \ + bit_atomic_test, 32) + +GEN_TEST_BIT_ACCESS(test_bit_atomic_access_64, bit_atomic_set, \ + bit_atomic_clear, bit_atomic_assign, \ + 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 main = { \ + .word = &word \ + }; \ + struct parallel_access_lcore_ ## size worker = { \ + .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); \ + \ + main.bit = rte_rand_max(size); \ + do { \ + worker.bit = rte_rand_max(size); \ + } while (worker.bit == main.bit); \ + \ + int rc = rte_eal_remote_launch(run_parallel_assign_ ## size, \ + &worker, worker_lcore_id); \ + TEST_ASSERT(rc == 0, "Worker thread launch failed"); \ + \ + run_parallel_assign_ ## size(&main); \ + \ + rte_eal_mp_wait_lcore(); \ + \ + TEST_ASSERT(!main.failed, "Main lcore atomic access failed"); \ + TEST_ASSERT(!worker.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 main = { \ + .word = &word, \ + .bit = bit \ + }; \ + struct parallel_test_and_set_lcore_ ## size worker = { \ + .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, \ + &worker, worker_lcore_id); \ + TEST_ASSERT(rc == 0, "Worker thread launch failed"); \ + \ + run_parallel_test_and_modify_ ## size(&main); \ + \ + rte_eal_mp_wait_lcore(); \ + \ + uint64_t total_flips = main.flips + worker.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) + static uint32_t val32; static uint64_t val64; @@ -178,6 +403,12 @@ static struct unit_test_suite test_suite = { TEST_CASE(test_bit_access_64), TEST_CASE(test_bit_once_access_32), TEST_CASE(test_bit_once_access_64), + TEST_CASE(test_bit_atomic_access_32), + TEST_CASE(test_bit_atomic_access_64), + TEST_CASE(test_bit_atomic_parallel_assign_32), + TEST_CASE(test_bit_atomic_parallel_assign_64), + TEST_CASE(test_bit_atomic_parallel_test_and_modify_32), + TEST_CASE(test_bit_atomic_parallel_test_and_modify_64), TEST_CASE(test_bit_relaxed_set), TEST_CASE(test_bit_relaxed_clear), TEST_CASE(test_bit_relaxed_test_set_clear), diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 8c38a1ac03..bc6d79086b 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -485,7 +485,6 @@ extern "C" { 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.