lkml.org 
[lkml]   [2015]   [Nov]   [18]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH 2/3] lib: Introduce 2 find bit api: all_is_bit_{one,zero}
Date
This patch introduces 2 lightweight bit api.
all_bit_is_zero return 1 if the bit string is all zero.
The addr is the start address, the size is the bit size of the bit string.
all_bit_is_one is the opposite.

Signed-off-by: Jia He <hejianet@gmail.com>
---
lib/find_bit.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)

diff --git a/lib/find_bit.c b/lib/find_bit.c
index 18072ea..1d56d8d 100644
--- a/lib/find_bit.c
+++ b/lib/find_bit.c
@@ -131,6 +131,56 @@ unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
EXPORT_SYMBOL(find_last_bit);
#endif

+#ifndef all_bit_is_zero
+/*
+ * return val: 1 means all bit is zero
+ */
+unsigned int all_bit_is_zero(const unsigned long *addr, unsigned size)
+{
+ unsigned long idx;
+ unsigned long mask = size;
+
+ if (unlikely(size == 0))
+ return 1;
+
+ if (size > BITS_PER_LONG) {
+ for (idx = 0; idx * BITS_PER_LONG < size; idx++)
+ if (addr[idx])
+ return 0;
+
+ mask = size - (idx - 1) * BITS_PER_LONG;
+ }
+
+ return !(*addr & BITMAP_LAST_WORD_MASK(mask));
+}
+EXPORT_SYMBOL(all_bit_is_zero);
+#endif
+
+#ifndef all_bit_is_one
+/*
+ * return val: 1 means all bit is one
+ */
+unsigned int all_bit_is_one(const unsigned long *addr, unsigned size)
+{
+ unsigned long idx;
+ unsigned long mask = size;
+
+ if (unlikely(size == 0))
+ return 1;
+
+ if (size > BITS_PER_LONG) {
+ for (idx = 0; idx * BITS_PER_LONG < size; idx++)
+ if (~addr[idx])
+ return 0;
+
+ mask = size - (idx - 1) * BITS_PER_LONG;
+ }
+
+ return !(~(*addr) & BITMAP_LAST_WORD_MASK(mask));
+}
+EXPORT_SYMBOL(all_bit_is_one);
+#endif
+
#ifdef __BIG_ENDIAN

/* include/linux/byteorder does not support "unsigned long" type */
--
2.5.0


\
 
 \ /
  Last update: 2015-11-19 04:01    [W:0.067 / U:34.896 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site