• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mesa 3-D graphics library
3  * Version:  6.5
4  *
5  * Copyright (C) 2006  Brian Paul   All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 "imports.h"
35 
36 /****************************************************************************
37  * generic bitset implementation
38  */
39 
40 #define BITSET_WORD GLuint
41 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
42 
43 /* bitset declarations
44  */
45 #define BITSET_DECLARE(name, size) \
46    BITSET_WORD name[((size) + BITSET_WORDBITS - 1) / BITSET_WORDBITS]
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
__bitset_ffs(const BITSET_WORD * x,int n)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, Elements(x))
98 
99 /****************************************************************************
100  * 64-bit bitset implementation
101  */
102 
103 #define BITSET64_WORD GLuint
104 #define BITSET64_WORDBITS (sizeof (BITSET64_WORD) * 8)
105 
106 /* bitset declarations
107  */
108 #define BITSET64_DECLARE(name, size) \
109    GLuint name[2]
110 
111 /* bitset operations
112  */
113 #define BITSET64_COPY(x, y) do { (x)[0] = (y)[0]; (x)[1] = (y)[1]; } while (0)
114 #define BITSET64_EQUAL(x, y) ( (x)[0] == (y)[0] && (x)[1] == (y)[1] )
115 #define BITSET64_ZERO(x) do { (x)[0] = 0; (x)[1] = 0; } while (0)
116 #define BITSET64_ONES(x) do { (x)[0] = 0xFF; (x)[1] = 0xFF; } while (0)
117 
118 #define BITSET64_BITWORD(b) ((b) / BITSET64_WORDBITS)
119 #define BITSET64_BIT(b) (1 << ((b) % BITSET64_WORDBITS))
120 
121 /* single bit operations
122  */
123 #define BITSET64_TEST(x, b) ((x)[BITSET64_BITWORD(b)] & BITSET64_BIT(b))
124 #define BITSET64_SET(x, b) ((x)[BITSET64_BITWORD(b)] |= BITSET64_BIT(b))
125 #define BITSET64_CLEAR(x, b) ((x)[BITSET64_BITWORD(b)] &= ~BITSET64_BIT(b))
126 
127 #define BITSET64_MASK(b) ((b) == BITSET64_WORDBITS ? ~0 : BITSET64_BIT(b) - 1)
128 #define BITSET64_RANGE(b, e) (BITSET64_MASK((e) + 1) & ~BITSET64_MASK(b))
129 
130 /* bit range operations
131  */
132 #define BITSET64_TEST_SUBRANGE(x, b, e) \
133    (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
134    ((x)[BITSET64_BITWORD(b)] & BITSET64_RANGE(b, e)) : \
135    (assert (!"BITSET64_TEST_RANGE: bit range crosses word boundary"), 0))
136 #define BITSET64_TEST_RANGE(x, b, e) \
137    (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
138    (BITSET64_TEST_SUBRANGE(x, b, e)) : \
139    (BITSET64_TEST_SUBRANGE(x, b, BITSET64_WORDBITS - 1) | \
140     BITSET64_TEST_SUBRANGE(x, BITSET64_WORDBITS, e)))
141 #define BITSET64_SET_SUBRANGE(x, b, e) \
142    (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
143    ((x)[BITSET64_BITWORD(b)] |= BITSET64_RANGE(b, e)) : \
144    (assert (!"BITSET64_SET_RANGE: bit range crosses word boundary"), 0))
145 #define BITSET64_SET_RANGE(x, b, e) \
146    (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
147    (BITSET64_SET_SUBRANGE(x, b, e)) : \
148    (BITSET64_SET_SUBRANGE(x, b, BITSET64_WORDBITS - 1) | \
149     BITSET64_SET_SUBRANGE(x, BITSET64_WORDBITS, e)))
150 #define BITSET64_CLEAR_SUBRANGE(x, b, e) \
151    (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
152    ((x)[BITSET64_BITWORD(b)] &= ~BITSET64_RANGE(b, e)) : \
153    (assert (!"BITSET64_CLEAR_RANGE: bit range crosses word boundary"), 0))
154 #define BITSET64_CLEAR_RANGE(x, b, e) \
155    (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
156    (BITSET64_CLEAR_SUBRANGE(x, b, e)) : \
157    (BITSET64_CLEAR_SUBRANGE(x, b, BITSET64_WORDBITS - 1) | \
158     BITSET64_CLEAR_SUBRANGE(x, BITSET64_WORDBITS, e)))
159 
160 #endif
161