public inbox for opensbi@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH 1/2] include: sbi_bitmap: add bitmap_empty() function
@ 2026-03-11 12:51 Yu-Chien Peter Lin
  2026-03-11 12:51 ` [PATCH 2/2] lib: sbi_bitmap_test: add tests for bitmap_empty() Yu-Chien Peter Lin
  2026-04-08 12:37 ` [PATCH 1/2] include: sbi_bitmap: add bitmap_empty() function Anup Patel
  0 siblings, 2 replies; 4+ messages in thread
From: Yu-Chien Peter Lin @ 2026-03-11 12:51 UTC (permalink / raw)
  To: opensbi; +Cc: Yu-Chien Peter Lin

Add bitmap_empty() to check if bitmap has no bits set.

Unlike bitmap_weight() which calls sbi_popcount() on every word,
bitmap_empty() uses simple non-zero comparisons with early exit.

Signed-off-by: Yu-Chien Peter Lin <peter.lin@sifive.com>
---
 include/sbi/sbi_bitmap.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/include/sbi/sbi_bitmap.h b/include/sbi/sbi_bitmap.h
index 596bcc7d..80d3fe3b 100644
--- a/include/sbi/sbi_bitmap.h
+++ b/include/sbi/sbi_bitmap.h
@@ -143,4 +143,20 @@ static inline int bitmap_weight(const unsigned long *src, int nbits)
 	return res;
 }
 
+static inline bool bitmap_empty(const unsigned long *src, int nbits)
+{
+	if (nbits == 0)
+		return true;
+
+	if (small_const_nbits(nbits))
+		return !(*src & BITMAP_LAST_WORD_MASK(nbits));
+	else {
+		size_t i, len = BITS_TO_LONGS(nbits);
+		for (i = 0; i < len - 1; i++)
+			if (src[i])
+				return false;
+		return !(src[len - 1] & BITMAP_LAST_WORD_MASK(nbits));
+	}
+}
+
 #endif
-- 
2.53.0


-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] lib: sbi_bitmap_test: add tests for bitmap_empty()
  2026-03-11 12:51 [PATCH 1/2] include: sbi_bitmap: add bitmap_empty() function Yu-Chien Peter Lin
