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