Skip to content

Commit 02bc11d

Browse files
olsajiriacmel
authored andcommitted
tools lib: Add for_each_clear_bit macro
Adding for_each_clear_bit macro plus all its the necessary backbone functions. Taken from related kernel code. It will be used in following patch. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Wang Nan <wangnan0@huawei.com> Link: http://lkml.kernel.org/n/tip-cayv2zbqi0nlmg5sjjxs1775@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent fe31672 commit 02bc11d

6 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎tools/include/asm-generic/bitops.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
#include <asm-generic/bitops/__ffs.h>
16+
#include <asm-generic/bitops/__ffz.h>
1617
#include <asm-generic/bitops/fls.h>
1718
#include <asm-generic/bitops/__fls.h>
1819
#include <asm-generic/bitops/fls64.h>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _ASM_GENERIC_BITOPS_FFZ_H_
2+
#define _ASM_GENERIC_BITOPS_FFZ_H_
3+
4+
/*
5+
* ffz - find first zero in word.
6+
* @word: The word to search
7+
*
8+
* Undefined if no zero exists, so code should check against ~0UL first.
9+
*/
10+
#define ffz(x) __ffs(~(x))
11+
12+
#endif /* _ASM_GENERIC_BITOPS_FFZ_H_ */

‎tools/include/asm-generic/bitops/find.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ extern unsigned long find_next_bit(const unsigned long *addr, unsigned long
1515
size, unsigned long offset);
1616
#endif
1717

18+
#ifndef find_next_zero_bit
19+
20+
/**
21+
* find_next_zero_bit - find the next cleared bit in a memory region
22+
* @addr: The address to base the search on
23+
* @offset: The bitnumber to start searching at
24+
* @size: The bitmap size in bits
25+
*
26+
* Returns the bit number of the next zero bit
27+
* If no bits are zero, returns @size.
28+
*/
29+
unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
30+
unsigned long offset);
31+
#endif
32+
1833
#ifndef find_first_bit
1934

2035
/**
@@ -30,4 +45,17 @@ extern unsigned long find_first_bit(const unsigned long *addr,
3045

3146
#endif /* find_first_bit */
3247

48+
#ifndef find_first_zero_bit
49+
50+
/**
51+
* find_first_zero_bit - find the first cleared bit in a memory region
52+
* @addr: The address to start the search at
53+
* @size: The maximum number of bits to search
54+
*
55+
* Returns the bit number of the first cleared bit.
56+
* If no bits are zero, returns @size.
57+
*/
58+
unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size);
59+
#endif
60+
3361
#endif /*_TOOLS_LINUX_ASM_GENERIC_BITOPS_FIND_H_ */

‎tools/include/linux/bitops.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ extern unsigned long __sw_hweight64(__u64 w);
3939
(bit) < (size); \
4040
(bit) = find_next_bit((addr), (size), (bit) + 1))
4141

42+
#define for_each_clear_bit(bit, addr, size) \
43+
for ((bit) = find_first_zero_bit((addr), (size)); \
44+
(bit) < (size); \
45+
(bit) = find_next_zero_bit((addr), (size), (bit) + 1))
46+
4247
/* same as for_each_set_bit() but use bit as value to start with */
4348
#define for_each_set_bit_from(bit, addr, size) \
4449
for ((bit) = find_next_bit((addr), (size), (bit)); \

‎tools/lib/find_bit.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,28 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
8282
return size;
8383
}
8484
#endif
85+
86+
#ifndef find_first_zero_bit
87+
/*
88+
* Find the first cleared bit in a memory region.
89+
*/
90+
unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
91+
{
92+
unsigned long idx;
93+
94+
for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
95+
if (addr[idx] != ~0UL)
96+
return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
97+
}
98+
99+
return size;
100+
}
101+
#endif
102+
103+
#ifndef find_next_zero_bit
104+
unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
105+
unsigned long offset)
106+
{
107+
return _find_next_bit(addr, size, offset, ~0UL);
108+
}
109+
#endif

‎tools/perf/MANIFEST‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ tools/include/asm-generic/bitops/arch_hweight.h
5151
tools/include/asm-generic/bitops/atomic.h
5252
tools/include/asm-generic/bitops/const_hweight.h
5353
tools/include/asm-generic/bitops/__ffs.h
54+
tools/include/asm-generic/bitops/__ffz.h
5455
tools/include/asm-generic/bitops/__fls.h
5556
tools/include/asm-generic/bitops/find.h
5657
tools/include/asm-generic/bitops/fls64.h

0 commit comments

Comments
 (0)