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.
36 /****************************************************************************
37 * generic bitset implementation
40 #define BITSET_WORD GLuint
41 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
43 /* bitset declarations
45 #define BITSET_WORDS(bits) (ALIGN(bits, BITSET_WORDBITS) / 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, Elements(x))