@ 2026-03-11 12:51 ` Yu-Chien Peter Lin
  2026-04-08 12:37   ` Anup Patel
  2026-04-08 12:37 ` [PATCH 1/2] include: sbi_bitmap: add bitmap_empty() function Anup Patel
  1 sibling, 1 reply; 4+ messages in thread
From: Yu-Chien Peter Lin @ 2026-03-11 12:51 UTC (permalink / raw)
  To: opensbi; +Cc: Yu-Chien Peter Lin

Add tests for bitmap_empty(), covers empty/non-empty bitmaps and
edge case nbits=0.

Signed-off-by: Yu-Chien Peter Lin <peter.lin@sifive.com>
---
 lib/sbi/tests/sbi_bitmap_test.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/lib/sbi/tests/sbi_bitmap_test.c b/lib/sbi/tests/sbi_bitmap_test.c
index d2c35996..b6586fe1 100644
--- a/lib/sbi/tests/sbi_bitmap_test.c
+++ b/lib/sbi/tests/sbi_bitmap_test.c
@@ -92,10 +92,39 @@ static void bitmap_xor_test(struct sbiunit_test_case *test)
 	SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
 }
 
+static void bitmap_empty_test(struct sbiunit_test_case *test)
+{
+	unsigned long res[DATA_SIZE];
+
+	/* All zeros = empty */
+	SBIUNIT_EXPECT_EQ(test, bitmap_empty(data_zero, DATA_BIT_SIZE), true);
+
+	/* Non-zero data = not empty */
+	SBIUNIT_EXPECT_EQ(test, bitmap_empty(data_a, DATA_BIT_SIZE), false);
+	SBIUNIT_EXPECT_EQ(test, bitmap_empty(data_b, DATA_BIT_SIZE), false);
+
+	/* bitmap_zero creates empty bitmap */
+	bitmap_zero(res, DATA_BIT_SIZE);
+	SBIUNIT_EXPECT_EQ(test, bitmap_empty(res, DATA_BIT_SIZE), true);
+
+	/* bitmap_fill creates non-empty bitmap */
+	bitmap_fill(res, DATA_BIT_SIZE);
+	SBIUNIT_EXPECT_EQ(test, bitmap_empty(res, DATA_BIT_SIZE), false);
+
+	/* Single bit set = not empty */
+	bitmap_zero(res, DATA_BIT_SIZE);
+	bitmap_set(res, 0, 1);
+	SBIUNIT_EXPECT_EQ(test, bitmap_empty(res, DATA_BIT_SIZE), false);
+
+	/* Zero nbits = empty */
+	SBIUNIT_EXPECT_EQ(test, bitmap_empty(data_a, 0), true);
+}
+
 static struct sbiunit_test_case bitmap_test_cases[] = {
 	SBIUNIT_TEST_CASE(bitmap_and_test),
 	SBIUNIT_TEST_CASE(bitmap_or_test),
 	SBIUNIT_TEST_CASE(bitmap_xor_test),
+	SBIUNIT_TEST_CASE(bitmap_empty_test),
 	SBIUNIT_END_CASE,
 };
 
-- 
2.53.0


-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] include: sbi_bitmap: add bitmap_empty() function
  2026-03-11 12:51 [PATCH 1/2] include: sbi_bitmap: add bitmap_empty() function Yu-Chien Peter Lin
  2026-03-11 12:51 ` [PATCH 2/2] lib: sbi_bitmap_test: add tests for bitmap_empty() Yu-Chien Peter Lin
@ 2026-04-08 12:37 ` Anup Patel
  1 sibling, 0 replies; 4+ messages in thread
From: Anup Patel @ 2026-04-08 12:37 UTC (permalink / raw)
  To: Yu-Chien Peter Lin; +Cc: opensbi

On Wed, Mar 11, 2026 at 6:21 PM Yu-Chien Peter Lin <peter.lin@sifive.com> wrote:
>
> Add bitmap_empty() to check if bitmap has no bits set.
>
> Unlike bitmap_weight() which calls sbi_popcount() on every word,
> bitmap_empty() uses simple non-zero comparisons with early exit.
>
> Signed-off-by: Yu-Chien Peter Lin <peter.lin@sifive.com>

LGTM.

Reviewed-by: Anup Patel <anup@brainfault.org>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

> ---
>  include/sbi/sbi_bitmap.h | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/include/sbi/sbi_bitmap.h b/include/sbi/sbi_bitmap.h
> index 596bcc7d..80d3fe3b 100644
> --- a/include/sbi/sbi_bitmap.h
> +++ b/include/sbi/sbi_bitmap.h
> @@ -143,4 +143,20 @@ static inline int bitmap_weight(const unsigned long *src, int nbits)
>         return res;
>  }
>
> +static inline bool bitmap_empty(const unsigned long *src, int nbits)
> +{
> +       if (nbits == 0)
> +               return true;
> +
> +       if (small_const_nbits(nbits))
> +               return !(*src & BITMAP_LAST_WORD_MASK(nbits));
> +       else {
> +               size_t i, len = BITS_TO_LONGS(nbits);
> +               for (i = 0; i < len - 1; i++)
> +                       if (src[i])
> +                               return false;
> +               return !(src[len - 1] & BITMAP_LAST_WORD_MASK(nbits));
> +       }
> +}
> +
>  #endif
> --
> 2.53.0
>
>
> --
> opensbi mailing list
> opensbi@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi

-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] lib: sbi_bitmap_test: add tests for bitmap_empty()
  2026-03-11 12:51 ` [PATCH 2/2] lib: sbi_bitmap_test: add tests for bitmap_empty() Yu-Chien Peter Lin
