lib: add fast path for find_next_*_bit()
[linux-2.6-microblaze.git] / include / asm-generic / bitops / find.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_BITOPS_FIND_H_
3 #define _ASM_GENERIC_BITOPS_FIND_H_
4
5 extern unsigned long _find_next_bit(const unsigned long *addr1,
6                 const unsigned long *addr2, unsigned long nbits,
7                 unsigned long start, unsigned long invert, unsigned long le);
8
9 #ifndef find_next_bit
10 /**
11  * find_next_bit - find the next set bit in a memory region
12  * @addr: The address to base the search on
13  * @offset: The bitnumber to start searching at
14  * @size: The bitmap size in bits
15  *
16  * Returns the bit number for the next set bit
17  * If no bits are set, returns @size.
18  */
19 static inline
20 unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
21                             unsigned long offset)
22 {
23         if (small_const_nbits(size)) {
24                 unsigned long val;
25
26                 if (unlikely(offset >= size))
27                         return size;
28
29                 val = *addr & GENMASK(size - 1, offset);
30                 return val ? __ffs(val) : size;
31         }
32
33         return _find_next_bit(addr, NULL, size, offset, 0UL, 0);
34 }
35 #endif
36
37 #ifndef find_next_and_bit
38 /**
39  * find_next_and_bit - find the next set bit in both memory regions
40  * @addr1: The first address to base the search on
41  * @addr2: The second address to base the search on
42  * @offset: The bitnumber to start searching at
43  * @size: The bitmap size in bits
44  *
45  * Returns the bit number for the next set bit
46  * If no bits are set, returns @size.
47  */
48 static inline
49 unsigned long find_next_and_bit(const unsigned long *addr1,
50                 const unsigned long *addr2, unsigned long size,
51                 unsigned long offset)
52 {
53         if (small_const_nbits(size)) {
54                 unsigned long val;
55
56                 if (unlikely(offset >= size))
57                         return size;
58
59                 val = *addr1 & *addr2 & GENMASK(size - 1, offset);
60                 return val ? __ffs(val) : size;
61         }
62
63         return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);
64 }
65 #endif
66
67 #ifndef find_next_zero_bit
68 /**
69  * find_next_zero_bit - find the next cleared bit in a memory region
70  * @addr: The address to base the search on
71  * @offset: The bitnumber to start searching at
72  * @size: The bitmap size in bits
73  *
74  * Returns the bit number of the next zero bit
75  * If no bits are zero, returns @size.
76  */
77 static inline
78 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
79                                  unsigned long offset)
80 {
81         if (small_const_nbits(size)) {
82                 unsigned long val;
83
84                 if (unlikely(offset >= size))
85                         return size;
86
87                 val = *addr | ~GENMASK(size - 1, offset);
88                 return val == ~0UL ? size : ffz(val);
89         }
90
91         return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);
92 }
93 #endif
94
95 #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
96
97 /**
98  * find_first_bit - find the first set bit in a memory region
99  * @addr: The address to start the search at
100  * @size: The maximum number of bits to search
101  *
102  * Returns the bit number of the first set bit.
103  * If no bits are set, returns @size.
104  */
105 extern unsigned long find_first_bit(const unsigned long *addr,
106                                     unsigned long size);
107
108 /**
109  * find_first_zero_bit - find the first cleared bit in a memory region
110  * @addr: The address to start the search at
111  * @size: The maximum number of bits to search
112  *
113  * Returns the bit number of the first cleared bit.
114  * If no bits are zero, returns @size.
115  */
116 extern unsigned long find_first_zero_bit(const unsigned long *addr,
117                                          unsigned long size);
118 #else /* CONFIG_GENERIC_FIND_FIRST_BIT */
119
120 #ifndef find_first_bit
121 #define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
122 #endif
123 #ifndef find_first_zero_bit
124 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
125 #endif
126
127 #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
128
129 /**
130  * find_next_clump8 - find next 8-bit clump with set bits in a memory region
131  * @clump: location to store copy of found clump
132  * @addr: address to base the search on
133  * @size: bitmap size in number of bits
134  * @offset: bit offset at which to start searching
135  *
136  * Returns the bit offset for the next set clump; the found clump value is
137  * copied to the location pointed by @clump. If no bits are set, returns @size.
138  */
139 extern unsigned long find_next_clump8(unsigned long *clump,
140                                       const unsigned long *addr,
141                                       unsigned long size, unsigned long offset);
142
143 #define find_first_clump8(clump, bits, size) \
144         find_next_clump8((clump), (bits), (size), 0)
145
146 #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */