bitstring.h revision 1.15 1 1.15 rillig /* $NetBSD: bitstring.h,v 1.15 2024/05/12 10:41:23 rillig 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.9 christos (uint32_t)((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.9 christos (uint32_t)((1 << (uint32_t)((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.9 christos (size_t)((uint32_t)((nbits) + 7) >> 3)
69 1.1 cgd
70 1.1 cgd /* allocate a bitstring */
71 1.1 cgd #define bit_alloc(nbits) \
72 1.9 christos calloc(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.9 christos /*LINTED bitwise on signed*/((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.13 christos /*LINTED bitwise on signed*/ \
85 1.14 christos ((name)[_bit_byte(bit)] = \
86 1.14 christos (unsigned char)(_bit_mask(bit) | (name)[_bit_byte(bit)]))
87 1.1 cgd
88 1.1 cgd /* clear bit N of bitstring name */
89 1.1 cgd #define bit_clear(name, bit) \
90 1.14 christos /*LINTED bitwise on signed*/ \
91 1.14 christos ((name)[_bit_byte(bit)] &= (unsigned char)~_bit_mask(bit))
92 1.1 cgd
93 1.1 cgd /* clear bits start ... stop in bitstring */
94 1.5 pk #define bit_nclear(name, start, stop) do { \
95 1.8 perry bitstr_t *_name = name; \
96 1.10 christos size_t _start = start, _stop = stop; \
97 1.2 cgd while (_start <= _stop) { \
98 1.2 cgd bit_clear(_name, _start); \
99 1.2 cgd _start++; \
100 1.9 christos } \
101 1.15 rillig } while (0)
102 1.1 cgd
103 1.1 cgd /* set bits start ... stop in bitstring */
104 1.5 pk #define bit_nset(name, start, stop) do { \
105 1.8 perry bitstr_t *_name = name; \
106 1.10 christos size_t _start = start, _stop = stop; \
107 1.2 cgd while (_start <= _stop) { \
108 1.2 cgd bit_set(_name, _start); \
109 1.2 cgd _start++; \
110 1.9 christos } \
111 1.15 rillig } while (0)
112 1.1 cgd
113 1.1 cgd /* find first bit clear in name */
114 1.5 pk #define bit_ffc(name, nbits, value) do { \
115 1.12 christos const bitstr_t *_name = name; \
116 1.11 christos size_t _bit, _nbits = nbits; \
117 1.11 christos int _value = -1; \
118 1.2 cgd for (_bit = 0; _bit < _nbits; ++_bit) \
119 1.2 cgd if (!bit_test(_name, _bit)) { \
120 1.2 cgd _value = _bit; \
121 1.1 cgd break; \
122 1.1 cgd } \
123 1.1 cgd *(value) = _value; \
124 1.15 rillig } while (0)
125 1.1 cgd
126 1.1 cgd /* find first bit set in name */
127 1.5 pk #define bit_ffs(name, nbits, value) do { \
128 1.12 christos const bitstr_t *_name = name; \
129 1.11 christos size_t _bit, _nbits = nbits; \
130 1.11 christos int _value = -1; \
131 1.2 cgd for (_bit = 0; _bit < _nbits; ++_bit) \
132 1.2 cgd if (bit_test(_name, _bit)) { \
133 1.2 cgd _value = _bit; \
134 1.1 cgd break; \
135 1.1 cgd } \
136 1.1 cgd *(value) = _value; \
137 1.15 rillig } while (0)
138 1.1 cgd
139 1.1 cgd #endif /* !_BITSTRING_H_ */
140