Skip to content

Commit 13f0a02

Browse files
YuryNorovbp3tk0v
authored andcommitted
find: Add find_first_andnot_bit()
The function helps to implement cpumask_andnot() APIs. Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com> Signed-off-by: James Morse <james.morse@arm.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: James Morse <james.morse@arm.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Reviewed-by: Fenghua Yu <fenghuay@nvidia.com> Tested-by: James Morse <james.morse@arm.com> Tested-by: Tony Luck <tony.luck@intel.com> Tested-by: Fenghua Yu <fenghuay@nvidia.com> Link: https://lore.kernel.org/20250515165855.31452-3-james.morse@arm.com
1 parent 189572b commit 13f0a02

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

include/linux/find.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1, const unsign
2929
unsigned long n);
3030
extern unsigned long _find_first_and_bit(const unsigned long *addr1,
3131
const unsigned long *addr2, unsigned long size);
32+
unsigned long _find_first_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
33+
unsigned long size);
3234
unsigned long _find_first_and_and_bit(const unsigned long *addr1, const unsigned long *addr2,
3335
const unsigned long *addr3, unsigned long size);
3436
extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);
@@ -347,6 +349,29 @@ unsigned long find_first_and_bit(const unsigned long *addr1,
347349
}
348350
#endif
349351

352+
/**
353+
* find_first_andnot_bit - find the first bit set in 1st memory region and unset in 2nd
354+
* @addr1: The first address to base the search on
355+
* @addr2: The second address to base the search on
356+
* @size: The bitmap size in bits
357+
*
358+
* Returns the bit number for the first set bit
359+
* If no bits are set, returns >= @size.
360+
*/
361+
static __always_inline
362+
unsigned long find_first_andnot_bit(const unsigned long *addr1,
363+
const unsigned long *addr2,
364+
unsigned long size)
365+
{
366+
if (small_const_nbits(size)) {
367+
unsigned long val = *addr1 & (~*addr2) & GENMASK(size - 1, 0);
368+
369+
return val ? __ffs(val) : size;
370+
}
371+
372+
return _find_first_andnot_bit(addr1, addr2, size);
373+
}
374+
350375
/**
351376
* find_first_and_and_bit - find the first set bit in 3 memory regions
352377
* @addr1: The first address to base the search on

lib/find_bit.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ unsigned long _find_first_and_bit(const unsigned long *addr1,
116116
EXPORT_SYMBOL(_find_first_and_bit);
117117
#endif
118118

119+
/*
120+
* Find the first bit set in 1st memory region and unset in 2nd.
121+
*/
122+
unsigned long _find_first_andnot_bit(const unsigned long *addr1,
123+
const unsigned long *addr2,
124+
unsigned long size)
125+
{
126+
return FIND_FIRST_BIT(addr1[idx] & ~addr2[idx], /* nop */, size);
127+
}
128+
EXPORT_SYMBOL(_find_first_andnot_bit);
129+
119130
/*
120131
* Find the first set bit in three memory regions.
121132
*/

0 commit comments

Comments
 (0)