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