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