|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1989 The Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * This code is derived from software contributed to Berkeley by ! 6: * Paul Vixie. ! 7: * ! 8: * Redistribution and use in source and binary forms are permitted ! 9: * provided that: (1) source distributions retain this entire copyright ! 10: * notice and comment, and (2) distributions including binaries display ! 11: * the following acknowledgement: ``This product includes software ! 12: * developed by the University of California, Berkeley and its contributors'' ! 13: * in the documentation or other materials provided with the distribution ! 14: * and in all advertising materials mentioning features or use of this ! 15: * software. Neither the name of the University nor the names of its ! 16: * contributors may be used to endorse or promote products derived ! 17: * from this software without specific prior written permission. ! 18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 19: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 20: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 21: * ! 22: * @(#)bitstring.h 5.4 (Berkeley) 6/1/90 ! 23: */ ! 24: ! 25: typedef unsigned char bitstr_t; ! 26: ! 27: /* internal macros */ ! 28: /* byte of the bitstring bit is in */ ! 29: #define _bit_byte(bit) \ ! 30: ((bit) >> 3) ! 31: ! 32: /* mask for the bit within its byte */ ! 33: #define _bit_mask(bit) \ ! 34: (1 << ((bit)&0x7)) ! 35: ! 36: /* external macros */ ! 37: /* bytes in a bitstring of nbits bits */ ! 38: #define bitstr_size(nbits) \ ! 39: ((((nbits) - 1) >> 3) + 1) ! 40: ! 41: /* allocate a bitstring */ ! 42: #define bit_alloc(nbits) \ ! 43: (bitstr_t *)calloc(1, \ ! 44: (unsigned int)_bitstr_size(nbits) * sizeof(bitstr_t)) ! 45: ! 46: /* allocate a bitstring on the stack */ ! 47: #define bit_decl(name, nbits) \ ! 48: (name)[bitstr_size(nbits)] ! 49: ! 50: /* is bit N of bitstring name set? */ ! 51: #define bit_test(name, bit) \ ! 52: ((name)[_bit_byte(bit)] & _bit_mask(bit)) ! 53: ! 54: /* set bit N of bitstring name */ ! 55: #define bit_set(name, bit) \ ! 56: (name)[_bit_byte(bit)] |= _bit_mask(bit) ! 57: ! 58: /* clear bit N of bitstring name */ ! 59: #define bit_clear(name, bit) \ ! 60: (name)[_bit_byte(bit)] &= ~_bit_mask(bit) ! 61: ! 62: /* clear bits start ... stop in bitstring */ ! 63: #define bit_nclear(name, start, stop) { \ ! 64: register bitstr_t *_name = name; \ ! 65: register int _start = start, _stop = stop; \ ! 66: register int _startbyte = _bit_byte(_start); \ ! 67: register int _stopbyte = _bit_byte(_stop); \ ! 68: _name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \ ! 69: while (++_startbyte < _stopbyte) \ ! 70: _name[_startbyte] = 0; \ ! 71: _name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \ ! 72: } ! 73: ! 74: /* set bits start ... stop in bitstring */ ! 75: #define bit_nset(name, start, stop) { \ ! 76: register bitstr_t *_name = name; \ ! 77: register int _start = start, _stop = stop; \ ! 78: register int _startbyte = _bit_byte(_start); \ ! 79: register int _stopbyte = _bit_byte(_stop); \ ! 80: _name[_startbyte] |= 0xff << ((start)&0x7); \ ! 81: while (++_startbyte < _stopbyte) \ ! 82: _name[_startbyte] = 0xff; \ ! 83: _name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \ ! 84: } ! 85: ! 86: /* find first bit clear in name */ ! 87: #define bit_ffc(name, nbits, value) { \ ! 88: register bitstr_t *_name = name; \ ! 89: register int _byte, _nbits = nbits; \ ! 90: register int _stopbyte = _bit_byte(_nbits), _value = -1; \ ! 91: for (_byte = 0; _byte <= _stopbyte; ++_byte) \ ! 92: if (_name[_byte] != 0xff) { \ ! 93: _value = _byte << 3; \ ! 94: for (_stopbyte = _name[_byte]; (_stopbyte&0x1); \ ! 95: ++_value, _stopbyte >>= 1); \ ! 96: break; \ ! 97: } \ ! 98: *(value) = _value; \ ! 99: } ! 100: ! 101: /* find first bit set in name */ ! 102: #define bit_ffs(name, nbits, value) { \ ! 103: register bitstr_t *_name = name; \ ! 104: register int _byte, _nbits = nbits; \ ! 105: register int _stopbyte = _bit_byte(_nbits), _value = -1; \ ! 106: for (_byte = 0; _byte <= _stopbyte; ++_byte) \ ! 107: if (_name[_byte]) { \ ! 108: _value = _byte << 3; \ ! 109: for (_stopbyte = _name[_byte]; !(_stopbyte&0x1); \ ! 110: ++_value, _stopbyte >>= 1); \ ! 111: break; \ ! 112: } \ ! 113: *(value) = _value; \ ! 114: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.