bitops.h revision 1.15 1 1.15 riastrad /* $NetBSD: bitops.h,v 1.15 2021/12/19 09:49:47 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #ifndef _LINUX_BITOPS_H_
33 1.1 riastrad #define _LINUX_BITOPS_H_
34 1.1 riastrad
35 1.1 riastrad #include <sys/cdefs.h>
36 1.1 riastrad #include <sys/types.h>
37 1.1 riastrad #include <sys/param.h>
38 1.1 riastrad #include <sys/atomic.h>
39 1.1 riastrad #include <sys/bitops.h>
40 1.1 riastrad
41 1.1 riastrad #include <machine/limits.h>
42 1.1 riastrad
43 1.1 riastrad #include <lib/libkern/libkern.h>
44 1.14 riastrad #include <linux/bits.h>
45 1.1 riastrad
46 1.1 riastrad /*
47 1.1 riastrad * Linux __ffs/__ffs64 is zero-based; zero input is undefined. Our
48 1.1 riastrad * ffs/ffs64 is one-based; zero input yields zero.
49 1.1 riastrad */
50 1.1 riastrad static inline unsigned long
51 1.1 riastrad __ffs(unsigned long x)
52 1.1 riastrad {
53 1.1 riastrad
54 1.1 riastrad KASSERT(x != 0);
55 1.1 riastrad return ffs64(x) - 1;
56 1.1 riastrad }
57 1.1 riastrad
58 1.1 riastrad static inline unsigned long
59 1.1 riastrad __ffs64(uint64_t x)
60 1.1 riastrad {
61 1.1 riastrad
62 1.1 riastrad KASSERT(x != 0);
63 1.1 riastrad return ffs64(x) - 1;
64 1.1 riastrad }
65 1.1 riastrad
66 1.4 riastrad /*
67 1.4 riastrad * Linux fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32, so it matches
68 1.4 riastrad * our fls semantics.
69 1.4 riastrad */
70 1.4 riastrad static inline int
71 1.4 riastrad fls(int x)
72 1.4 riastrad {
73 1.4 riastrad return fls32(x);
74 1.4 riastrad }
75 1.4 riastrad
76 1.1 riastrad static inline unsigned int
77 1.7 riastrad hweight8(uint8_t w)
78 1.7 riastrad {
79 1.7 riastrad return popcount(w & 0xff);
80 1.7 riastrad }
81 1.7 riastrad
82 1.7 riastrad static inline unsigned int
83 1.1 riastrad hweight16(uint16_t n)
84 1.1 riastrad {
85 1.1 riastrad return popcount32(n);
86 1.1 riastrad }
87 1.1 riastrad
88 1.1 riastrad static inline unsigned int
89 1.1 riastrad hweight32(uint32_t n)
90 1.1 riastrad {
91 1.1 riastrad return popcount32(n);
92 1.1 riastrad }
93 1.1 riastrad
94 1.3 riastrad static inline unsigned int
95 1.3 riastrad hweight64(uint64_t n)
96 1.3 riastrad {
97 1.3 riastrad return popcount64(n);
98 1.3 riastrad }
99 1.3 riastrad
100 1.11 riastrad static inline int64_t
101 1.11 riastrad sign_extend64(uint64_t x, unsigned n)
102 1.11 riastrad {
103 1.11 riastrad return (int64_t)(x << (63 - n)) >> (63 - n);
104 1.11 riastrad }
105 1.11 riastrad
106 1.1 riastrad #define BITS_TO_LONGS(n) \
107 1.1 riastrad roundup2((n), (sizeof(unsigned long) * CHAR_BIT))
108 1.1 riastrad
109 1.15 riastrad #define BITS_PER_TYPE(type) (sizeof(type) * NBBY)
110 1.15 riastrad #define BITS_PER_BYTE NBBY
111 1.15 riastrad #define BITS_PER_LONG (__SIZEOF_LONG__ * CHAR_BIT)
112 1.10 riastrad
113 1.10 riastrad #define BIT(n) ((unsigned long)__BIT(n))
114 1.10 riastrad #define BIT_ULL(n) ((unsigned long long)__BIT(n))
115 1.11 riastrad #define GENMASK(h,l) ((unsigned long)__BITS(h,l))
116 1.11 riastrad #define GENMASK_ULL(h,l)((unsigned long long)__BITS(h,l))
117 1.1 riastrad
118 1.1 riastrad static inline int
119 1.1 riastrad test_bit(unsigned int n, const volatile unsigned long *p)
120 1.1 riastrad {
121 1.1 riastrad const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
122 1.1 riastrad
123 1.1 riastrad return ((p[n / units] & (1UL << (n % units))) != 0);
124 1.1 riastrad }
125 1.1 riastrad
126 1.1 riastrad static inline void
127 1.1 riastrad __set_bit(unsigned int n, volatile unsigned long *p)
128 1.1 riastrad {
129 1.1 riastrad const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
130 1.1 riastrad
131 1.1 riastrad p[n / units] |= (1UL << (n % units));
132 1.1 riastrad }
133 1.1 riastrad
134 1.1 riastrad static inline void
135 1.1 riastrad __clear_bit(unsigned int n, volatile unsigned long *p)
136 1.1 riastrad {
137 1.1 riastrad const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
138 1.1 riastrad
139 1.1 riastrad p[n / units] &= ~(1UL << (n % units));
140 1.1 riastrad }
141 1.1 riastrad
142 1.1 riastrad static inline void
143 1.1 riastrad __change_bit(unsigned int n, volatile unsigned long *p)
144 1.1 riastrad {
145 1.1 riastrad const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
146 1.1 riastrad
147 1.1 riastrad p[n / units] ^= (1UL << (n % units));
148 1.1 riastrad }
149 1.1 riastrad
150 1.1 riastrad static inline unsigned long
151 1.1 riastrad __test_and_set_bit(unsigned int bit, volatile unsigned long *ptr)
152 1.1 riastrad {
153 1.1 riastrad const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
154 1.1 riastrad volatile unsigned long *const p = &ptr[bit / units];
155 1.1 riastrad const unsigned long mask = (1UL << (bit % units));
156 1.1 riastrad unsigned long v;
157 1.1 riastrad
158 1.1 riastrad v = *p;
159 1.1 riastrad *p |= mask;
160 1.1 riastrad
161 1.1 riastrad return ((v & mask) != 0);
162 1.1 riastrad }
163 1.1 riastrad
164 1.1 riastrad static inline unsigned long
165 1.1 riastrad __test_and_clear_bit(unsigned int bit, volatile unsigned long *ptr)
166 1.1 riastrad {
167 1.1 riastrad const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
168 1.1 riastrad volatile unsigned long *const p = &ptr[bit / units];
169 1.1 riastrad const unsigned long mask = (1UL << (bit % units));
170 1.1 riastrad unsigned long v;
171 1.1 riastrad
172 1.1 riastrad v = *p;
173 1.1 riastrad *p &= ~mask;
174 1.1 riastrad
175 1.1 riastrad return ((v & mask) != 0);
176 1.1 riastrad }
177 1.1 riastrad
178 1.1 riastrad static inline unsigned long
179 1.1 riastrad __test_and_change_bit(unsigned int bit, volatile unsigned long *ptr)
180 1.1 riastrad {
181 1.1 riastrad const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
182 1.1 riastrad volatile unsigned long *const p = &ptr[bit / units];
183 1.1 riastrad const unsigned long mask = (1UL << (bit % units));
184 1.1 riastrad unsigned long v;
185 1.1 riastrad
186 1.1 riastrad v = *p;
187 1.1 riastrad *p ^= mask;
188 1.1 riastrad
189 1.1 riastrad return ((v & mask) != 0);
190 1.1 riastrad }
191 1.1 riastrad
192 1.1 riastrad static inline unsigned long
193 1.6 riastrad __find_next_bit(const unsigned long *ptr, unsigned long nbits,
194 1.6 riastrad unsigned long startbit, unsigned long toggle)
195 1.1 riastrad {
196 1.1 riastrad const size_t bpl = (CHAR_BIT * sizeof(*ptr));
197 1.5 riastrad const unsigned long *p = ptr + startbit/bpl;
198 1.8 riastrad size_t n = howmany(nbits, bpl);
199 1.8 riastrad unsigned long result;
200 1.5 riastrad uint64_t word;
201 1.5 riastrad
202 1.5 riastrad /*
203 1.5 riastrad * We use ffs64 because NetBSD doesn't have a handy ffsl that
204 1.5 riastrad * works on unsigned long. This is a waste on 32-bit systems
205 1.5 riastrad * but I'd rather not maintain multiple copies of this -- the
206 1.5 riastrad * first version had enough bugs already.
207 1.5 riastrad */
208 1.5 riastrad
209 1.5 riastrad /* Do we need to examine a partial starting word? */
210 1.5 riastrad if (startbit % bpl) {
211 1.8 riastrad /* Toggle the bits and convert to 64 bits for ffs64. */
212 1.8 riastrad word = *p ^ toggle;
213 1.8 riastrad
214 1.8 riastrad /* Clear the low startbit%bpl bits. */
215 1.8 riastrad word &= (~0UL << (startbit % bpl));
216 1.8 riastrad
217 1.8 riastrad /* Are any of these bits set now? */
218 1.8 riastrad if (word)
219 1.8 riastrad goto out;
220 1.8 riastrad
221 1.8 riastrad /* Move past it. */
222 1.8 riastrad p++;
223 1.8 riastrad n--;
224 1.5 riastrad }
225 1.1 riastrad
226 1.8 riastrad /* Find the first word with any bits set. */
227 1.8 riastrad for (; n --> 0; p++) {
228 1.8 riastrad /* Toggle the bits and convert to 64 bits for ffs64. */
229 1.8 riastrad word = *p ^ toggle;
230 1.8 riastrad
231 1.8 riastrad /* Are any of these bits set now? */
232 1.8 riastrad if (word)
233 1.8 riastrad goto out;
234 1.1 riastrad }
235 1.1 riastrad
236 1.8 riastrad /* Nada. */
237 1.8 riastrad return nbits;
238 1.8 riastrad
239 1.8 riastrad out:
240 1.8 riastrad /* Count how many words we've skipped. */
241 1.8 riastrad result = bpl*(p - ptr);
242 1.5 riastrad
243 1.8 riastrad /* Find the first set bit in this word, zero-based. */
244 1.8 riastrad result += ffs64(word) - 1;
245 1.5 riastrad
246 1.8 riastrad /* We may have overshot, so clamp down to at most nbits. */
247 1.5 riastrad return MIN(result, nbits);
248 1.5 riastrad }
249 1.5 riastrad
250 1.5 riastrad static inline unsigned long
251 1.6 riastrad find_next_bit(const unsigned long *ptr, unsigned long nbits,
252 1.6 riastrad unsigned long startbit)
253 1.6 riastrad {
254 1.6 riastrad return __find_next_bit(ptr, nbits, startbit, 0);
255 1.6 riastrad }
256 1.6 riastrad
257 1.6 riastrad static inline unsigned long
258 1.6 riastrad find_first_bit(const unsigned long *ptr, unsigned long nbits)
259 1.6 riastrad {
260 1.6 riastrad return find_next_bit(ptr, nbits, 0);
261 1.6 riastrad }
262 1.6 riastrad
263 1.6 riastrad static inline unsigned long
264 1.6 riastrad find_next_zero_bit(const unsigned long *ptr, unsigned long nbits,
265 1.6 riastrad unsigned long startbit)
266 1.6 riastrad {
267 1.6 riastrad return __find_next_bit(ptr, nbits, startbit, ~0UL);
268 1.6 riastrad }
269 1.6 riastrad
270 1.6 riastrad static inline unsigned long
271 1.5 riastrad find_first_zero_bit(const unsigned long *ptr, unsigned long nbits)
272 1.5 riastrad {
273 1.5 riastrad return find_next_zero_bit(ptr, nbits, 0);
274 1.1 riastrad }
275 1.1 riastrad
276 1.6 riastrad #define for_each_set_bit(BIT, PTR, NBITS) \
277 1.6 riastrad for ((BIT) = find_first_bit((PTR), (NBITS)); \
278 1.6 riastrad (BIT) < (NBITS); \
279 1.6 riastrad (BIT) = find_next_bit((PTR), (NBITS), (BIT) + 1))
280 1.6 riastrad
281 1.11 riastrad #define for_each_clear_bit(BIT, PTR, NBITS) \
282 1.11 riastrad for ((BIT) = find_first_zero_bit((PTR), (NBITS)); \
283 1.11 riastrad (BIT) < (NBITS); \
284 1.11 riastrad (BIT) = find_next_zero_bit((PTR), (NBITS), (BIT) + 1))
285 1.11 riastrad
286 1.1 riastrad #endif /* _LINUX_BITOPS_H_ */
287