]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
sbitmap: Add bitmap_all_bits_in_range_p function
authorKonstantinos Eleftheriou <konstantinos.eleftheriou@vrull.eu>
Mon, 19 May 2025 11:00:04 +0000 (13:00 +0200)
committerPhilipp Tomsich <philipp.tomsich@vrull.eu>
Tue, 27 May 2025 13:16:00 +0000 (15:16 +0200)
This patch adds the `bitmap_all_bits_in_range_p` function in sbitmap,
which checks if all the bits in a range are set.

Helper function `bitmap_bit_in_range_p` has also been added, in order
to be used by `bitmap_all_bits_in_range_p` and
`bitmap_any_bit_in_range_p`.  When the function's `any_inverted`
parameter is true, the function checks if any of the bits in the range
is unset, otherwise it checks if any of them is set.

Function `bitmap_any_bit_in_range_p` has been updated to call
`bitmap_bit_in_range_p` with the `any_inverted` parameter set to
false, retaining its previous functionality.

Function `bitmap_all_bits_in_range_p` calls `bitmap_bit_in_range_p`
with `any_inverted` set to true and returns the negation of the
result, i.e. true if all the bits in the range are set.

gcc/ChangeLog:

* sbitmap.cc (bitmap_any_bit_in_range_p):
Call and return the result of `bitmap_bit_in_range_p` with the
`any_inverted` parameter set to false.
(bitmap_bit_in_range_p): New function.
(bitmap_all_bits_in_range_p): New function.
* sbitmap.h (bitmap_all_bits_in_range_p): New function.

gcc/sbitmap.cc
gcc/sbitmap.h

index 64c7517397b3d5dfd5590db8158400f0de2c5524..075d0d35b810b44689adc52571a0edc9b013f781 100644 (file)
@@ -326,12 +326,13 @@ bitmap_set_range (sbitmap bmap, unsigned int start, unsigned int count)
   bmap->elms[start_word] |= mask;
 }
 
-/* Return TRUE if any bit between START and END inclusive is set within
-   the simple bitmap BMAP.  Return FALSE otherwise.  */
+/* Helper function for bitmap_any_bit_in_range_p and
+   bitmap_all_bits_in_range_p.  If ANY_INVERTED is true, the function checks
+   if any bit in the range is unset.  */
 
-bool
-bitmap_any_bit_in_range_p (const_sbitmap bmap, unsigned int start,
-                          unsigned int end)
+static bool
+bitmap_bit_in_range_p (const_sbitmap bmap, unsigned int start,
+                      unsigned int end, bool any_inverted)
 {
   gcc_checking_assert (start <= end);
   bitmap_check_index (bmap, end);
@@ -351,7 +352,8 @@ bitmap_any_bit_in_range_p (const_sbitmap bmap, unsigned int start,
 
       SBITMAP_ELT_TYPE low_mask = ((SBITMAP_ELT_TYPE)1 << start_bitno) - 1;
       SBITMAP_ELT_TYPE mask = high_mask - low_mask;
-      if (bmap->elms[start_word] & mask)
+      const SBITMAP_ELT_TYPE expected_partial = any_inverted ? mask : 0;
+      if ((bmap->elms[start_word] & mask) != expected_partial)
        return true;
       start_word++;
     }
@@ -361,9 +363,10 @@ bitmap_any_bit_in_range_p (const_sbitmap bmap, unsigned int start,
 
   /* Now test words at a time until we hit a partial word.  */
   unsigned int nwords = (end_word - start_word);
+  const SBITMAP_ELT_TYPE expected = any_inverted ? ~(SBITMAP_ELT_TYPE)0 : 0;
   while (nwords)
     {
-      if (bmap->elms[start_word])
+      if (bmap->elms[start_word] != expected)
        return true;
       start_word++;
       nwords--;
@@ -373,7 +376,28 @@ bitmap_any_bit_in_range_p (const_sbitmap bmap, unsigned int start,
   SBITMAP_ELT_TYPE mask = ~(SBITMAP_ELT_TYPE)0;
   if (end_bitno + 1 < SBITMAP_ELT_BITS)
     mask = ((SBITMAP_ELT_TYPE)1 << (end_bitno + 1)) - 1;
-  return (bmap->elms[start_word] & mask) != 0;
+  const SBITMAP_ELT_TYPE expected_partial = any_inverted ? mask : 0;
+  return (bmap->elms[start_word] & mask) != expected_partial;
+}
+
+/* Return TRUE if all bits between START and END inclusive are set within
+   the simple bitmap BMAP.  Return FALSE otherwise.  */
+
+bool
+bitmap_all_bits_in_range_p (const_sbitmap bmap, unsigned int start,
+                           unsigned int end)
+{
+  return !bitmap_bit_in_range_p (bmap, start, end, true);
+}
+
+/* Return TRUE if any bit between START and END inclusive is set within
+   the simple bitmap BMAP.  Return FALSE otherwise.  */
+
+bool
+bitmap_any_bit_in_range_p (const_sbitmap bmap, unsigned int start,
+                          unsigned int end)
+{
+  return bitmap_bit_in_range_p (bmap, start, end, false);
 }
 
 #if GCC_VERSION < 3400
index 927d296f4606624a769db4d87151aea0d7bc3f05..633d84fa15103c93c2a057e296a04dd5f8b0905b 100644 (file)
@@ -289,6 +289,8 @@ extern bool bitmap_xor (sbitmap, const_sbitmap, const_sbitmap);
 extern bool bitmap_subset_p (const_sbitmap, const_sbitmap);
 extern bool bitmap_any_bit_in_range_p (const_sbitmap, unsigned int,
                                       unsigned int);
+extern bool bitmap_all_bits_in_range_p (const_sbitmap, unsigned int,
+                                       unsigned int);
 
 extern int bitmap_first_set_bit (const_sbitmap);
 extern int bitmap_last_set_bit (const_sbitmap);