xref: /netbsd-src/include/bitstring.h (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: bitstring.h,v 1.5 1997/05/14 15:49:55 pk Exp $	*/
2 
3 /*
4  * Copyright (c) 1989 The Regents of the University of California.
5  * 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 are permitted
11  * provided that the above copyright notice and this paragraph are
12  * duplicated in all such forms and that any documentation,
13  * advertising materials, and other materials related to such
14  * distribution and use acknowledge that the software was developed
15  * by the University of California, Berkeley.  The name of the
16  * University may not be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  *
22  *	@(#)bitstring.h	5.2 (Berkeley) 4/4/90
23  */
24 
25 #ifndef _BITSTRING_H_
26 #define _BITSTRING_H_
27 
28 /* modified for SV/AT and bitstring bugfix by M.R.Murphy, 11oct91
29  * bitstr_size changed gratuitously, but shorter
30  * bit_alloc   spelling error fixed
31  * the following were efficient, but didn't work, they've been made to
32  * work, but are no longer as efficient :-)
33  * bit_nclear, bit_nset, bit_ffc, bit_ffs
34  */
35 typedef	unsigned char bitstr_t;
36 
37 /* internal macros */
38 				/* byte of the bitstring bit is in */
39 #define	_bit_byte(bit) \
40 	((bit) >> 3)
41 
42 				/* mask for the bit within its byte */
43 #define	_bit_mask(bit) \
44 	(1 << ((bit)&0x7))
45 
46 /* external macros */
47 				/* bytes in a bitstring of nbits bits */
48 #define	bitstr_size(nbits) \
49 	(((nbits) + 7) >> 3)
50 
51 				/* allocate a bitstring */
52 #define	bit_alloc(nbits) \
53 	(bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
54 
55 				/* allocate a bitstring on the stack */
56 #define	bit_decl(name, nbits) \
57 	((name)[bitstr_size(nbits)])
58 
59 				/* is bit N of bitstring name set? */
60 #define	bit_test(name, bit) \
61 	((name)[_bit_byte(bit)] & _bit_mask(bit))
62 
63 				/* set bit N of bitstring name */
64 #define	bit_set(name, bit) \
65 	((name)[_bit_byte(bit)] |= _bit_mask(bit))
66 
67 				/* clear bit N of bitstring name */
68 #define	bit_clear(name, bit) \
69 	((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
70 
71 				/* clear bits start ... stop in bitstring */
72 #define	bit_nclear(name, start, stop) do { \
73 	register bitstr_t *_name = name; \
74 	register int _start = start, _stop = stop; \
75 	while (_start <= _stop) { \
76 		bit_clear(_name, _start); \
77 		_start++; \
78 		} \
79 } while(0)
80 
81 				/* set bits start ... stop in bitstring */
82 #define	bit_nset(name, start, stop) do { \
83 	register bitstr_t *_name = name; \
84 	register int _start = start, _stop = stop; \
85 	while (_start <= _stop) { \
86 		bit_set(_name, _start); \
87 		_start++; \
88 		} \
89 } while(0)
90 
91 				/* find first bit clear in name */
92 #define	bit_ffc(name, nbits, value) do { \
93 	register bitstr_t *_name = name; \
94 	register int _bit, _nbits = nbits, _value = -1; \
95 	for (_bit = 0; _bit < _nbits; ++_bit) \
96 		if (!bit_test(_name, _bit)) { \
97 			_value = _bit; \
98 			break; \
99 		} \
100 	*(value) = _value; \
101 } while(0)
102 
103 				/* find first bit set in name */
104 #define	bit_ffs(name, nbits, value) do { \
105 	register bitstr_t *_name = name; \
106 	register int _bit, _nbits = nbits, _value = -1; \
107 	for (_bit = 0; _bit < _nbits; ++_bit) \
108 		if (bit_test(_name, _bit)) { \
109 			_value = _bit; \
110 			break; \
111 		} \
112 	*(value) = _value; \
113 } while(0)
114 
115 #endif /* !_BITSTRING_H_ */
116