2 * Mesa 3-D graphics library
4 * Copyright (C) 2006 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
27 * \brief Bitset of arbitrary size definitions.
34 #include "util/u_math.h"
36 /****************************************************************************
37 * generic bitset implementation
40 #define BITSET_WORD unsigned int
41 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
43 /* bitset declarations
45 #define BITSET_WORDS(bits) (((bits) + BITSET_WORDBITS - 1) / BITSET_WORDBITS)
46 #define BITSET_DECLARE(name, bits) BITSET_WORD name[BITSET_WORDS(bits)]
50 #define BITSET_COPY(x, y) memcpy( (x), (y), sizeof (x) )
51 #define BITSET_EQUAL(x, y) (memcmp( (x), (y), sizeof (x) ) == 0)
52 #define BITSET_ZERO(x) memset( (x), 0, sizeof (x) )
53 #define BITSET_ONES(x) memset( (x), 0xff, sizeof (x) )
55 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
56 #define BITSET_BIT(b) (1 << ((b) % BITSET_WORDBITS))
58 /* single bit operations
60 #define BITSET_TEST(x, b) ((x)[BITSET_BITWORD(b)] & BITSET_BIT(b))
61 #define BITSET_SET(x, b) ((x)[BITSET_BITWORD(b)] |= BITSET_BIT(b))
62 #define BITSET_CLEAR(x, b) ((x)[BITSET_BITWORD(b)] &= ~BITSET_BIT(b))
64 #define BITSET_MASK(b) ((b) == BITSET_WORDBITS ? ~0 : BITSET_BIT(b) - 1)
65 #define BITSET_RANGE(b, e) (BITSET_MASK((e) + 1) & ~BITSET_MASK(b))
67 /* bit range operations
69 #define BITSET_TEST_RANGE(x, b, e) \
70 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
71 ((x)[BITSET_BITWORD(b)] & BITSET_RANGE(b, e)) : \
72 (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
73 #define BITSET_SET_RANGE(x, b, e) \
74 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
75 ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
76 (assert (!"BITSET_SET_RANGE: bit range crosses word boundary"), 0))
77 #define BITSET_CLEAR_RANGE(x, b, e) \
78 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
79 ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
80 (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
82 /* Get first bit set in a bitset.
85 __bitset_ffs(const BITSET_WORD
*x
, int n
)
89 for (i
= 0; i
< n
; i
++) {
91 return ffs(x
[i
]) + BITSET_WORDBITS
* i
;
97 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
99 static inline unsigned
100 __bitset_next_set(unsigned i
, BITSET_WORD
*tmp
,
101 const BITSET_WORD
*set
, unsigned size
)
105 /* NOTE: The initial conditions for this function are very specific. At
106 * the start of the loop, the tmp variable must be set to *set and the
107 * initial i value set to 0. This way, if there is a bit set in the first
108 * word, we ignore the i-value and just grab that bit (so 0 is ok, even
109 * though 0 may be returned). If the first word is 0, then the value of
110 * `word` will be 0 and we will go on to look at the second word.
112 word
= BITSET_BITWORD(i
);
116 if (word
>= BITSET_WORDS(size
))
122 /* Find the next set bit in the non-zero word */
126 *tmp
&= ~(1ull << bit
);
128 return word
* BITSET_WORDBITS
+ bit
;
131 #define BITSET_FOREACH_SET(__i, __tmp, __set, __size) \
132 for (__tmp = *(__set), __i = 0; \
133 (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)