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