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 14B8C43B8F; Mon, 4 Mar 2024 16:41:13 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id B9D4840A4B; Mon, 4 Mar 2024 16:41:12 +0100 (CET) Received: from mail.lysator.liu.se (mail.lysator.liu.se [130.236.254.3]) by mails.dpdk.org (Postfix) with ESMTP id B5A0E40695 for ; Mon, 4 Mar 2024 16:41:11 +0100 (CET) Received: from mail.lysator.liu.se (localhost [127.0.0.1]) by mail.lysator.liu.se (Postfix) with ESMTP id 6718BA0D2 for ; Mon, 4 Mar 2024 16:41:11 +0100 (CET) Received: by mail.lysator.liu.se (Postfix, from userid 1004) id 59CBAA1A4; Mon, 4 Mar 2024 16:41:11 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on hermod.lysator.liu.se X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=ALL_TRUSTED,AWL, T_SCC_BODY_TEXT_LINE autolearn=disabled version=4.0.0 X-Spam-Score: -1.4 Received: from [192.168.1.59] (h-62-63-215-114.A163.priv.bahnhof.se [62.63.215.114]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.lysator.liu.se (Postfix) with ESMTPSA id E9A09A0D1; Mon, 4 Mar 2024 16:41:06 +0100 (CET) Message-ID: <0db7db8e-ca00-45e5-aaf1-c13e6cd1fbff@lysator.liu.se> Date: Mon, 4 Mar 2024 16:41:06 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 2/7] eal: add generic bit manipulation macros Content-Language: en-US To: Heng Wang , =?UTF-8?Q?Mattias_R=C3=B6nnblom?= , "dev@dpdk.org" References: <20240302135328.531940-1-mattias.ronnblom@ericsson.com> <20240302135328.531940-3-mattias.ronnblom@ericsson.com> From: =?UTF-8?Q?Mattias_R=C3=B6nnblom?= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: ClamAV using ClamSMTP 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 On 2024-03-04 09:16, Heng Wang wrote: > Hi Mattias, > I have a comment about the _Generic. What if the user gives uint8_t * or uint16_t * as the address. One improvement is that we could add a default branch in _Generic to throw a compiler error or assert false. If the user pass an incompatible pointer, the compiler will generate an error. > Another question is what if nr >= sizeof(type) ? What if you do, for example, (uint32_t)1 << 35? Maybe we could add an assert in the implementation? > There are already such asserts in the functions the macro delegates to. That said, DPDK RTE_ASSERT()s are disabled even in debug builds, so I'm not sure it's going to help anyone. > Regards, > Heng > > -----Original Message----- > From: Mattias Rönnblom > Sent: Saturday, March 2, 2024 2:53 PM > To: dev@dpdk.org > Cc: hofors@lysator.liu.se; Heng Wang ; Mattias Rönnblom > Subject: [RFC 2/7] eal: add generic bit manipulation macros > > Add bit-level test/set/clear/assign macros operating on both 32-bit and 64-bit words by means of C11 generic selection. > > Signed-off-by: Mattias Rönnblom > --- > lib/eal/include/rte_bitops.h | 81 ++++++++++++++++++++++++++++++++++++ > 1 file changed, 81 insertions(+) > > diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 9a368724d5..afd0f11033 100644 > --- a/lib/eal/include/rte_bitops.h > +++ b/lib/eal/include/rte_bitops.h > @@ -107,6 +107,87 @@ extern "C" { > #define RTE_FIELD_GET64(mask, reg) \ > ((typeof(mask))(((reg) & (mask)) >> rte_ctz64(mask))) > > +/** > + * 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) > + > +/** > + * 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) > + > +/** > + * 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) > + > +/** > + * 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) > + > /** > * Test if a particular bit in a 32-bit word is set. > * > -- > 2.34.1 >