2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 2014 Spectra Logic Corporation
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions, and the following disclaimer,
40 * without modification.
41 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
42 * substantially similar to the "NO WARRANTY" disclaimer below
43 * ("Disclaimer") and any redistribution must be conditioned upon
44 * including a substantially similar Disclaimer requirement for further
45 * binary redistribution.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
52 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
57 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGES.
62 #ifndef _SYS_FREEBSD_BITSTRING_H_
63 #define _SYS_FREEBSD_BITSTRING_H_
65 #include <sys/types.h>
68 * Begin: Ad hoc definitions from FreeBSD sys/sys/types.h
72 * Population count algorithm using SWAR approach
73 * - "SIMD Within A Register".
75 static __inline __uint16_t
76 __bitcount16(__uint16_t _x)
79 _x = (_x & 0x5555) + ((_x & 0xaaaa) >> 1);
80 _x = (_x & 0x3333) + ((_x & 0xcccc) >> 2);
81 _x = (_x + (_x >> 4)) & 0x0f0f;
82 _x = (_x + (_x >> 8)) & 0x00ff;
86 static __inline __uint32_t
87 __bitcount32(__uint32_t _x)
90 _x = (_x & 0x55555555) + ((_x & 0xaaaaaaaa) >> 1);
91 _x = (_x & 0x33333333) + ((_x & 0xcccccccc) >> 2);
92 _x = (_x + (_x >> 4)) & 0x0f0f0f0f;
93 _x = (_x + (_x >> 8));
94 _x = (_x + (_x >> 16)) & 0x000000ff;
99 static __inline __uint64_t
100 __bitcount64(__uint64_t _x)
103 _x = (_x & 0x5555555555555555) + ((_x & 0xaaaaaaaaaaaaaaaa) >> 1);
104 _x = (_x & 0x3333333333333333) + ((_x & 0xcccccccccccccccc) >> 2);
105 _x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0f;
106 _x = (_x + (_x >> 8));
107 _x = (_x + (_x >> 16));
108 _x = (_x + (_x >> 32)) & 0x000000ff;
112 #define __bitcountl(x) __bitcount64((unsigned long)(x))
114 static __inline __uint64_t
115 __bitcount64(__uint64_t _x)
118 return (__bitcount32(_x >> 32) + __bitcount32(_x));
121 #define __bitcountl(x) __bitcount32((unsigned long)(x))
123 #define __bitcount(x) __bitcount32((unsigned int)(x))
125 * End: Ad hoc definitions from FreeBSD sys/sys/types.h
128 typedef unsigned long bitstr_t;
130 /*---------------------- Private Implementation Details ----------------------*/
131 #define _BITSTR_MASK (~0UL)
132 #define _BITSTR_BITS (sizeof(bitstr_t) * 8)
135 #define _bit_roundup2 roundup2
137 #define _bit_roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
140 /* bitstr_t in bit string containing the bit. */
144 return (_bit / _BITSTR_BITS);
147 /* bit number within bitstr_t at _bit_idx(_bit). */
149 _bit_offset(int _bit)
151 return (_bit % _BITSTR_BITS);
154 /* Mask for the bit within its long. */
155 static inline bitstr_t
158 return (1UL << _bit_offset(_bit));
161 static inline bitstr_t
162 _bit_make_mask(int _start, int _stop)
164 return ((_BITSTR_MASK << _bit_offset(_start)) &
165 (_BITSTR_MASK >> (_BITSTR_BITS - _bit_offset(_stop) - 1)));
168 /*----------------------------- Public Interface -----------------------------*/
169 /* Number of bytes allocated for a bit string of nbits bits */
170 #define bitstr_size(_nbits) (_bit_roundup2(_nbits, _BITSTR_BITS) / 8)
172 /* Allocate a bit string initialized with no bits set. */
174 static inline bitstr_t *
175 bit_alloc(int _nbits, struct malloc_type *type, int flags)
177 return ((bitstr_t *)kmalloc(bitstr_size(_nbits), type, flags | M_ZERO));
180 static inline bitstr_t *
181 bit_alloc(int _nbits)
183 return ((bitstr_t *)calloc(bitstr_size(_nbits), 1));
187 /* Allocate a bit string on the stack */
188 #define bit_decl(name, nbits) \
189 ((name)[bitstr_size(nbits) / sizeof(bitstr_t)])
191 /* Is bit N of bit string set? */
193 bit_test(const bitstr_t *_bitstr, int _bit)
195 return ((_bitstr[_bit_idx(_bit)] & _bit_mask(_bit)) != 0);
198 /* Set bit N of bit string. */
200 bit_set(bitstr_t *_bitstr, int _bit)
202 _bitstr[_bit_idx(_bit)] |= _bit_mask(_bit);
205 /* clear bit N of bit string name */
207 bit_clear(bitstr_t *_bitstr, int _bit)
209 _bitstr[_bit_idx(_bit)] &= ~_bit_mask(_bit);
212 /* Set bits start ... stop inclusive in bit string. */
214 bit_nset(bitstr_t *_bitstr, int _start, int _stop)
216 bitstr_t *_stopbitstr;
218 _stopbitstr = _bitstr + _bit_idx(_stop);
219 _bitstr += _bit_idx(_start);
221 if (_bitstr == _stopbitstr) {
222 *_bitstr |= _bit_make_mask(_start, _stop);
224 *_bitstr |= _bit_make_mask(_start, _BITSTR_BITS - 1);
225 while (++_bitstr < _stopbitstr)
226 *_bitstr = _BITSTR_MASK;
227 *_stopbitstr |= _bit_make_mask(0, _stop);
231 /* Clear bits start ... stop inclusive in bit string. */
233 bit_nclear(bitstr_t *_bitstr, int _start, int _stop)
235 bitstr_t *_stopbitstr;
237 _stopbitstr = _bitstr + _bit_idx(_stop);
238 _bitstr += _bit_idx(_start);
240 if (_bitstr == _stopbitstr) {
241 *_bitstr &= ~_bit_make_mask(_start, _stop);
243 *_bitstr &= ~_bit_make_mask(_start, _BITSTR_BITS - 1);
244 while (++_bitstr < _stopbitstr)
246 *_stopbitstr &= ~_bit_make_mask(0, _stop);
250 /* Find the first bit set in bit string at or after bit start. */
252 bit_ffs_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
254 bitstr_t *_curbitstr;
255 bitstr_t *_stopbitstr;
260 _curbitstr = _bitstr + _bit_idx(_start);
261 _stopbitstr = _bitstr + _bit_idx(_nbits - 1);
264 if (_bit_offset(_start) != 0)
265 _test &= _bit_make_mask(_start, _BITSTR_BITS - 1);
266 while (_test == 0 && _curbitstr < _stopbitstr)
267 _test = *(++_curbitstr);
269 _offset = ffsl(_test);
270 _value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1;
271 if (_offset == 0 || _value >= _nbits)
279 /* Find the first bit clear in bit string at or after bit start. */
281 bit_ffc_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
283 bitstr_t *_curbitstr;
284 bitstr_t *_stopbitstr;
289 _curbitstr = _bitstr + _bit_idx(_start);
290 _stopbitstr = _bitstr + _bit_idx(_nbits - 1);
293 if (_bit_offset(_start) != 0)
294 _test |= _bit_make_mask(0, _start - 1);
295 while (_test == _BITSTR_MASK && _curbitstr < _stopbitstr)
296 _test = *(++_curbitstr);
298 _offset = ffsl(~_test);
299 _value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1;
300 if (_offset == 0 || _value >= _nbits)
308 /* Find the first bit set in bit string. */
310 bit_ffs(bitstr_t *_bitstr, int _nbits, int *_result)
312 bit_ffs_at(_bitstr, /*start*/0, _nbits, _result);
315 /* Find the first bit clear in bit string. */
317 bit_ffc(bitstr_t *_bitstr, int _nbits, int *_result)
319 bit_ffc_at(_bitstr, /*start*/0, _nbits, _result);
322 /* Count the number of bits set in a bitstr of size _nbits at or after _start */
324 bit_count(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
326 bitstr_t *_curbitstr, mask;
327 int _value = 0, curbitstr_len;
329 if (_start >= _nbits)
332 _curbitstr = _bitstr + _bit_idx(_start);
333 _nbits -= _BITSTR_BITS * _bit_idx(_start);
334 _start -= _BITSTR_BITS * _bit_idx(_start);
337 curbitstr_len = (int)_BITSTR_BITS < _nbits ?
338 (int)_BITSTR_BITS : _nbits;
339 mask = _bit_make_mask(_start, _bit_offset(curbitstr_len - 1));
340 _value += __bitcountl(*_curbitstr & mask);
342 _nbits -= _BITSTR_BITS;
344 while (_nbits >= (int)_BITSTR_BITS) {
345 _value += __bitcountl(*_curbitstr);
347 _nbits -= _BITSTR_BITS;
350 mask = _bit_make_mask(0, _bit_offset(_nbits - 1));
351 _value += __bitcountl(*_curbitstr & mask);
358 #endif /* _SYS_FREEBSD_BITSTRING_H_ */