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