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