home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / include / bitstring.h < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  134 lines

  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, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  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    5.5 (Berkeley) 4/3/91
  37.  */
  38.  
  39. #ifndef _BITSTRING_H_
  40. #define    _BITSTRING_H_
  41.  
  42. typedef    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)
  57.  
  58.                 /* allocate a bitstring */
  59. #define    bit_alloc(nbits) \
  60.     (bitstr_t *)calloc(1, \
  61.         (unsigned int)_bitstr_size(nbits) * sizeof(bitstr_t))
  62.  
  63.                 /* allocate a bitstring on the stack */
  64. #define    bit_decl(name, nbits) \
  65.     (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) \
  73.     (name)[_bit_byte(bit)] |= _bit_mask(bit)
  74.  
  75.                 /* clear bit N of bitstring name */
  76. #define    bit_clear(name, bit) \
  77.     (name)[_bit_byte(bit)] &= ~_bit_mask(bit)
  78.  
  79.                 /* 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; \
  83.     register int _startbyte = _bit_byte(_start); \
  84.     register int _stopbyte = _bit_byte(_stop); \
  85.     _name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \
  86.     while (++_startbyte < _stopbyte) \
  87.         _name[_startbyte] = 0; \
  88.     _name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
  89. }
  90.  
  91.                 /* set bits start ... stop in bitstring */
  92. #define    bit_nset(name, start, stop) { \
  93.     register bitstr_t *_name = name; \
  94.     register int _start = start, _stop = stop; \
  95.     register int _startbyte = _bit_byte(_start); \
  96.     register int _stopbyte = _bit_byte(_stop); \
  97.     _name[_startbyte] |= 0xff << ((start)&0x7); \
  98.     while (++_startbyte < _stopbyte) \
  99.         _name[_startbyte] = 0xff; \
  100.     _name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
  101. }
  102.  
  103.                 /* find first bit clear in name */
  104. #define    bit_ffc(name, nbits, value) { \
  105.     register bitstr_t *_name = name; \
  106.     register int _byte, _nbits = nbits; \
  107.     register int _stopbyte = _bit_byte(_nbits), _value = -1; \
  108.     for (_byte = 0; _byte <= _stopbyte; ++_byte) \
  109.         if (_name[_byte] != 0xff) { \
  110.             _value = _byte << 3; \
  111.             for (_stopbyte = _name[_byte]; (_stopbyte&0x1); \
  112.                 ++_value, _stopbyte >>= 1); \
  113.             break; \
  114.         } \
  115.     *(value) = _value; \
  116. }
  117.  
  118.                 /* find first bit set in name */
  119. #define    bit_ffs(name, nbits, value) { \
  120.     register bitstr_t *_name = name; \
  121.     register int _byte, _nbits = nbits; \
  122.     register int _stopbyte = _bit_byte(_nbits), _value = -1; \
  123.     for (_byte = 0; _byte <= _stopbyte; ++_byte) \
  124.         if (_name[_byte]) { \
  125.             _value = _byte << 3; \
  126.             for (_stopbyte = _name[_byte]; !(_stopbyte&0x1); \
  127.                 ++_value, _stopbyte >>= 1); \
  128.             break; \
  129.         } \
  130.     *(value) = _value; \
  131. }
  132.  
  133. #endif /* !_BITSTRING_H_ */
  134.