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 04153A00C2; Fri, 6 Jan 2023 23:02:12 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9C5BA42D27; Fri, 6 Jan 2023 23:01:56 +0100 (CET) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id C749D40141 for ; Fri, 6 Jan 2023 23:01:52 +0100 (CET) Received: by linux.microsoft.com (Postfix, from userid 1086) id 24DB620B928C; Fri, 6 Jan 2023 14:01:52 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 24DB620B928C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1673042512; bh=fg0cx4IITuocHIJnOdtQsMDf/87VOCioobtL0TXOi7k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lK0/L6LH/nRtzSiEZEP+0O+CJzWmHILerPbnqr6yFlg+34XITHkphUbRHTm71iteq Wn/P4QuUU1mw6rvKE2DavMY7Z/tETQR69zUrPmp+d6hDheM+VKrcd5Wk0X3kdrdwfp nuLjioRVfbHKPRtMM/zwnIjIF/R+reDPuHFBFUsU= From: Tyler Retzlaff To: dev@dpdk.org Cc: mb@smartsharesystems.com, thomas@monjalon.net, david.marchand@redhat.com, Tyler Retzlaff , Tyler Retzlaff Subject: [PATCH v3 2/3] eal: provide leading and trailing zero bit count abstraction Date: Fri, 6 Jan 2023 14:01:44 -0800 Message-Id: <1673042505-756-3-git-send-email-roretzla@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1673042505-756-1-git-send-email-roretzla@linux.microsoft.com> References: <1669241687-18810-1-git-send-email-roretzla@linux.microsoft.com> <1673042505-756-1-git-send-email-roretzla@linux.microsoft.com> 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 From: Tyler Retzlaff Provide an abstraction for leading and trailing zero bit counting functions to hide compiler specific intrinsics and builtins. Signed-off-by: Tyler Retzlaff --- lib/eal/include/rte_bitops.h | 168 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index 531479e..387d7aa 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2020 Arm Limited + * Copyright(c) 2010-2019 Intel Corporation + * Copyright(c) 2023 Microsoft Corporation */ #ifndef _RTE_BITOPS_H_ @@ -275,6 +277,172 @@ return val & mask; } +#ifdef RTE_TOOLCHAIN_MSVC + +/** + * @warning + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice + * + * Get the count of leading 0-bits in v. + * + * @param v + * The value. + * @return + * The count of leading zero bits. + */ +__rte_experimental +static inline unsigned int +rte_clz32(uint32_t v) +{ + unsigned long rv; + + (void)_BitScanReverse(&rv, v); + + return (unsigned int)rv; +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice + * + * Get the count of leading 0-bits in v. + * + * @param v + * The value. + * @return + * The count of leading zero bits. + */ +__rte_experimental +static inline unsigned int +rte_clz64(uint64_t v) +{ + unsigned long rv; + + (void)_BitScanReverse64(&rv, v); + + return (unsigned int)rv; +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice + * + * Get the count of trailing 0-bits in v. + * + * @param v + * The value. + * @return + * The count of trailing zero bits. + */ +__rte_experimental +static inline unsigned int +rte_ctz32(uint32_t v) +{ + unsigned long rv; + + (void)_BitScanForward(&rv, v); + + return (unsigned int)rv; +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice + * + * Get the count of trailing 0-bits in v. + * + * @param v + * The value. + * @return + * The count of trailing zero bits. + */ +__rte_experimental +static inline unsigned int +rte_ctz64(uint64_t v) +{ + unsigned long rv; + + (void)_BitScanForward64(&rv, v); + + return (unsigned int)rv; +} + +#else + +/** + * @warning + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice + * + * Get the count of leading 0-bits in v. + * + * @param v + * The value. + * @return + * The count of leading zero bits. + */ +__rte_experimental +static inline unsigned int +rte_clz32(uint32_t v) +{ + return (unsigned int)__builtin_clz(v); +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice + * + * Get the count of leading 0-bits in v. + * + * @param v + * The value. + * @return + * The count of leading zero bits. + */ +__rte_experimental +static inline unsigned int +rte_clz64(uint64_t v) +{ + return (unsigned int)__builtin_clzll(v); +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice + * + * Get the count of trailing 0-bits in v. + * + * @param v + * The value. + * @return + * The count of trailing zero bits. + */ +__rte_experimental +static inline unsigned int +rte_ctz32(uint32_t v) +{ + return (unsigned int)__builtin_ctz(v); +} + +/** + * @warning + * @b EXPERIMENTAL: this API may change, or be removed, without prior notice + * + * Get the count of trailing 0-bits in v. + * + * @param v + * The value. + * @return + * The count of trailing zero bits. + */ +__rte_experimental +static inline unsigned int +rte_ctz64(uint64_t v) +{ + return (unsigned int)__builtin_ctzll(v); +} + +#endif + /** * Combines 32b inputs most significant set bits into the least * significant bits to construct a value with the same MSBs as x -- 1.8.3.1