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 19661A00C2; Thu, 24 Nov 2022 00:43:26 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2075742DA5; Thu, 24 Nov 2022 00:43:22 +0100 (CET) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 3BC68410DC for ; Thu, 24 Nov 2022 00:43:20 +0100 (CET) Received: by linux.microsoft.com (Postfix, from userid 1086) id 8AF1220B83C2; Wed, 23 Nov 2022 15:43:19 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 8AF1220B83C2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1669246999; bh=a29LKHUBn6dYjPv1TuHjxdpre2KT+LcfpURiTOEORR4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B1d6p/FUHmpW1XCGjU7lsJ2LwTLvmdHimIrY8F3g5yWQdSArhe7GcRg8N0Jpg8Uk3 xHkAjRPKb4aZr2c3Qn77I2exnYWTWcxW2DMHGzI7DfgNIjF+4DUEzppuWsOyCEnaoI eVtu1gFlIj718SjksxYtV0iOYAGPynIYE1LQFnfE= From: Tyler Retzlaff To: dev@dpdk.org Cc: Tyler Retzlaff Subject: [PATCH v2 1/2] eal: provide leading and trailing zero bit count abstraction Date: Wed, 23 Nov 2022 15:43:16 -0800 Message-Id: <1669246997-30592-2-git-send-email-roretzla@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1669246997-30592-1-git-send-email-roretzla@linux.microsoft.com> References: <1669241687-18810-1-git-send-email-roretzla@linux.microsoft.com> <1669246997-30592-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 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/meson.build | 1 + lib/eal/include/rte_bitcount.h | 265 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 lib/eal/include/rte_bitcount.h diff --git a/lib/eal/include/meson.build b/lib/eal/include/meson.build index cfcd40a..8ff1d65 100644 --- a/lib/eal/include/meson.build +++ b/lib/eal/include/meson.build @@ -5,6 +5,7 @@ includes += include_directories('.') headers += files( 'rte_alarm.h', + 'rte_bitcount.h', 'rte_bitmap.h', 'rte_bitops.h', 'rte_branch_prediction.h', diff --git a/lib/eal/include/rte_bitcount.h b/lib/eal/include/rte_bitcount.h new file mode 100644 index 0000000..587de52 --- /dev/null +++ b/lib/eal/include/rte_bitcount.h @@ -0,0 +1,265 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (C) 2022 Microsoft Corporation + */ + +#ifndef _RTE_BITCOUNT_H_ +#define _RTE_BITCOUNT_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#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_clz(unsigned int 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_clzl(unsigned long 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_clzll(unsigned long long 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_ctz(unsigned int 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_ctzl(unsigned long 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_ctzll(unsigned long long 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_clz(unsigned int 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_clzl(unsigned long v) +{ + return (unsigned int)__builtin_clzl(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_clzll(unsigned long 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_ctz(unsigned int 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_ctzl(unsigned long v) +{ + return (unsigned int)__builtin_ctzl(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_ctzll(unsigned long v) +{ + return (unsigned int)__builtin_ctzll(v); +} + +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_BITCOUNT_H_ */ + -- 1.8.3.1