xref: /f-stack/freebsd/sys/bitstring.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 1989, 1993
5a9643ea8Slogwang  *	The Regents of the University of California.  All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * This code is derived from software contributed to Berkeley by
8a9643ea8Slogwang  * Paul Vixie.
9a9643ea8Slogwang  *
10a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
11a9643ea8Slogwang  * modification, are permitted provided that the following conditions
12a9643ea8Slogwang  * are met:
13a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
15a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
16a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
17a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
18*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
19a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
20a9643ea8Slogwang  *    without specific prior written permission.
21a9643ea8Slogwang  *
22a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32a9643ea8Slogwang  * SUCH DAMAGE.
33a9643ea8Slogwang  *
34a9643ea8Slogwang  * Copyright (c) 2014 Spectra Logic Corporation
35a9643ea8Slogwang  * All rights reserved.
36a9643ea8Slogwang  *
37a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
38a9643ea8Slogwang  * modification, are permitted provided that the following conditions
39a9643ea8Slogwang  * are met:
40a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
41a9643ea8Slogwang  *    notice, this list of conditions, and the following disclaimer,
42a9643ea8Slogwang  *    without modification.
43a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
44a9643ea8Slogwang  *    substantially similar to the "NO WARRANTY" disclaimer below
45a9643ea8Slogwang  *    ("Disclaimer") and any redistribution must be conditioned upon
46a9643ea8Slogwang  *    including a substantially similar Disclaimer requirement for further
47a9643ea8Slogwang  *    binary redistribution.
48a9643ea8Slogwang  *
49a9643ea8Slogwang  * NO WARRANTY
50a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51a9643ea8Slogwang  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52a9643ea8Slogwang  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
53a9643ea8Slogwang  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54a9643ea8Slogwang  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
58a9643ea8Slogwang  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
59a9643ea8Slogwang  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60a9643ea8Slogwang  * POSSIBILITY OF SUCH DAMAGES.
61a9643ea8Slogwang  *
62a9643ea8Slogwang  * $FreeBSD$
63a9643ea8Slogwang  */
64a9643ea8Slogwang #ifndef _SYS_BITSTRING_H_
65a9643ea8Slogwang #define	_SYS_BITSTRING_H_
66a9643ea8Slogwang 
67a9643ea8Slogwang #ifdef _KERNEL
68a9643ea8Slogwang #include <sys/libkern.h>
69a9643ea8Slogwang #include <sys/malloc.h>
70a9643ea8Slogwang #endif
71a9643ea8Slogwang 
72a9643ea8Slogwang #include <sys/types.h>
73a9643ea8Slogwang 
74a9643ea8Slogwang typedef	unsigned long bitstr_t;
75a9643ea8Slogwang 
76a9643ea8Slogwang /*---------------------- Private Implementation Details ----------------------*/
77a9643ea8Slogwang #define	_BITSTR_MASK (~0UL)
78a9643ea8Slogwang #define	_BITSTR_BITS (sizeof(bitstr_t) * 8)
79a9643ea8Slogwang 
80a9643ea8Slogwang #ifdef roundup2
81a9643ea8Slogwang #define        _bit_roundup2 roundup2
82a9643ea8Slogwang #else
83a9643ea8Slogwang #define        _bit_roundup2(x, y)        (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
84a9643ea8Slogwang #endif
85a9643ea8Slogwang 
86a9643ea8Slogwang /* bitstr_t in bit string containing the bit. */
87a9643ea8Slogwang static inline int
_bit_idx(int _bit)88a9643ea8Slogwang _bit_idx(int _bit)
89a9643ea8Slogwang {
90a9643ea8Slogwang 	return (_bit / _BITSTR_BITS);
91a9643ea8Slogwang }
92a9643ea8Slogwang 
93a9643ea8Slogwang /* bit number within bitstr_t at _bit_idx(_bit). */
94a9643ea8Slogwang static inline int
_bit_offset(int _bit)95a9643ea8Slogwang _bit_offset(int _bit)
96a9643ea8Slogwang {
97a9643ea8Slogwang 	return (_bit % _BITSTR_BITS);
98a9643ea8Slogwang }
99a9643ea8Slogwang 
100a9643ea8Slogwang /* Mask for the bit within its long. */
101a9643ea8Slogwang static inline bitstr_t
_bit_mask(int _bit)102a9643ea8Slogwang _bit_mask(int _bit)
103a9643ea8Slogwang {
104a9643ea8Slogwang 	return (1UL << _bit_offset(_bit));
105a9643ea8Slogwang }
106a9643ea8Slogwang 
107a9643ea8Slogwang static inline bitstr_t
_bit_make_mask(int _start,int _stop)108a9643ea8Slogwang _bit_make_mask(int _start, int _stop)
109a9643ea8Slogwang {
110a9643ea8Slogwang 	return ((_BITSTR_MASK << _bit_offset(_start)) &
111a9643ea8Slogwang 	    (_BITSTR_MASK >> (_BITSTR_BITS - _bit_offset(_stop) - 1)));
112a9643ea8Slogwang }
113a9643ea8Slogwang 
114a9643ea8Slogwang /*----------------------------- Public Interface -----------------------------*/
115a9643ea8Slogwang /* Number of bytes allocated for a bit string of nbits bits */
116a9643ea8Slogwang #define	bitstr_size(_nbits) (_bit_roundup2(_nbits, _BITSTR_BITS) / 8)
117a9643ea8Slogwang 
118a9643ea8Slogwang /* Allocate a bit string initialized with no bits set. */
119a9643ea8Slogwang #ifdef _KERNEL
120a9643ea8Slogwang static inline bitstr_t *
bit_alloc(int _nbits,struct malloc_type * type,int flags)121a9643ea8Slogwang bit_alloc(int _nbits, struct malloc_type *type, int flags)
122a9643ea8Slogwang {
123a9643ea8Slogwang 	return ((bitstr_t *)malloc(bitstr_size(_nbits), type, flags | M_ZERO));
124a9643ea8Slogwang }
125a9643ea8Slogwang #else
126a9643ea8Slogwang static inline bitstr_t *
bit_alloc(int _nbits)127a9643ea8Slogwang bit_alloc(int _nbits)
128a9643ea8Slogwang {
129a9643ea8Slogwang 	return ((bitstr_t *)calloc(bitstr_size(_nbits), 1));
130a9643ea8Slogwang }
131a9643ea8Slogwang #endif
132a9643ea8Slogwang 
133a9643ea8Slogwang /* Allocate a bit string on the stack */
134a9643ea8Slogwang #define	bit_decl(name, nbits) \
135a9643ea8Slogwang 	((name)[bitstr_size(nbits) / sizeof(bitstr_t)])
136a9643ea8Slogwang 
137a9643ea8Slogwang /* Is bit N of bit string set? */
138a9643ea8Slogwang static inline int
bit_test(const bitstr_t * _bitstr,int _bit)139a9643ea8Slogwang bit_test(const bitstr_t *_bitstr, int _bit)
140a9643ea8Slogwang {
141a9643ea8Slogwang 	return ((_bitstr[_bit_idx(_bit)] & _bit_mask(_bit)) != 0);
142a9643ea8Slogwang }
143a9643ea8Slogwang 
144a9643ea8Slogwang /* Set bit N of bit string. */
145a9643ea8Slogwang static inline void
bit_set(bitstr_t * _bitstr,int _bit)146a9643ea8Slogwang bit_set(bitstr_t *_bitstr, int _bit)
147a9643ea8Slogwang {
148a9643ea8Slogwang 	_bitstr[_bit_idx(_bit)] |= _bit_mask(_bit);
149a9643ea8Slogwang }
150a9643ea8Slogwang 
151a9643ea8Slogwang /* clear bit N of bit string name */
152a9643ea8Slogwang static inline void
bit_clear(bitstr_t * _bitstr,int _bit)153a9643ea8Slogwang bit_clear(bitstr_t *_bitstr, int _bit)
154a9643ea8Slogwang {
155a9643ea8Slogwang 	_bitstr[_bit_idx(_bit)] &= ~_bit_mask(_bit);
156a9643ea8Slogwang }
157a9643ea8Slogwang 
158a9643ea8Slogwang /* Set bits start ... stop inclusive in bit string. */
159a9643ea8Slogwang static inline void
bit_nset(bitstr_t * _bitstr,int _start,int _stop)160a9643ea8Slogwang bit_nset(bitstr_t *_bitstr, int _start, int _stop)
161a9643ea8Slogwang {
162a9643ea8Slogwang 	bitstr_t *_stopbitstr;
163a9643ea8Slogwang 
164a9643ea8Slogwang 	_stopbitstr = _bitstr + _bit_idx(_stop);
165a9643ea8Slogwang 	_bitstr += _bit_idx(_start);
166a9643ea8Slogwang 
167a9643ea8Slogwang 	if (_bitstr == _stopbitstr) {
168a9643ea8Slogwang 		*_bitstr |= _bit_make_mask(_start, _stop);
169a9643ea8Slogwang 	} else {
170a9643ea8Slogwang 		*_bitstr |= _bit_make_mask(_start, _BITSTR_BITS - 1);
171a9643ea8Slogwang 		while (++_bitstr < _stopbitstr)
172a9643ea8Slogwang 	    		*_bitstr = _BITSTR_MASK;
173a9643ea8Slogwang 		*_stopbitstr |= _bit_make_mask(0, _stop);
174a9643ea8Slogwang 	}
175a9643ea8Slogwang }
176a9643ea8Slogwang 
177a9643ea8Slogwang /* Clear bits start ... stop inclusive in bit string. */
178a9643ea8Slogwang static inline void
bit_nclear(bitstr_t * _bitstr,int _start,int _stop)179a9643ea8Slogwang bit_nclear(bitstr_t *_bitstr, int _start, int _stop)
180a9643ea8Slogwang {
181a9643ea8Slogwang 	bitstr_t *_stopbitstr;
182a9643ea8Slogwang 
183a9643ea8Slogwang 	_stopbitstr = _bitstr + _bit_idx(_stop);
184a9643ea8Slogwang 	_bitstr += _bit_idx(_start);
185a9643ea8Slogwang 
186a9643ea8Slogwang 	if (_bitstr == _stopbitstr) {
187a9643ea8Slogwang 		*_bitstr &= ~_bit_make_mask(_start, _stop);
188a9643ea8Slogwang 	} else {
189a9643ea8Slogwang 		*_bitstr &= ~_bit_make_mask(_start, _BITSTR_BITS - 1);
190a9643ea8Slogwang 		while (++_bitstr < _stopbitstr)
191a9643ea8Slogwang 			*_bitstr = 0;
192a9643ea8Slogwang 		*_stopbitstr &= ~_bit_make_mask(0, _stop);
193a9643ea8Slogwang 	}
194a9643ea8Slogwang }
195a9643ea8Slogwang 
196a9643ea8Slogwang /* Find the first bit set in bit string at or after bit start. */
197a9643ea8Slogwang static inline void
bit_ffs_at(bitstr_t * _bitstr,int _start,int _nbits,int * _result)198a9643ea8Slogwang bit_ffs_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
199a9643ea8Slogwang {
200a9643ea8Slogwang 	bitstr_t *_curbitstr;
201a9643ea8Slogwang 	bitstr_t *_stopbitstr;
202a9643ea8Slogwang 	bitstr_t _test;
203a9643ea8Slogwang 	int _value, _offset;
204a9643ea8Slogwang 
205*22ce4affSfengbojiang 	if (_start >= _nbits) {
206*22ce4affSfengbojiang 		*_result = -1;
207*22ce4affSfengbojiang 		return;
208*22ce4affSfengbojiang 	}
209*22ce4affSfengbojiang 
210a9643ea8Slogwang 	if (_nbits > 0) {
211a9643ea8Slogwang 		_curbitstr = _bitstr + _bit_idx(_start);
212a9643ea8Slogwang 		_stopbitstr = _bitstr + _bit_idx(_nbits - 1);
213a9643ea8Slogwang 
214a9643ea8Slogwang 		_test = *_curbitstr;
215a9643ea8Slogwang 		if (_bit_offset(_start) != 0)
216a9643ea8Slogwang 			_test &= _bit_make_mask(_start, _BITSTR_BITS - 1);
217a9643ea8Slogwang 		while (_test == 0 && _curbitstr < _stopbitstr)
218a9643ea8Slogwang 			_test = *(++_curbitstr);
219a9643ea8Slogwang 
220a9643ea8Slogwang 		_offset = ffsl(_test);
221a9643ea8Slogwang 		_value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1;
222a9643ea8Slogwang 		if (_offset == 0 || _value >= _nbits)
223a9643ea8Slogwang 			_value = -1;
224a9643ea8Slogwang 	} else {
225a9643ea8Slogwang 		_value = -1;
226a9643ea8Slogwang 	}
227a9643ea8Slogwang 	*_result = _value;
228a9643ea8Slogwang }
229a9643ea8Slogwang 
230a9643ea8Slogwang /* Find the first bit clear in bit string at or after bit start. */
231a9643ea8Slogwang static inline void
bit_ffc_at(bitstr_t * _bitstr,int _start,int _nbits,int * _result)232a9643ea8Slogwang bit_ffc_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
233a9643ea8Slogwang {
234a9643ea8Slogwang 	bitstr_t *_curbitstr;
235a9643ea8Slogwang 	bitstr_t *_stopbitstr;
236a9643ea8Slogwang 	bitstr_t _test;
237a9643ea8Slogwang 	int _value, _offset;
238a9643ea8Slogwang 
239*22ce4affSfengbojiang 	if (_start >= _nbits) {
240*22ce4affSfengbojiang 		*_result = -1;
241*22ce4affSfengbojiang 		return;
242*22ce4affSfengbojiang 	}
243*22ce4affSfengbojiang 
244a9643ea8Slogwang 	if (_nbits > 0) {
245a9643ea8Slogwang 		_curbitstr = _bitstr + _bit_idx(_start);
246a9643ea8Slogwang 		_stopbitstr = _bitstr + _bit_idx(_nbits - 1);
247a9643ea8Slogwang 
248a9643ea8Slogwang 		_test = *_curbitstr;
249a9643ea8Slogwang 		if (_bit_offset(_start) != 0)
250a9643ea8Slogwang 			_test |= _bit_make_mask(0, _start - 1);
251a9643ea8Slogwang 		while (_test == _BITSTR_MASK && _curbitstr < _stopbitstr)
252a9643ea8Slogwang 			_test = *(++_curbitstr);
253a9643ea8Slogwang 
254a9643ea8Slogwang 		_offset = ffsl(~_test);
255a9643ea8Slogwang 		_value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1;
256a9643ea8Slogwang 		if (_offset == 0 || _value >= _nbits)
257a9643ea8Slogwang 			_value = -1;
258a9643ea8Slogwang 	} else {
259a9643ea8Slogwang 		_value = -1;
260a9643ea8Slogwang 	}
261a9643ea8Slogwang 	*_result = _value;
262a9643ea8Slogwang }
263a9643ea8Slogwang 
264a9643ea8Slogwang /* Find the first bit set in bit string. */
265a9643ea8Slogwang static inline void
bit_ffs(bitstr_t * _bitstr,int _nbits,int * _result)266a9643ea8Slogwang bit_ffs(bitstr_t *_bitstr, int _nbits, int *_result)
267a9643ea8Slogwang {
268a9643ea8Slogwang 	bit_ffs_at(_bitstr, /*start*/0, _nbits, _result);
269a9643ea8Slogwang }
270a9643ea8Slogwang 
271a9643ea8Slogwang /* Find the first bit clear in bit string. */
272a9643ea8Slogwang static inline void
bit_ffc(bitstr_t * _bitstr,int _nbits,int * _result)273a9643ea8Slogwang bit_ffc(bitstr_t *_bitstr, int _nbits, int *_result)
274a9643ea8Slogwang {
275a9643ea8Slogwang 	bit_ffc_at(_bitstr, /*start*/0, _nbits, _result);
276a9643ea8Slogwang }
277a9643ea8Slogwang 
278*22ce4affSfengbojiang /* Find contiguous sequence of at least size set bits at or after start */
279*22ce4affSfengbojiang static inline void
bit_ffs_area_at(bitstr_t * _bitstr,int _start,int _nbits,int _size,int * _result)280*22ce4affSfengbojiang bit_ffs_area_at(bitstr_t *_bitstr, int _start, int _nbits, int _size,
281*22ce4affSfengbojiang     int *_result)
282*22ce4affSfengbojiang {
283*22ce4affSfengbojiang 	bitstr_t *_curbitstr;
284*22ce4affSfengbojiang 	bitstr_t _test;
285*22ce4affSfengbojiang 	int _value, _offset, _logsize, _b;
286*22ce4affSfengbojiang 
287*22ce4affSfengbojiang 	if (_start + _size > _nbits || _nbits <= 0) {
288*22ce4affSfengbojiang 		*_result = -1;
289*22ce4affSfengbojiang 		return;
290*22ce4affSfengbojiang 	}
291*22ce4affSfengbojiang 
292*22ce4affSfengbojiang 	_logsize = fls(_size - 1);
293*22ce4affSfengbojiang 	_value = _start;
294*22ce4affSfengbojiang 	_curbitstr = _bitstr + _bit_idx(_start);
295*22ce4affSfengbojiang 	_test = ~*_curbitstr;
296*22ce4affSfengbojiang 	if (_bit_offset(_start) != 0)
297*22ce4affSfengbojiang 		_test |= _bit_make_mask(0, _start - 1);
298*22ce4affSfengbojiang 	for (_offset = 0;; _offset -= _BITSTR_BITS, _test = ~*++_curbitstr) {
299*22ce4affSfengbojiang 		if (_test != 0) {
300*22ce4affSfengbojiang 			/* If leading 0s in _test can finish 0-area, stop. */
301*22ce4affSfengbojiang 			if (_offset + _size < (int)_BITSTR_BITS &&
302*22ce4affSfengbojiang 			    (_test & _bit_make_mask(0, _offset + _size)) == 0)
303*22ce4affSfengbojiang 				break;
304*22ce4affSfengbojiang 			/* Shrink-left every 0-area in _test by size-1 bits. */
305*22ce4affSfengbojiang 			_b = _logsize;
306*22ce4affSfengbojiang 			while ((_test & (_test + 1)) != 0 && _b-- > 0)
307*22ce4affSfengbojiang 				_test |= _test >> (((_size - 1) >> _b) + 1) / 2;
308*22ce4affSfengbojiang 			/* Find the start of the first 0-area in _test. */
309*22ce4affSfengbojiang 			_offset = (~_test == 0) ? (int)_BITSTR_BITS :
310*22ce4affSfengbojiang 			    ffsl(~_test) - 1;
311*22ce4affSfengbojiang 			_value = (_curbitstr - _bitstr) * _BITSTR_BITS +
312*22ce4affSfengbojiang 			    _offset;
313*22ce4affSfengbojiang 			/* If there's insufficient space left, give up. */
314*22ce4affSfengbojiang 			if (_value + _size > _nbits) {
315*22ce4affSfengbojiang 				_value = -1;
316*22ce4affSfengbojiang 				break;
317*22ce4affSfengbojiang 			}
318*22ce4affSfengbojiang 		}
319*22ce4affSfengbojiang 		if (_offset + _size <= (int)_BITSTR_BITS)
320*22ce4affSfengbojiang 			break;
321*22ce4affSfengbojiang 	}
322*22ce4affSfengbojiang 	*_result = _value;
323*22ce4affSfengbojiang }
324*22ce4affSfengbojiang 
325*22ce4affSfengbojiang /* Find contiguous sequence of at least size cleared bits at or after start */
326*22ce4affSfengbojiang static inline void
bit_ffc_area_at(bitstr_t * _bitstr,int _start,int _nbits,int _size,int * _result)327*22ce4affSfengbojiang bit_ffc_area_at(bitstr_t *_bitstr, int _start, int _nbits, int _size,
328*22ce4affSfengbojiang     int *_result)
329*22ce4affSfengbojiang {
330*22ce4affSfengbojiang 	bitstr_t *_curbitstr;
331*22ce4affSfengbojiang 	bitstr_t _test;
332*22ce4affSfengbojiang 	int _value, _offset, _logsize, _b;
333*22ce4affSfengbojiang 
334*22ce4affSfengbojiang 	if (_start + _size > _nbits || _nbits <= 0) {
335*22ce4affSfengbojiang 		*_result = -1;
336*22ce4affSfengbojiang 		return;
337*22ce4affSfengbojiang 	}
338*22ce4affSfengbojiang 
339*22ce4affSfengbojiang 	_logsize = fls(_size - 1);
340*22ce4affSfengbojiang 	_value = _start;
341*22ce4affSfengbojiang 	_curbitstr = _bitstr + _bit_idx(_start);
342*22ce4affSfengbojiang 	_test = *_curbitstr;
343*22ce4affSfengbojiang 	if (_bit_offset(_start) != 0)
344*22ce4affSfengbojiang 		_test |= _bit_make_mask(0, _start - 1);
345*22ce4affSfengbojiang 	for (_offset = 0;; _offset -= _BITSTR_BITS, _test = *++_curbitstr) {
346*22ce4affSfengbojiang 		if (_test != 0) {
347*22ce4affSfengbojiang 			/* If leading 0s in _test can finish 0-area, stop. */
348*22ce4affSfengbojiang 			if (_offset + _size < (int)_BITSTR_BITS &&
349*22ce4affSfengbojiang 			    (_test & _bit_make_mask(0, _offset + _size)) == 0)
350*22ce4affSfengbojiang 				break;
351*22ce4affSfengbojiang 			/* Shrink-left every 0-area in _test by size-1 bits. */
352*22ce4affSfengbojiang 			_b = _logsize;
353*22ce4affSfengbojiang 			while ((_test & (_test + 1)) != 0 && _b-- > 0)
354*22ce4affSfengbojiang 				_test |= _test >> (((_size - 1) >> _b) + 1) / 2;
355*22ce4affSfengbojiang 			/* Find the start of the first 0-area in _test. */
356*22ce4affSfengbojiang 			_offset = (~_test == 0) ? (int)_BITSTR_BITS :
357*22ce4affSfengbojiang 			    ffsl(~_test) - 1;
358*22ce4affSfengbojiang 			_value = (_curbitstr - _bitstr) * _BITSTR_BITS +
359*22ce4affSfengbojiang 			    _offset;
360*22ce4affSfengbojiang 			/* If there's insufficient space left, give up. */
361*22ce4affSfengbojiang 			if (_value + _size > _nbits) {
362*22ce4affSfengbojiang 				_value = -1;
363*22ce4affSfengbojiang 				break;
364*22ce4affSfengbojiang 			}
365*22ce4affSfengbojiang 		}
366*22ce4affSfengbojiang 		if (_offset + _size <= (int)_BITSTR_BITS)
367*22ce4affSfengbojiang 			break;
368*22ce4affSfengbojiang 	}
369*22ce4affSfengbojiang 	*_result = _value;
370*22ce4affSfengbojiang }
371*22ce4affSfengbojiang 
372*22ce4affSfengbojiang /* Find contiguous sequence of at least size set bits in bit string */
373*22ce4affSfengbojiang static inline void
bit_ffs_area(bitstr_t * _bitstr,int _nbits,int _size,int * _result)374*22ce4affSfengbojiang bit_ffs_area(bitstr_t *_bitstr, int _nbits, int _size, int *_result)
375*22ce4affSfengbojiang {
376*22ce4affSfengbojiang 	bit_ffs_area_at(_bitstr, /*start*/0, _nbits, _size, _result);
377*22ce4affSfengbojiang }
378*22ce4affSfengbojiang 
379*22ce4affSfengbojiang /* Find contiguous sequence of at least size cleared bits in bit string */
380*22ce4affSfengbojiang static inline void
bit_ffc_area(bitstr_t * _bitstr,int _nbits,int _size,int * _result)381*22ce4affSfengbojiang bit_ffc_area(bitstr_t *_bitstr, int _nbits, int _size, int *_result)
382*22ce4affSfengbojiang {
383*22ce4affSfengbojiang 	bit_ffc_area_at(_bitstr, /*start*/0, _nbits, _size, _result);
384*22ce4affSfengbojiang }
385*22ce4affSfengbojiang 
386a9643ea8Slogwang /* Count the number of bits set in a bitstr of size _nbits at or after _start */
387a9643ea8Slogwang static inline void
bit_count(bitstr_t * _bitstr,int _start,int _nbits,int * _result)388a9643ea8Slogwang bit_count(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
389a9643ea8Slogwang {
390a9643ea8Slogwang 	bitstr_t *_curbitstr, mask;
391a9643ea8Slogwang 	int _value = 0, curbitstr_len;
392a9643ea8Slogwang 
393a9643ea8Slogwang 	if (_start >= _nbits)
394a9643ea8Slogwang 		goto out;
395a9643ea8Slogwang 
396a9643ea8Slogwang 	_curbitstr = _bitstr + _bit_idx(_start);
397a9643ea8Slogwang 	_nbits -= _BITSTR_BITS * _bit_idx(_start);
398a9643ea8Slogwang 	_start -= _BITSTR_BITS * _bit_idx(_start);
399a9643ea8Slogwang 
400a9643ea8Slogwang 	if (_start > 0) {
401a9643ea8Slogwang 		curbitstr_len = (int)_BITSTR_BITS < _nbits ?
402a9643ea8Slogwang 				(int)_BITSTR_BITS : _nbits;
403a9643ea8Slogwang 		mask = _bit_make_mask(_start, _bit_offset(curbitstr_len - 1));
404a9643ea8Slogwang 		_value += __bitcountl(*_curbitstr & mask);
405a9643ea8Slogwang 		_curbitstr++;
406a9643ea8Slogwang 		_nbits -= _BITSTR_BITS;
407a9643ea8Slogwang 	}
408a9643ea8Slogwang 	while (_nbits >= (int)_BITSTR_BITS) {
409a9643ea8Slogwang 		_value += __bitcountl(*_curbitstr);
410a9643ea8Slogwang 		_curbitstr++;
411a9643ea8Slogwang 		_nbits -= _BITSTR_BITS;
412a9643ea8Slogwang 	}
413a9643ea8Slogwang 	if (_nbits > 0) {
414a9643ea8Slogwang 		mask = _bit_make_mask(0, _bit_offset(_nbits - 1));
415a9643ea8Slogwang 		_value += __bitcountl(*_curbitstr & mask);
416a9643ea8Slogwang 	}
417a9643ea8Slogwang 
418a9643ea8Slogwang out:
419a9643ea8Slogwang 	*_result = _value;
420a9643ea8Slogwang }
421a9643ea8Slogwang 
422a9643ea8Slogwang #endif	/* _SYS_BITSTRING_H_ */
423