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