DPDK patches and discussions
 help / color / mirror / Atom feed
From: Jerin Jacob <jerin.jacob@caviumnetworks.com>
To: "dev@dpdk.org" <dev@dpdk.org>
Cc: "thomas@monjalon.net" <thomas@monjalon.net>,
	"sthemmin@microsoft.com" <sthemmin@microsoft.com>,
	"shaopeng.he@intel.com" <shaopeng.he@intel.com>,
	"Jacob,  Jerin" <Jerin.JacobKollanukkaran@cavium.com>
Subject: [dpdk-dev]  [PATCH v2 1/2] eal: introduce rte version of fls
Date: Wed, 7 Nov 2018 06:59:03 +0000	[thread overview]
Message-ID: <20181107065833.16756-1-jerin.jacob@caviumnetworks.com> (raw)
In-Reply-To: <20181106114435.14770-1-jerin.jacob@caviumnetworks.com>

The function returns the last (most-significant) bit set.
Added unit testcase to verify rte_fls_u32().

Signed-off-by: Jerin Jacob <jerin.jacob@caviumnetworks.com>
---
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/common/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);
 }
 
+
+/**
+ * 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 int
+rte_fls_u32(uint32_t x)
+{
+	return (x == 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;
 }
 
+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[] = {
+		{0x0, 0},
+		{0x1, 1},
+		{0x4000, 15},
+		{0x80000000, 32},
+	};
+
+	for (i = 0; i < RTE_DIM(test); i++) {
+		arg = test[i].arg;
+		rc = rte_fls_u32(arg);
+		expected = test[i].rc;
+		if (rc != expected) {
+			printf("Wrong rte_fls_u32(0x%x) rc=%d, expected=%d\n",
+				arg, rc, expected);
+			return TEST_FAILED;
+		}
+	}
+
+	return 0;
+}
+
 static int
 test_common(void)
 {
@@ -196,6 +227,7 @@ test_common(void)
 	ret |= test_macros(0);
 	ret |= test_misc();
 	ret |= test_log2();
+	ret |= test_fls();
 
 	return ret;
 }
-- 
2.19.1

  parent reply	other threads:[~2018-11-07  6:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-06 11:45 [dpdk-dev] [PATCH] eal: fix build issue Jerin Jacob
2018-11-06 12:29 ` Thomas Monjalon
2018-11-06 13:31   ` Jerin Jacob
2018-11-06 20:27     ` Thomas Monjalon
2018-11-07  6:59 ` Jerin Jacob [this message]
2018-11-07  6:59   ` [dpdk-dev] [PATCH v2 2/2] " Jerin Jacob
2018-11-12  1:37     ` [dpdk-dev] [dpdk-stable] " Thomas Monjalon
2018-11-12  4:50       ` Jerin Jacob
2018-11-12  9:01         ` Ferruh Yigit
2018-11-12  9:17           ` Thomas Monjalon
2018-11-12 12:28     ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181107065833.16756-1-jerin.jacob@caviumnetworks.com \
    --to=jerin.jacob@caviumnetworks.com \
    --cc=Jerin.JacobKollanukkaran@cavium.com \
    --cc=dev@dpdk.org \
    --cc=shaopeng.he@intel.com \
    --cc=sthemmin@microsoft.com \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).