From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jerin Jacob Subject: [PATCH v2 1/2] eal: introduce rte version of fls Date: Wed, 7 Nov 2018 06:59:03 +0000 Message-ID: <20181107065833.16756-1-jerin.jacob@caviumnetworks.com> References: <20181106114435.14770-1-jerin.jacob@caviumnetworks.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Cc: "thomas@monjalon.net" , "sthemmin@microsoft.com" , "shaopeng.he@intel.com" , "Jacob, Jerin" To: "dev@dpdk.org" Return-path: Received: from NAM03-DM3-obe.outbound.protection.outlook.com (mail-dm3nam03on0076.outbound.protection.outlook.com [104.47.41.76]) by dpdk.org (Postfix) with ESMTP id 2767B1BE0 for ; Wed, 7 Nov 2018 07:59:06 +0100 (CET) In-Reply-To: <20181106114435.14770-1-jerin.jacob@caviumnetworks.com> Content-Language: en-US List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The function returns the last (most-significant) bit set. Added unit testcase to verify rte_fls_u32(). Signed-off-by: Jerin Jacob --- v2: - Introduce rte_fls_u32() --- lib/librte_eal/common/include/rte_common.h | 19 +++++++++++++ test/test/test_common.c | 32 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/co= mmon/include/rte_common.h index cba7bbc1d..87f0f6302 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -473,6 +473,25 @@ rte_log2_u32(uint32_t v) return rte_bsf32(v); } =20 + +/** + * Return the last (most-significant) bit set. + * + * @note The last (most significant) bit is at position 32. + * @note rte_fls_u32(0) =3D 0, rte_fls_u32(1) =3D 1, rte_fls_u32(0x8000000= 0) =3D 32 + * + * @param x + * The input parameter. + * @return + * The last (most-significant) bit set, or 0 if the input is 0. + */ +static inline int +rte_fls_u32(uint32_t x) +{ + return (x =3D=3D 0) ? 0 : 32 - __builtin_clz(x); +} + + #ifndef offsetof /** Return the offset of a field in a structure. */ #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER) diff --git a/test/test/test_common.c b/test/test/test_common.c index 7a67e458e..c6d17baae 100644 --- a/test/test/test_common.c +++ b/test/test/test_common.c @@ -188,6 +188,37 @@ test_log2(void) return 0; } =20 +static int +test_fls(void) +{ + struct fls_test_vector { + uint32_t arg; + int rc; + }; + int expected, rc; + uint32_t i, arg; + + const struct fls_test_vector test[] =3D { + {0x0, 0}, + {0x1, 1}, + {0x4000, 15}, + {0x80000000, 32}, + }; + + for (i =3D 0; i < RTE_DIM(test); i++) { + arg =3D test[i].arg; + rc =3D rte_fls_u32(arg); + expected =3D test[i].rc; + if (rc !=3D expected) { + printf("Wrong rte_fls_u32(0x%x) rc=3D%d, expected=3D%d\n", + arg, rc, expected); + return TEST_FAILED; + } + } + + return 0; +} + static int test_common(void) { @@ -196,6 +227,7 @@ test_common(void) ret |=3D test_macros(0); ret |=3D test_misc(); ret |=3D test_log2(); + ret |=3D test_fls(); =20 return ret; } --=20 2.19.1