@ 2026-04-08 12:37   ` Anup Patel
  0 siblings, 0 replies; 4+ messages in thread
From: Anup Patel @ 2026-04-08 12:37 UTC (permalink / raw)
  To: Yu-Chien Peter Lin; +Cc: opensbi

On Wed, Mar 11, 2026 at 6:21 PM Yu-Chien Peter Lin <peter.lin@sifive.com> wrote:
>
> Add tests for bitmap_empty(), covers empty/non-empty bitmaps and
> edge case nbits=0.
>
> Signed-off-by: Yu-Chien Peter Lin <peter.lin@sifive.com>

LGTM.

Reviewed-by: Anup Patel <anup@brainfault.org>

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

> ---
>  lib/sbi/tests/sbi_bitmap_test.c | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
>
> diff --git a/lib/sbi/tests/sbi_bitmap_test.c b/lib/sbi/tests/sbi_bitmap_test.c
> index d2c35996..b6586fe1 100644
> --- a/lib/sbi/tests/sbi_bitmap_test.c
> +++ b/lib/sbi/tests/sbi_bitmap_test.c
> @@ -92,10 +92,39 @@ static void bitmap_xor_test(struct sbiunit_test_case *test)
>         SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
>  }
>
> +static void bitmap_empty_test(struct sbiunit_test_case *test)
> +{
> +       unsigned long res[DATA_SIZE];
> +
> +       /* All zeros = empty */
> +       SBIUNIT_EXPECT_EQ(test, bitmap_empty(data_zero, DATA_BIT_SIZE), true);
> +
> +       /* Non-zero data = not empty */
> +       SBIUNIT_EXPECT_EQ(test, bitmap_empty(data_a, DATA_BIT_SIZE), false);
> +       SBIUNIT_EXPECT_EQ(test, bitmap_empty(data_b, DATA_BIT_SIZE), false);
> +
> +       /* bitmap_zero creates empty bitmap */
> +       bitmap_zero(res, DATA_BIT_SIZE);
> +       SBIUNIT_EXPECT_EQ(test, bitmap_empty(res, DATA_BIT_SIZE), true);
> +
> +       /* bitmap_fill creates non-empty bitmap */
> +       bitmap_fill(res, DATA_BIT_SIZE);
> +       SBIUNIT_EXPECT_EQ(test, bitmap_empty(res, DATA_BIT_SIZE), false);
> +
> +       /* Single bit set = not empty */
> +       bitmap_zero(res, DATA_BIT_SIZE);
> +       bitmap_set(res, 0, 1);
> +       SBIUNIT_EXPECT_EQ(test, bitmap_empty(res, DATA_BIT_SIZE), false);
> +
> +       /* Zero nbits = empty */
> +       SBIUNIT_EXPECT_EQ(test, bitmap_empty(data_a, 0), true);
> +}
> +
>  static struct sbiunit_test_case bitmap_test_cases[] = {
>         SBIUNIT_TEST_CASE(bitmap_and_test),
>         SBIUNIT_TEST_CASE(bitmap_or_test),
>         SBIUNIT_TEST_CASE(bitmap_xor_test),
> +       SBIUNIT_TEST_CASE(bitmap_empty_test),
>         SBIUNIT_END_CASE,
>  };
>
> --
> 2.53.0
>
>
> --
> opensbi mailing list
> opensbi@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi

-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-04-08 12:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-11 12:51 [PATCH 1/2] include: sbi_bitmap: add bitmap_empty() function Yu-Chien Peter Lin
2026-03-11 12:51 ` [PATCH 2/2] lib: sbi_bitmap_test: add tests for bitmap_empty() Yu-Chien Peter Lin
2026-04-08 12:37   ` Anup Patel
2026-04-08 12:37 ` [PATCH 1/2] include: sbi_bitmap: add bitmap_empty() function Anup Patel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox