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