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