Add 'gcc46' as a CCVER value (using /usr/pkgsrc/lang/gnat-aux).
[dragonfly.git] / include / bitstring.h
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Paul Vixie.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/include/bitstring.h,v 1.1.1.1.14.1 2000/10/30 11:21:04 dwmalone Exp $
37  * $DragonFly: src/include/bitstring.h,v 1.3 2007/04/18 18:39:11 swildner Exp $
38  */
39
40 #ifndef _BITSTRING_H_
41 #define _BITSTRING_H_
42
43 typedef unsigned char bitstr_t;
44
45 /* internal macros */
46                                 /* byte of the bitstring bit is in */
47 #define _bit_byte(bit) \
48         ((bit) >> 3)
49
50                                 /* mask for the bit within its byte */
51 #define _bit_mask(bit) \
52         (1 << ((bit)&0x7))
53
54 /* external macros */
55                                 /* bytes in a bitstring of nbits bits */
56 #define bitstr_size(nbits) \
57         (((nbits) + 7) >> 3)
58
59                                 /* allocate a bitstring */
60 #define bit_alloc(nbits) \
61         (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
62
63                                 /* allocate a bitstring on the stack */
64 #define bit_decl(name, nbits) \
65         ((name)[bitstr_size(nbits)])
66
67                                 /* is bit N of bitstring name set? */
68 #define bit_test(name, bit) \
69         ((name)[_bit_byte(bit)] & _bit_mask(bit))
70
71                                 /* set bit N of bitstring name */
72 #define bit_set(name, bit) \
73         ((name)[_bit_byte(bit)] |= _bit_mask(bit))
74
75                                 /* clear bit N of bitstring name */
76 #define bit_clear(name, bit) \
77         ((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
78
79                                 /* clear bits start ... stop in bitstring */
80 #define bit_nclear(name, start, stop) do { \
81         bitstr_t *_name = (name); \
82         int _start = (start), _stop = (stop); \
83         int _startbyte = _bit_byte(_start); \
84         int _stopbyte = _bit_byte(_stop); \
85         if (_startbyte == _stopbyte) { \
86                 _name[_startbyte] &= ((0xff >> (8 - (_start&0x7))) | \
87                                       (0xff << ((_stop&0x7) + 1))); \
88         } else { \
89                 _name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \
90                 while (++_startbyte < _stopbyte) \
91                         _name[_startbyte] = 0; \
92                 _name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
93         } \
94 } while (0)
95
96                                 /* set bits start ... stop in bitstring */
97 #define bit_nset(name, start, stop) do { \
98         bitstr_t *_name = (name); \
99         int _start = (start), _stop = (stop); \
100         int _startbyte = _bit_byte(_start); \
101         int _stopbyte = _bit_byte(_stop); \
102         if (_startbyte == _stopbyte) { \
103                 _name[_startbyte] |= ((0xff << (_start&0x7)) & \
104                                     (0xff >> (7 - (_stop&0x7)))); \
105         } else { \
106                 _name[_startbyte] |= 0xff << ((_start)&0x7); \
107                 while (++_startbyte < _stopbyte) \
108                         _name[_startbyte] = 0xff; \
109                 _name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
110         } \
111 } while (0)
112
113                                 /* find first bit clear in name */
114 #define bit_ffc(name, nbits, value) do { \
115         bitstr_t *_name = (name); \
116         int _byte, _nbits = (nbits); \
117         int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
118         if (_nbits > 0) \
119                 for (_byte = 0; _byte <= _stopbyte; ++_byte) \
120                         if (_name[_byte] != 0xff) { \
121                                 bitstr_t _lb; \
122                                 _value = _byte << 3; \
123                                 for (_lb = _name[_byte]; (_lb&0x1); \
124                                     ++_value, _lb >>= 1); \
125                                 break; \
126                         } \
127         if (_value >= nbits) \
128                 _value = -1; \
129         *(value) = _value; \
130 } while (0)
131
132                                 /* find first bit set in name */
133 #define bit_ffs(name, nbits, value) do { \
134         bitstr_t *_name = (name); \
135         int _byte, _nbits = (nbits); \
136         int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
137         if (_nbits > 0) \
138                 for (_byte = 0; _byte <= _stopbyte; ++_byte) \
139                         if (_name[_byte]) { \
140                                 bitstr_t _lb; \
141                                 _value = _byte << 3; \
142                                 for (_lb = _name[_byte]; !(_lb&0x1); \
143                                     ++_value, _lb >>= 1); \
144                                 break; \
145                         } \
146         if (_value >= nbits) \
147                 _value = -1; \
148         *(value) = _value; \
149 } while (0)
150
151 #define _fls(mask, value) do { \
152         int _fmask = (mask); \
153         int *_value = (value); \
154         int _bit = 0; \
155         if (_fmask == 0) { \
156                 *(_value) = 0; \
157                 break; \
158         } \
159         for (_bit = 1; _fmask != 1; _bit++) \
160                 _fmask = (unsigned char) _fmask >> 1; \
161         *(_value) = _bit; \
162 } while (0)
163
164                                 /* find last bit set in name */
165 #define bit_fls(name, nbits, value) do { \
166         bitstr_t *_name = (name); \
167         int _nbits = (nbits); \
168         int _byte = _bit_byte(_nbits - 1); \
169         int *_value = (value); \
170         int _mask = 0;\
171         if (_nbits > 0) \
172                 for (; _byte >= 0; _byte--) { \
173                         if (!_name[_byte]) \
174                                 continue; \
175                         _fls(_name[_byte], &_mask); \
176                         break; \
177                 } \
178         *(_value) = (_mask * (_byte + 1)) - 1; \
179 } while (0)
180
181                                 /* find clear range of length len */
182 #define bit_nsearch(name, nbits, value, len) do { \
183         bitstr_t *_name = (name); \
184         int _nbits = (nbits); \
185         int *_value = (value); \
186         int _len = (len); \
187         int _bit, _bit0; \
188         int _tmplen = _len; \
189         int _match = 0; \
190         *(_value) = -1; \
191         for (_bit = 0; _bit < _nbits; _bit++) { \
192                 if (_match == 0) { \
193                         if (bit_test((_name), _bit) == 0) { \
194                                 _match = 1; \
195                                 _tmplen--; \
196                                 _bit0 = _bit; \
197                         } else { \
198                                 continue; \
199                         } \
200                 } else { \
201                         if (_tmplen >= 0) { \
202                                 if (bit_test((_name), _bit) == 0) { \
203                                         _tmplen--; \
204                                 } else { \
205                                         _match = 0; \
206                                         _tmplen = _len; \
207                                         continue; \
208                                 } \
209                         } else { \
210                                 *(_value) = _bit0; \
211                         } \
212                 } \
213         } \
214 } while (0)
215
216 #endif /* !_BITSTRING_H_ */