2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 2014 Spectra Logic Corporation
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions, and the following disclaimer,
42 * without modification.
43 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
44 * substantially similar to the "NO WARRANTY" disclaimer below
45 * ("Disclaimer") and any redistribution must be conditioned upon
46 * including a substantially similar Disclaimer requirement for further
47 * binary redistribution.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
58 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
59 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGES.
62 #ifndef _SYS_BITSTRING_H_
63 #define _SYS_BITSTRING_H_
66 #include <sys/libkern.h>
67 #include <sys/malloc.h>
70 #include <sys/types.h>
72 typedef unsigned long bitstr_t;
74 /*---------------------- Private Implementation Details ----------------------*/
75 #define _BITSTR_MASK (~0UL)
76 #define _BITSTR_BITS (sizeof(bitstr_t) * 8)
79 #define _bit_roundup2 roundup2
81 #define _bit_roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
84 /* bitstr_t in bit string containing the bit. */
88 return (_bit / _BITSTR_BITS);
91 /* bit number within bitstr_t at _bit_idx(_bit). */
95 return (_bit % _BITSTR_BITS);
98 /* Mask for the bit within its long. */
99 static inline bitstr_t
102 return (1UL << _bit_offset(_bit));
105 static inline bitstr_t
106 _bit_make_mask(int _start, int _stop)
108 return ((_BITSTR_MASK << _bit_offset(_start)) &
109 (_BITSTR_MASK >> (_BITSTR_BITS - _bit_offset(_stop) - 1)));
112 /*----------------------------- Public Interface -----------------------------*/
113 /* Number of bytes allocated for a bit string of nbits bits */
114 #define bitstr_size(_nbits) (_bit_roundup2(_nbits, _BITSTR_BITS) / 8)
116 /* Allocate a bit string initialized with no bits set. */
118 static inline bitstr_t *
119 bit_alloc(int _nbits, struct malloc_type *type, int flags)
121 return ((bitstr_t *)malloc(bitstr_size(_nbits), type, flags | M_ZERO));
124 static inline bitstr_t *
125 bit_alloc(int _nbits)
127 return ((bitstr_t *)calloc(bitstr_size(_nbits), 1));
131 /* Allocate a bit string on the stack */
132 #define bit_decl(name, nbits) \
133 ((name)[bitstr_size(nbits) / sizeof(bitstr_t)])
135 /* Is bit N of bit string set? */
137 bit_test(const bitstr_t *_bitstr, int _bit)
139 return ((_bitstr[_bit_idx(_bit)] & _bit_mask(_bit)) != 0);
142 /* Set bit N of bit string. */
144 bit_set(bitstr_t *_bitstr, int _bit)
146 _bitstr[_bit_idx(_bit)] |= _bit_mask(_bit);
149 /* clear bit N of bit string name */
151 bit_clear(bitstr_t *_bitstr, int _bit)
153 _bitstr[_bit_idx(_bit)] &= ~_bit_mask(_bit);
156 /* Are bits in [start ... stop] in bit string all 0 or all 1? */
158 bit_ntest(const bitstr_t *_bitstr, int _start, int _stop, int _match)
160 const bitstr_t *_stopbitstr;
163 _mask = (_match == 0) ? 0 : _BITSTR_MASK;
164 _stopbitstr = _bitstr + _bit_idx(_stop);
165 _bitstr += _bit_idx(_start);
167 if (_bitstr == _stopbitstr)
168 return (0 == ((*_bitstr ^ _mask) &
169 _bit_make_mask(_start, _stop)));
170 if (_bit_offset(_start) != 0 &&
171 0 != ((*_bitstr++ ^ _mask) &
172 _bit_make_mask(_start, _BITSTR_BITS - 1)))
174 if (_bit_offset(_stop) == _BITSTR_BITS - 1)
176 while (_bitstr < _stopbitstr) {
177 if (*_bitstr++ != _mask)
180 return (_bit_offset(_stop) == _BITSTR_BITS - 1 ||
181 0 == ((*_stopbitstr ^ _mask) & _bit_make_mask(0, _stop)));
184 /* Set bits start ... stop inclusive in bit string. */
186 bit_nset(bitstr_t *_bitstr, int _start, int _stop)
188 bitstr_t *_stopbitstr;
190 _stopbitstr = _bitstr + _bit_idx(_stop);
191 _bitstr += _bit_idx(_start);
193 if (_bitstr == _stopbitstr) {
194 *_bitstr |= _bit_make_mask(_start, _stop);
196 if (_bit_offset(_start) != 0)
197 *_bitstr++ |= _bit_make_mask(_start, _BITSTR_BITS - 1);
198 if (_bit_offset(_stop) == _BITSTR_BITS - 1)
200 while (_bitstr < _stopbitstr)
201 *_bitstr++ = _BITSTR_MASK;
202 if (_bit_offset(_stop) != _BITSTR_BITS - 1)
203 *_stopbitstr |= _bit_make_mask(0, _stop);
207 /* Clear bits start ... stop inclusive in bit string. */
209 bit_nclear(bitstr_t *_bitstr, int _start, int _stop)
211 bitstr_t *_stopbitstr;
213 _stopbitstr = _bitstr + _bit_idx(_stop);
214 _bitstr += _bit_idx(_start);
216 if (_bitstr == _stopbitstr) {
217 *_bitstr &= ~_bit_make_mask(_start, _stop);
219 if (_bit_offset(_start) != 0)
220 *_bitstr++ &= ~_bit_make_mask(_start, _BITSTR_BITS - 1);
221 if (_bit_offset(_stop) == _BITSTR_BITS - 1)
223 while (_bitstr < _stopbitstr)
225 if (_bit_offset(_stop) != _BITSTR_BITS - 1)
226 *_stopbitstr &= ~_bit_make_mask(0, _stop);
230 /* Find the first '_match'-bit in bit string at or after bit start. */
232 bit_ff_at(bitstr_t *_bitstr, int _start, int _nbits, int _match,
235 bitstr_t *_curbitstr;
236 bitstr_t *_stopbitstr;
241 if (_start >= _nbits || _nbits <= 0) {
246 _curbitstr = _bitstr + _bit_idx(_start);
247 _stopbitstr = _bitstr + _bit_idx(_nbits - 1);
248 _mask = _match ? 0 : _BITSTR_MASK;
250 _test = _mask ^ *_curbitstr;
251 if (_bit_offset(_start) != 0)
252 _test &= _bit_make_mask(_start, _BITSTR_BITS - 1);
253 while (_test == 0 && _curbitstr < _stopbitstr)
254 _test = _mask ^ *(++_curbitstr);
256 _value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + ffsl(_test) - 1;
258 (_bit_offset(_nbits) != 0 && _value >= _nbits))
263 /* Find the first bit set in bit string at or after bit start. */
265 bit_ffs_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
267 bit_ff_at(_bitstr, _start, _nbits, 1, _result);
270 /* Find the first bit clear in bit string at or after bit start. */
272 bit_ffc_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
274 bit_ff_at(_bitstr, _start, _nbits, 0, _result);
277 /* Find the first bit set in bit string. */
279 bit_ffs(bitstr_t *_bitstr, int _nbits, int *_result)
281 bit_ffs_at(_bitstr, /*start*/0, _nbits, _result);
284 /* Find the first bit clear in bit string. */
286 bit_ffc(bitstr_t *_bitstr, int _nbits, int *_result)
288 bit_ffc_at(_bitstr, /*start*/0, _nbits, _result);
291 /* Find contiguous sequence of at least size '_match'-bits at or after start */
293 bit_ff_area_at(bitstr_t *_bitstr, int _start, int _nbits, int _size,
294 int _match, int *_result)
296 bitstr_t *_curbitstr, _mask, _test;
297 int _value, _last, _shft, _maxshft;
299 if (_start + _size > _nbits || _nbits <= 0) {
304 _mask = _match ? _BITSTR_MASK : 0;
305 _maxshft = _bit_idx(_size - 1) == 0 ? _size : (int)_BITSTR_BITS;
307 _curbitstr = _bitstr + _bit_idx(_start);
308 _test = ~(_BITSTR_MASK << _bit_offset(_start));
309 for (_last = _size - 1, _test |= _mask ^ *_curbitstr;
310 !(_bit_idx(_last) == 0 &&
311 (_test & _bit_make_mask(0, _last)) == 0);
312 _last -= _BITSTR_BITS, _test = _mask ^ *++_curbitstr) {
315 /* Shrink-left every 0-area in _test by maxshft-1 bits. */
316 for (_shft = _maxshft; _shft > 1 && (_test & (_test + 1)) != 0;
317 _shft = (_shft + 1) / 2)
318 _test |= _test >> _shft / 2;
319 /* Find the start of the first 0-area in _test. */
320 _last = ffsl(~(_test >> 1));
321 _value = (_curbitstr - _bitstr) * _BITSTR_BITS + _last;
322 /* If there's insufficient space left, give up. */
323 if (_value + _size > _nbits) {
328 /* If a solution is contained in _test, success! */
329 if (_bit_idx(_last) == 0)
331 /* A solution here needs bits from the next word. */
336 /* Find contiguous sequence of at least size set bits at or after start */
338 bit_ffs_area_at(bitstr_t *_bitstr, int _start, int _nbits, int _size,
341 bit_ff_area_at(_bitstr, _start, _nbits, _size, 1, _result);
344 /* Find contiguous sequence of at least size cleared bits at or after start */
346 bit_ffc_area_at(bitstr_t *_bitstr, int _start, int _nbits, int _size,
349 bit_ff_area_at(_bitstr, _start, _nbits, _size, 0, _result);
352 /* Find contiguous sequence of at least size set bits in bit string */
354 bit_ffs_area(bitstr_t *_bitstr, int _nbits, int _size, int *_result)
356 bit_ffs_area_at(_bitstr, /*start*/0, _nbits, _size, _result);
359 /* Find contiguous sequence of at least size cleared bits in bit string */
361 bit_ffc_area(bitstr_t *_bitstr, int _nbits, int _size, int *_result)
363 bit_ffc_area_at(_bitstr, /*start*/0, _nbits, _size, _result);
366 /* Count the number of bits set in a bitstr of size _nbits at or after _start */
368 bit_count(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
370 bitstr_t *_curbitstr, mask;
371 int _value = 0, curbitstr_len;
373 if (_start >= _nbits)
376 _curbitstr = _bitstr + _bit_idx(_start);
377 _nbits -= _BITSTR_BITS * _bit_idx(_start);
378 _start -= _BITSTR_BITS * _bit_idx(_start);
381 curbitstr_len = (int)_BITSTR_BITS < _nbits ?
382 (int)_BITSTR_BITS : _nbits;
383 mask = _bit_make_mask(_start, _bit_offset(curbitstr_len - 1));
384 _value += __bitcountl(*_curbitstr & mask);
386 _nbits -= _BITSTR_BITS;
388 while (_nbits >= (int)_BITSTR_BITS) {
389 _value += __bitcountl(*_curbitstr);
391 _nbits -= _BITSTR_BITS;
394 mask = _bit_make_mask(0, _bit_offset(_nbits - 1));
395 _value += __bitcountl(*_curbitstr & mask);
402 /* Traverse all set bits, assigning each location in turn to iter */
403 #define bit_foreach_at(_bitstr, _start, _nbits, _iter) \
404 for (bit_ffs_at((_bitstr), (_start), (_nbits), &(_iter)); \
406 bit_ffs_at((_bitstr), (_iter) + 1, (_nbits), &(_iter)))
407 #define bit_foreach(_bitstr, _nbits, _iter) \
408 bit_foreach_at(_bitstr, /*start*/0, _nbits, _iter)
410 /* Traverse all unset bits, assigning each location in turn to iter */
411 #define bit_foreach_unset_at(_bitstr, _start, _nbits, _iter) \
412 for (bit_ffc_at((_bitstr), (_start), (_nbits), &(_iter)); \
414 bit_ffc_at((_bitstr), (_iter) + 1, (_nbits), &(_iter)))
415 #define bit_foreach_unset(_bitstr, _nbits, _iter) \
416 bit_foreach_unset_at(_bitstr, /*start*/0, _nbits, _iter)
418 #endif /* _SYS_BITSTRING_H_ */