Skip to content

Commit f68edc9

Browse files
committed
lib: add find_first_and_bit()
Currently find_first_and_bit() is an alias to find_next_and_bit(). However, it is widely used in cpumask, so it worth to optimize it. This patch adds its own implementation for find_first_and_bit(). On x86_64 find_bit_benchmark says: Before (#define find_first_and_bit(...) find_next_and_bit(..., 0): Start testing find_bit() with random-filled bitmap [ 140.291468] find_first_and_bit: 46890919 ns, 32671 iterations Start testing find_bit() with sparse bitmap [ 140.295028] find_first_and_bit: 7103 ns, 1 iterations After: Start testing find_bit() with random-filled bitmap [ 162.574907] find_first_and_bit: 25045813 ns, 32846 iterations Start testing find_bit() with sparse bitmap [ 162.578458] find_first_and_bit: 4900 ns, 1 iterations (Thanks to Alexey Klimov for thorough testing.) Signed-off-by: Yury Norov <[email protected]> Tested-by: Wolfram Sang <[email protected]> Tested-by: Alexey Klimov <[email protected]>
1 parent c126a53 commit f68edc9

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

include/linux/find.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extern unsigned long _find_next_bit(const unsigned long *addr1,
1212
const unsigned long *addr2, unsigned long nbits,
1313
unsigned long start, unsigned long invert, unsigned long le);
1414
extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);
15+
extern unsigned long _find_first_and_bit(const unsigned long *addr1,
16+
const unsigned long *addr2, unsigned long size);
1517
extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);
1618
extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size);
1719

@@ -123,6 +125,31 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
123125
}
124126
#endif
125127

128+
#ifndef find_first_and_bit
129+
/**
130+
* find_first_and_bit - find the first set bit in both memory regions
131+
* @addr1: The first address to base the search on
132+
* @addr2: The second address to base the search on
133+
* @size: The bitmap size in bits
134+
*
135+
* Returns the bit number for the next set bit
136+
* If no bits are set, returns @size.
137+
*/
138+
static inline
139+
unsigned long find_first_and_bit(const unsigned long *addr1,
140+
const unsigned long *addr2,
141+
unsigned long size)
142+
{
143+
if (small_const_nbits(size)) {
144+
unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
145+
146+
return val ? __ffs(val) : size;
147+
}
148+
149+
return _find_first_and_bit(addr1, addr2, size);
150+
}
151+
#endif
152+
126153
#ifndef find_first_zero_bit
127154
/**
128155
* find_first_zero_bit - find the first cleared bit in a memory region

lib/find_bit.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,27 @@ unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
8989
EXPORT_SYMBOL(_find_first_bit);
9090
#endif
9191

92+
#ifndef find_first_and_bit
93+
/*
94+
* Find the first set bit in two memory regions.
95+
*/
96+
unsigned long _find_first_and_bit(const unsigned long *addr1,
97+
const unsigned long *addr2,
98+
unsigned long size)
99+
{
100+
unsigned long idx, val;
101+
102+
for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
103+
val = addr1[idx] & addr2[idx];
104+
if (val)
105+
return min(idx * BITS_PER_LONG + __ffs(val), size);
106+
}
107+
108+
return size;
109+
}
110+
EXPORT_SYMBOL(_find_first_and_bit);
111+
#endif
112+
92113
#ifndef find_first_zero_bit
93114
/*
94115
* Find the first cleared bit in a memory region.

lib/find_bit_benchmark.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ static int __init test_find_first_bit(void *bitmap, unsigned long len)
4949
return 0;
5050
}
5151

52+
static int __init test_find_first_and_bit(void *bitmap, const void *bitmap2, unsigned long len)
53+
{
54+
static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata;
55+
unsigned long i, cnt;
56+
ktime_t time;
57+
58+
bitmap_copy(cp, bitmap, BITMAP_LEN);
59+
60+
time = ktime_get();
61+
for (cnt = i = 0; i < len; cnt++) {
62+
i = find_first_and_bit(cp, bitmap2, len);
63+
__clear_bit(i, cp);
64+
}
65+
time = ktime_get() - time;
66+
pr_err("find_first_and_bit: %18llu ns, %6ld iterations\n", time, cnt);
67+
68+
return 0;
69+
}
70+
5271
static int __init test_find_next_bit(const void *bitmap, unsigned long len)
5372
{
5473
unsigned long i, cnt;
@@ -129,6 +148,7 @@ static int __init find_bit_test(void)
129148
* traverse only part of bitmap to avoid soft lockup.
130149
*/
131150
test_find_first_bit(bitmap, BITMAP_LEN / 10);
151+
test_find_first_and_bit(bitmap, bitmap2, BITMAP_LEN / 2);
132152
test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
133153

134154
pr_err("\nStart testing find_bit() with sparse bitmap\n");
@@ -145,6 +165,7 @@ static int __init find_bit_test(void)
145165
test_find_next_zero_bit(bitmap, BITMAP_LEN);
146166
test_find_last_bit(bitmap, BITMAP_LEN);
147167
test_find_first_bit(bitmap, BITMAP_LEN);
168+
test_find_first_and_bit(bitmap, bitmap2, BITMAP_LEN);
148169
test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
149170

150171
/*

0 commit comments

Comments
 (0)