xref: /illumos-gate/usr/src/boot/sys/sys/bitstring.h (revision 199767f8)
1*199767f8SToomas Soome /*-
2*199767f8SToomas Soome  * Copyright (c) 1989, 1993
3*199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
4*199767f8SToomas Soome  *
5*199767f8SToomas Soome  * This code is derived from software contributed to Berkeley by
6*199767f8SToomas Soome  * Paul Vixie.
7*199767f8SToomas Soome  *
8*199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
9*199767f8SToomas Soome  * modification, are permitted provided that the following conditions
10*199767f8SToomas Soome  * are met:
11*199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
12*199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
13*199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
14*199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
15*199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
16*199767f8SToomas Soome  * 4. Neither the name of the University nor the names of its contributors
17*199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
18*199767f8SToomas Soome  *    without specific prior written permission.
19*199767f8SToomas Soome  *
20*199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21*199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24*199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*199767f8SToomas Soome  * SUCH DAMAGE.
31*199767f8SToomas Soome  *
32*199767f8SToomas Soome  * $FreeBSD$
33*199767f8SToomas Soome  */
34*199767f8SToomas Soome 
35*199767f8SToomas Soome #ifndef _SYS_BITSTRING_H_
36*199767f8SToomas Soome #define	_SYS_BITSTRING_H_
37*199767f8SToomas Soome 
38*199767f8SToomas Soome typedef	unsigned char bitstr_t;
39*199767f8SToomas Soome 
40*199767f8SToomas Soome /* internal macros */
41*199767f8SToomas Soome 				/* byte of the bitstring bit is in */
42*199767f8SToomas Soome #define	_bit_byte(bit) \
43*199767f8SToomas Soome 	((bit) >> 3)
44*199767f8SToomas Soome 
45*199767f8SToomas Soome 				/* mask for the bit within its byte */
46*199767f8SToomas Soome #define	_bit_mask(bit) \
47*199767f8SToomas Soome 	(1 << ((bit)&0x7))
48*199767f8SToomas Soome 
49*199767f8SToomas Soome /* external macros */
50*199767f8SToomas Soome 				/* bytes in a bitstring of nbits bits */
51*199767f8SToomas Soome #define	bitstr_size(nbits) \
52*199767f8SToomas Soome 	(((nbits) + 7) >> 3)
53*199767f8SToomas Soome 
54*199767f8SToomas Soome 				/* allocate a bitstring */
55*199767f8SToomas Soome #define	bit_alloc(nbits) \
56*199767f8SToomas Soome 	(bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
57*199767f8SToomas Soome 
58*199767f8SToomas Soome 				/* allocate a bitstring on the stack */
59*199767f8SToomas Soome #define	bit_decl(name, nbits) \
60*199767f8SToomas Soome 	((name)[bitstr_size(nbits)])
61*199767f8SToomas Soome 
62*199767f8SToomas Soome 				/* is bit N of bitstring name set? */
63*199767f8SToomas Soome #define	bit_test(name, bit) \
64*199767f8SToomas Soome 	((name)[_bit_byte(bit)] & _bit_mask(bit))
65*199767f8SToomas Soome 
66*199767f8SToomas Soome 				/* set bit N of bitstring name */
67*199767f8SToomas Soome #define	bit_set(name, bit) \
68*199767f8SToomas Soome 	((name)[_bit_byte(bit)] |= _bit_mask(bit))
69*199767f8SToomas Soome 
70*199767f8SToomas Soome 				/* clear bit N of bitstring name */
71*199767f8SToomas Soome #define	bit_clear(name, bit) \
72*199767f8SToomas Soome 	((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
73*199767f8SToomas Soome 
74*199767f8SToomas Soome 				/* clear bits start ... stop in bitstring */
75*199767f8SToomas Soome #define	bit_nclear(name, start, stop) do { \
76*199767f8SToomas Soome 	register bitstr_t *_name = (name); \
77*199767f8SToomas Soome 	register int _start = (start), _stop = (stop); \
78*199767f8SToomas Soome 	register int _startbyte = _bit_byte(_start); \
79*199767f8SToomas Soome 	register int _stopbyte = _bit_byte(_stop); \
80*199767f8SToomas Soome 	if (_startbyte == _stopbyte) { \
81*199767f8SToomas Soome 		_name[_startbyte] &= ((0xff >> (8 - (_start&0x7))) | \
82*199767f8SToomas Soome 				      (0xff << ((_stop&0x7) + 1))); \
83*199767f8SToomas Soome 	} else { \
84*199767f8SToomas Soome 		_name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \
85*199767f8SToomas Soome 		while (++_startbyte < _stopbyte) \
86*199767f8SToomas Soome 			_name[_startbyte] = 0; \
87*199767f8SToomas Soome 		_name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
88*199767f8SToomas Soome 	} \
89*199767f8SToomas Soome } while (0)
90*199767f8SToomas Soome 
91*199767f8SToomas Soome 				/* set bits start ... stop in bitstring */
92*199767f8SToomas Soome #define	bit_nset(name, start, stop) do { \
93*199767f8SToomas Soome 	register bitstr_t *_name = (name); \
94*199767f8SToomas Soome 	register int _start = (start), _stop = (stop); \
95*199767f8SToomas Soome 	register int _startbyte = _bit_byte(_start); \
96*199767f8SToomas Soome 	register int _stopbyte = _bit_byte(_stop); \
97*199767f8SToomas Soome 	if (_startbyte == _stopbyte) { \
98*199767f8SToomas Soome 		_name[_startbyte] |= ((0xff << (_start&0x7)) & \
99*199767f8SToomas Soome 				    (0xff >> (7 - (_stop&0x7)))); \
100*199767f8SToomas Soome 	} else { \
101*199767f8SToomas Soome 		_name[_startbyte] |= 0xff << ((_start)&0x7); \
102*199767f8SToomas Soome 		while (++_startbyte < _stopbyte) \
103*199767f8SToomas Soome 	    		_name[_startbyte] = 0xff; \
104*199767f8SToomas Soome 		_name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
105*199767f8SToomas Soome 	} \
106*199767f8SToomas Soome } while (0)
107*199767f8SToomas Soome 
108*199767f8SToomas Soome 				/* find first bit clear in name */
109*199767f8SToomas Soome #define	bit_ffc(name, nbits, value) do { \
110*199767f8SToomas Soome 	register bitstr_t *_name = (name); \
111*199767f8SToomas Soome 	register int _byte, _nbits = (nbits); \
112*199767f8SToomas Soome 	register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
113*199767f8SToomas Soome 	if (_nbits > 0) \
114*199767f8SToomas Soome 		for (_byte = 0; _byte <= _stopbyte; ++_byte) \
115*199767f8SToomas Soome 			if (_name[_byte] != 0xff) { \
116*199767f8SToomas Soome 				bitstr_t _lb; \
117*199767f8SToomas Soome 				_value = _byte << 3; \
118*199767f8SToomas Soome 				for (_lb = _name[_byte]; (_lb&0x1); \
119*199767f8SToomas Soome 				    ++_value, _lb >>= 1); \
120*199767f8SToomas Soome 				break; \
121*199767f8SToomas Soome 			} \
122*199767f8SToomas Soome 	if (_value >= nbits) \
123*199767f8SToomas Soome 		_value = -1; \
124*199767f8SToomas Soome 	*(value) = _value; \
125*199767f8SToomas Soome } while (0)
126*199767f8SToomas Soome 
127*199767f8SToomas Soome 				/* find first bit set in name */
128*199767f8SToomas Soome #define	bit_ffs(name, nbits, value) do { \
129*199767f8SToomas Soome 	register bitstr_t *_name = (name); \
130*199767f8SToomas Soome 	register int _byte, _nbits = (nbits); \
131*199767f8SToomas Soome 	register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
132*199767f8SToomas Soome 	if (_nbits > 0) \
133*199767f8SToomas Soome 		for (_byte = 0; _byte <= _stopbyte; ++_byte) \
134*199767f8SToomas Soome 			if (_name[_byte]) { \
135*199767f8SToomas Soome 				bitstr_t _lb; \
136*199767f8SToomas Soome 				_value = _byte << 3; \
137*199767f8SToomas Soome 				for (_lb = _name[_byte]; !(_lb&0x1); \
138*199767f8SToomas Soome 				    ++_value, _lb >>= 1); \
139*199767f8SToomas Soome 				break; \
140*199767f8SToomas Soome 			} \
141*199767f8SToomas Soome 	if (_value >= nbits) \
142*199767f8SToomas Soome 		_value = -1; \
143*199767f8SToomas Soome 	*(value) = _value; \
144*199767f8SToomas Soome } while (0)
145*199767f8SToomas Soome 
146*199767f8SToomas Soome #endif /* !_SYS_BITSTRING_H_ */
147