[llvm] [LLVM][ADT] Adding range-based test_all and test_any methods (PR #209790)
Markos Horro via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 15 08:46:34 PDT 2026
https://github.com/markoshorro updated https://github.com/llvm/llvm-project/pull/209790
>From ac743ef1589bf606f9c3b98ce7386b2dbdee22a7 Mon Sep 17 00:00:00 2001
From: Markos Horro <Marcos.HorroVarela at amd.com>
Date: Wed, 15 Jul 2026 17:08:42 +0200
Subject: [PATCH 1/2] [LLVM][ADT] Adding range-based test_all and test_any
methods
---
llvm/include/llvm/ADT/BitVector.h | 18 +++++++++++++
llvm/include/llvm/ADT/SmallBitVector.h | 21 ++++++++++++++-
llvm/unittests/ADT/BitVectorTest.cpp | 37 ++++++++++++++++++++++++++
3 files changed, 75 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/BitVector.h b/llvm/include/llvm/ADT/BitVector.h
index a9902f603081c..38fbe66388a72 100644
--- a/llvm/include/llvm/ADT/BitVector.h
+++ b/llvm/include/llvm/ADT/BitVector.h
@@ -483,6 +483,24 @@ class BitVector {
return (*this)[Idx];
}
+ /// Returns true if all bits in the range [Begin, End) are set.
+ bool test_all(unsigned Begin, unsigned End) const {
+ for (unsigned i = Begin; i < End; ++i) {
+ if (!test(i))
+ return false;
+ }
+ return true;
+ }
+
+ /// Returns true if any of the bits in the range [Begin, End) are set.
+ bool test_any(unsigned Begin, unsigned End) const {
+ for (unsigned i = Begin; i < End; ++i) {
+ if (test(i))
+ return true;
+ }
+ return false;
+ }
+
// Push single bit to end of bitvector.
void push_back(bool Val) {
unsigned OldSize = Size;
diff --git a/llvm/include/llvm/ADT/SmallBitVector.h b/llvm/include/llvm/ADT/SmallBitVector.h
index 01bd502d1552b..90ee74cb3da94 100644
--- a/llvm/include/llvm/ADT/SmallBitVector.h
+++ b/llvm/include/llvm/ADT/SmallBitVector.h
@@ -470,11 +470,30 @@ class SmallBitVector {
assert(!empty() && "Getting last element of empty vector.");
return (*this)[size() - 1];
}
-
+
+ /// Returns true if bit \p Idx is set.
bool test(unsigned Idx) const {
return (*this)[Idx];
}
+ /// Returns true if all bits in the range [Begin, End) are set.
+ bool test_all(unsigned Begin, unsigned End) const {
+ for (unsigned i = Begin; i < End; ++i) {
+ if (!test(i))
+ return false;
+ }
+ return true;
+ }
+
+ /// Returns true if any of the bits in the range [Begin, End) are set.
+ bool test_any(unsigned Begin, unsigned End) const {
+ for (unsigned i = Begin; i < End; ++i) {
+ if (test(i))
+ return true;
+ }
+ return false;
+ }
+
// Push single bit to end of vector.
void push_back(bool Val) {
resize(size() + 1, Val);
diff --git a/llvm/unittests/ADT/BitVectorTest.cpp b/llvm/unittests/ADT/BitVectorTest.cpp
index 14f14660048b3..808e63c08cbaa 100644
--- a/llvm/unittests/ADT/BitVectorTest.cpp
+++ b/llvm/unittests/ADT/BitVectorTest.cpp
@@ -199,6 +199,43 @@ TYPED_TEST(BitVectorTest, Equality) {
EXPECT_TRUE(A == B);
}
+TYPED_TEST(BitVectorTest, TestAllAndAny) {
+ TypeParam Vec;
+ Vec.resize(10);
+
+ // Empty range
+ EXPECT_TRUE(Vec.test_all(3, 3));
+ EXPECT_FALSE(Vec.test_any(3, 3));
+
+ Vec.set(2, 5);
+ EXPECT_TRUE(Vec.test_all(2, 5));
+ EXPECT_FALSE(Vec.test_all(2, 6));
+ EXPECT_FALSE(Vec.test_all(1, 5));
+ EXPECT_TRUE(Vec.test_any(2, 5));
+ EXPECT_TRUE(Vec.test_any(4, 5));
+ EXPECT_FALSE(Vec.test_any(0, 2));
+
+ Vec.set(7);
+ EXPECT_FALSE(Vec.test_all(0, Vec.size()));
+ EXPECT_TRUE(Vec.test_any(6, Vec.size()));
+ Vec.reset(7);
+
+ TypeParam LargeVec;
+ LargeVec.resize(80);
+ LargeVec.set(10, 30);
+ EXPECT_TRUE(LargeVec.test_any(10, 30));
+ EXPECT_FALSE(LargeVec.test_all(10, 35));
+ EXPECT_FALSE(LargeVec.test_any(0, 10));
+
+ LargeVec.set(30, 35);
+ EXPECT_TRUE(LargeVec.test_all(10, 35));
+
+ LargeVec.set(70);
+ EXPECT_TRUE(LargeVec.test_any(68, 72));
+ LargeVec.reset(70);
+ EXPECT_FALSE(LargeVec.test_any(68, 72));
+}
+
TYPED_TEST(BitVectorTest, SimpleFindOpsMultiWord) {
TypeParam A;
>From d8b58d0474eacb4b7ea590bda4db3b25b0b9cf4b Mon Sep 17 00:00:00 2001
From: Markos Horro <Marcos.HorroVarela at amd.com>
Date: Wed, 15 Jul 2026 17:46:21 +0200
Subject: [PATCH 2/2] [nit] clang-format fix for SmallBitVector.h
---
llvm/include/llvm/ADT/SmallBitVector.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/SmallBitVector.h b/llvm/include/llvm/ADT/SmallBitVector.h
index 90ee74cb3da94..5d8c4be8d245f 100644
--- a/llvm/include/llvm/ADT/SmallBitVector.h
+++ b/llvm/include/llvm/ADT/SmallBitVector.h
@@ -470,7 +470,7 @@ class SmallBitVector {
assert(!empty() && "Getting last element of empty vector.");
return (*this)[size() - 1];
}
-
+
/// Returns true if bit \p Idx is set.
bool test(unsigned Idx) const {
return (*this)[Idx];
More information about the llvm-commits
mailing list