[1/6] bitmap: add bitmap_empty_from()

Message ID 20221028014834.572819-2-yury.norov@gmail.com
State New
Headers
Series bitmap: remove _reg_op() |

Commit Message

Yury Norov Oct. 28, 2022, 1:48 a.m. UTC
  New function checks if a bitmap is empty starting from a specific bit.
In the following patch, it's used to replace _reg_op(REG_OP_ISFREE).

Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 include/linux/bitmap.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
  

Patch

diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h
index 40e53a2ecc0d..f84553805c9c 100644
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -49,6 +49,7 @@  struct device;
  *  bitmap_intersects(src1, src2, nbits)        Do *src1 and *src2 overlap?
  *  bitmap_subset(src1, src2, nbits)            Is *src1 a subset of *src2?
  *  bitmap_empty(src, nbits)                    Are all bits zero in *src?
+ *  bitmap_empty_from(src, start, nbits)        Are all bits zero in *src starting from @start?
  *  bitmap_full(src, nbits)                     Are all bits set in *src?
  *  bitmap_weight(src, nbits)                   Hamming Weight: number set bits
  *  bitmap_weight_and(src1, src2, nbits)        Hamming Weight of and'ed bitmap
@@ -433,6 +434,16 @@  static __always_inline bool bitmap_full(const unsigned long *src, unsigned int n
 	return find_first_zero_bit(src, nbits) == nbits;
 }
 
+static __always_inline
+bool bitmap_empty_from(const unsigned long *src, unsigned int start, unsigned int nbits)
+{
+	if (small_const_nbits_off(nbits, start))
+		return !(src[start/BITS_PER_LONG] &
+			 GENMASK((nbits - 1) % BITS_PER_LONG, start % BITS_PER_LONG));
+
+	return find_next_bit(src, nbits, start) == nbits;
+}
+
 static __always_inline
 unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
 {