All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tyler Retzlaff <roretzla@linux.microsoft.com>
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 <roretzla@linux.microsoft.com>
Subject: [PATCH v7 2/4] eal: provide abstracted bit count functions
Date: Fri, 31 Mar 2023 17:45:05 -0700	[thread overview]
Message-ID: <1680309907-30879-3-git-send-email-roretzla@linux.microsoft.com> (raw)
In-Reply-To: <1680309907-30879-1-git-send-email-roretzla@linux.microsoft.com>

Provide abstracted bit counting functions for count, leading and
trailing bits in v to hide compiler specific intrinsics and builtins.

Include basic unit test of following functions added.

    rte_clz32
    rte_clz64
    rte_ctz32
    rte_ctz64
    rte_popcount32
    rte_popcount64

Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
---
 app/test/meson.build         |   2 +
 app/test/test_bitcount.c     | 136 ++++++++++++++++++++++++
 lib/eal/include/rte_bitops.h | 240 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 378 insertions(+)
 create mode 100644 app/test/test_bitcount.c

diff --git a/app/test/meson.build b/app/test/meson.build
index b9b5432..dafd509 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -13,6 +13,7 @@ test_sources = files(
         'test_alarm.c',
         'test_atomic.c',
         'test_barrier.c',
+        'test_bitcount.c',
         'test_bitops.c',
         'test_bitmap.c',
         'test_bpf.c',
@@ -161,6 +162,7 @@ test_deps += ['bus_pci', 'bus_vdev']
 fast_tests = [
         ['acl_autotest', true, true],
         ['atomic_autotest', false, true],
+        ['bitcount_autotest', true, true],
         ['bitmap_autotest', true, true],
         ['bpf_autotest', true, true],
         ['bpf_convert_autotest', true, true],
diff --git a/app/test/test_bitcount.c b/app/test/test_bitcount.c
new file mode 100644
index 0000000..5287ef7
--- /dev/null
+++ b/app/test/test_bitcount.c
@@ -0,0 +1,136 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (C) 2022 Microsoft Corporation
+ */
+
+#include <limits.h>
+#include <string.h>
+
+#include <rte_bitops.h>
+#include <rte_debug.h>
+
+#include "test.h"
+
+RTE_LOG_REGISTER(bitcount_logtype_test, test.bitcount, INFO);
+
+static int
+test_clz32(void)
+{
+	size_t leading;
+	uint32_t v = 0xffffffff;
+
+	for (leading = 0; v; leading++) {
+		RTE_TEST_ASSERT(rte_clz32(v) == leading,
+		    "Unexpected count.");
+		v >>= 1;
+	}
+
+	return 0;
+}
+
+static int
+test_clz64(void)
+{
+	size_t leading;
+	uint64_t v = 0xffffffffffffffff;
+
+	for (leading = 0; v; leading++) {
+		RTE_TEST_ASSERT(rte_clz64(v) == leading,
+		    "Unexpected count.");
+		v >>= 1;
+	}
+
+	return 0;
+}
+
+static int
+test_ctz32(void)
+{
+	size_t trailing;
+	uint32_t v = 1;
+
+	for (trailing = 0; v; trailing++) {
+		RTE_TEST_ASSERT(rte_ctz32(v) == trailing,
+		    "Unexpected count.");
+		v <<= 1;
+	}
+
+	return 0;
+}
+
+static int
+test_ctz64(void)
+{
+	size_t trailing;
+	uint64_t v = 1;
+
+	for (trailing = 0; v; trailing++) {
+		RTE_TEST_ASSERT(rte_ctz64(v) == trailing,
+		    "Unexpected count.");
+		v <<= 1;
+	}
+
+	return 0;
+}
+
+static int
+test_popcount32(void)
+{
+	size_t shift;
+	uint32_t v = 0;
+	const size_t bits = sizeof(v) * CHAR_BIT;
+
+	for (shift = 0; shift < bits; shift++) {
+		RTE_TEST_ASSERT(rte_popcount32(v) == shift,
+		    "Unexpected count.");
+		v <<= 1;
+		v |= 1;
+	}
+
+	RTE_TEST_ASSERT(rte_popcount32(v) == bits,
+	    "Unexpected count.");
+
+	return 0;
+}
+
+static int
+test_popcount64(void)
+{
+	size_t shift;
+	uint64_t v = 0;
+	const size_t bits = sizeof(v) * CHAR_BIT;
+
+	for (shift = 0; shift < bits; shift++) {
+		RTE_TEST_ASSERT(rte_popcount64(v) == shift,
+		    "Unexpected count.");
+		v <<= 1;
+		v |= 1;
+	}
+
+	RTE_TEST_ASSERT(rte_popcount64(v) == bits,
+	    "Unexpected count.");
+
+	return 0;
+}
+
+static struct unit_test_suite bitcount_test_suite = {
+	.suite_name = "bitcount autotest",
+	.setup = NULL,
+	.teardown = NULL,
+	.unit_test_cases = {
+		TEST_CASE(test_clz32),
+		TEST_CASE(test_clz64),
+		TEST_CASE(test_ctz32),
+		TEST_CASE(test_ctz64),
+		TEST_CASE(test_popcount32),
+		TEST_CASE(test_popcount64),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_bitcount(void)
+{
+	return unit_test_suite_runner(&bitcount_test_suite);
+}
+
+REGISTER_TEST_COMMAND(bitcount_autotest, test_bitcount);
diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h
index 531479e..d45aa54 100644
--- a/lib/eal/include/rte_bitops.h
+++ b/lib/eal/include/rte_bitops.h
@@ -1,5 +1,7 @@
 /* SPDX-License-Identifier: BSD-3-Clause
  * Copyright(c) 2020 Arm Limited
+ * Copyright(c) 2010-2019 Intel Corporation
+ * Copyright(c) 2023 Microsoft Corporation
  */
 
 #ifndef _RTE_BITOPS_H_
@@ -275,6 +277,244 @@
 	return val & mask;
 }
 
+#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_clz32(uint32_t v)
+{
+	unsigned long rv;
+
+	(void)_BitScanReverse(&rv, v);
+
+	return (unsigned int)(sizeof(v) * CHAR_BIT - 1 - 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_clz64(uint64_t v)
+{
+	unsigned long rv;
+
+	(void)_BitScanReverse64(&rv, v);
+
+	return (unsigned int)(sizeof(v) * CHAR_BIT - 1 - 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_ctz32(uint32_t 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_ctz64(uint64_t v)
+{
+	unsigned long rv;
+
+	(void)_BitScanForward64(&rv, v);
+
+	return (unsigned int)rv;
+}
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
+ *
+ * Get the count of 1-bits in v.
+ *
+ * @param v
+ *   The value.
+ * @return
+ *   The count of 1-bits.
+ */
+__rte_experimental
+static inline unsigned int
+rte_popcount32(uint32_t v)
+{
+	return (unsigned int)__popcnt(v);
+}
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
+ *
+ * Get the count of 1-bits in v.
+ *
+ * @param v
+ *   The value.
+ * @return
+ *   The count of 1-bits.
+ */
+__rte_experimental
+static inline unsigned int
+rte_popcount64(uint64_t v)
+{
+	return (unsigned int)__popcnt64(v);
+}
+
+#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_clz32(uint32_t 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_clz64(uint64_t 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_ctz32(uint32_t 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_ctz64(uint64_t v)
+{
+	return (unsigned int)__builtin_ctzll(v);
+}
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
+ *
+ * Get the count of 1-bits in v.
+ *
+ * @param v
+ *   The value.
+ * @return
+ *   The count of 1-bits.
+ */
+__rte_experimental
+static inline unsigned int
+rte_popcount32(uint32_t v)
+{
+	return (unsigned int)__builtin_popcount(v);
+}
+
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
+ *
+ * Get the count of 1-bits in v.
+ *
+ * @param v
+ *   The value.
+ * @return
+ *   The count of 1-bits.
+ */
+__rte_experimental
+static inline unsigned int
+rte_popcount64(uint64_t v)
+{
+	return (unsigned int)__builtin_popcountll(v);
+}
+
+#endif
+
 /**
  * Combines 32b inputs most significant set bits into the least
  * significant bits to construct a value with the same MSBs as x
-- 
1.8.3.1


  parent reply	other threads:[~2023-04-01  0:45 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-23 22:14 [PATCH 0/2] eal: provide leading and trailing zero bit count Tyler Retzlaff
2022-11-23 22:14 ` [PATCH 1/2] eal: provide leading and trailing zero bit count abstraction Tyler Retzlaff
2023-04-03 21:46   ` Mattias Rönnblom
2022-11-23 22:14 ` [PATCH 2/2] test/bitcount: add bitcount tests Tyler Retzlaff
2022-11-23 23:43 ` [PATCH v2 0/2] eal: provide leading and trailing zero bit count Tyler Retzlaff
2022-11-23 23:43   ` [PATCH v2 1/2] eal: provide leading and trailing zero bit count abstraction Tyler Retzlaff
2022-11-24 10:17     ` Morten Brørup
2022-11-28 17:13       ` Tyler Retzlaff
2022-11-28 17:22         ` Morten Brørup
2022-11-28 17:27           ` Tyler Retzlaff
2023-01-05  9:04             ` Thomas Monjalon
2023-01-05 17:23               ` Tyler Retzlaff
2023-01-05 17:27                 ` Tyler Retzlaff
2023-01-05 20:57                   ` Tyler Retzlaff
2023-01-05 21:34                     ` Morten Brørup
2023-01-05 22:06                       ` Tyler Retzlaff
2023-01-05 23:10                         ` Morten Brørup
2023-01-06  1:04                           ` Tyler Retzlaff
2023-01-06 10:09                             ` Thomas Monjalon
2023-01-06 10:00                   ` Thomas Monjalon
2023-01-05  7:09     ` Morten Brørup
2023-01-05  9:01       ` Thomas Monjalon
2023-01-05 17:21         ` Tyler Retzlaff
2023-01-06  0:32           ` Stephen Hemminger
2023-01-06 11:48             ` Bruce Richardson
2023-01-06 12:41               ` Morten Brørup
2023-01-06 13:40                 ` Thomas Monjalon
2023-01-06 18:58                   ` Tyler Retzlaff
2023-01-06 20:51                     ` Thomas Monjalon
2023-01-10  9:18                       ` Ferruh Yigit
2023-01-06 18:47               ` Tyler Retzlaff
2023-01-09  8:50                 ` Bruce Richardson
2023-04-04 21:23       ` Tyler Retzlaff
2023-04-05  8:44         ` Bruce Richardson
2023-04-05 15:22           ` Tyler Retzlaff
2023-04-05 15:51             ` Bruce Richardson
2023-04-05 17:25               ` Tyler Retzlaff
2022-11-23 23:43   ` [PATCH v2 2/2] test/bitcount: add bitcount tests Tyler Retzlaff
2023-01-04 23:46   ` [PATCH v2 0/2] eal: provide leading and trailing zero bit count Tyler Retzlaff
2023-01-09 17:36   ` [PATCH v4 0/2] eal: provide leading and trailing zero bit count abstraction Tyler Retzlaff
2023-01-09 17:36     ` [PATCH v4 1/2] eal: move bit operation functions from common to bitops header Tyler Retzlaff
2023-01-10 13:56       ` Ferruh Yigit
2023-01-09 17:36     ` [PATCH v4 2/2] eal: provide leading and trailing zero bit count abstraction Tyler Retzlaff
2023-01-10 13:55       ` Ferruh Yigit
2023-01-10 17:34         ` Tyler Retzlaff
2023-01-10 18:37         ` Tyler Retzlaff
2023-01-10 13:56     ` [PATCH v4 0/2] " Ferruh Yigit
2023-01-06 22:01 ` [PATCH v3 0/3] eal: provide leading and trailing zero bit count Tyler Retzlaff
2023-01-06 22:01   ` [PATCH v3 1/3] eal: move bit functions from common to bitops header Tyler Retzlaff
2023-01-06 22:01   ` [PATCH v3 2/3] eal: provide leading and trailing zero bit count abstraction Tyler Retzlaff
2023-01-06 22:01   ` [PATCH v3 3/3] test/bitcount: add bitcount tests Tyler Retzlaff
2023-01-07  8:15     ` Thomas Monjalon
2023-01-09 16:57       ` Tyler Retzlaff
2023-01-09 17:26         ` Tyler Retzlaff
2023-01-07 13:40     ` Morten Brørup
2023-01-09  8:51   ` [PATCH v3 0/3] eal: provide leading and trailing zero bit count Bruce Richardson
2023-01-10 19:38 ` [PATCH v5 0/2] eal: provide leading and trailing zero bit count abstraction Tyler Retzlaff
2023-01-10 19:38   ` [PATCH v5 1/2] eal: move bit operation common to bitops header Tyler Retzlaff
2023-01-10 19:38   ` [PATCH v5 2/2] eal: provide leading and trailing zero bit count abstraction Tyler Retzlaff
2023-01-10 19:46 ` [PATCH v6 0/2] " Tyler Retzlaff
2023-01-10 19:46   ` [PATCH v6 1/2] eal: move bit operation common to bitops header Tyler Retzlaff
2023-01-10 19:46   ` [PATCH v6 2/2] eal: provide leading and trailing zero bit count abstraction Tyler Retzlaff
2023-01-20 22:14   ` [PATCH v6 0/2] " Tyler Retzlaff
2023-02-02  9:14     ` David Marchand
2023-02-02 10:56       ` David Marchand
2023-02-02 15:57         ` Tyler Retzlaff
2023-02-03  9:14           ` David Marchand
2023-02-02 15:56       ` Tyler Retzlaff
2023-02-03  9:21         ` David Marchand
2023-04-01  0:45 ` [PATCH v7 0/4] eal: provide abstracted bit counting functions Tyler Retzlaff
2023-04-01  0:45   ` [PATCH v7 1/4] eal: move bit count functions to bitops header Tyler Retzlaff
2023-04-01  0:45   ` Tyler Retzlaff [this message]
2023-04-01  0:45   ` [PATCH v7 3/4] pipeline: add include of bitops Tyler Retzlaff
2023-04-01  0:45   ` [PATCH v7 4/4] maintainers: add bitcount test under EAL API and common code Tyler Retzlaff
2023-04-01  7:08   ` [PATCH v7 0/4] eal: provide abstracted bit counting functions Morten Brørup
2023-04-04  0:11 ` [PATCH v8 0/3] " Tyler Retzlaff
2023-04-04  0:11   ` [PATCH v8 1/3] eal: move bit count functions to bitops header Tyler Retzlaff
2023-04-04  0:11   ` [PATCH v8 2/3] eal: provide abstracted bit count functions Tyler Retzlaff
2023-04-04  0:11   ` [PATCH v8 3/3] maintainers: add bitcount test under EAL API and common code Tyler Retzlaff
2023-04-04  8:27   ` [PATCH v8 0/3] eal: provide abstracted bit counting functions Bruce Richardson
2023-08-25  8:41   ` David Marchand

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=1680309907-30879-3-git-send-email-roretzla@linux.microsoft.com \
    --to=roretzla@linux.microsoft.com \
    --cc=bruce.richardson@intel.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@amd.com \
    --cc=mb@smartsharesystems.com \
    --cc=stephen@networkplumber.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.