bitstring.h revision 299090
1139825Simp/*-
21539Srgrimes * Copyright (c) 1989, 1993
31539Srgrimes *	The Regents of the University of California.  All rights reserved.
41539Srgrimes *
51539Srgrimes * This code is derived from software contributed to Berkeley by
61539Srgrimes * Paul Vixie.
71539Srgrimes *
81539Srgrimes * Redistribution and use in source and binary forms, with or without
91539Srgrimes * modification, are permitted provided that the following conditions
101539Srgrimes * are met:
111539Srgrimes * 1. Redistributions of source code must retain the above copyright
121539Srgrimes *    notice, this list of conditions and the following disclaimer.
131539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141539Srgrimes *    notice, this list of conditions and the following disclaimer in the
151539Srgrimes *    documentation and/or other materials provided with the distribution.
161539Srgrimes * 4. Neither the name of the University nor the names of its contributors
171539Srgrimes *    may be used to endorse or promote products derived from this software
181539Srgrimes *    without specific prior written permission.
191539Srgrimes *
201539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301539Srgrimes * SUCH DAMAGE.
311539Srgrimes *
32299090Sasomers * Copyright (c) 2014 Spectra Logic Corporation
33299090Sasomers * All rights reserved.
34299090Sasomers *
35299090Sasomers * Redistribution and use in source and binary forms, with or without
36299090Sasomers * modification, are permitted provided that the following conditions
37299090Sasomers * are met:
38299090Sasomers * 1. Redistributions of source code must retain the above copyright
39299090Sasomers *    notice, this list of conditions, and the following disclaimer,
40299090Sasomers *    without modification.
41299090Sasomers * 2. Redistributions in binary form must reproduce at minimum a disclaimer
42299090Sasomers *    substantially similar to the "NO WARRANTY" disclaimer below
43299090Sasomers *    ("Disclaimer") and any redistribution must be conditioned upon
44299090Sasomers *    including a substantially similar Disclaimer requirement for further
45299090Sasomers *    binary redistribution.
46299090Sasomers *
47299090Sasomers * NO WARRANTY
48299090Sasomers * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49299090Sasomers * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50299090Sasomers * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
51299090Sasomers * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
52299090Sasomers * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53299090Sasomers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54299090Sasomers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55299090Sasomers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56299090Sasomers * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
57299090Sasomers * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58299090Sasomers * POSSIBILITY OF SUCH DAMAGES.
59299090Sasomers *
6066872Sdwmalone * $FreeBSD: head/sys/sys/bitstring.h 299090 2016-05-04 22:34:11Z asomers $
611539Srgrimes */
62116306Sphk#ifndef _SYS_BITSTRING_H_
63116306Sphk#define	_SYS_BITSTRING_H_
641539Srgrimes
65299090Sasomers#ifdef _KERNEL
66299090Sasomers#include <sys/libkern.h>
67299090Sasomers#include <sys/malloc.h>
68299090Sasomers#endif
691539Srgrimes
70299090Sasomerstypedef	unsigned long bitstr_t;
711539Srgrimes
72299090Sasomers/*---------------------- Private Implementation Details ----------------------*/
73299090Sasomers#define	_BITSTR_MASK (~0UL)
74299090Sasomers#define	_BITSTR_BITS (sizeof(bitstr_t) * 8)
751539Srgrimes
76299090Sasomers/* bitstr_t in bit string containing the bit. */
77299090Sasomersstatic inline int
78299090Sasomers_bit_idx(int _bit)
79299090Sasomers{
80299090Sasomers	return (_bit / _BITSTR_BITS);
81299090Sasomers}
821539Srgrimes
83299090Sasomers/* bit number within bitstr_t at _bit_idx(_bit). */
84299090Sasomersstatic inline int
85299090Sasomers_bit_offset(int _bit)
86299090Sasomers{
87299090Sasomers	return (_bit % _BITSTR_BITS);
88299090Sasomers}
891539Srgrimes
90299090Sasomers/* Mask for the bit within its long. */
91299090Sasomersstatic inline bitstr_t
92299090Sasomers_bit_mask(int _bit)
93299090Sasomers{
94299090Sasomers	return (1UL << _bit_offset(_bit));
95299090Sasomers}
96299090Sasomers
97299090Sasomersstatic inline bitstr_t
98299090Sasomers_bit_make_mask(int _start, int _stop)
99299090Sasomers{
100299090Sasomers	return ((_BITSTR_MASK << _bit_offset(_start)) &
101299090Sasomers	    (_BITSTR_MASK >> (_BITSTR_BITS - _bit_offset(_stop) - 1)));
102299090Sasomers}
103299090Sasomers
104299090Sasomers/*----------------------------- Public Interface -----------------------------*/
105299090Sasomers/* Number of bytes consumed by a bit string of nbits bits */
106299090Sasomers#define	bitstr_size(_nbits) \
107299090Sasomers	(((_nbits) + _BITSTR_BITS - 1) / 8)
108299090Sasomers
109299090Sasomers/* Allocate a bit string initialized with no bits set. */
110299090Sasomers#ifdef _KERNEL
111299090Sasomersstatic inline bitstr_t *
112299090Sasomersbit_alloc(int _nbits, struct malloc_type *type, int flags)
113299090Sasomers{
114299090Sasomers	return ((bitstr_t *)malloc(bitstr_size(_nbits), type, flags | M_ZERO));
115299090Sasomers}
116299090Sasomers#else
117299090Sasomersstatic inline bitstr_t *
118299090Sasomersbit_alloc(int _nbits)
119299090Sasomers{
120299090Sasomers	return ((bitstr_t *)calloc(bitstr_size(_nbits), 1));
121299090Sasomers}
122299090Sasomers#endif
123299090Sasomers
124299090Sasomers/* Allocate a bit string on the stack with no bits set. */
1251539Srgrimes#define	bit_decl(name, nbits) \
126299090Sasomers	((name)[bitstr_size(nbits) / sizeof(bitstr_t)])
1271539Srgrimes
128299090Sasomers/* Is bit N of bit string set? */
129299090Sasomersstatic inline int
130299090Sasomersbit_test(const bitstr_t *_bitstr, int _bit)
131299090Sasomers{
132299090Sasomers	return ((_bitstr[_bit_idx(_bit)] & _bit_mask(_bit)) != 0);
133299090Sasomers}
1341539Srgrimes
135299090Sasomers/* Set bit N of bit string. */
136299090Sasomersstatic inline void
137299090Sasomersbit_set(bitstr_t *_bitstr, int _bit)
138299090Sasomers{
139299090Sasomers	_bitstr[_bit_idx(_bit)] |= _bit_mask(_bit);
140299090Sasomers}
1411539Srgrimes
142299090Sasomers/* clear bit N of bit string name */
143299090Sasomersstatic inline void
144299090Sasomersbit_clear(bitstr_t *_bitstr, int _bit)
145299090Sasomers{
146299090Sasomers	_bitstr[_bit_idx(_bit)] &= ~_bit_mask(_bit);
147299090Sasomers}
1481539Srgrimes
149299090Sasomers/* Set bits start ... stop inclusive in bit string. */
150299090Sasomersstatic inline void
151299090Sasomersbit_nset(bitstr_t *_bitstr, int _start, int _stop)
152299090Sasomers{
153299090Sasomers	bitstr_t *_stopbitstr;
1541539Srgrimes
155299090Sasomers	_stopbitstr = _bitstr + _bit_idx(_stop);
156299090Sasomers	_bitstr += _bit_idx(_start);
1571539Srgrimes
158299090Sasomers	if (_bitstr == _stopbitstr) {
159299090Sasomers		*_bitstr |= _bit_make_mask(_start, _stop);
160299090Sasomers	} else {
161299090Sasomers		*_bitstr |= _bit_make_mask(_start, _BITSTR_BITS - 1);
162299090Sasomers		while (++_bitstr < _stopbitstr)
163299090Sasomers	    		*_bitstr = _BITSTR_MASK;
164299090Sasomers		*_stopbitstr |= _bit_make_mask(0, _stop);
165299090Sasomers	}
166299090Sasomers}
1671539Srgrimes
168299090Sasomers/* Clear bits start ... stop inclusive in bit string. */
169299090Sasomersstatic inline void
170299090Sasomersbit_nclear(bitstr_t *_bitstr, int _start, int _stop)
171299090Sasomers{
172299090Sasomers	bitstr_t *_stopbitstr;
1731539Srgrimes
174299090Sasomers	_stopbitstr = _bitstr + _bit_idx(_stop);
175299090Sasomers	_bitstr += _bit_idx(_start);
176299090Sasomers
177299090Sasomers	if (_bitstr == _stopbitstr) {
178299090Sasomers		*_bitstr &= ~_bit_make_mask(_start, _stop);
179299090Sasomers	} else {
180299090Sasomers		*_bitstr &= ~_bit_make_mask(_start, _BITSTR_BITS - 1);
181299090Sasomers		while (++_bitstr < _stopbitstr)
182299090Sasomers			*_bitstr = 0;
183299090Sasomers		*_stopbitstr &= ~_bit_make_mask(0, _stop);
184299090Sasomers	}
185299090Sasomers}
186299090Sasomers
187299090Sasomers/* Find the first bit set in bit string at or after bit start. */
188299090Sasomersstatic inline void
189299090Sasomersbit_ffs_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
190299090Sasomers{
191299090Sasomers	bitstr_t *_curbitstr;
192299090Sasomers	bitstr_t *_stopbitstr;
193299090Sasomers	bitstr_t _test;
194299090Sasomers	int _value, _offset;
195299090Sasomers
196299090Sasomers	if (_nbits > 0) {
197299090Sasomers		_curbitstr = _bitstr + _bit_idx(_start);
198299090Sasomers		_stopbitstr = _bitstr + _bit_idx(_nbits - 1);
199299090Sasomers
200299090Sasomers		_test = *_curbitstr;
201299090Sasomers		if (_bit_offset(_start) != 0)
202299090Sasomers			_test &= _bit_make_mask(_start, _BITSTR_BITS - 1);
203299090Sasomers		while (_test == 0 && _curbitstr < _stopbitstr)
204299090Sasomers			_test = *(++_curbitstr);
205299090Sasomers
206299090Sasomers		_offset = ffsl(_test);
207299090Sasomers		_value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1;
208299090Sasomers		if (_offset == 0 || _value >= _nbits)
209299090Sasomers			_value = -1;
210299090Sasomers	} else {
211299090Sasomers		_value = -1;
212299090Sasomers	}
213299090Sasomers	*_result = _value;
214299090Sasomers}
215299090Sasomers
216299090Sasomers/* Find the first bit clear in bit string at or after bit start. */
217299090Sasomersstatic inline void
218299090Sasomersbit_ffc_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
219299090Sasomers{
220299090Sasomers	bitstr_t *_curbitstr;
221299090Sasomers	bitstr_t *_stopbitstr;
222299090Sasomers	bitstr_t _test;
223299090Sasomers	int _value, _offset;
224299090Sasomers
225299090Sasomers	if (_nbits > 0) {
226299090Sasomers		_curbitstr = _bitstr + _bit_idx(_start);
227299090Sasomers		_stopbitstr = _bitstr + _bit_idx(_nbits - 1);
228299090Sasomers
229299090Sasomers		_test = *_curbitstr;
230299090Sasomers		if (_bit_offset(_start) != 0)
231299090Sasomers			_test |= _bit_make_mask(0, _start - 1);
232299090Sasomers		while (_test == _BITSTR_MASK && _curbitstr < _stopbitstr)
233299090Sasomers			_test = *(++_curbitstr);
234299090Sasomers
235299090Sasomers		_offset = ffsl(~_test);
236299090Sasomers		_value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1;
237299090Sasomers		if (_offset == 0 || _value >= _nbits)
238299090Sasomers			_value = -1;
239299090Sasomers	} else {
240299090Sasomers		_value = -1;
241299090Sasomers	}
242299090Sasomers	*_result = _value;
243299090Sasomers}
244299090Sasomers
245299090Sasomers/* Find the first bit set in bit string. */
246299090Sasomersstatic inline void
247299090Sasomersbit_ffs(bitstr_t *_bitstr, int _nbits, int *_result)
248299090Sasomers{
249299090Sasomers	bit_ffs_at(_bitstr, /*start*/0, _nbits, _result);
250299090Sasomers}
251299090Sasomers
252299090Sasomers/* Find the first bit clear in bit string. */
253299090Sasomersstatic inline void
254299090Sasomersbit_ffc(bitstr_t *_bitstr, int _nbits, int *_result)
255299090Sasomers{
256299090Sasomers	bit_ffc_at(_bitstr, /*start*/0, _nbits, _result);
257299090Sasomers}
258299090Sasomers
259299090Sasomers#endif	/* _SYS_BITSTRING_H_ */
260