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 F2EDA43F49; Mon, 29 Apr 2024 12:02:10 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 67D2F402BE; Mon, 29 Apr 2024 12:02:07 +0200 (CEST) Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on2083.outbound.protection.outlook.com [40.107.6.83]) by mails.dpdk.org (Postfix) with ESMTP id 3D203402C6 for ; Mon, 29 Apr 2024 12:02:06 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CfV9DJgPxBTwBC++0L99FWER4YgavUImCL7F0nBGVzBzA3wHqRd8bFT/37zkTHJlGvu2npCTYLlCrvK5PWDOX+0P80D9CnQFVBD5QOYWJlCf1Qm1ilHK0J9hh6MrHi3hnIZ/LesL0fFB0hAtIDjVnLyGNtUukeIo3RdAeEnDMvMoiZaY6YCmnVxyrTNgw9C/raG4RUAAfnC9P1Wlk/PuDnqftrQpar+uUDbgDv9wu90Bjck+scXXeLcZsUz0vYcEV7I9n36K45fjkBmJtr/6L+510Oux61Yypu6kpmaOHru5XxaEwM5b53yVG+FmcRpzbSX4puXouKL6aoQtpnBXvQ== 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=IqnbVv24AfUXeFe4Y3k5gSs3DT/2YMpJZI9hY4gF6co=; b=dPRH3nEM4k7J5O6ad7EL99EHK3fw1nzLexvLmjkFHZrS4IcPVpCcWi7RbKKBYOCzH3PeACng3ugEYsqWgVjryvna8WsU4WzYgigjXoOOKngGJx8TorJ2nV2oBEMb0xYikzuHjnXBC0dlSdevuFT+p1lQlZY5TO9xiJ4/jzlen8+YVipAPpRpDrXS02hpa8rF1RHPidPLKFfxyH1c9/epgE9QdxzqSwQ9lrFtPrZ1koL4iUv4s/LImIiYusTb9bzX7NIDZp3ixr/u1Yt65YyX9b8AD7fV/uYcYChFWdj+NRnA/ns3i/QBkdmTAHF036lowpC/qklbIqlYUHLsxCHGwA== 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=IqnbVv24AfUXeFe4Y3k5gSs3DT/2YMpJZI9hY4gF6co=; b=H9bPWPyFbdKdFaaTIlCf9mf7vExofd9/axsFijBNtmayjX85c5EPgPy3PrczPDwkrZ79/MerpYQIkpbZbUXTAQzLrkmD8k1F4bJp6LAq5il9SsgBBfr/g4+O7hiH1CxXHBqhxyxKluys+53/ZnSkqd+85rYnghx5DJZXvrH3ZeFGVrVEsMWSJluuct5Y6Lf0YYuDqsCWUHQtRhHuBwDjTLK5JUkInH79xYuqkJZqEBVJz0nLLlgsE4xMbUGDot9ctWvXb+/070rUPNXBhH+iAci8EHaJs0XGGr2COzTAhX9kiCNF6oOtoq9mduS081H049+0yS1f8R9kBGMDXH3IZw== Received: from AM0PR06CA0094.eurprd06.prod.outlook.com (2603:10a6:208:fa::35) by DU2PR07MB8238.eurprd07.prod.outlook.com (2603:10a6:10:277::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.34; Mon, 29 Apr 2024 10:02:04 +0000 Received: from AMS0EPF000001B6.eurprd05.prod.outlook.com (2603:10a6:208:fa:cafe::3c) by AM0PR06CA0094.outlook.office365.com (2603:10a6:208:fa::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.34 via Frontend Transport; Mon, 29 Apr 2024 10:02:04 +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 AMS0EPF000001B6.mail.protection.outlook.com (10.167.16.170) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.18 via Frontend Transport; Mon, 29 Apr 2024 10:02:04 +0000 Received: from seliicinfr00050.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; Mon, 29 Apr 2024 12:02:04 +0200 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id 68CE91C006A; Mon, 29 Apr 2024 12:02:04 +0200 (CEST) From: =?UTF-8?q?Mattias=20R=C3=B6nnblom?= To: CC: , Heng Wang , "Stephen Hemminger" , Tyler Retzlaff , =?UTF-8?q?Morten=20Br=C3=B8rup?= , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Subject: [RFC v3 1/6] eal: extend bit manipulation functionality Date: Mon, 29 Apr 2024 11:51:33 +0200 Message-ID: <20240429095138.106849-2-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240429095138.106849-1-mattias.ronnblom@ericsson.com> References: <20240425085853.97888-2-mattias.ronnblom@ericsson.com> <20240429095138.106849-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: AMS0EPF000001B6:EE_|DU2PR07MB8238:EE_ X-MS-Office365-Filtering-Correlation-Id: 34454810-d851-42aa-7768-08dc68336c3a 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?aTU0R0tSOUhDdVJIZm5QNThsSkpPNzNSbjB3VEpYWnFpUFBpZlp3eFdaaFNQ?= =?utf-8?B?U2pwK2xhTzZVK1JwMkZ3bko0aHNWNi9uTVlpMW9veWRuQzdZZGtjUnB5MkJ5?= =?utf-8?B?cU5rWDY3OWFNUmNFTnBwT01pbjNHcE5RQi9WY3RuUGJiMlBvQVU0Mzh5U09M?= =?utf-8?B?Vnc3Y0NTRGZxTnhyWFNBYTF4VTBDcTV4L0lWMGx3QktCbDFqSWJkenRXWEQx?= =?utf-8?B?STc1NUNIUWF4V05oc3dhMEN5TVFjWHdadXMvZ0ExNjlZRkwrMHI3SytDVlcv?= =?utf-8?B?Rm02M3NGdkNhTllNTWNhYkZUb2d6QmkrRzZCajhGcGY5Vnh4SUdmVEtyREpF?= =?utf-8?B?Q1dhZjdwUXBMWjhDcnVwV0U3ZzlOaWN6YXh6dWFjeGpYWG1VKzhoS01IWi8z?= =?utf-8?B?aWJJcmozRXFFM2toSnB6T0pQZG51b1pjdjRWSDFNTHRKVnF6RllXczBYU1JW?= =?utf-8?B?UTF0eXQ1SXNqTFp1K2p1V0F6d25MaXJGMjFNZGZJbWxpczJOZVVrdUloOUJE?= =?utf-8?B?ZVZnYjFGWFZRNzdSTjJna0pHQmVlY2hBRnN5MC9jbGpZSE5TTlNKUGRrUWNz?= =?utf-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E5ucmpJ?= =?utf-8?B?NitUYzJTR0lPaVQxbFZETUMzOGZVY3ZWTTREMFlSMWdPeDk3SThReFNWZmpY?= =?utf-8?B?ODJleWcyU2hRQmRxWHRRaDJXWkhkL1VrSXkzMlI5MytzbXgvbFplWDNxWEFs?= =?utf-8?B?N1doNG80THdNWTdzVFVvNERFQzlsL01RU2VZVW1WMlNpMTZ4NkVxcDVKdklG?= =?utf-8?B?dzU1YUthcHFOYUJ5K0FIUFNnMWEvNzJLOEw5aFFxYXIzWHhrRXBuMGw1R3NW?= =?utf-8?B?UzRDSXFyb1ZJeVh6Rkt4c1ppbFo4ZnJMMDZBY2lqNW1uY01JYTdxbXVnZ01u?= =?utf-8?B?UkRadEcvNkZ0ZkxOQURuSVl0NjZDdDV5R0prU1FXd2w5ZHEwU1hxUVhuWm1N?= =?utf-8?Q?F6QoWHH+nslGj?= 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: 29 Apr 2024 10:02:04.7690 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 34454810-d851-42aa-7768-08dc68336c3a 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: AMS0EPF000001B6.eurprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR07MB8238 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. RFC v3: * Work around lack of C++ support for _Generic (Tyler Retzlaff). * Fix ','-related checkpatch warnings. Signed-off-by: Mattias Rönnblom --- lib/eal/include/rte_bitops.h | 218 ++++++++++++++++++++++++++++++++++- 1 file changed, 216 insertions(+), 2 deletions(-) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 449565eeae..fb2e3dae7b 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 ------------------------*/ /** @@ -787,6 +941,66 @@ 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 + +#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) + #endif #endif /* _RTE_BITOPS_H_ */ -- 2.34.1