From patchwork Tue Apr 4 00:11:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tyler Retzlaff X-Patchwork-Id: 125742 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id BE46E428C1; Tue, 4 Apr 2023 02:12:07 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A681F41141; Tue, 4 Apr 2023 02:12:02 +0200 (CEST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 825E140141 for ; Tue, 4 Apr 2023 02:12:00 +0200 (CEST) Received: by linux.microsoft.com (Postfix, from userid 1086) id C4529210DC6D; Mon, 3 Apr 2023 17:11:59 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com C4529210DC6D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680567119; bh=6NCEK3mmwXvFFERVSw4kuPcVm11KhEaaVdwhRzOGCeU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KPWNVpaBQ2IF6KOmgJ2g+8OJt4JuCHKfnSXeTRU/x1OyCnyyE9LSUnuidXX5kQbwg C6XOqYdqDEP2F6OxSTH9M+bn2j99tvqaeK6KlTvZBnBw0JDsLUJv2E14Pv2k/tdmho wh8fMP0W4tfa9hPIKHgko4ZrtIF+FT5B5AjFsk5c= From: Tyler Retzlaff To: dev@dpdk.org Cc: thomas@monjalon.net, stephen@networkplumber.org, mb@smartsharesystems.com, bruce.richardson@intel.com, ferruh.yigit@amd.com, david.marchand@redhat.com, Tyler Retzlaff Subject: [PATCH v8 1/3] eal: move bit count functions to bitops header Date: Mon, 3 Apr 2023 17:11:56 -0700 Message-Id: <1680567118-6435-2-git-send-email-roretzla@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1680567118-6435-1-git-send-email-roretzla@linux.microsoft.com> References: <1669241687-18810-1-git-send-email-roretzla@linux.microsoft.com> <1680567118-6435-1-git-send-email-roretzla@linux.microsoft.com> MIME-Version: 1.0 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 Move the following inline functions from rte_common.h to rte_bitops.h rte_combine32ms1b rte_combine64ms1b rte_bsf32 rte_bsf32_safe rte_bsf64 rte_bsf64_safe rte_fls_u32 rte_fls_u64 rte_is_power_of_2 rte_align32pow2 rte_align32prevpow2 rte_align64pow2 rte_align64prevpow2 rte_log2_u32 rte_log2_u64 Signed-off-by: Tyler Retzlaff Acked-by: Ferruh Yigit Acked-by: Morten Brørup Acked-by: Bruce Richardson --- app/test/test_common.c | 1 + lib/eal/common/rte_reciprocal.c | 1 + lib/eal/include/rte_bitops.h | 292 ++++++++++++++++++++++++++++++ lib/eal/include/rte_common.h | 293 ------------------------------- lib/pipeline/rte_swx_pipeline_internal.h | 1 + 5 files changed, 295 insertions(+), 293 deletions(-) diff --git a/app/test/test_common.c b/app/test/test_common.c index f89e1eb..cf4a2c7 100644 --- a/app/test/test_common.c +++ b/app/test/test_common.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include diff --git a/lib/eal/common/rte_reciprocal.c b/lib/eal/common/rte_reciprocal.c index 42dfa44..d47dc47 100644 --- a/lib/eal/common/rte_reciprocal.c +++ b/lib/eal/common/rte_reciprocal.c @@ -8,6 +8,7 @@ #include #include +#include #include "rte_reciprocal.h" diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h index f50dbe4..531479e 100644 --- a/lib/eal/include/rte_bitops.h +++ b/lib/eal/include/rte_bitops.h @@ -275,6 +275,298 @@ return val & mask; } +/** + * Combines 32b inputs most significant set bits into the least + * significant bits to construct a value with the same MSBs as x + * but all 1's under it. + * + * @param x + * The integer whose MSBs need to be combined with its LSBs + * @return + * The combined value. + */ +static inline uint32_t +rte_combine32ms1b(uint32_t x) +{ + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + + return x; +} + +/** + * Combines 64b inputs most significant set bits into the least + * significant bits to construct a value with the same MSBs as x + * but all 1's under it. + * + * @param v + * The integer whose MSBs need to be combined with its LSBs + * @return + * The combined value. + */ +static inline uint64_t +rte_combine64ms1b(uint64_t v) +{ + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v |= v >> 32; + + return v; +} + +/** + * Searches the input parameter for the least significant set bit + * (starting from zero). + * If a least significant 1 bit is found, its bit index is returned. + * If the content of the input parameter is zero, then the content of the return + * value is undefined. + * @param v + * input parameter, should not be zero. + * @return + * least significant set bit in the input parameter. + */ +static inline uint32_t +rte_bsf32(uint32_t v) +{ + return (uint32_t)__builtin_ctz(v); +} + +/** + * Searches the input parameter for the least significant set bit + * (starting from zero). Safe version (checks for input parameter being zero). + * + * @warning ``pos`` must be a valid pointer. It is not checked! + * + * @param v + * The input parameter. + * @param pos + * If ``v`` was not 0, this value will contain position of least significant + * bit within the input parameter. + * @return + * Returns 0 if ``v`` was 0, otherwise returns 1. + */ +static inline int +rte_bsf32_safe(uint32_t v, uint32_t *pos) +{ + if (v == 0) + return 0; + + *pos = rte_bsf32(v); + return 1; +} + +/** + * Searches the input parameter for the least significant set bit + * (starting from zero). + * If a least significant 1 bit is found, its bit index is returned. + * If the content of the input parameter is zero, then the content of the return + * value is undefined. + * @param v + * input parameter, should not be zero. + * @return + * least significant set bit in the input parameter. + */ +static inline uint32_t +rte_bsf64(uint64_t v) +{ + return (uint32_t)__builtin_ctzll(v); +} + +/** + * Searches the input parameter for the least significant set bit + * (starting from zero). Safe version (checks for input parameter being zero). + * + * @warning ``pos`` must be a valid pointer. It is not checked! + * + * @param v + * The input parameter. + * @param pos + * If ``v`` was not 0, this value will contain position of least significant + * bit within the input parameter. + * @return + * Returns 0 if ``v`` was 0, otherwise returns 1. + */ +static inline int +rte_bsf64_safe(uint64_t v, uint32_t *pos) +{ + if (v == 0) + return 0; + + *pos = rte_bsf64(v); + return 1; +} + +/** + * Return the last (most-significant) bit set. + * + * @note The last (most significant) bit is at position 32. + * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32 + * + * @param x + * The input parameter. + * @return + * The last (most-significant) bit set, or 0 if the input is 0. + */ +static inline uint32_t +rte_fls_u32(uint32_t x) +{ + return (x == 0) ? 0 : 32 - __builtin_clz(x); +} + +/** + * Return the last (most-significant) bit set. + * + * @note The last (most significant) bit is at position 64. + * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1, + * rte_fls_u64(0x8000000000000000) = 64 + * + * @param x + * The input parameter. + * @return + * The last (most-significant) bit set, or 0 if the input is 0. + */ +static inline uint32_t +rte_fls_u64(uint64_t x) +{ + return (x == 0) ? 0 : 64 - __builtin_clzll(x); +} + +/*********** Macros to work with powers of 2 ********/ + +/** + * Macro to return 1 if n is a power of 2, 0 otherwise + */ +#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n))) + +/** + * Returns true if n is a power of 2 + * @param n + * Number to check + * @return 1 if true, 0 otherwise + */ +static inline int +rte_is_power_of_2(uint32_t n) +{ + return n && !(n & (n - 1)); +} + +/** + * Aligns input parameter to the next power of 2 + * + * @param x + * The integer value to align + * + * @return + * Input parameter aligned to the next power of 2 + */ +static inline uint32_t +rte_align32pow2(uint32_t x) +{ + x--; + x = rte_combine32ms1b(x); + + return x + 1; +} + +/** + * Aligns input parameter to the previous power of 2 + * + * @param x + * The integer value to align + * + * @return + * Input parameter aligned to the previous power of 2 + */ +static inline uint32_t +rte_align32prevpow2(uint32_t x) +{ + x = rte_combine32ms1b(x); + + return x - (x >> 1); +} + +/** + * Aligns 64b input parameter to the next power of 2 + * + * @param v + * The 64b value to align + * + * @return + * Input parameter aligned to the next power of 2 + */ +static inline uint64_t +rte_align64pow2(uint64_t v) +{ + v--; + v = rte_combine64ms1b(v); + + return v + 1; +} + +/** + * Aligns 64b input parameter to the previous power of 2 + * + * @param v + * The 64b value to align + * + * @return + * Input parameter aligned to the previous power of 2 + */ +static inline uint64_t +rte_align64prevpow2(uint64_t v) +{ + v = rte_combine64ms1b(v); + + return v - (v >> 1); +} + +/** + * Return the rounded-up log2 of a integer. + * + * @note Contrary to the logarithm mathematical operation, + * rte_log2_u32(0) == 0 and not -inf. + * + * @param v + * The input parameter. + * @return + * The rounded-up log2 of the input, or 0 if the input is 0. + */ +static inline uint32_t +rte_log2_u32(uint32_t v) +{ + if (v == 0) + return 0; + v = rte_align32pow2(v); + return rte_bsf32(v); +} + +/** + * Return the rounded-up log2 of a 64-bit integer. + * + * @note Contrary to the logarithm mathematical operation, + * rte_log2_u64(0) == 0 and not -inf. + * + * @param v + * The input parameter. + * @return + * The rounded-up log2 of the input, or 0 if the input is 0. + */ +static inline uint32_t +rte_log2_u64(uint64_t v) +{ + if (v == 0) + return 0; + v = rte_align64pow2(v); + /* we checked for v being 0 already, so no undefined behavior */ + return rte_bsf64(v); +} + #ifdef __cplusplus } #endif diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h index 15765b4..c5ad69c 100644 --- a/lib/eal/include/rte_common.h +++ b/lib/eal/include/rte_common.h @@ -471,140 +471,6 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void) /** Marker for 8B alignment in a structure. */ __extension__ typedef uint64_t RTE_MARKER64[0]; -/** - * Combines 32b inputs most significant set bits into the least - * significant bits to construct a value with the same MSBs as x - * but all 1's under it. - * - * @param x - * The integer whose MSBs need to be combined with its LSBs - * @return - * The combined value. - */ -static inline uint32_t -rte_combine32ms1b(uint32_t x) -{ - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; - - return x; -} - -/** - * Combines 64b inputs most significant set bits into the least - * significant bits to construct a value with the same MSBs as x - * but all 1's under it. - * - * @param v - * The integer whose MSBs need to be combined with its LSBs - * @return - * The combined value. - */ -static inline uint64_t -rte_combine64ms1b(uint64_t v) -{ - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v |= v >> 32; - - return v; -} - -/*********** Macros to work with powers of 2 ********/ - -/** - * Macro to return 1 if n is a power of 2, 0 otherwise - */ -#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n))) - -/** - * Returns true if n is a power of 2 - * @param n - * Number to check - * @return 1 if true, 0 otherwise - */ -static inline int -rte_is_power_of_2(uint32_t n) -{ - return n && !(n & (n - 1)); -} - -/** - * Aligns input parameter to the next power of 2 - * - * @param x - * The integer value to align - * - * @return - * Input parameter aligned to the next power of 2 - */ -static inline uint32_t -rte_align32pow2(uint32_t x) -{ - x--; - x = rte_combine32ms1b(x); - - return x + 1; -} - -/** - * Aligns input parameter to the previous power of 2 - * - * @param x - * The integer value to align - * - * @return - * Input parameter aligned to the previous power of 2 - */ -static inline uint32_t -rte_align32prevpow2(uint32_t x) -{ - x = rte_combine32ms1b(x); - - return x - (x >> 1); -} - -/** - * Aligns 64b input parameter to the next power of 2 - * - * @param v - * The 64b value to align - * - * @return - * Input parameter aligned to the next power of 2 - */ -static inline uint64_t -rte_align64pow2(uint64_t v) -{ - v--; - v = rte_combine64ms1b(v); - - return v + 1; -} - -/** - * Aligns 64b input parameter to the previous power of 2 - * - * @param v - * The 64b value to align - * - * @return - * Input parameter aligned to the previous power of 2 - */ -static inline uint64_t -rte_align64prevpow2(uint64_t v) -{ - v = rte_combine64ms1b(v); - - return v - (v >> 1); -} - /*********** Macros for calculating min and max **********/ /** @@ -629,165 +495,6 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void) /*********** Other general functions / macros ********/ -/** - * Searches the input parameter for the least significant set bit - * (starting from zero). - * If a least significant 1 bit is found, its bit index is returned. - * If the content of the input parameter is zero, then the content of the return - * value is undefined. - * @param v - * input parameter, should not be zero. - * @return - * least significant set bit in the input parameter. - */ -static inline uint32_t -rte_bsf32(uint32_t v) -{ - return (uint32_t)__builtin_ctz(v); -} - -/** - * Searches the input parameter for the least significant set bit - * (starting from zero). Safe version (checks for input parameter being zero). - * - * @warning ``pos`` must be a valid pointer. It is not checked! - * - * @param v - * The input parameter. - * @param pos - * If ``v`` was not 0, this value will contain position of least significant - * bit within the input parameter. - * @return - * Returns 0 if ``v`` was 0, otherwise returns 1. - */ -static inline int -rte_bsf32_safe(uint32_t v, uint32_t *pos) -{ - if (v == 0) - return 0; - - *pos = rte_bsf32(v); - return 1; -} - -/** - * Return the rounded-up log2 of a integer. - * - * @note Contrary to the logarithm mathematical operation, - * rte_log2_u32(0) == 0 and not -inf. - * - * @param v - * The input parameter. - * @return - * The rounded-up log2 of the input, or 0 if the input is 0. - */ -static inline uint32_t -rte_log2_u32(uint32_t v) -{ - if (v == 0) - return 0; - v = rte_align32pow2(v); - return rte_bsf32(v); -} - - -/** - * Return the last (most-significant) bit set. - * - * @note The last (most significant) bit is at position 32. - * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32 - * - * @param x - * The input parameter. - * @return - * The last (most-significant) bit set, or 0 if the input is 0. - */ -static inline uint32_t -rte_fls_u32(uint32_t x) -{ - return (x == 0) ? 0 : 32 - __builtin_clz(x); -} - -/** - * Searches the input parameter for the least significant set bit - * (starting from zero). - * If a least significant 1 bit is found, its bit index is returned. - * If the content of the input parameter is zero, then the content of the return - * value is undefined. - * @param v - * input parameter, should not be zero. - * @return - * least significant set bit in the input parameter. - */ -static inline uint32_t -rte_bsf64(uint64_t v) -{ - return (uint32_t)__builtin_ctzll(v); -} - -/** - * Searches the input parameter for the least significant set bit - * (starting from zero). Safe version (checks for input parameter being zero). - * - * @warning ``pos`` must be a valid pointer. It is not checked! - * - * @param v - * The input parameter. - * @param pos - * If ``v`` was not 0, this value will contain position of least significant - * bit within the input parameter. - * @return - * Returns 0 if ``v`` was 0, otherwise returns 1. - */ -static inline int -rte_bsf64_safe(uint64_t v, uint32_t *pos) -{ - if (v == 0) - return 0; - - *pos = rte_bsf64(v); - return 1; -} - -/** - * Return the last (most-significant) bit set. - * - * @note The last (most significant) bit is at position 64. - * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1, - * rte_fls_u64(0x8000000000000000) = 64 - * - * @param x - * The input parameter. - * @return - * The last (most-significant) bit set, or 0 if the input is 0. - */ -static inline uint32_t -rte_fls_u64(uint64_t x) -{ - return (x == 0) ? 0 : 64 - __builtin_clzll(x); -} - -/** - * Return the rounded-up log2 of a 64-bit integer. - * - * @note Contrary to the logarithm mathematical operation, - * rte_log2_u64(0) == 0 and not -inf. - * - * @param v - * The input parameter. - * @return - * The rounded-up log2 of the input, or 0 if the input is 0. - */ -static inline uint32_t -rte_log2_u64(uint64_t v) -{ - if (v == 0) - return 0; - v = rte_align64pow2(v); - /* we checked for v being 0 already, so no undefined behavior */ - return rte_bsf64(v); -} - #ifndef offsetof /** Return the offset of a field in a structure. */ #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER) diff --git a/lib/pipeline/rte_swx_pipeline_internal.h b/lib/pipeline/rte_swx_pipeline_internal.h index 2f24e1a..a67b6e9 100644 --- a/lib/pipeline/rte_swx_pipeline_internal.h +++ b/lib/pipeline/rte_swx_pipeline_internal.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include