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