mesa: remove GLvertexformat::EvalMesh1(), EvalMesh2()
[mesa.git] / src / mesa / main / bitset.h
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file bitset.h
28 * \brief Bitset of arbitrary size definitions.
29 * \author Michal Krol
30 */
31
32 #ifndef BITSET_H
33 #define BITSET_H
34
35 #include "imports.h"
36
37 /****************************************************************************
38 * generic bitset implementation
39 */
40
41 #define BITSET_WORD GLuint
42 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
43
44 /* bitset declarations
45 */
46 #define BITSET_WORDS(bits) (ALIGN(bits, BITSET_WORDBITS) / BITSET_WORDBITS)
47 #define BITSET_DECLARE(name, bits) BITSET_WORD name[BITSET_WORDS(bits)]
48
49 /* bitset operations
50 */
51 #define BITSET_COPY(x, y) memcpy( (x), (y), sizeof (x) )
52 #define BITSET_EQUAL(x, y) (memcmp( (x), (y), sizeof (x) ) == 0)
53 #define BITSET_ZERO(x) memset( (x), 0, sizeof (x) )
54 #define BITSET_ONES(x) memset( (x), 0xff, sizeof (x) )
55
56 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
57 #define BITSET_BIT(b) (1 << ((b) % BITSET_WORDBITS))
58
59 /* single bit operations
60 */
61 #define BITSET_TEST(x, b) ((x)[BITSET_BITWORD(b)] & BITSET_BIT(b))
62 #define BITSET_SET(x, b) ((x)[BITSET_BITWORD(b)] |= BITSET_BIT(b))
63 #define BITSET_CLEAR(x, b) ((x)[BITSET_BITWORD(b)] &= ~BITSET_BIT(b))
64
65 #define BITSET_MASK(b) ((b) == BITSET_WORDBITS ? ~0 : BITSET_BIT(b) - 1)
66 #define BITSET_RANGE(b, e) (BITSET_MASK((e) + 1) & ~BITSET_MASK(b))
67
68 /* bit range operations
69 */
70 #define BITSET_TEST_RANGE(x, b, e) \
71 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
72 ((x)[BITSET_BITWORD(b)] & BITSET_RANGE(b, e)) : \
73 (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
74 #define BITSET_SET_RANGE(x, b, e) \
75 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
76 ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
77 (assert (!"BITSET_SET_RANGE: bit range crosses word boundary"), 0))
78 #define BITSET_CLEAR_RANGE(x, b, e) \
79 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
80 ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
81 (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
82
83 /* Get first bit set in a bitset.
84 */
85 static inline int
86 __bitset_ffs(const BITSET_WORD *x, int n)
87 {
88 int i;
89
90 for (i = 0; i < n; i++) {
91 if (x[i])
92 return ffs(x[i]) + BITSET_WORDBITS * i;
93 }
94
95 return 0;
96 }
97
98 #define BITSET_FFS(x) __bitset_ffs(x, Elements(x))
99
100 /****************************************************************************
101 * 64-bit bitset implementation
102 */
103
104 #define BITSET64_WORD GLuint
105 #define BITSET64_WORDBITS (sizeof (BITSET64_WORD) * 8)
106
107 /* bitset declarations
108 */
109 #define BITSET64_DECLARE(name, size) \
110 GLuint name[2]
111
112 /* bitset operations
113 */
114 #define BITSET64_COPY(x, y) do { (x)[0] = (y)[0]; (x)[1] = (y)[1]; } while (0)
115 #define BITSET64_EQUAL(x, y) ( (x)[0] == (y)[0] && (x)[1] == (y)[1] )
116 #define BITSET64_ZERO(x) do { (x)[0] = 0; (x)[1] = 0; } while (0)
117 #define BITSET64_ONES(x) do { (x)[0] = 0xFF; (x)[1] = 0xFF; } while (0)
118
119 #define BITSET64_BITWORD(b) ((b) / BITSET64_WORDBITS)
120 #define BITSET64_BIT(b) (1 << ((b) % BITSET64_WORDBITS))
121
122 /* single bit operations
123 */
124 #define BITSET64_TEST(x, b) ((x)[BITSET64_BITWORD(b)] & BITSET64_BIT(b))
125 #define BITSET64_SET(x, b) ((x)[BITSET64_BITWORD(b)] |= BITSET64_BIT(b))
126 #define BITSET64_CLEAR(x, b) ((x)[BITSET64_BITWORD(b)] &= ~BITSET64_BIT(b))
127
128 #define BITSET64_MASK(b) ((b) == BITSET64_WORDBITS ? ~0 : BITSET64_BIT(b) - 1)
129 #define BITSET64_RANGE(b, e) (BITSET64_MASK((e) + 1) & ~BITSET64_MASK(b))
130
131 /* bit range operations
132 */
133 #define BITSET64_TEST_SUBRANGE(x, b, e) \
134 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
135 ((x)[BITSET64_BITWORD(b)] & BITSET64_RANGE(b, e)) : \
136 (assert (!"BITSET64_TEST_RANGE: bit range crosses word boundary"), 0))
137 #define BITSET64_TEST_RANGE(x, b, e) \
138 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
139 (BITSET64_TEST_SUBRANGE(x, b, e)) : \
140 (BITSET64_TEST_SUBRANGE(x, b, BITSET64_WORDBITS - 1) | \
141 BITSET64_TEST_SUBRANGE(x, BITSET64_WORDBITS, e)))
142 #define BITSET64_SET_SUBRANGE(x, b, e) \
143 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
144 ((x)[BITSET64_BITWORD(b)] |= BITSET64_RANGE(b, e)) : \
145 (assert (!"BITSET64_SET_RANGE: bit range crosses word boundary"), 0))
146 #define BITSET64_SET_RANGE(x, b, e) \
147 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
148 (BITSET64_SET_SUBRANGE(x, b, e)) : \
149 (BITSET64_SET_SUBRANGE(x, b, BITSET64_WORDBITS - 1) | \
150 BITSET64_SET_SUBRANGE(x, BITSET64_WORDBITS, e)))
151 #define BITSET64_CLEAR_SUBRANGE(x, b, e) \
152 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
153 ((x)[BITSET64_BITWORD(b)] &= ~BITSET64_RANGE(b, e)) : \
154 (assert (!"BITSET64_CLEAR_RANGE: bit range crosses word boundary"), 0))
155 #define BITSET64_CLEAR_RANGE(x, b, e) \
156 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
157 (BITSET64_CLEAR_SUBRANGE(x, b, e)) : \
158 (BITSET64_CLEAR_SUBRANGE(x, b, BITSET64_WORDBITS - 1) | \
159 BITSET64_CLEAR_SUBRANGE(x, BITSET64_WORDBITS, e)))
160
161 #endif