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