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.

116 lines
3.5 KiB

  1. /* $OpenBSD: bitstring.h,v 1.3 1997/09/21 10:45:26 niklas Exp $ */
  2. /* $NetBSD: bitstring.h,v 1.4 1994/10/26 00:55:45 cgd Exp $ */
  3. /*
  4. * Copyright (c) 1989 The Regents of the University of California.
  5. * 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 are permitted
  11. * provided that the above copyright notice and this paragraph are
  12. * duplicated in all such forms and that any documentation,
  13. * advertising materials, and other materials related to such
  14. * distribution and use acknowledge that the software was developed
  15. * by the University of California, Berkeley. The name of the
  16. * University may not be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  19. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21. *
  22. * @(#)bitstring.h 5.2 (Berkeley) 4/4/90
  23. */
  24. #ifndef _BITSTRING_H_
  25. #define _BITSTRING_H_
  26. /* modified for SV/AT and bitstring bugfix by M.R.Murphy, 11oct91
  27. * bitstr_size changed gratuitously, but shorter
  28. * bit_alloc spelling error fixed
  29. * the following were efficient, but didn't work, they've been made to
  30. * work, but are no longer as efficient :-)
  31. * bit_nclear, bit_nset, bit_ffc, bit_ffs
  32. */
  33. typedef unsigned char bitstr_t;
  34. /* internal macros */
  35. /* byte of the bitstring bit is in */
  36. #define _bit_byte(bit) \
  37. ((bit) >> 3)
  38. /* mask for the bit within its byte */
  39. #define _bit_mask(bit) \
  40. (1 << ((bit)&0x7))
  41. /* external macros */
  42. /* bytes in a bitstring of nbits bits */
  43. #define bitstr_size(nbits) \
  44. (((nbits) + 7) >> 3)
  45. /* allocate a bitstring */
  46. #define bit_alloc(nbits) \
  47. (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
  48. /* allocate a bitstring on the stack */
  49. #define bit_decl(name, nbits) \
  50. (name)[bitstr_size(nbits)]
  51. /* is bit N of bitstring name set? */
  52. #define bit_test(name, bit) \
  53. ((name)[_bit_byte(bit)] & _bit_mask(bit))
  54. /* set bit N of bitstring name */
  55. #define bit_set(name, bit) \
  56. (name)[_bit_byte(bit)] |= _bit_mask(bit)
  57. /* clear bit N of bitstring name */
  58. #define bit_clear(name, bit) \
  59. (name)[_bit_byte(bit)] &= ~_bit_mask(bit)
  60. /* clear bits start ... stop in bitstring */
  61. #define bit_nclear(name, start, stop) do { \
  62. register bitstr_t *_name = name; \
  63. register int _start = start, _stop = stop; \
  64. while (_start <= _stop) { \
  65. bit_clear(_name, _start); \
  66. _start++; \
  67. } \
  68. } while (0)
  69. /* set bits start ... stop in bitstring */
  70. #define bit_nset(name, start, stop) do { \
  71. register bitstr_t *_name = name; \
  72. register int _start = start, _stop = stop; \
  73. while (_start <= _stop) { \
  74. bit_set(_name, _start); \
  75. _start++; \
  76. } \
  77. } while (0)
  78. /* find first bit clear in name */
  79. #define bit_ffc(name, nbits, value) do { \
  80. register bitstr_t *_name = name; \
  81. register int _bit, _nbits = nbits, _value = -1; \
  82. for (_bit = 0; _bit < _nbits; ++_bit) \
  83. if (!bit_test(_name, _bit)) { \
  84. _value = _bit; \
  85. break; \
  86. } \
  87. *(value) = _value; \
  88. } while (0)
  89. /* find first bit set in name */
  90. #define bit_ffs(name, nbits, value) do { \
  91. register bitstr_t *_name = name; \
  92. register int _bit, _nbits = nbits, _value = -1; \
  93. for (_bit = 0; _bit < _nbits; ++_bit) \
  94. if (bit_test(_name, _bit)) { \
  95. _value = _bit; \
  96. break; \
  97. } \
  98. *(value) = _value; \
  99. } while (0)
  100. #endif /* !_BITSTRING_H_ */