Merge remote-tracking branch 'origin/master' into vulkan
[mesa.git] / src / util / bitset.h
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 /**
26 * \file bitset.h
27 * \brief Bitset of arbitrary size definitions.
28 * \author Michal Krol
29 */
30
31 #ifndef BITSET_H
32 #define BITSET_H
33
34 #include "util/u_math.h"
35
36 /****************************************************************************
37 * generic bitset implementation
38 */
39
40 #define BITSET_WORD unsigned int
41 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
42
43 /* bitset declarations
44 */
45 #define BITSET_WORDS(bits) (((bits) + BITSET_WORDBITS - 1) / BITSET_WORDBITS)
46 #define BITSET_DECLARE(name, bits) BITSET_WORD name[BITSET_WORDS(bits)]
47
48 /* bitset operations
49 */
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) )
54
55 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
56 #define BITSET_BIT(b) (1 << ((b) % BITSET_WORDBITS))
57
58 /* single bit operations
59 */
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))
63
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))
66
67 /* bit range operations
68 */
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))
81
82 /* Get first bit set in a bitset.
83 */
84 static inline int
85 __bitset_ffs(const BITSET_WORD *x, int n)
86 {
87 int i;
88
89 for (i = 0; i < n; i++) {
90 if (x[i])
91 return ffs(x[i]) + BITSET_WORDBITS * i;
92 }
93
94 return 0;
95 }
96
97 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
98
99 static inline unsigned
100 __bitset_next_set(unsigned i, BITSET_WORD *tmp,
101 const BITSET_WORD *set, unsigned size)
102 {
103 unsigned bit, word;
104
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.
111 */
112 word = BITSET_BITWORD(i);
113 while (*tmp == 0) {
114 word++;
115
116 if (word >= BITSET_WORDS(size))
117 return size;
118
119 *tmp = set[word];
120 }
121
122 /* Find the next set bit in the non-zero word */
123 bit = ffs(*tmp) - 1;
124
125 /* Unset the bit */
126 *tmp &= ~(1ull << bit);
127
128 return word * BITSET_WORDBITS + bit;
129 }
130
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;)
134
135 #endif