1 /* $NetBSD: bitstring.h,v 1.6 1998/02/02 21:07:19 perry Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Paul Vixie. 9 * 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. 25 * 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. 37 * 38 * @(#)bitstring.h 8.1 (Berkeley) 7/19/93 39 */ 40 41 #ifndef _BITSTRING_H_ 42 #define _BITSTRING_H_ 43 44 /* modified for SV/AT and bitstring bugfix by M.R.Murphy, 11oct91 45 * bitstr_size changed gratuitously, but shorter 46 * bit_alloc spelling error fixed 47 * the following were efficient, but didn't work, they've been made to 48 * work, but are no longer as efficient :-) 49 * bit_nclear, bit_nset, bit_ffc, bit_ffs 50 */ 51 /* 52 * The comment above may or may not bear any resemblance to reality. 53 * This code has been maintained in a confusing way, with little 54 * information available on the provenance of much of it. "At least it 55 * works." 56 * /s/ Perry E. Metzger, 2 Feb 98 57 */ 58 typedef unsigned char bitstr_t; 59 60 /* internal macros */ 61 /* byte of the bitstring bit is in */ 62 #define _bit_byte(bit) \ 63 ((bit) >> 3) 64 65 /* mask for the bit within its byte */ 66 #define _bit_mask(bit) \ 67 (1 << ((bit)&0x7)) 68 69 /* external macros */ 70 /* bytes in a bitstring of nbits bits */ 71 #define bitstr_size(nbits) \ 72 (((nbits) + 7) >> 3) 73 74 /* allocate a bitstring */ 75 #define bit_alloc(nbits) \ 76 (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t)) 77 78 /* allocate a bitstring on the stack */ 79 #define bit_decl(name, nbits) \ 80 ((name)[bitstr_size(nbits)]) 81 82 /* is bit N of bitstring name set? */ 83 #define bit_test(name, bit) \ 84 ((name)[_bit_byte(bit)] & _bit_mask(bit)) 85 86 /* set bit N of bitstring name */ 87 #define bit_set(name, bit) \ 88 ((name)[_bit_byte(bit)] |= _bit_mask(bit)) 89 90 /* clear bit N of bitstring name */ 91 #define bit_clear(name, bit) \ 92 ((name)[_bit_byte(bit)] &= ~_bit_mask(bit)) 93 94 /* clear bits start ... stop in bitstring */ 95 #define bit_nclear(name, start, stop) do { \ 96 register bitstr_t *_name = name; \ 97 register int _start = start, _stop = stop; \ 98 while (_start <= _stop) { \ 99 bit_clear(_name, _start); \ 100 _start++; \ 101 } \ 102 } while(0) 103 104 /* set bits start ... stop in bitstring */ 105 #define bit_nset(name, start, stop) do { \ 106 register bitstr_t *_name = name; \ 107 register int _start = start, _stop = stop; \ 108 while (_start <= _stop) { \ 109 bit_set(_name, _start); \ 110 _start++; \ 111 } \ 112 } while(0) 113 114 /* find first bit clear in name */ 115 #define bit_ffc(name, nbits, value) do { \ 116 register bitstr_t *_name = name; \ 117 register int _bit, _nbits = nbits, _value = -1; \ 118 for (_bit = 0; _bit < _nbits; ++_bit) \ 119 if (!bit_test(_name, _bit)) { \ 120 _value = _bit; \ 121 break; \ 122 } \ 123 *(value) = _value; \ 124 } while(0) 125 126 /* find first bit set in name */ 127 #define bit_ffs(name, nbits, value) do { \ 128 register bitstr_t *_name = name; \ 129 register int _bit, _nbits = nbits, _value = -1; \ 130 for (_bit = 0; _bit < _nbits; ++_bit) \ 131 if (bit_test(_name, _bit)) { \ 132 _value = _bit; \ 133 break; \ 134 } \ 135 *(value) = _value; \ 136 } while(0) 137 138 #endif /* !_BITSTRING_H_ */ 139