xref: /minix3/include/bitstring.h (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*	$NetBSD: bitstring.h,v 1.9 2010/05/06 18:54:22 christos Exp $	*/
2*2fe8fb19SBen Gras 
3*2fe8fb19SBen Gras /*
4*2fe8fb19SBen Gras  * Copyright (c) 1989, 1993
5*2fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
6*2fe8fb19SBen Gras  *
7*2fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
8*2fe8fb19SBen Gras  * Paul Vixie.
9*2fe8fb19SBen Gras  *
10*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
11*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
12*2fe8fb19SBen Gras  * are met:
13*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
14*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
15*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
16*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
17*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
18*2fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
19*2fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
20*2fe8fb19SBen Gras  *    without specific prior written permission.
21*2fe8fb19SBen Gras  *
22*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*2fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*2fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*2fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*2fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*2fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*2fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*2fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*2fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*2fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*2fe8fb19SBen Gras  * SUCH DAMAGE.
33*2fe8fb19SBen Gras  *
34*2fe8fb19SBen Gras  *	@(#)bitstring.h	8.1 (Berkeley) 7/19/93
35*2fe8fb19SBen Gras  */
36*2fe8fb19SBen Gras 
37*2fe8fb19SBen Gras #ifndef _BITSTRING_H_
38*2fe8fb19SBen Gras #define	_BITSTRING_H_
39*2fe8fb19SBen Gras 
40*2fe8fb19SBen Gras /* modified for SV/AT and bitstring bugfix by M.R.Murphy, 11oct91
41*2fe8fb19SBen Gras  * bitstr_size changed gratuitously, but shorter
42*2fe8fb19SBen Gras  * bit_alloc   spelling error fixed
43*2fe8fb19SBen Gras  * the following were efficient, but didn't work, they've been made to
44*2fe8fb19SBen Gras  * work, but are no longer as efficient :-)
45*2fe8fb19SBen Gras  * bit_nclear, bit_nset, bit_ffc, bit_ffs
46*2fe8fb19SBen Gras  */
47*2fe8fb19SBen Gras /*
48*2fe8fb19SBen Gras  * The comment above may or may not bear any resemblance to reality.
49*2fe8fb19SBen Gras  * This code has been maintained in a confusing way, with little
50*2fe8fb19SBen Gras  * information available on the provenance of much of it. "At least it
51*2fe8fb19SBen Gras  * works."
52*2fe8fb19SBen Gras  *  /s/ Perry E. Metzger, 2 Feb 98
53*2fe8fb19SBen Gras  */
54*2fe8fb19SBen Gras typedef	unsigned char bitstr_t;
55*2fe8fb19SBen Gras 
56*2fe8fb19SBen Gras /* internal macros */
57*2fe8fb19SBen Gras 				/* byte of the bitstring bit is in */
58*2fe8fb19SBen Gras #define	_bit_byte(bit) \
59*2fe8fb19SBen Gras 	(uint32_t)((bit) >> 3)
60*2fe8fb19SBen Gras 
61*2fe8fb19SBen Gras 				/* mask for the bit within its byte */
62*2fe8fb19SBen Gras #define	_bit_mask(bit) \
63*2fe8fb19SBen Gras 	(uint32_t)((1 << (uint32_t)((bit)&0x7)))
64*2fe8fb19SBen Gras 
65*2fe8fb19SBen Gras /* external macros */
66*2fe8fb19SBen Gras 				/* bytes in a bitstring of nbits bits */
67*2fe8fb19SBen Gras #define	bitstr_size(nbits) \
68*2fe8fb19SBen Gras 	(size_t)((uint32_t)((nbits) + 7) >> 3)
69*2fe8fb19SBen Gras 
70*2fe8fb19SBen Gras 				/* allocate a bitstring */
71*2fe8fb19SBen Gras #define	bit_alloc(nbits) \
72*2fe8fb19SBen Gras 	calloc(bitstr_size(nbits), sizeof(bitstr_t))
73*2fe8fb19SBen Gras 
74*2fe8fb19SBen Gras 				/* allocate a bitstring on the stack */
75*2fe8fb19SBen Gras #define	bit_decl(name, nbits) \
76*2fe8fb19SBen Gras 	((name)[bitstr_size(nbits)])
77*2fe8fb19SBen Gras 
78*2fe8fb19SBen Gras 				/* is bit N of bitstring name set? */
79*2fe8fb19SBen Gras #define	bit_test(name, bit) \
80*2fe8fb19SBen Gras 	/*LINTED bitwise on signed*/((name)[_bit_byte(bit)] & _bit_mask(bit))
81*2fe8fb19SBen Gras 
82*2fe8fb19SBen Gras 				/* set bit N of bitstring name */
83*2fe8fb19SBen Gras #define	bit_set(name, bit) \
84*2fe8fb19SBen Gras 	/*LINTED bitwise on signed*/((name)[_bit_byte(bit)] |= _bit_mask(bit))
85*2fe8fb19SBen Gras 
86*2fe8fb19SBen Gras 				/* clear bit N of bitstring name */
87*2fe8fb19SBen Gras #define	bit_clear(name, bit) \
88*2fe8fb19SBen Gras 	/*LINTED bitwise on signed*/((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
89*2fe8fb19SBen Gras 
90*2fe8fb19SBen Gras 				/* clear bits start ... stop in bitstring */
91*2fe8fb19SBen Gras #define	bit_nclear(name, start, stop) do { \
92*2fe8fb19SBen Gras 	bitstr_t *_name = name; \
93*2fe8fb19SBen Gras 	int _start = start, _stop = stop; \
94*2fe8fb19SBen Gras 	while (_start <= _stop) { \
95*2fe8fb19SBen Gras 		bit_clear(_name, _start); \
96*2fe8fb19SBen Gras 		_start++; \
97*2fe8fb19SBen Gras 	} \
98*2fe8fb19SBen Gras } while(/*CONSTCOND*/0)
99*2fe8fb19SBen Gras 
100*2fe8fb19SBen Gras 				/* set bits start ... stop in bitstring */
101*2fe8fb19SBen Gras #define	bit_nset(name, start, stop) do { \
102*2fe8fb19SBen Gras 	bitstr_t *_name = name; \
103*2fe8fb19SBen Gras 	int _start = start, _stop = stop; \
104*2fe8fb19SBen Gras 	while (_start <= _stop) { \
105*2fe8fb19SBen Gras 		bit_set(_name, _start); \
106*2fe8fb19SBen Gras 		_start++; \
107*2fe8fb19SBen Gras 	} \
108*2fe8fb19SBen Gras } while(/*CONSTCOND*/0)
109*2fe8fb19SBen Gras 
110*2fe8fb19SBen Gras 				/* find first bit clear in name */
111*2fe8fb19SBen Gras #define	bit_ffc(name, nbits, value) do { \
112*2fe8fb19SBen Gras 	bitstr_t *_name = name; \
113*2fe8fb19SBen Gras 	int _bit, _nbits = nbits, _value = -1; \
114*2fe8fb19SBen Gras 	for (_bit = 0; _bit < _nbits; ++_bit) \
115*2fe8fb19SBen Gras 		if (!bit_test(_name, _bit)) { \
116*2fe8fb19SBen Gras 			_value = _bit; \
117*2fe8fb19SBen Gras 			break; \
118*2fe8fb19SBen Gras 		} \
119*2fe8fb19SBen Gras 	*(value) = _value; \
120*2fe8fb19SBen Gras } while(/*CONSTCOND*/0)
121*2fe8fb19SBen Gras 
122*2fe8fb19SBen Gras 				/* find first bit set in name */
123*2fe8fb19SBen Gras #define	bit_ffs(name, nbits, value) do { \
124*2fe8fb19SBen Gras 	bitstr_t *_name = name; \
125*2fe8fb19SBen Gras 	int _bit, _nbits = nbits, _value = -1; \
126*2fe8fb19SBen Gras 	for (_bit = 0; _bit < _nbits; ++_bit) \
127*2fe8fb19SBen Gras 		if (bit_test(_name, _bit)) { \
128*2fe8fb19SBen Gras 			_value = _bit; \
129*2fe8fb19SBen Gras 			break; \
130*2fe8fb19SBen Gras 		} \
131*2fe8fb19SBen Gras 	*(value) = _value; \
132*2fe8fb19SBen Gras } while(/*CONSTCOND*/0)
133*2fe8fb19SBen Gras 
134*2fe8fb19SBen Gras #endif /* !_BITSTRING_H_ */
135