From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 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=20R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit 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?B?UGZMdStJTk1TU3k4WEw1VjRMT3h2ejl1TFZodE1yY2REUkV0VkdlVGl0MkZK?= =?utf-8?B?YTdWbU12Q1N5Ymo0YUpET1duaTdKaDF1RFpZWXdHUDVNb1F4QXVSZFR3amFz?= =?utf-8?B?UVZkU3BES3p5bi9oOFRIK0hDaUZDZDZhN3NlVFBCVnovbDh6UDk2VXNQR0lv?= =?utf-8?B?aTVFLzJEcDI1bGd0QUhuQ1I5bVo5dXhUbWRBMDlUcnpiWENMa2dEQ1BKR0xy?= =?utf-8?B?bDNsalJNWWI3QVI0ZlMrdnhOVVlMY3RabW9pWU1Nc3JWVW5GZHA0cnNYM2gw?= =?utf-8?B?djR5NGYvZXBvTkxrOFFvS3lMRXNzY2hhWVRWVktTNVJ6Nm1Wb2tpV1U5NkdI?= =?utf-8?B?S0JDRmpnb1lzb1JuUWlzYUNPRlhBVW9BY2s4c1FuOHlmd3FJZ3Vkc2JqbHhs?= =?utf-8?B?TlJWV3ltNjkydFdWT2dDL1dKbGFWbER6anltRTFFQzMrQThpN0F3Zjh5VURu?= =?utf-8?B?Q3ZOTk1MY3RCSFp0dVc5YnhpdUpTR3Q4SlJISTdwTFNDK0p5dnVRRldsaHQ3?= =?utf-8?B?RFVGbzQrRlp3akgxYTZDVm5ucHpJN21JVERma2pST1pDUXljNlpUdS80bGNu?= =?utf-8?B?cEhFTF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8?B?ZzdlK0JsbHNoU0t5MHowWlRld25PS0VSQ1NIbjBrdFRaK2VzRERiWVFsbWtx?= =?utf-8?B?Wm5oM1BEUVFUOW9PSVJkeEorcFlPYUdzRm1NUHRVV2R5Z1JzRDZuOTRtK0RI?= =?utf-8?B?V1FzMGJsSjBOMzE4WEFIVFhRVVFrZHVPV0gwZjFLQnRqdE5uTGR4RHRxTS9E?= =?utf-8?B?MW43RHU4Y1AyM1NPUHdLS2RaV1dORCtJYjJXTi8xSERPU3ZkSmtsRHlSdHFp?= =?utf-8?B?RGplQ2tZMFkvTm8yeGFQNU5Ld0pOZzZpYTduZ3ErUjA5Wmh1a3IydlJxb0Vq?= =?utf-8?B?WTk1azNBQi9xVVlhUjVzU3AxTGFwc2IrbFEwOG5Ka0UreUpFMmxJZlgrcUs5?= =?utf-8?Q?iLscufQS4EaGD?= 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)(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 ------------------------*/ /** -- 2.34.1