bitstring.h (1540) | bitstring.h (66872) |
---|---|
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. 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, with or without --- 19 unchanged lines hidden (view full) --- 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 * | 1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. 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, with or without --- 19 unchanged lines hidden (view full) --- 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 | 36 * $FreeBSD: head/include/bitstring.h 66872 2000-10-09 12:34:51Z dwmalone $ |
37 */ 38 39#ifndef _BITSTRING_H_ 40#define _BITSTRING_H_ 41 42typedef unsigned char bitstr_t; 43 44/* internal macros */ 45 /* byte of the bitstring bit is in */ 46#define _bit_byte(bit) \ 47 ((bit) >> 3) 48 49 /* mask for the bit within its byte */ 50#define _bit_mask(bit) \ 51 (1 << ((bit)&0x7)) 52 53/* external macros */ 54 /* bytes in a bitstring of nbits bits */ 55#define bitstr_size(nbits) \ | 37 */ 38 39#ifndef _BITSTRING_H_ 40#define _BITSTRING_H_ 41 42typedef unsigned char bitstr_t; 43 44/* internal macros */ 45 /* byte of the bitstring bit is in */ 46#define _bit_byte(bit) \ 47 ((bit) >> 3) 48 49 /* mask for the bit within its byte */ 50#define _bit_mask(bit) \ 51 (1 << ((bit)&0x7)) 52 53/* external macros */ 54 /* bytes in a bitstring of nbits bits */ 55#define bitstr_size(nbits) \ |
56 ((((nbits) - 1) >> 3) + 1) | 56 (((nbits) + 7) >> 3) |
57 58 /* allocate a bitstring */ 59#define bit_alloc(nbits) \ | 57 58 /* allocate a bitstring */ 59#define bit_alloc(nbits) \ |
60 (bitstr_t *)calloc(1, \ 61 (unsigned int)bitstr_size(nbits) * sizeof(bitstr_t)) | 60 (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t)) |
62 63 /* allocate a bitstring on the stack */ 64#define bit_decl(name, nbits) \ | 61 62 /* allocate a bitstring on the stack */ 63#define bit_decl(name, nbits) \ |
65 (name)[bitstr_size(nbits)] | 64 ((name)[bitstr_size(nbits)]) |
66 67 /* is bit N of bitstring name set? */ 68#define bit_test(name, bit) \ 69 ((name)[_bit_byte(bit)] & _bit_mask(bit)) 70 71 /* set bit N of bitstring name */ 72#define bit_set(name, bit) \ | 65 66 /* is bit N of bitstring name set? */ 67#define bit_test(name, bit) \ 68 ((name)[_bit_byte(bit)] & _bit_mask(bit)) 69 70 /* set bit N of bitstring name */ 71#define bit_set(name, bit) \ |
73 (name)[_bit_byte(bit)] |= _bit_mask(bit) | 72 ((name)[_bit_byte(bit)] |= _bit_mask(bit)) |
74 75 /* clear bit N of bitstring name */ 76#define bit_clear(name, bit) \ | 73 74 /* clear bit N of bitstring name */ 75#define bit_clear(name, bit) \ |
77 (name)[_bit_byte(bit)] &= ~_bit_mask(bit) | 76 ((name)[_bit_byte(bit)] &= ~_bit_mask(bit)) |
78 79 /* clear bits start ... stop in bitstring */ | 77 78 /* clear bits start ... stop in bitstring */ |
80#define bit_nclear(name, start, stop) { \ 81 register bitstr_t *_name = name; \ 82 register int _start = start, _stop = stop; \ | 79#define bit_nclear(name, start, stop) do { \ 80 register bitstr_t *_name = (name); \ 81 register int _start = (start), _stop = (stop); \ |
83 register int _startbyte = _bit_byte(_start); \ 84 register int _stopbyte = _bit_byte(_stop); \ 85 if (_startbyte == _stopbyte) { \ 86 _name[_startbyte] &= ((0xff >> (8 - (_start&0x7))) | \ 87 (0xff << ((_stop&0x7) + 1))); \ 88 } else { \ 89 _name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \ 90 while (++_startbyte < _stopbyte) \ 91 _name[_startbyte] = 0; \ 92 _name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \ 93 } \ | 82 register int _startbyte = _bit_byte(_start); \ 83 register int _stopbyte = _bit_byte(_stop); \ 84 if (_startbyte == _stopbyte) { \ 85 _name[_startbyte] &= ((0xff >> (8 - (_start&0x7))) | \ 86 (0xff << ((_stop&0x7) + 1))); \ 87 } else { \ 88 _name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \ 89 while (++_startbyte < _stopbyte) \ 90 _name[_startbyte] = 0; \ 91 _name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \ 92 } \ |
94} | 93} while (0) |
95 96 /* set bits start ... stop in bitstring */ | 94 95 /* set bits start ... stop in bitstring */ |
97#define bit_nset(name, start, stop) { \ 98 register bitstr_t *_name = name; \ 99 register int _start = start, _stop = stop; \ | 96#define bit_nset(name, start, stop) do { \ 97 register bitstr_t *_name = (name); \ 98 register int _start = (start), _stop = (stop); \ |
100 register int _startbyte = _bit_byte(_start); \ 101 register int _stopbyte = _bit_byte(_stop); \ 102 if (_startbyte == _stopbyte) { \ 103 _name[_startbyte] |= ((0xff << (_start&0x7)) & \ 104 (0xff >> (7 - (_stop&0x7)))); \ 105 } else { \ 106 _name[_startbyte] |= 0xff << ((_start)&0x7); \ 107 while (++_startbyte < _stopbyte) \ 108 _name[_startbyte] = 0xff; \ 109 _name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \ 110 } \ | 99 register int _startbyte = _bit_byte(_start); \ 100 register int _stopbyte = _bit_byte(_stop); \ 101 if (_startbyte == _stopbyte) { \ 102 _name[_startbyte] |= ((0xff << (_start&0x7)) & \ 103 (0xff >> (7 - (_stop&0x7)))); \ 104 } else { \ 105 _name[_startbyte] |= 0xff << ((_start)&0x7); \ 106 while (++_startbyte < _stopbyte) \ 107 _name[_startbyte] = 0xff; \ 108 _name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \ 109 } \ |
111} | 110} while (0) |
112 113 /* find first bit clear in name */ | 111 112 /* find first bit clear in name */ |
114#define bit_ffc(name, nbits, value) { \ 115 register bitstr_t *_name = name; \ 116 register int _byte, _nbits = nbits; \ 117 register int _stopbyte = _bit_byte(_nbits), _value = -1; \ 118 for (_byte = 0; _byte <= _stopbyte; ++_byte) \ 119 if (_name[_byte] != 0xff) { \ 120 _value = _byte << 3; \ 121 for (_stopbyte = _name[_byte]; (_stopbyte&0x1); \ 122 ++_value, _stopbyte >>= 1); \ 123 break; \ 124 } \ | 113#define bit_ffc(name, nbits, value) do { \ 114 register bitstr_t *_name = (name); \ 115 register int _byte, _nbits = (nbits); \ 116 register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \ 117 if (_nbits > 0) \ 118 for (_byte = 0; _byte <= _stopbyte; ++_byte) \ 119 if (_name[_byte] != 0xff) { \ 120 bitstr_t _lb; \ 121 _value = _byte << 3; \ 122 for (_lb = _name[_byte]; (_lb&0x1); \ 123 ++_value, _lb >>= 1); \ 124 break; \ 125 } \ 126 if (_value >= nbits) \ 127 _value = -1; \ |
125 *(value) = _value; \ | 128 *(value) = _value; \ |
126} | 129} while (0) |
127 128 /* find first bit set in name */ | 130 131 /* find first bit set in name */ |
129#define bit_ffs(name, nbits, value) { \ 130 register bitstr_t *_name = name; \ 131 register int _byte, _nbits = nbits; \ 132 register int _stopbyte = _bit_byte(_nbits), _value = -1; \ 133 for (_byte = 0; _byte <= _stopbyte; ++_byte) \ 134 if (_name[_byte]) { \ 135 _value = _byte << 3; \ 136 for (_stopbyte = _name[_byte]; !(_stopbyte&0x1); \ 137 ++_value, _stopbyte >>= 1); \ 138 break; \ 139 } \ | 132#define bit_ffs(name, nbits, value) do { \ 133 register bitstr_t *_name = (name); \ 134 register int _byte, _nbits = (nbits); \ 135 register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \ 136 if (_nbits > 0) \ 137 for (_byte = 0; _byte <= _stopbyte; ++_byte) \ 138 if (_name[_byte]) { \ 139 bitstr_t _lb; \ 140 _value = _byte << 3; \ 141 for (_lb = _name[_byte]; !(_lb&0x1); \ 142 ++_value, _lb >>= 1); \ 143 break; \ 144 } \ 145 if (_value >= nbits) \ 146 _value = -1; \ |
140 *(value) = _value; \ | 147 *(value) = _value; \ |
141} | 148} while (0) |
142 143#endif /* !_BITSTRING_H_ */ | 149 150#endif /* !_BITSTRING_H_ */ |