]>
Commit | Line | Data |
---|---|---|
ef017502 | 1 | /* Base bitset stuff. |
7d424de1 | 2 | |
575619af JD |
3 | Copyright (C) 2002-2004, 2006, 2009-2011 Free Software Foundation, |
4 | Inc. | |
7d424de1 | 5 | |
ef017502 AD |
6 | Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz). |
7 | ||
f16b0819 PE |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
11 | (at your option) any later version. | |
ef017502 | 12 | |
f16b0819 PE |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
ef017502 | 17 | |
f16b0819 PE |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
ef017502 AD |
20 | |
21 | #ifndef _BBITSET_H | |
22 | #define _BBITSET_H | |
23 | ||
613f5e1a | 24 | #include "libiberty.h" |
ef017502 | 25 | |
d0829076 | 26 | #include <stdbool.h> |
ef017502 | 27 | #include <limits.h> |
8a2757b9 | 28 | #include <stddef.h> |
ef017502 | 29 | |
25e845d1 | 30 | /* Currently we support five flavours of bitsets: |
6aa452a6 | 31 | BITSET_ARRAY: Array of bits (fixed size, fast for dense bitsets). |
25e845d1 PE |
32 | Memory for bit array and bitset structure allocated |
33 | contiguously. | |
34 | BITSET_LIST: Linked list of arrays of bits (variable size, least storage | |
59fc3dcd | 35 | for large very sparse sets). |
25e845d1 | 36 | BITSET_TABLE: Expandable table of pointers to arrays of bits |
59fc3dcd | 37 | (variable size, less storage for large sparse sets). |
25e845d1 | 38 | Faster than BITSET_LIST for random access. |
04098407 | 39 | BITSET_VARRAY: Variable array of bits (variable size, fast for |
25e845d1 PE |
40 | dense bitsets). |
41 | BITSET_STATS: Wrapper bitset for internal use only. Used for gathering | |
42 | statistics and/or better run-time checking. | |
ef017502 | 43 | */ |
25e845d1 PE |
44 | enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_VARRAY, |
45 | BITSET_TYPE_NUM, BITSET_STATS}; | |
46 | #define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset", "vbitset"} | |
ef017502 | 47 | |
6aa452a6 AD |
48 | extern const char * const bitset_type_names[]; |
49 | ||
613f5e1a | 50 | enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC}; |
ef017502 | 51 | |
ef017502 | 52 | /* Data type used to store a word of bits. */ |
779e7ceb PE |
53 | typedef unsigned long int bitset_word; |
54 | #define BITSET_WORD_BITS ((unsigned int) (CHAR_BIT * sizeof (bitset_word))) | |
ef017502 | 55 | |
50f095c9 PE |
56 | /* Bit index. In theory we might need a type wider than size_t, but |
57 | in practice we lose at most a factor of CHAR_BIT by going with | |
f6ebdb31 PE |
58 | size_t, and that is good enough. If this type is changed to be |
59 | wider than size_t, the code needs to be modified to check for | |
60 | overflow when converting bit counts to byte or word counts. | |
61 | The bit and word index types must be unsigned. */ | |
50f095c9 | 62 | typedef size_t bitset_bindex; |
ef017502 AD |
63 | |
64 | /* Word index. */ | |
50f095c9 | 65 | typedef size_t bitset_windex; |
ef017502 | 66 | |
f6ebdb31 PE |
67 | /* Maximum values for commonly-used unsigned types. BITSET_SIZE_MAX |
68 | always equals SIZE_MAX, but some older systems lack SIZE_MAX. */ | |
69 | #define BITSET_BINDEX_MAX ((bitset_bindex) -1) | |
5beedd9b PE |
70 | |
71 | /* Limit max word index to the maximum value of a signed integer | |
72 | to simplify cache disabling. */ | |
73 | #define BITSET_WINDEX_MAX (((bitset_windex) -1) >> 1) | |
f6ebdb31 | 74 | #define BITSET_SIZE_MAX ((size_t) -1) |
ef017502 | 75 | |
50f095c9 | 76 | #define BITSET_MSB ((bitset_word) 1 << (BITSET_WORD_BITS - 1)) |
ef017502 AD |
77 | |
78 | #define BITSET_LIST_SIZE 1024 | |
79 | ||
59fc3dcd PE |
80 | enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES, |
81 | BITSET_OP_COPY, BITSET_OP_NOT, | |
ef017502 AD |
82 | BITSET_OP_EMPTY_P, BITSET_OP_EQUAL_P, |
83 | BITSET_OP_SUBSET_P, BITSET_OP_DISJOINT_P, | |
59fc3dcd | 84 | BITSET_OP_AND, BITSET_OP_OR, BITSET_OP_XOR, BITSET_OP_ANDN, |
ef017502 AD |
85 | BITSET_OP_OR_AND, BITSET_OP_AND_OR, BITSET_OP_ANDN_OR}; |
86 | ||
6aa452a6 AD |
87 | struct bbitset_struct |
88 | { | |
25e845d1 | 89 | const struct bitset_vtable *vtable; |
6aa452a6 AD |
90 | bitset_windex cindex; /* Cache word index. */ |
91 | bitset_windex csize; /* Cache size in words. */ | |
92 | bitset_word *cdata; /* Cache data pointer. */ | |
25e845d1 | 93 | bitset_bindex n_bits; /* Number of bits. */ |
6aa452a6 AD |
94 | /* Perhaps we could sacrifice another word to indicate |
95 | that the bitset is known to be zero, that a bit has been set | |
96 | in the cache, and that a bit has been cleared in the cache. | |
97 | This would speed up some of the searches but slightly slow down | |
98 | bit set/reset operations of cached bits. */ | |
99 | }; | |
100 | ||
101 | ||
d9d83ef2 | 102 | typedef union bitset_union *bitset; |
6aa452a6 AD |
103 | |
104 | ||
25e845d1 PE |
105 | /* Private accessor macros to bitset structure. */ |
106 | #define BITSET_VTABLE_(SRC) (SRC)->b.vtable | |
107 | #define BITSET_CINDEX_(SRC) (SRC)->b.cindex | |
108 | #define BITSET_CDATA_(SRC) (SRC)->b.cdata | |
109 | #define BITSET_CSIZE_(SRC) (SRC)->b.csize | |
110 | #define BITSET_NBITS_(SRC) (SRC)->b.n_bits | |
111 | ||
112 | ||
6aa452a6 AD |
113 | /* The contents of this structure should be considered private. */ |
114 | struct bitset_vtable | |
115 | { | |
7d7d6663 PE |
116 | void (*set) (bitset, bitset_bindex); |
117 | void (*reset) (bitset, bitset_bindex); | |
118 | bool (*toggle) (bitset, bitset_bindex); | |
119 | bool (*test) (bitset, bitset_bindex); | |
120 | bitset_bindex (*resize) (bitset, bitset_bindex); | |
121 | bitset_bindex (*size) (bitset); | |
122 | bitset_bindex (*count) (bitset); | |
123 | ||
124 | bool (*empty_p) (bitset); | |
125 | void (*ones) (bitset); | |
126 | void (*zero) (bitset); | |
127 | ||
128 | void (*copy) (bitset, bitset); | |
129 | bool (*disjoint_p) (bitset, bitset); | |
130 | bool (*equal_p) (bitset, bitset); | |
67a0dc4f | 131 | void (*not_) (bitset, bitset); |
7d7d6663 PE |
132 | bool (*subset_p) (bitset, bitset); |
133 | ||
67a0dc4f | 134 | void (*and_) (bitset, bitset, bitset); |
7d7d6663 PE |
135 | bool (*and_cmp) (bitset, bitset, bitset); |
136 | void (*andn) (bitset, bitset, bitset); | |
137 | bool (*andn_cmp) (bitset, bitset, bitset); | |
67a0dc4f | 138 | void (*or_) (bitset, bitset, bitset); |
7d7d6663 | 139 | bool (*or_cmp) (bitset, bitset, bitset); |
67a0dc4f | 140 | void (*xor_) (bitset, bitset, bitset); |
7d7d6663 PE |
141 | bool (*xor_cmp) (bitset, bitset, bitset); |
142 | ||
143 | void (*and_or) (bitset, bitset, bitset, bitset); | |
144 | bool (*and_or_cmp) (bitset, bitset, bitset, bitset); | |
145 | void (*andn_or) (bitset, bitset, bitset, bitset); | |
146 | bool (*andn_or_cmp) (bitset, bitset, bitset, bitset); | |
147 | void (*or_and) (bitset, bitset, bitset, bitset); | |
148 | bool (*or_and_cmp) (bitset, bitset, bitset, bitset); | |
149 | ||
150 | bitset_bindex (*list) (bitset, bitset_bindex *, bitset_bindex, | |
151 | bitset_bindex *); | |
152 | bitset_bindex (*list_reverse) (bitset, bitset_bindex *, bitset_bindex, | |
153 | bitset_bindex *); | |
154 | void (*free) (bitset); | |
6aa452a6 AD |
155 | enum bitset_type type; |
156 | }; | |
157 | ||
25e845d1 PE |
158 | #define BITSET_COMPATIBLE_(BSET1, BSET2) \ |
159 | ((BSET1)->b.vtable == (BSET2)->b.vtable) | |
6aa452a6 AD |
160 | |
161 | #define BITSET_CHECK2_(DST, SRC) \ | |
162 | if (!BITSET_COMPATIBLE_ (DST, SRC)) abort (); | |
163 | ||
164 | #define BITSET_CHECK3_(DST, SRC1, SRC2) \ | |
165 | if (!BITSET_COMPATIBLE_ (DST, SRC1) \ | |
166 | || !BITSET_COMPATIBLE_ (DST, SRC2)) abort (); | |
167 | ||
168 | #define BITSET_CHECK4_(DST, SRC1, SRC2, SRC3) \ | |
169 | if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \ | |
170 | || !BITSET_COMPATIBLE_ (DST, SRC3)) abort (); | |
171 | ||
172 | ||
25e845d1 PE |
173 | /* Redefine number of bits in bitset DST. */ |
174 | #define BITSET_RESIZE_(DST, SIZE) (DST)->b.vtable->resize (DST, SIZE) | |
175 | ||
ef017502 | 176 | /* Return size in bits of bitset SRC. */ |
59fc3dcd | 177 | #define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC) |
6aa452a6 AD |
178 | |
179 | /* Return number of bits set in bitset SRC. */ | |
59fc3dcd | 180 | #define BITSET_COUNT_(SRC) (SRC)->b.vtable->count (SRC) |
ef017502 AD |
181 | |
182 | /* Return type of bitset SRC. */ | |
59fc3dcd | 183 | #define BITSET_TYPE_(DST) (DST)->b.vtable->type |
ef017502 AD |
184 | |
185 | /* Set bit BITNO in bitset DST. */ | |
59fc3dcd | 186 | #define BITSET_SET_(DST, BITNO) (DST)->b.vtable->set (DST, BITNO) |
ef017502 AD |
187 | |
188 | /* Reset bit BITNO in bitset DST. */ | |
59fc3dcd | 189 | #define BITSET_RESET_(DST, BITNO) (DST)->b.vtable->reset (DST, BITNO) |
6aa452a6 AD |
190 | |
191 | /* Toggle bit BITNO in bitset DST. */ | |
59fc3dcd | 192 | #define BITSET_TOGGLE_(DST, BITNO) (DST)->b.vtable->toggle (DST, BITNO) |
ef017502 AD |
193 | |
194 | /* Return non-zero if bit BITNO in bitset SRC is set. */ | |
59fc3dcd | 195 | #define BITSET_TEST_(SRC, BITNO) (SRC)->b.vtable->test (SRC, BITNO) |
ef017502 AD |
196 | |
197 | /* Free bitset SRC. */ | |
198 | #define BITSET_FREE_(SRC)\ | |
6aa452a6 | 199 | ((SRC)->b.vtable->free ? (SRC)->b.vtable->free (SRC) :(void)0) |
ef017502 | 200 | |
ef017502 | 201 | |
6aa452a6 AD |
202 | /* Return SRC == 0. */ |
203 | #define BITSET_EMPTY_P_(SRC) (SRC)->b.vtable->empty_p (SRC) | |
ef017502 | 204 | |
6aa452a6 AD |
205 | /* DST = ~0. */ |
206 | #define BITSET_ONES_(DST) (DST)->b.vtable->ones (DST) | |
ef017502 AD |
207 | |
208 | /* DST = 0. */ | |
6aa452a6 | 209 | #define BITSET_ZERO_(DST) (DST)->b.vtable->zero (DST) |
ef017502 | 210 | |
ef017502 | 211 | |
6aa452a6 AD |
212 | |
213 | /* DST = SRC. */ | |
214 | #define BITSET_COPY_(DST, SRC) (SRC)->b.vtable->copy (DST, SRC) | |
215 | ||
216 | /* Return DST & SRC == 0. */ | |
217 | #define BITSET_DISJOINT_P_(DST, SRC) (SRC)->b.vtable->disjoint_p (DST, SRC) | |
ef017502 AD |
218 | |
219 | /* Return DST == SRC. */ | |
6aa452a6 AD |
220 | #define BITSET_EQUAL_P_(DST, SRC) (SRC)->b.vtable->equal_p (DST, SRC) |
221 | ||
222 | /* DST = ~SRC. */ | |
67a0dc4f | 223 | #define BITSET_NOT_(DST, SRC) (SRC)->b.vtable->not_ (DST, SRC) |
ef017502 AD |
224 | |
225 | /* Return DST == DST | SRC. */ | |
6aa452a6 | 226 | #define BITSET_SUBSET_P_(DST, SRC) (SRC)->b.vtable->subset_p (DST, SRC) |
ef017502 | 227 | |
ef017502 | 228 | |
6aa452a6 | 229 | /* DST = SRC1 & SRC2. */ |
67a0dc4f | 230 | #define BITSET_AND_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_ (DST, SRC1, SRC2) |
6aa452a6 | 231 | #define BITSET_AND_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->and_cmp (DST, SRC1, SRC2) |
ef017502 | 232 | |
6aa452a6 AD |
233 | /* DST = SRC1 & ~SRC2. */ |
234 | #define BITSET_ANDN_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn (DST, SRC1, SRC2) | |
235 | #define BITSET_ANDN_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->andn_cmp (DST, SRC1, SRC2) | |
ef017502 AD |
236 | |
237 | /* DST = SRC1 | SRC2. */ | |
67a0dc4f | 238 | #define BITSET_OR_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_ (DST, SRC1, SRC2) |
6aa452a6 | 239 | #define BITSET_OR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->or_cmp (DST, SRC1, SRC2) |
ef017502 AD |
240 | |
241 | /* DST = SRC1 ^ SRC2. */ | |
67a0dc4f | 242 | #define BITSET_XOR_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_ (DST, SRC1, SRC2) |
6aa452a6 | 243 | #define BITSET_XOR_CMP_(DST, SRC1, SRC2) (SRC1)->b.vtable->xor_cmp (DST, SRC1, SRC2) |
ef017502 | 244 | |
ef017502 | 245 | |
ef017502 | 246 | |
ef017502 AD |
247 | /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if |
248 | DST != (SRC1 & SRC2) | SRC3. */ | |
6aa452a6 AD |
249 | #define BITSET_AND_OR_(DST, SRC1, SRC2, SRC3) \ |
250 | (SRC1)->b.vtable->and_or (DST, SRC1, SRC2, SRC3) | |
251 | #define BITSET_AND_OR_CMP_(DST, SRC1, SRC2, SRC3) \ | |
252 | (SRC1)->b.vtable->and_or_cmp (DST, SRC1, SRC2, SRC3) | |
253 | ||
254 | /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if | |
255 | DST != (SRC1 & ~SRC2) | SRC3. */ | |
256 | #define BITSET_ANDN_OR_(DST, SRC1, SRC2, SRC3) \ | |
257 | (SRC1)->b.vtable->andn_or (DST, SRC1, SRC2, SRC3) | |
258 | #define BITSET_ANDN_OR_CMP_(DST, SRC1, SRC2, SRC3) \ | |
259 | (SRC1)->b.vtable->andn_or_cmp (DST, SRC1, SRC2, SRC3) | |
ef017502 AD |
260 | |
261 | /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if | |
262 | DST != (SRC1 | SRC2) & SRC3. */ | |
6aa452a6 AD |
263 | #define BITSET_OR_AND_(DST, SRC1, SRC2, SRC3) \ |
264 | (SRC1)->b.vtable->or_and (DST, SRC1, SRC2, SRC3) | |
265 | #define BITSET_OR_AND_CMP_(DST, SRC1, SRC2, SRC3) \ | |
266 | (SRC1)->b.vtable->or_and_cmp (DST, SRC1, SRC2, SRC3) | |
ef017502 | 267 | |
ef017502 | 268 | |
59fc3dcd | 269 | /* Find list of up to NUM bits set in BSET starting from and including |
ef017502 AD |
270 | *NEXT. Return with actual number of bits found and with *NEXT |
271 | indicating where search stopped. */ | |
272 | #define BITSET_LIST_(BSET, LIST, NUM, NEXT) \ | |
59fc3dcd | 273 | (BSET)->b.vtable->list (BSET, LIST, NUM, NEXT) |
ef017502 AD |
274 | |
275 | /* Find reverse list of up to NUM bits set in BSET starting from and | |
276 | including NEXT. Return with actual number of bits found and with | |
277 | *NEXT indicating where search stopped. */ | |
6aa452a6 | 278 | #define BITSET_LIST_REVERSE_(BSET, LIST, NUM, NEXT) \ |
59fc3dcd | 279 | (BSET)->b.vtable->list_reverse (BSET, LIST, NUM, NEXT) |
ef017502 AD |
280 | |
281 | ||
6aa452a6 | 282 | /* Private functions for bitset implementations. */ |
59fc3dcd | 283 | |
7d7d6663 | 284 | extern bool bitset_toggle_ (bitset, bitset_bindex); |
ef017502 | 285 | |
7d7d6663 | 286 | extern bitset_bindex bitset_count_ (bitset); |
ef017502 | 287 | |
7d7d6663 | 288 | extern bitset_bindex bitset_size_ (bitset); |
25e845d1 | 289 | |
7d7d6663 | 290 | extern bool bitset_copy_ (bitset, bitset); |
ef017502 | 291 | |
7d7d6663 | 292 | extern void bitset_and_or_ (bitset, bitset, bitset, bitset); |
d9d83ef2 | 293 | |
7d7d6663 | 294 | extern bool bitset_and_or_cmp_ (bitset, bitset, bitset, bitset); |
ef017502 | 295 | |
7d7d6663 | 296 | extern void bitset_andn_or_ (bitset, bitset, bitset, bitset); |
d9d83ef2 | 297 | |
7d7d6663 | 298 | extern bool bitset_andn_or_cmp_ (bitset, bitset, bitset, bitset); |
ef017502 | 299 | |
7d7d6663 | 300 | extern void bitset_or_and_ (bitset, bitset, bitset, bitset); |
d9d83ef2 | 301 | |
7d7d6663 | 302 | extern bool bitset_or_and_cmp_ (bitset, bitset, bitset, bitset); |
ef017502 AD |
303 | |
304 | #endif /* _BBITSET_H */ |