bitset.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Mesa 3-D graphics library
  3. *
  4. * Copyright (C) 2006 Brian Paul All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /**
  25. * \file bitset.h
  26. * \brief Bitset of arbitrary size definitions.
  27. * \author Michal Krol
  28. */
  29. #ifndef BITSET_H
  30. #define BITSET_H
  31. //#include "util/bitscan.h"
  32. //#include "util/macros.h"
  33. /****************************************************************************
  34. * generic bitset implementation
  35. */
  36. #define BITSET_WORD unsigned int
  37. #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
  38. /* bitset declarations
  39. */
  40. #define BITSET_WORDS(bits) (((bits) + BITSET_WORDBITS - 1) / BITSET_WORDBITS)
  41. #define BITSET_DECLARE(name, bits) BITSET_WORD name[BITSET_WORDS(bits)]
  42. /* bitset operations
  43. */
  44. #define BITSET_COPY(x, y) memcpy( (x), (y), sizeof (x) )
  45. #define BITSET_EQUAL(x, y) (memcmp( (x), (y), sizeof (x) ) == 0)
  46. #define BITSET_ZERO(x) memset( (x), 0, sizeof (x) )
  47. #define BITSET_ONES(x) memset( (x), 0xff, sizeof (x) )
  48. #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
  49. #define BITSET_BIT(b) (1u << ((b) % BITSET_WORDBITS))
  50. /* single bit operations
  51. */
  52. #define BITSET_TEST(x, b) (((x)[BITSET_BITWORD(b)] & BITSET_BIT(b)) != 0)
  53. #define BITSET_SET(x, b) ((x)[BITSET_BITWORD(b)] |= BITSET_BIT(b))
  54. #define BITSET_CLEAR(x, b) ((x)[BITSET_BITWORD(b)] &= ~BITSET_BIT(b))
  55. #define BITSET_MASK(b) (((b) % BITSET_WORDBITS == 0) ? ~0 : BITSET_BIT(b) - 1)
  56. #define BITSET_RANGE(b, e) ((BITSET_MASK((e) + 1)) & ~(BITSET_BIT(b) - 1))
  57. /* bit range operations
  58. */
  59. #define BITSET_TEST_RANGE(x, b, e) \
  60. (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
  61. (((x)[BITSET_BITWORD(b)] & BITSET_RANGE(b, e)) != 0) : \
  62. (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
  63. #define BITSET_SET_RANGE(x, b, e) \
  64. (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
  65. ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
  66. (assert (!"BITSET_SET_RANGE: bit range crosses word boundary"), 0))
  67. #define BITSET_CLEAR_RANGE(x, b, e) \
  68. (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
  69. ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
  70. (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
  71. /* Get first bit set in a bitset.
  72. */
  73. static inline int
  74. __bitset_ffs(const BITSET_WORD *x, int n)
  75. {
  76. int i;
  77. for (i = 0; i < n; i++) {
  78. if (x[i])
  79. return ffs(x[i]) + BITSET_WORDBITS * i;
  80. }
  81. return 0;
  82. }
  83. #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
  84. static inline unsigned
  85. __bitset_next_set(unsigned i, BITSET_WORD *tmp,
  86. const BITSET_WORD *set, unsigned size)
  87. {
  88. unsigned bit, word;
  89. /* NOTE: The initial conditions for this function are very specific. At
  90. * the start of the loop, the tmp variable must be set to *set and the
  91. * initial i value set to 0. This way, if there is a bit set in the first
  92. * word, we ignore the i-value and just grab that bit (so 0 is ok, even
  93. * though 0 may be returned). If the first word is 0, then the value of
  94. * `word` will be 0 and we will go on to look at the second word.
  95. */
  96. word = BITSET_BITWORD(i);
  97. while (*tmp == 0) {
  98. word++;
  99. if (word >= BITSET_WORDS(size))
  100. return size;
  101. *tmp = set[word];
  102. }
  103. /* Find the next set bit in the non-zero word */
  104. bit = ffs(*tmp) - 1;
  105. /* Unset the bit */
  106. *tmp &= ~(1ull << bit);
  107. return word * BITSET_WORDBITS + bit;
  108. }
  109. /**
  110. * Iterates over each set bit in a set
  111. *
  112. * @param __i iteration variable, bit number
  113. * @param __set the bitset to iterate (will not be modified)
  114. * @param __size number of bits in the set to consider
  115. */
  116. #define BITSET_FOREACH_SET(__i, __set, __size) \
  117. for (BITSET_WORD __tmp = *(__set), *__foo = &__tmp; __foo != NULL; __foo = NULL) \
  118. for (__i = 0; \
  119. (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
  120. #ifdef __cplusplus
  121. /**
  122. * Simple C++ wrapper of a bitset type of static size, with value semantics
  123. * and basic bitwise arithmetic operators. The operators defined below are
  124. * expected to have the same semantics as the same operator applied to other
  125. * fundamental integer types. T is the name of the struct to instantiate
  126. * it as, and N is the number of bits in the bitset.
  127. */
  128. #define DECLARE_BITSET_T(T, N) struct T { \
  129. EXPLICIT_CONVERSION \
  130. operator bool() const \
  131. { \
  132. for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
  133. if (words[i]) \
  134. return true; \
  135. return false; \
  136. } \
  137. \
  138. T & \
  139. operator=(int x) \
  140. { \
  141. const T c = {{ (BITSET_WORD)x }}; \
  142. return *this = c; \
  143. } \
  144. \
  145. friend bool \
  146. operator==(const T &b, const T &c) \
  147. { \
  148. return BITSET_EQUAL(b.words, c.words); \
  149. } \
  150. \
  151. friend bool \
  152. operator!=(const T &b, const T &c) \
  153. { \
  154. return !(b == c); \
  155. } \
  156. \
  157. friend bool \
  158. operator==(const T &b, int x) \
  159. { \
  160. const T c = {{ (BITSET_WORD)x }}; \
  161. return b == c; \
  162. } \
  163. \
  164. friend bool \
  165. operator!=(const T &b, int x) \
  166. { \
  167. return !(b == x); \
  168. } \
  169. \
  170. friend T \
  171. operator~(const T &b) \
  172. { \
  173. T c; \
  174. for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
  175. c.words[i] = ~b.words[i]; \
  176. return c; \
  177. } \
  178. \
  179. T & \
  180. operator|=(const T &b) \
  181. { \
  182. for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
  183. words[i] |= b.words[i]; \
  184. return *this; \
  185. } \
  186. \
  187. friend T \
  188. operator|(const T &b, const T &c) \
  189. { \
  190. T d = b; \
  191. d |= c; \
  192. return d; \
  193. } \
  194. \
  195. T & \
  196. operator&=(const T &b) \
  197. { \
  198. for (unsigned i = 0; i < BITSET_WORDS(N); i++) \
  199. words[i] &= b.words[i]; \
  200. return *this; \
  201. } \
  202. \
  203. friend T \
  204. operator&(const T &b, const T &c) \
  205. { \
  206. T d = b; \
  207. d &= c; \
  208. return d; \
  209. } \
  210. \
  211. bool \
  212. test(unsigned i) const \
  213. { \
  214. return BITSET_TEST(words, i); \
  215. } \
  216. \
  217. T & \
  218. set(unsigned i) \
  219. { \
  220. BITSET_SET(words, i); \
  221. return *this; \
  222. } \
  223. \
  224. T & \
  225. clear(unsigned i) \
  226. { \
  227. BITSET_CLEAR(words, i); \
  228. return *this; \
  229. } \
  230. \
  231. BITSET_WORD words[BITSET_WORDS(N)]; \
  232. }
  233. #endif
  234. #endif