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