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