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 B775043C3A; Sat, 2 Mar 2024 15:02:23 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 5C8B742D66; Sat, 2 Mar 2024 15:02:18 +0100 (CET) Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on2085.outbound.protection.outlook.com [40.107.8.85]) by mails.dpdk.org (Postfix) with ESMTP id 4CC72400D5 for ; Sat, 2 Mar 2024 15:02:16 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UHgc8D1bDOLHLOiDHICeivseQ1UFHIT7/8DqnrNFJkIi9ZXW7B8aUvyZgUlwDxZXe8YlTPppXcTUCDDl+9lPHIq3OtndmaIu0JG7y1wYUDt4hMmvQPtjO+uOFImlu04qghxEWUWrxnUiYR7T2E8KD4FbPaYuqMpLpYS+wH7vPhVYWKtHk1Q4d/MC9BRGldmQxbCbetWKaHKhIglT7Jg877o4lwy1JgAJqtZ58MRzuUB963DtwNpFWpWdcv46eXZjhku8RXs93h8tchHix1gbr0an3Mvw1jyE8BIO7v9I7Csu8rM4YTrpf+bkXPnUMtrtjcwGC65YuUBQzpBrWzV9pw== 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=SM8IGRjrTAoWi97L464kK8j1ZttmMeaybTdxjWzZlAQ=; b=BPCezhtZVwcFCT7ayxbSY1ckIe1B32dfxUOJGD9j1bLD7c160JuyiRFrTLdPnOoQBhD45qIsEmKK2AT9nnNJ2PkBwFlgG2AKZvHH4h3z11eFvzN/35MOIqoNVAo2mHrLIG2ZxbvEOxx1ORoMoOw8khQUsix4XM1lxFXsg+ro+hxgCf29pGjdyBk4I0FTfNz5ClDQhh2aM+XL6apzsQWC3eAQKPl/vVRg0x7gWSVoYdvrxWSk0sEgaqKNrsBzMWsbQ4kglhnzSSw+xuwrT/dlQy7G3Ai7YNwsFh/kSo6jhJj1tZ1QaUcmtysscxXLo/JsgFp3T9pS9D1zsTYvmIYWtw== 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=SM8IGRjrTAoWi97L464kK8j1ZttmMeaybTdxjWzZlAQ=; b=PxZMwGPEY/GjO/15mYZmpWmBukgIYLNOOXM+7hb4bmHWJfWuzYwHHEAHi/XapvWMO3ruIXhEkGAijH39ZoJZgs7nzzwkUhpVVtLFY8M6SoFNXE4G477fAIFFkqOwxg9wPDEtzXLp6AUrcKuI/6Cg8Wc73GuByvgech/45VGOofrha6XiNa64luEkqIeSQugZY40ObQAP+wpVLcDPzayhFIx0ivxBMxKgwNEtjZiJtYs2QFnupPWq5OTm0pct1su2un19BIoAXw+MqZe6t/wU4euZEdbvhIJBHS7OLJLiRuTbRaMn+D9KAI0byZrpgzxMPTwLeDL3b+7/IDuc2TrOWg== Received: from AM0PR04CA0064.eurprd04.prod.outlook.com (2603:10a6:208:1::41) by DBAPR07MB6549.eurprd07.prod.outlook.com (2603:10a6:10:186::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7339.32; Sat, 2 Mar 2024 14:02:14 +0000 Received: from AM4PEPF00027A69.eurprd04.prod.outlook.com (2603:10a6:208:1:cafe::30) by AM0PR04CA0064.outlook.office365.com (2603:10a6:208:1::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7339.36 via Frontend Transport; Sat, 2 Mar 2024 14:02:14 +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 AM4PEPF00027A69.mail.protection.outlook.com (10.167.16.87) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7362.11 via Frontend Transport; Sat, 2 Mar 2024 14:02:14 +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.1258.12; Sat, 2 Mar 2024 15:02:13 +0100 Received: from breslau.. (seliicwb00002.seli.gic.ericsson.se [10.156.25.100]) by seliicinfr00050.seli.gic.ericsson.se (Postfix) with ESMTP id AF30B1C006A; Sat, 2 Mar 2024 15:02:13 +0100 (CET) From: =?UTF-8?q?Mattias=20R=C3=B6nnblom?= To: CC: , Heng Wang , =?UTF-8?q?Mattias=20R=C3=B6nnblom?= Subject: [RFC 1/7] eal: extend bit manipulation functions Date: Sat, 2 Mar 2024 14:53:22 +0100 Message-ID: <20240302135328.531940-2-mattias.ronnblom@ericsson.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240302135328.531940-1-mattias.ronnblom@ericsson.com> References: <20240302135328.531940-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: AM4PEPF00027A69:EE_|DBAPR07MB6549:EE_ X-MS-Office365-Filtering-Correlation-Id: 615df190-2ece-4eb4-5c8f-08dc3ac15d05 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: n3VxHhbhHxwc2jqsg+JnRGOmEu+Lz5M5/o2R23Mk06I8CAhJiU/wajjoSwwMFXkYy1KzyT8dHtG0IIg2m7+R9jCkAl1WXrp7FO8VRBC5tPuVR8V1fvGVydmYv+LB/0dad4XkInK4FaWmcer6PDDW7K6JTjWjw7Kixh66NRn6ZERelisULROz3wzUl6/Lo0xm8WiRNl9yV+h4RsjyWGizBhR26spi8l6zaVbKmDCb/FvBp9IQaxjfWXTaJMZC5VnlNJDDiAs6G4F3zMLmJF7+v3Wn4pLCBP10W1nkfz+QQYcPcNBPIw6Upd6BuIzJMD2DtLCwsdoQKOnRAI7BIU+PynPasJ4jT5OLPWO+wV1T2hCVZ0CAHXI1TzldyrGBt/PiUfnnqOUfQCsKeuKPieTQ6dvjWIaOxtcm0vR9Bor+PrgK0lzoq0oqsWha/Wwy20hpNOTgOD30myuWWWWmRF/ZEHD0bNI2VwnxCQKDGmjYkXs3+Y98qRLZ4z4xs2hSoMGI4HjCRBRm9WAENraC1Qdb0wVLmQhjGONWbDsk5EM46MbMpgkklezPl4uLtd23obM/Hs5P5M3gRL7ULOGVrdf7WVM7teNe+y/n5N8J3cfwEG4iBTDM7O6Yla5O78TU/E1sHCvx9DTovasBvh/TwxNeEnQwKwomGlVpGUylUnvo1Va89GY1jM/F6wAL1olbvzs9bCJ97KlMQpDIKe8BILVTEF+IMP//ytR7lyjxbrE3AB8dOT7bsmoyoSMlbCMnYKmy 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); DIR:OUT; SFP:1101; X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Mar 2024 14:02:14.2874 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 615df190-2ece-4eb4-5c8f-08dc3ac15d05 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: AM4PEPF00027A69.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR07MB6549 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 | 194 ++++++++++++++++++++++++++++++++++- 1 file changed, 192 insertions(+), 2 deletions(-) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 449565eeae..9a368724d5 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,8 +12,9 @@ * @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 @@ -105,6 +107,194 @@ extern "C" { #define RTE_FIELD_GET64(mask, reg) \ ((typeof(mask))(((reg) & (mask)) >> rte_ctz64(mask))) +/** + * Test if a particular bit in a 32-bit word is set. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the 32-bit word to query. + * @param nr + * The index of the bit (0-31). + * @return + * Returns true if the bit is set, and false otherwise. + */ +static inline bool +rte_bit_test32(const uint32_t *addr, unsigned int nr); + +/** + * Set bit in 32-bit word. + * + * Set bit specified by @c nr in the 32-bit word pointed to by + * @c addr to '1'. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the 32-bit word to modify. + * @param nr + * The index of the bit (0-31). + */ +static inline void +rte_bit_set32(uint32_t *addr, unsigned int nr); + +/** + * Clear bit in 32-bit word. + * + * Set bit specified by @c nr in the 32-bit word pointed to by + * @c addr to '0'. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the 32-bit word to modify. + * @param nr + * The index of the bit (0-31). + */ +static inline void +rte_bit_clear32(uint32_t *addr, unsigned int nr); + +/** + * Assign a value to bit in a 32-bit word. + * + * Set bit specified by @c nr in the 32-bit word pointed to by + * @c addr to the value indicated by @c value. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the 32-bit word to modify. + * @param nr + * The index of the bit (0-31). + * @param value + * The new value of the bit - true for '1', or false for '0'. + */ +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); +} + +/** + * Test if a particular bit in a 64-bit word is set. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the 64-bit word to query. + * @param nr + * The index of the bit (0-63). + * @return + * Returns true if the bit is set, and false otherwise. + */ +static inline bool +rte_bit_test64(const uint64_t *addr, unsigned int nr); + +/** + * Set bit in 64-bit word. + * + * Set bit specified by @c nr in the 64-bit word pointed to by + * @c addr to '1'. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the 64-bit word to modify. + * @param nr + * The index of the bit (0-63). + */ +static inline void +rte_bit_set64(uint64_t *addr, unsigned int nr); + +/** + * Clear bit in 64-bit word. + * + * Set bit specified by @c nr in the 64-bit word pointed to by + * @c addr to '0'. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the 64-bit word to modify. + * @param nr + * The index of the bit (0-63). + */ +static inline void +rte_bit_clear64(uint64_t *addr, unsigned int nr); + +/** + * Assign a value to bit in a 64-bit word. + * + * Set bit specified by @c nr in the 64-bit word pointed to by + * @c addr to the value indicated by @c value. + * + * This function does not give any guarantees in regards to memory + * ordering or atomicity. + * + * @param addr + * A pointer to the 64-bit word to modify. + * @param nr + * The index of the bit (0-63). + * @param value + * The new value of the bit - true for '1', or false for '0'. + */ +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); +} + +#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,) + /*------------------------ 32-bit relaxed operations ------------------------*/ /** -- 2.34.1