Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB

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