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