1139825Simp/*- 21539Srgrimes * Copyright (c) 1989, 1993 31539Srgrimes * The Regents of the University of California. All rights reserved. 41539Srgrimes * 51539Srgrimes * This code is derived from software contributed to Berkeley by 61539Srgrimes * Paul Vixie. 71539Srgrimes * 81539Srgrimes * Redistribution and use in source and binary forms, with or without 91539Srgrimes * modification, are permitted provided that the following conditions 101539Srgrimes * are met: 111539Srgrimes * 1. Redistributions of source code must retain the above copyright 121539Srgrimes * notice, this list of conditions and the following disclaimer. 131539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141539Srgrimes * notice, this list of conditions and the following disclaimer in the 151539Srgrimes * documentation and/or other materials provided with the distribution. 161539Srgrimes * 4. Neither the name of the University nor the names of its contributors 171539Srgrimes * may be used to endorse or promote products derived from this software 181539Srgrimes * without specific prior written permission. 191539Srgrimes * 201539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231539Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301539Srgrimes * SUCH DAMAGE. 311539Srgrimes * 3266872Sdwmalone * $FreeBSD$ 331539Srgrimes */ 341539Srgrimes 35116306Sphk#ifndef _SYS_BITSTRING_H_ 36116306Sphk#define _SYS_BITSTRING_H_ 371539Srgrimes 381539Srgrimestypedef unsigned char bitstr_t; 391539Srgrimes 401539Srgrimes/* internal macros */ 411539Srgrimes /* byte of the bitstring bit is in */ 421539Srgrimes#define _bit_byte(bit) \ 431539Srgrimes ((bit) >> 3) 441539Srgrimes 451539Srgrimes /* mask for the bit within its byte */ 461539Srgrimes#define _bit_mask(bit) \ 471539Srgrimes (1 << ((bit)&0x7)) 481539Srgrimes 491539Srgrimes/* external macros */ 501539Srgrimes /* bytes in a bitstring of nbits bits */ 511539Srgrimes#define bitstr_size(nbits) \ 5266872Sdwmalone (((nbits) + 7) >> 3) 531539Srgrimes 541539Srgrimes /* allocate a bitstring */ 551539Srgrimes#define bit_alloc(nbits) \ 5666872Sdwmalone (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t)) 571539Srgrimes 581539Srgrimes /* allocate a bitstring on the stack */ 591539Srgrimes#define bit_decl(name, nbits) \ 6066872Sdwmalone ((name)[bitstr_size(nbits)]) 611539Srgrimes 621539Srgrimes /* is bit N of bitstring name set? */ 631539Srgrimes#define bit_test(name, bit) \ 641539Srgrimes ((name)[_bit_byte(bit)] & _bit_mask(bit)) 651539Srgrimes 661539Srgrimes /* set bit N of bitstring name */ 671539Srgrimes#define bit_set(name, bit) \ 6866872Sdwmalone ((name)[_bit_byte(bit)] |= _bit_mask(bit)) 691539Srgrimes 701539Srgrimes /* clear bit N of bitstring name */ 711539Srgrimes#define bit_clear(name, bit) \ 7266872Sdwmalone ((name)[_bit_byte(bit)] &= ~_bit_mask(bit)) 731539Srgrimes 741539Srgrimes /* clear bits start ... stop in bitstring */ 7566872Sdwmalone#define bit_nclear(name, start, stop) do { \ 7666872Sdwmalone register bitstr_t *_name = (name); \ 7766872Sdwmalone register int _start = (start), _stop = (stop); \ 781539Srgrimes register int _startbyte = _bit_byte(_start); \ 791539Srgrimes register int _stopbyte = _bit_byte(_stop); \ 801539Srgrimes if (_startbyte == _stopbyte) { \ 811539Srgrimes _name[_startbyte] &= ((0xff >> (8 - (_start&0x7))) | \ 821539Srgrimes (0xff << ((_stop&0x7) + 1))); \ 831539Srgrimes } else { \ 841539Srgrimes _name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \ 851539Srgrimes while (++_startbyte < _stopbyte) \ 861539Srgrimes _name[_startbyte] = 0; \ 871539Srgrimes _name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \ 881539Srgrimes } \ 8966872Sdwmalone} while (0) 901539Srgrimes 911539Srgrimes /* set bits start ... stop in bitstring */ 9266872Sdwmalone#define bit_nset(name, start, stop) do { \ 9366872Sdwmalone register bitstr_t *_name = (name); \ 9466872Sdwmalone register int _start = (start), _stop = (stop); \ 951539Srgrimes register int _startbyte = _bit_byte(_start); \ 961539Srgrimes register int _stopbyte = _bit_byte(_stop); \ 971539Srgrimes if (_startbyte == _stopbyte) { \ 981539Srgrimes _name[_startbyte] |= ((0xff << (_start&0x7)) & \ 991539Srgrimes (0xff >> (7 - (_stop&0x7)))); \ 1001539Srgrimes } else { \ 1011539Srgrimes _name[_startbyte] |= 0xff << ((_start)&0x7); \ 1021539Srgrimes while (++_startbyte < _stopbyte) \ 1031539Srgrimes _name[_startbyte] = 0xff; \ 1041539Srgrimes _name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \ 1051539Srgrimes } \ 10666872Sdwmalone} while (0) 1071539Srgrimes 1081539Srgrimes /* find first bit clear in name */ 10966872Sdwmalone#define bit_ffc(name, nbits, value) do { \ 11066872Sdwmalone register bitstr_t *_name = (name); \ 11166872Sdwmalone register int _byte, _nbits = (nbits); \ 11266872Sdwmalone register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \ 11366872Sdwmalone if (_nbits > 0) \ 11466872Sdwmalone for (_byte = 0; _byte <= _stopbyte; ++_byte) \ 11566872Sdwmalone if (_name[_byte] != 0xff) { \ 11666872Sdwmalone bitstr_t _lb; \ 11766872Sdwmalone _value = _byte << 3; \ 11866872Sdwmalone for (_lb = _name[_byte]; (_lb&0x1); \ 11966872Sdwmalone ++_value, _lb >>= 1); \ 12066872Sdwmalone break; \ 12166872Sdwmalone } \ 12266872Sdwmalone if (_value >= nbits) \ 12366872Sdwmalone _value = -1; \ 1241539Srgrimes *(value) = _value; \ 12566872Sdwmalone} while (0) 1261539Srgrimes 1271539Srgrimes /* find first bit set in name */ 12866872Sdwmalone#define bit_ffs(name, nbits, value) do { \ 12966872Sdwmalone register bitstr_t *_name = (name); \ 13066872Sdwmalone register int _byte, _nbits = (nbits); \ 13166872Sdwmalone register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \ 13266872Sdwmalone if (_nbits > 0) \ 13366872Sdwmalone for (_byte = 0; _byte <= _stopbyte; ++_byte) \ 13466872Sdwmalone if (_name[_byte]) { \ 13566872Sdwmalone bitstr_t _lb; \ 13666872Sdwmalone _value = _byte << 3; \ 13766872Sdwmalone for (_lb = _name[_byte]; !(_lb&0x1); \ 13866872Sdwmalone ++_value, _lb >>= 1); \ 13966872Sdwmalone break; \ 14066872Sdwmalone } \ 14166872Sdwmalone if (_value >= nbits) \ 14266872Sdwmalone _value = -1; \ 1431539Srgrimes *(value) = _value; \ 14466872Sdwmalone} while (0) 1451539Srgrimes 146116306Sphk#endif /* !_SYS_BITSTRING_H_ */ 147