From patchwork Thu Dec 20 12:09:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anatoly Burakov X-Patchwork-Id: 49183 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id BD0061BCAF; Thu, 20 Dec 2018 13:10:04 +0100 (CET) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by dpdk.org (Postfix) with ESMTP id F18C91BC75 for ; Thu, 20 Dec 2018 13:09:59 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Dec 2018 04:09:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,376,1539673200"; d="scan'208";a="110905620" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by fmsmga008.fm.intel.com with ESMTP; 20 Dec 2018 04:09:56 -0800 Received: from sivswdev05.ir.intel.com (sivswdev05.ir.intel.com [10.243.17.64]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id wBKC9uYQ014124; Thu, 20 Dec 2018 12:09:56 GMT Received: from sivswdev05.ir.intel.com (localhost [127.0.0.1]) by sivswdev05.ir.intel.com with ESMTP id wBKC9tgL001185; Thu, 20 Dec 2018 12:09:55 GMT Received: (from aburakov@localhost) by sivswdev05.ir.intel.com with LOCAL id wBKC9oVO001177; Thu, 20 Dec 2018 12:09:50 GMT From: Anatoly Burakov To: dev@dpdk.org Cc: Neil Horman , John McNamara , Marko Kovacevic , Cristian Dumitrescu , jerin.jacob@caviumnetworks.com, thomas@monjalon.net Date: Thu, 20 Dec 2018 12:09:47 +0000 Message-Id: X-Mailer: git-send-email 1.7.0.7 In-Reply-To: <1cf22c82b6ca7561966be674afb8a02add0913b9.1544550999.git.anatoly.burakov@intel.com> References: <1cf22c82b6ca7561966be674afb8a02add0913b9.1544550999.git.anatoly.burakov@intel.com> Subject: [dpdk-dev] [PATCH v2 1/4] bitmap: remove deprecated bsf64 function X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list 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 rte_bsf64 was deprecated in a previous release, so remove the function, and the deprecation notice associated with it. Signed-off-by: Anatoly Burakov --- doc/guides/rel_notes/deprecation.rst | 5 ----- doc/guides/rel_notes/release_19_02.rst | 4 ++++ lib/librte_eal/common/include/rte_bitmap.h | 6 ------ 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/doc/guides/rel_notes/deprecation.rst b/doc/guides/rel_notes/deprecation.rst index ac7fb29a7..61d94c7de 100644 --- a/doc/guides/rel_notes/deprecation.rst +++ b/doc/guides/rel_notes/deprecation.rst @@ -20,11 +20,6 @@ Deprecation Notices * kvargs: The function ``rte_kvargs_process`` will get a new parameter for returning key match count. It will ease handling of no-match case. -* eal: function ``rte_bsf64`` in ``rte_bitmap.h`` has been renamed to - ``rte_bsf64_safe`` and moved to ``rte_common.h``. A new ``rte_bsf64`` function - will be added in the next release in ``rte_common.h`` that follows convention - set by existing ``rte_bsf32`` function. - * eal: both declaring and identifying devices will be streamlined in v18.11. New functions will appear to query a specific port from buses, classes of device and device drivers. Device declaration will be made coherent with the diff --git a/doc/guides/rel_notes/release_19_02.rst b/doc/guides/rel_notes/release_19_02.rst index 069f429a7..c4cce4c98 100644 --- a/doc/guides/rel_notes/release_19_02.rst +++ b/doc/guides/rel_notes/release_19_02.rst @@ -99,6 +99,10 @@ API Changes since 18.05 and are removed in this release. +* eal: function ``rte_bsf64`` in ``rte_bitmap.h`` has been renamed to + ``rte_bsf64_safe`` and moved to ``rte_common.h``. + + ABI Changes ----------- diff --git a/lib/librte_eal/common/include/rte_bitmap.h b/lib/librte_eal/common/include/rte_bitmap.h index 77727c828..6b846f251 100644 --- a/lib/librte_eal/common/include/rte_bitmap.h +++ b/lib/librte_eal/common/include/rte_bitmap.h @@ -93,12 +93,6 @@ __rte_bitmap_index2_set(struct rte_bitmap *bmp) bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2); } -static inline int __rte_deprecated -rte_bsf64(uint64_t slab, uint32_t *pos) -{ - return rte_bsf64_safe(slab, pos); -} - static inline uint32_t __rte_bitmap_get_memory_footprint(uint32_t n_bits, uint32_t *array1_byte_offset, uint32_t *array1_slabs, From patchwork Thu Dec 20 12:09:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anatoly Burakov X-Patchwork-Id: 49181 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 773181BC7F; Thu, 20 Dec 2018 13:10:01 +0100 (CET) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by dpdk.org (Postfix) with ESMTP id 359C31BC75 for ; Thu, 20 Dec 2018 13:09:59 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Dec 2018 04:09:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,376,1539673200"; d="scan'208";a="119938850" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by FMSMGA003.fm.intel.com with ESMTP; 20 Dec 2018 04:09:57 -0800 Received: from sivswdev05.ir.intel.com (sivswdev05.ir.intel.com [10.243.17.64]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id wBKC9uwx014132; Thu, 20 Dec 2018 12:09:56 GMT Received: from sivswdev05.ir.intel.com (localhost [127.0.0.1]) by sivswdev05.ir.intel.com with ESMTP id wBKC9unE001192; Thu, 20 Dec 2018 12:09:56 GMT Received: (from aburakov@localhost) by sivswdev05.ir.intel.com with LOCAL id wBKC9urU001188; Thu, 20 Dec 2018 12:09:56 GMT From: Anatoly Burakov To: dev@dpdk.org Cc: John McNamara , Marko Kovacevic , jerin.jacob@caviumnetworks.com, thomas@monjalon.net Date: Thu, 20 Dec 2018 12:09:48 +0000 Message-Id: X-Mailer: git-send-email 1.7.0.7 In-Reply-To: References: In-Reply-To: <1cf22c82b6ca7561966be674afb8a02add0913b9.1544550999.git.anatoly.burakov@intel.com> References: <1cf22c82b6ca7561966be674afb8a02add0913b9.1544550999.git.anatoly.burakov@intel.com> Subject: [dpdk-dev] [PATCH v2 2/4] common: add bsf64 and bsf32_safe functions X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add an rte_bsf64 function that follows the convention of existing rte_bsf32 function. Also, add missing implementation for safe version of rte_bsf32, and implement unit tests for all recently added bsf varieties. Signed-off-by: Anatoly Burakov --- doc/guides/rel_notes/release_19_02.rst | 4 +- lib/librte_eal/common/include/rte_common.h | 43 +++++++++++++++++++++- test/test/test_common.c | 41 ++++++++++++++++++++- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/doc/guides/rel_notes/release_19_02.rst b/doc/guides/rel_notes/release_19_02.rst index c4cce4c98..0f446ed74 100644 --- a/doc/guides/rel_notes/release_19_02.rst +++ b/doc/guides/rel_notes/release_19_02.rst @@ -100,7 +100,9 @@ API Changes * eal: function ``rte_bsf64`` in ``rte_bitmap.h`` has been renamed to - ``rte_bsf64_safe`` and moved to ``rte_common.h``. + ``rte_bsf64_safe`` and moved to ``rte_common.h``. A new ``rte_bsf64`` function + has been added in ``rte_common.h`` that follows convention set by existing + ``rte_bsf32`` function. ABI Changes diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h index 66cdf60b2..d102f2cec 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -447,6 +447,30 @@ 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(uint64_t v, uint32_t *pos) +{ + if (v == 0) + return 0; + + *pos = rte_bsf32(v); + return 1; +} + /** * Return the rounded-up log2 of a integer. * @@ -482,6 +506,23 @@ 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 int +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). @@ -502,7 +543,7 @@ rte_bsf64_safe(uint64_t v, uint32_t *pos) if (v == 0) return 0; - *pos = __builtin_ctzll(v); + *pos = rte_bsf64(v); return 1; } diff --git a/test/test/test_common.c b/test/test/test_common.c index c6d17baae..4a42aaed8 100644 --- a/test/test/test_common.c +++ b/test/test/test_common.c @@ -50,12 +50,48 @@ test_macros(int __rte_unused unused_parm) return 0; } +static int +test_bsf(void) +{ + uint32_t shift, pos; + + /* safe versions should be able to handle 0 */ + if (rte_bsf32_safe(0, &pos) != 0) + FAIL("rte_bsf32_safe"); + if (rte_bsf64_safe(0, &pos) != 0) + FAIL("rte_bsf64_safe"); + + for (shift = 0; shift < 63; shift++) { + uint32_t val32; + uint64_t val64; + + val64 = 1ULL << shift; + if ((uint32_t)rte_bsf64(val64) != shift) + FAIL("rte_bsf64"); + if (rte_bsf64_safe(val64, &pos) != 1) + FAIL("rte_bsf64_safe"); + if (pos != shift) + FAIL("rte_bsf64_safe"); + + if (shift > 31) + continue; + + val32 = 1U << shift; + if ((uint32_t)rte_bsf32(val32) != shift) + FAIL("rte_bsf32"); + if (rte_bsf32_safe(val32, &pos) != 1) + FAIL("rte_bsf32_safe"); + if (pos != shift) + FAIL("rte_bsf32_safe"); + } + + return 0; +} + static int test_misc(void) { char memdump[] = "memdump_test"; - if (rte_bsf32(129)) - FAIL("rte_bsf32"); rte_memdump(stdout, "test", memdump, sizeof(memdump)); rte_hexdump(stdout, "test", memdump, sizeof(memdump)); @@ -226,6 +262,7 @@ test_common(void) ret |= test_align(); ret |= test_macros(0); ret |= test_misc(); + ret |= test_bsf(); ret |= test_log2(); ret |= test_fls(); From patchwork Thu Dec 20 12:09:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anatoly Burakov X-Patchwork-Id: 49182 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3058D1BCA7; Thu, 20 Dec 2018 13:10:03 +0100 (CET) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by dpdk.org (Postfix) with ESMTP id 8CA421BC7F for ; Thu, 20 Dec 2018 13:09:59 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Dec 2018 04:09:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,376,1539673200"; d="scan'208";a="260997380" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by orsmga004.jf.intel.com with ESMTP; 20 Dec 2018 04:09:57 -0800 Received: from sivswdev05.ir.intel.com (sivswdev05.ir.intel.com [10.243.17.64]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id wBKC9uME014135; Thu, 20 Dec 2018 12:09:56 GMT Received: from sivswdev05.ir.intel.com (localhost [127.0.0.1]) by sivswdev05.ir.intel.com with ESMTP id wBKC9uYk001199; Thu, 20 Dec 2018 12:09:56 GMT Received: (from aburakov@localhost) by sivswdev05.ir.intel.com with LOCAL id wBKC9u1k001195; Thu, 20 Dec 2018 12:09:56 GMT From: Anatoly Burakov To: dev@dpdk.org Cc: jerin.jacob@caviumnetworks.com, thomas@monjalon.net Date: Thu, 20 Dec 2018 12:09:49 +0000 Message-Id: <223b9255bd5283abab2d69dfb18e1b5ac12f6eef.1545307762.git.anatoly.burakov@intel.com> X-Mailer: git-send-email 1.7.0.7 In-Reply-To: References: In-Reply-To: <1cf22c82b6ca7561966be674afb8a02add0913b9.1544550999.git.anatoly.burakov@intel.com> References: <1cf22c82b6ca7561966be674afb8a02add0913b9.1544550999.git.anatoly.burakov@intel.com> Subject: [dpdk-dev] [PATCH v2 3/4] common: add 64-bit fls function X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add missing implementation for 64-bit fls function, and extend unit test to test the new function as well. Signed-off-by: Anatoly Burakov --- lib/librte_eal/common/include/rte_common.h | 18 ++++++++++++++++++ test/test/test_common.c | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h index d102f2cec..7b50b8479 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -547,6 +547,24 @@ rte_bsf64_safe(uint64_t v, uint32_t *pos) 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 int +rte_fls_u64(uint64_t x) +{ + return (x == 0) ? 0 : 64 - __builtin_clzll(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 4a42aaed8..f6dd4c18d 100644 --- a/test/test/test_common.c +++ b/test/test/test_common.c @@ -242,6 +242,8 @@ test_fls(void) }; for (i = 0; i < RTE_DIM(test); i++) { + uint64_t arg64; + arg = test[i].arg; rc = rte_fls_u32(arg); expected = test[i].rc; @@ -250,6 +252,25 @@ test_fls(void) arg, rc, expected); return TEST_FAILED; } + /* 64-bit version */ + arg = test[i].arg; + rc = rte_fls_u64(arg); + expected = test[i].rc; + if (rc != expected) { + printf("Wrong rte_fls_u64(0x%x) rc=%d, expected=%d\n", + arg, rc, expected); + return TEST_FAILED; + } + /* 64-bit version shifted by 32 bits */ + arg64 = (uint64_t)test[i].arg << 32; + rc = rte_fls_u64(arg64); + /* don't shift zero */ + expected = test[i].rc == 0 ? 0 : test[i].rc + 32; + if (rc != expected) { + printf("Wrong rte_fls_u64(0x%" PRIx64 ") rc=%d, expected=%d\n", + arg64, rc, expected); + return TEST_FAILED; + } } return 0; From patchwork Thu Dec 20 12:09:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anatoly Burakov X-Patchwork-Id: 49184 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 6873D1BCB7; Thu, 20 Dec 2018 13:10:07 +0100 (CET) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by dpdk.org (Postfix) with ESMTP id 23D751BCA3 for ; Thu, 20 Dec 2018 13:10:01 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Dec 2018 04:10:00 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,376,1539673200"; d="scan'208";a="285260242" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by orsmga005.jf.intel.com with ESMTP; 20 Dec 2018 04:09:57 -0800 Received: from sivswdev05.ir.intel.com (sivswdev05.ir.intel.com [10.243.17.64]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id wBKC9vif014139; Thu, 20 Dec 2018 12:09:57 GMT Received: from sivswdev05.ir.intel.com (localhost [127.0.0.1]) by sivswdev05.ir.intel.com with ESMTP id wBKC9vO2001206; Thu, 20 Dec 2018 12:09:57 GMT Received: (from aburakov@localhost) by sivswdev05.ir.intel.com with LOCAL id wBKC9uSb001202; Thu, 20 Dec 2018 12:09:57 GMT From: Anatoly Burakov To: dev@dpdk.org Cc: Wenzhuo Lu , Jingjing Wu , Bernard Iremonger , jerin.jacob@caviumnetworks.com, thomas@monjalon.net Date: Thu, 20 Dec 2018 12:09:50 +0000 Message-Id: X-Mailer: git-send-email 1.7.0.7 In-Reply-To: References: In-Reply-To: <1cf22c82b6ca7561966be674afb8a02add0913b9.1544550999.git.anatoly.burakov@intel.com> References: <1cf22c82b6ca7561966be674afb8a02add0913b9.1544550999.git.anatoly.burakov@intel.com> Subject: [dpdk-dev] [PATCH v2 4/4] common: add 64-bit log2 function X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add missing implementation for 64-bit log2 function, and extend the unit test to test this new function. Also, remove duplicate reimplementation of this function from testpmd and memalloc. Signed-off-by: Anatoly Burakov --- app/test-pmd/testpmd.c | 17 +---------------- lib/librte_eal/common/include/rte_common.h | 18 ++++++++++++++++++ lib/librte_eal/linuxapp/eal/eal_memalloc.c | 17 +---------------- test/test/test_common.c | 20 +++++++++++++++++++- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c index a10bc40bb..8d584b008 100644 --- a/app/test-pmd/testpmd.c +++ b/app/test-pmd/testpmd.c @@ -649,28 +649,13 @@ calc_mem_size(uint32_t nb_mbufs, uint32_t mbuf_sz, size_t pgsz, size_t *out) return 0; } -static inline uint32_t -bsf64(uint64_t v) -{ - return (uint32_t)__builtin_ctzll(v); -} - -static inline uint32_t -log2_u64(uint64_t v) -{ - if (v == 0) - return 0; - v = rte_align64pow2(v); - return bsf64(v); -} - static int pagesz_flags(uint64_t page_sz) { /* as per mmap() manpage, all page sizes are log2 of page size * shifted by MAP_HUGE_SHIFT */ - int log2 = log2_u64(page_sz); + int log2 = rte_log2_u64(page_sz); return (log2 << HUGE_SHIFT); } diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h index 7b50b8479..7178ba1e9 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -565,6 +565,24 @@ rte_fls_u64(uint64_t x) return (x == 0) ? 0 : 64 - __builtin_clzll(x); } +/** + * Return the rounded-up log2 of a 64-bit integer. + * + * @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/librte_eal/linuxapp/eal/eal_memalloc.c b/lib/librte_eal/linuxapp/eal/eal_memalloc.c index 784939566..6e6af5b06 100644 --- a/lib/librte_eal/linuxapp/eal/eal_memalloc.c +++ b/lib/librte_eal/linuxapp/eal/eal_memalloc.c @@ -208,28 +208,13 @@ get_file_size(int fd) return st.st_size; } -static inline uint32_t -bsf64(uint64_t v) -{ - return (uint32_t)__builtin_ctzll(v); -} - -static inline uint32_t -log2_u64(uint64_t v) -{ - if (v == 0) - return 0; - v = rte_align64pow2(v); - return bsf64(v); -} - static int pagesz_flags(uint64_t page_sz) { /* as per mmap() manpage, all page sizes are log2 of page size * shifted by MAP_HUGE_SHIFT */ - int log2 = log2_u64(page_sz); + int log2 = rte_log2_u64(page_sz); return log2 << RTE_MAP_HUGE_SHIFT; } diff --git a/test/test/test_common.c b/test/test/test_common.c index f6dd4c18d..94d367471 100644 --- a/test/test/test_common.c +++ b/test/test/test_common.c @@ -213,13 +213,31 @@ test_log2(void) const uint32_t step = 1; for (i = 0; i < max; i = i + step) { + uint64_t i64; + + /* extend range for 64-bit */ + i64 = (uint64_t)i << 32; + base = (uint32_t)ceilf(log2(i64)); + compare = rte_log2_u64(i64); + if (base != compare) { + printf("Wrong rte_log2_u64(%" PRIx64 ") val %x, expected %x\n", + i64, compare, base); + return TEST_FAILED; + } + base = (uint32_t)ceilf(log2((uint32_t)i)); - compare = rte_log2_u32(i); + compare = rte_log2_u32((uint32_t)i); if (base != compare) { printf("Wrong rte_log2_u32(%x) val %x, expected %x\n", i, compare, base); return TEST_FAILED; } + compare = rte_log2_u64((uint64_t)i); + if (base != compare) { + printf("Wrong rte_log2_u64(%x) val %x, expected %x\n", + i, compare, base); + return TEST_FAILED; + } } return 0; }