Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[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 * 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 /****************************************************************************
32 * generic bitset implementation
33 */
34
35 #define BITSET_WORD GLuint
36 #define BITSET_WORDBITS (sizeof (BITSET_WORD) * 8)
37
38 /* bitset declarations
39 */
40 #define BITSET_DECLARE(name, size) \
41 BITSET_WORD name[((size) + BITSET_WORDBITS - 1) / BITSET_WORDBITS]
42
43 /* bitset operations
44 */
45 #define BITSET_COPY(x, y) _mesa_memcpy( (x), (y), sizeof (x) )
46 #define BITSET_EQUAL(x, y) (_mesa_memcmp( (x), (y), sizeof (x) ) == 0)
47 #define BITSET_ZERO(x) _mesa_memset( (x), 0, sizeof (x) )
48 #define BITSET_ONES(x) _mesa_memset( (x), 0xff, sizeof (x) )
49
50 #define BITSET_BITWORD(b) ((b) / BITSET_WORDBITS)
51 #define BITSET_BIT(b) (1 << ((b) % BITSET_WORDBITS))
52
53 /* single bit operations
54 */
55 #define BITSET_TEST(x, b) ((x)[BITSET_BITWORD(b)] & BITSET_BIT(b))
56 #define BITSET_SET(x, b) ((x)[BITSET_BITWORD(b)] |= BITSET_BIT(b))
57 #define BITSET_CLEAR(x, b) ((x)[BITSET_BITWORD(b)] &= ~BITSET_BIT(b))
58
59 #define BITSET_MASK(b) ((b) == BITSET_WORDBITS ? ~0 : BITSET_BIT(b) - 1)
60 #define BITSET_RANGE(b, e) (BITSET_MASK((e) + 1) & ~BITSET_MASK(b))
61
62 /* bit range operations
63 */
64 #define BITSET_TEST_RANGE(x, b, e) \
65 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
66 ((x)[BITSET_BITWORD(b)] & BITSET_RANGE(b, e)) : \
67 (assert (!"BITSET_TEST_RANGE: bit range crosses word boundary"), 0))
68 #define BITSET_SET_RANGE(x, b, e) \
69 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
70 ((x)[BITSET_BITWORD(b)] |= BITSET_RANGE(b, e)) : \
71 (assert (!"BITSET_SET_RANGE: bit range crosses word boundary"), 0))
72 #define BITSET_CLEAR_RANGE(x, b, e) \
73 (BITSET_BITWORD(b) == BITSET_BITWORD(e) ? \
74 ((x)[BITSET_BITWORD(b)] &= ~BITSET_RANGE(b, e)) : \
75 (assert (!"BITSET_CLEAR_RANGE: bit range crosses word boundary"), 0))
76
77 /****************************************************************************
78 * 64-bit bitset implementation
79 */
80
81 #define BITSET64_WORD GLuint
82 #define BITSET64_WORDBITS (sizeof (BITSET64_WORD) * 8)
83
84 /* bitset declarations
85 */
86 #define BITSET64_DECLARE(name, size) \
87 GLuint name[2]
88
89 /* bitset operations
90 */
91 #define BITSET64_COPY(x, y) do { (x)[0] = (y)[0]; (x)[1] = (y)[1]; } while (0)
92 #define BITSET64_EQUAL(x, y) ( (x)[0] == (y)[0] && (x)[1] == (y)[1] )
93 #define BITSET64_ZERO(x) do { (x)[0] = 0; (x)[1] = 0; } while (0)
94 #define BITSET64_ONES(x) do { (x)[0] = 0xFF; (x)[1] = 0xFF; } while (0)
95
96 #define BITSET64_BITWORD(b) ((b) / BITSET64_WORDBITS)
97 #define BITSET64_BIT(b) (1 << ((b) % BITSET64_WORDBITS))
98
99 /* single bit operations
100 */
101 #define BITSET64_TEST(x, b) ((x)[BITSET64_BITWORD(b)] & BITSET64_BIT(b))
102 #define BITSET64_SET(x, b) ((x)[BITSET64_BITWORD(b)] |= BITSET64_BIT(b))
103 #define BITSET64_CLEAR(x, b) ((x)[BITSET64_BITWORD(b)] &= ~BITSET64_BIT(b))
104
105 #define BITSET64_MASK(b) ((b) == BITSET64_WORDBITS ? ~0 : BITSET64_BIT(b) - 1)
106 #define BITSET64_RANGE(b, e) (BITSET64_MASK((e) + 1) & ~BITSET64_MASK(b))
107
108 /* bit range operations
109 */
110 #define BITSET64_TEST_RANGE(x, b, e) \
111 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
112 ((x)[BITSET64_BITWORD(b)] & BITSET64_RANGE(b, e)) : \
113 (assert (!"BITSET64_TEST_RANGE: bit range crosses word boundary"), 0))
114 #define BITSET64_SET_RANGE(x, b, e) \
115 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
116 ((x)[BITSET64_BITWORD(b)] |= BITSET64_RANGE(b, e)) : \
117 (assert (!"BITSET64_SET_RANGE: bit range crosses word boundary"), 0))
118 #define BITSET64_CLEAR_RANGE(x, b, e) \
119 (BITSET64_BITWORD(b) == BITSET64_BITWORD(e) ? \
120 ((x)[BITSET64_BITWORD(b)] &= ~BITSET64_RANGE(b, e)) : \
121 (assert (!"BITSET64_CLEAR_RANGE: bit range crosses word boundary"), 0))
122