Skip to content

Commit 90d4829

Browse files
valschneiderYuryNorov
authored andcommitted
lib/find_bit: Introduce find_next_andnot_bit()
In preparation of introducing for_each_cpu_andnot(), add a variant of find_next_bit() that negate the bits in @ADDR2 when ANDing them with the bits in @addr1. Signed-off-by: Valentin Schneider <vschneid@redhat.com>
1 parent 78e5a33 commit 90d4829

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

include/linux/find.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits,
1212
unsigned long start);
1313
unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,
1414
unsigned long nbits, unsigned long start);
15+
unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
16+
unsigned long nbits, unsigned long start);
1517
unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
1618
unsigned long start);
1719
extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);
@@ -91,6 +93,37 @@ unsigned long find_next_and_bit(const unsigned long *addr1,
9193
}
9294
#endif
9395

96+
#ifndef find_next_andnot_bit
97+
/**
98+
* find_next_andnot_bit - find the next set bit in *addr1 excluding all the bits
99+
* in *addr2
100+
* @addr1: The first address to base the search on
101+
* @addr2: The second address to base the search on
102+
* @size: The bitmap size in bits
103+
* @offset: The bitnumber to start searching at
104+
*
105+
* Returns the bit number for the next set bit
106+
* If no bits are set, returns @size.
107+
*/
108+
static inline
109+
unsigned long find_next_andnot_bit(const unsigned long *addr1,
110+
const unsigned long *addr2, unsigned long size,
111+
unsigned long offset)
112+
{
113+
if (small_const_nbits(size)) {
114+
unsigned long val;
115+
116+
if (unlikely(offset >= size))
117+
return size;
118+
119+
val = *addr1 & ~*addr2 & GENMASK(size - 1, offset);
120+
return val ? __ffs(val) : size;
121+
}
122+
123+
return _find_next_andnot_bit(addr1, addr2, size, offset);
124+
}
125+
#endif
126+
94127
#ifndef find_next_zero_bit
95128
/**
96129
* find_next_zero_bit - find the next cleared bit in a memory region

lib/find_bit.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long
164164
EXPORT_SYMBOL(_find_next_and_bit);
165165
#endif
166166

167+
#ifndef find_next_andnot_bit
168+
unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
169+
unsigned long nbits, unsigned long start)
170+
{
171+
return FIND_NEXT_BIT(addr1[idx] & ~addr2[idx], /* nop */, nbits, start);
172+
}
173+
EXPORT_SYMBOL(_find_next_andnot_bit);
174+
#endif
175+
167176
#ifndef find_next_zero_bit
168177
unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
169178
unsigned long start)

0 commit comments

Comments
 (0)