Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:11:22

0001 /*-
0002  * Copyright (c) 1989, 1993
0003  *  The Regents of the University of California.  All rights reserved.
0004  *
0005  * This code is derived from software contributed to Berkeley by
0006  * Paul Vixie.
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions
0010  * are met:
0011  * 1. Redistributions of source code must retain the above copyright
0012  *    notice, this list of conditions and the following disclaimer.
0013  * 2. Redistributions in binary form must reproduce the above copyright
0014  *    notice, this list of conditions and the following disclaimer in the
0015  *    documentation and/or other materials provided with the distribution.
0016  * 4. Neither the name of the University nor the names of its contributors
0017  *    may be used to endorse or promote products derived from this software
0018  *    without specific prior written permission.
0019  *
0020  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0023  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0024  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0025  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0026  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0027  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0029  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0030  * SUCH DAMAGE.
0031  *
0032  * $FreeBSD$
0033  */
0034 
0035 #ifndef LIBBSD_SYS_BITSTRING_H
0036 #define LIBBSD_SYS_BITSTRING_H
0037 
0038 typedef unsigned char bitstr_t;
0039 
0040 /* internal macros */
0041                 /* byte of the bitstring bit is in */
0042 #define _bit_byte(bit) \
0043     ((bit) >> 3)
0044 
0045                 /* mask for the bit within its byte */
0046 #define _bit_mask(bit) \
0047     (1 << ((bit)&0x7))
0048 
0049 /* external macros */
0050                 /* bytes in a bitstring of nbits bits */
0051 #define bitstr_size(nbits) \
0052     (((nbits) + 7) >> 3)
0053 
0054                 /* allocate a bitstring */
0055 #define bit_alloc(nbits) \
0056     (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
0057 
0058                 /* allocate a bitstring on the stack */
0059 #define bit_decl(name, nbits) \
0060     ((name)[bitstr_size(nbits)])
0061 
0062                 /* is bit N of bitstring name set? */
0063 #define bit_test(name, bit) \
0064     ((name)[_bit_byte(bit)] & _bit_mask(bit))
0065 
0066                 /* set bit N of bitstring name */
0067 #define bit_set(name, bit) \
0068     ((name)[_bit_byte(bit)] |= _bit_mask(bit))
0069 
0070                 /* clear bit N of bitstring name */
0071 #define bit_clear(name, bit) \
0072     ((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
0073 
0074                 /* clear bits start ... stop in bitstring */
0075 #define bit_nclear(name, start, stop) do { \
0076     register bitstr_t *_name = (name); \
0077     register int _start = (start), _stop = (stop); \
0078     register int _startbyte = _bit_byte(_start); \
0079     register int _stopbyte = _bit_byte(_stop); \
0080     if (_startbyte == _stopbyte) { \
0081         _name[_startbyte] &= ((0xff >> (8 - (_start&0x7))) | \
0082                       (0xff << ((_stop&0x7) + 1))); \
0083     } else { \
0084         _name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \
0085         while (++_startbyte < _stopbyte) \
0086             _name[_startbyte] = 0; \
0087         _name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
0088     } \
0089 } while (0)
0090 
0091                 /* set bits start ... stop in bitstring */
0092 #define bit_nset(name, start, stop) do { \
0093     register bitstr_t *_name = (name); \
0094     register int _start = (start), _stop = (stop); \
0095     register int _startbyte = _bit_byte(_start); \
0096     register int _stopbyte = _bit_byte(_stop); \
0097     if (_startbyte == _stopbyte) { \
0098         _name[_startbyte] |= ((0xff << (_start&0x7)) & \
0099                     (0xff >> (7 - (_stop&0x7)))); \
0100     } else { \
0101         _name[_startbyte] |= 0xff << ((_start)&0x7); \
0102         while (++_startbyte < _stopbyte) \
0103                 _name[_startbyte] = 0xff; \
0104         _name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
0105     } \
0106 } while (0)
0107 
0108                 /* find first bit clear in name */
0109 #define bit_ffc(name, nbits, value) do { \
0110     register bitstr_t *_name = (name); \
0111     register int _byte, _nbits = (nbits); \
0112     register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
0113     if (_nbits > 0) \
0114         for (_byte = 0; _byte <= _stopbyte; ++_byte) \
0115             if (_name[_byte] != 0xff) { \
0116                 bitstr_t _lb; \
0117                 _value = _byte << 3; \
0118                 for (_lb = _name[_byte]; (_lb&0x1); \
0119                     ++_value, _lb >>= 1); \
0120                 break; \
0121             } \
0122     if (_value >= nbits) \
0123         _value = -1; \
0124     *(value) = _value; \
0125 } while (0)
0126 
0127                 /* find first bit set in name */
0128 #define bit_ffs(name, nbits, value) do { \
0129     register bitstr_t *_name = (name); \
0130     register int _byte, _nbits = (nbits); \
0131     register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
0132     if (_nbits > 0) \
0133         for (_byte = 0; _byte <= _stopbyte; ++_byte) \
0134             if (_name[_byte]) { \
0135                 bitstr_t _lb; \
0136                 _value = _byte << 3; \
0137                 for (_lb = _name[_byte]; !(_lb&0x1); \
0138                     ++_value, _lb >>= 1); \
0139                 break; \
0140             } \
0141     if (_value >= nbits) \
0142         _value = -1; \
0143     *(value) = _value; \
0144 } while (0)
0145 
0146 #endif /* !LIBBSD_SYS_BITSTRING_H */