summaryrefslogtreecommitdiffhomepage
path: root/ir/adt/bitset.h
blob: a6801e2a52b0acc61d8abe9c1e624df945e01413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief   convenience layer over raw_bitsets (stores number of bits
 *          with the bitfield)
 * @author  Matthias Braun
 */
#ifndef FIRM_ADT_BITSET_H
#define FIRM_ADT_BITSET_H

#include <stdio.h>
#include <assert.h>

#include "xmalloc.h"
#include "bitfiddle.h"
#include "raw_bitset.h"

typedef struct {
	size_t   size;    /**< size of the bitset in bits */
	unsigned data[];
} bitset_t;

/**
 * Return the number of bytes a bitset would need
 */
static inline size_t bitset_total_size(size_t const n_bits)
{
	return sizeof(bitset_t) + BITSET_SIZE_BYTES(n_bits);
}

/**
 * Initialize a bitset for bitsize size (bitset should point to memory
 * with a size calculated by bitset_total_size)
 */
static inline bitset_t *bitset_init(void *memory, size_t size)
{
	bitset_t *result = (bitset_t*)memory;
	result->size = size;
	rbitset_clear_all(result->data, size);
	return result;
}

/**
 * Allocate a bitset on an obstack.
 * @param obst The obstack.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.
 */
static inline bitset_t *bitset_obstack_alloc(struct obstack *obst,
                                             size_t n_bits)
{
	size_t size   = bitset_total_size(n_bits);
	void  *memory = obstack_alloc(obst, size);
	return bitset_init(memory, n_bits);
}

/**
 * Allocate a bitset via malloc.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.
 */
static inline bitset_t *bitset_malloc(size_t n_bits)
{
	size_t  size   = bitset_total_size(n_bits);
	void   *memory = xmalloc(size);
	return bitset_init(memory, n_bits);
}

/**
 * Allocate a bitset on the stack via alloca.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.
 */
#define bitset_alloca(size) \
	bitset_init(alloca(bitset_total_size(size)), (size))

/**
 * Get the size of the bitset in bits.
 * @note Note the difference between capacity and size.
 * @param bs The bitset.
 * @return The highest bit which can be set or cleared plus 1.
 */
static inline size_t bitset_size(bitset_t const *bitset)
{
	return bitset->size;
}

/**
 * Set a bit in the bitset.
 * @param bs The bitset.
 * @param bit The bit to set.
 */
static inline void bitset_set(bitset_t *bs, size_t bit)
{
	assert(bit < bs->size);
	rbitset_set(bs->data, bit);
}

/**
 * Clear a bit in the bitset.
 * @param bs The bitset.
 * @param bit The bit to clear.
 */
static inline void bitset_clear(bitset_t *bs, size_t bit)
{
	assert(bit < bs->size);
	rbitset_clear(bs->data, bit);
}

/**
 * Check, if a bit is set.
 * @param bs The bitset.
 * @param bit The bit to check for.
 * @return 1, if the bit was set, 0 if not.
 */
static inline bool bitset_is_set(bitset_t const *bs, size_t bit)
{
	assert(bit < bs->size);
	return rbitset_is_set(bs->data, bit);
}

/**
 * Flip a bit in a bitset.
 * @param bs The bitset.
 * @param bit The bit to flip.
 */
static inline void bitset_flip(bitset_t *bs, size_t bit)
{
	assert(bit < bs->size);
	rbitset_flip(bs->data, bit);
}

/**
 * Flip the whole bitset.
 * @param bs The bitset.
 */
static inline void bitset_flip_all(bitset_t *bs)
{
	rbitset_flip_all(bs->data, bs->size);
}

/**
 * Copy a bitset to another. Both bitset must be initialized and have the same
 * number of bits.
 * @param tgt The target bitset.
 * @param src The source bitset.
 * @return The target bitset.
 */
static inline void bitset_copy(bitset_t *tgt, bitset_t const *src)
{
	assert(tgt->size == src->size);
	rbitset_copy(tgt->data, src->data, src->size);
}

/**
 * Find the next unset bit from a given bit.
 * @note Note that if pos is unset, pos is returned.
 * @param bs The bitset.
 * @param pos The bit from which to search for the next set bit.
 * @return The next set bit from pos on, or (size_t)-1, if no unset bit was
 * found after pos.
 */
static inline size_t bitset_next_clear(bitset_t const *bs, size_t pos)
{
	return rbitset_next_max(bs->data, pos, bs->size, false);
}

/**
 * Find the next set bit from a given bit.
 * @note Note that if pos is set, pos is returned.
 * @param bs The bitset.
 * @param pos The bit from which to search for the next set bit.
 * @return The next set bit from pos on, or (size_t)-1, if no set bit was
 * found after pos.
 */
static inline size_t bitset_next_set(bitset_t const *bs, size_t pos)
{
	return rbitset_next_max(bs->data, pos, bs->size, true);
}

/**
 * Convenience macro for bitset iteration.
 * @param bitset The bitset.
 * @param elm A size_t variable.
 */
#define bitset_foreach(bitset, elm) \
	for (size_t elm = 0; (elm = bitset_next_set((bitset), elm)) != (size_t)-1; ++elm)


#define bitset_foreach_clear(bitset, elm) \
	for (size_t elm = 0; (elm = bitset_next_clear((bitset), elm)) != (size_t)-1; ++elm)

/**
 * Find the previous unset bit from a given bit.
 * @param bs The bitset.
 * @param pos The bit from which to search for the previous unset bit.
 * @return The previous unset bit from pos on, or (size_t)-1, if no unset bit was
 * found before pos.
 */
static inline size_t bitset_prev_clear(bitset_t const *bs, size_t pos)
{
	return rbitset_prev(bs->data, pos, false);
}

/**
 * Find the previous set bit from a given bit.
 * @param bs The bitset.
 * @param pos The bit from which to search for the next set bit.
 * @return The previous set bit from pos on, or (size_t)-1, if no set bit was
 * found before pos.
 */
static inline size_t bitset_prev_set(bitset_t const *bs, size_t pos)
{
	return rbitset_prev(bs->data, pos, true);
}


#define bitset_foreach_rev(bitset, elm) \
  for (size_t elm = bitset->size; (elm = rbitset_prev(bitset->data, elm, true)) != (size_t)-1;)


#define bitset_foreach_clear_rev(bitset, elm) \
  for (size_t elm = bitset->size; (elm = rbitset_prev(bitset->data, elm, false)) != (size_t)-1;)

/**
 * Count the bits set.
 * This can also be seen as the cardinality of the set.
 * @param bs The bitset.
 * @return The number of bits set in the bitset.
 */
static inline size_t bitset_popcount(bitset_t const *bs)
{
	return rbitset_popcount(bs->data, bs->size);
}

/**
 * Clear the bitset.
 * This sets all bits to zero.
 * @param bs The bitset.
 */
static inline void bitset_clear_all(bitset_t *bs)
{
	rbitset_clear_all(bs->data, bs->size);
}

/**
 * Set the bitset.
 * This sets all bits to one.
 * @param bs The bitset.
 */
static inline void bitset_set_all(bitset_t *bs)
{
	rbitset_set_all(bs->data, bs->size);
}

/**
 * Check, if one bitset is contained by another.
 * That is, each bit set in lhs is also set in rhs.
 * @param lhs A bitset.
 * @param rhs Another bitset.
 * @return 1, if all bits in lhs are also set in rhs, 0 otherwise.
 */
static inline bool bitset_contains(bitset_t const *lhs, bitset_t const *rhs)
{
	assert(lhs->size == rhs->size);
	return rbitset_contains(lhs->data, rhs->data, lhs->size);
}

/**
 * Treat the bitset as a number and subtract 1.
 * @param bs The bitset.
 * @return The same bitset.
 */
static inline void bitset_minus1(bitset_t *bs)
{
	rbitset_minus1(bs->data, bs->size);
}

/**
 * Check if two bitsets intersect.
 * @param a The first bitset.
 * @param b The second bitset.
 * @return 1 if they have a bit in common, 0 if not.
 */
static inline bool bitset_intersect(bitset_t const *a, bitset_t const *b)
{
	assert(a->size == b->size);
	return rbitsets_have_common(a->data, b->data, a->size);
}

/**
 * set or clear all bits in the range [from;to[.
 * @param a      The bitset.
 * @param from   The first index to set to one.
 * @param to     The last index plus one to set to one.
 * @param do_set If 1 the bits are set, if 0, they are cleared.
 */
static inline void bitset_mod_range(bitset_t *a, size_t from, size_t to,
                                    bool do_set)
{
	if (from == to)
	    return;

	if (to < from) {
		size_t tmp = from;
		from = to;
		to = tmp;
	}

	if (to > a->size)
		to = a->size;

	rbitset_set_range(a->data, from, to, do_set);
}

#define bitset_set_range(bs, from, to)   bitset_mod_range((bs), (from), (to), 1)
#define bitset_clear_range(bs, from, to) bitset_mod_range((bs), (from), (to), 0)

/**
 * Check, if a bitset is empty.
 * @param a The bitset.
 * @return 1, if the bitset is empty, 0 if not.
 */
static inline bool bitset_is_empty(bitset_t const *bs)
{
	return rbitset_is_empty(bs->data, bs->size);
}

/**
 * Print a bitset to a stream.
 * The bitset is printed as a comma separated list of bits set.
 * @param file The stream.
 * @param bs The bitset.
 */
void bitset_fprint(FILE *file, bitset_t const *bs);

/**
 * Perform tgt = tgt & src operation.
 * @param tgt  The target bitset.
 * @param src  The source bitset.
 * @return the tgt set.
 */
static inline void bitset_and(bitset_t *tgt, bitset_t const *src)
{
	assert(tgt->size == src->size);
	rbitset_and(tgt->data, src->data, src->size);
}

/**
 * Perform tgt = tgt & ~src operation.
 * @param tgt  The target bitset.
 * @param src  The source bitset.
 * @return the tgt set.
 */
static inline void bitset_andnot(bitset_t *tgt, bitset_t const *src)
{
	assert(tgt->size == src->size);
	rbitset_andnot(tgt->data, src->data, src->size);
}

/**
 * Perform Union, tgt = tgt u src operation.
 * @param tgt  The target bitset.
 * @param src  The source bitset.
 * @return the tgt set.
 */
static inline void bitset_or(bitset_t *tgt, bitset_t const *src)
{
	assert(tgt->size == src->size);
	rbitset_or(tgt->data, src->data, src->size);
}

/**
 * Perform tgt = tgt ^ src operation.
 * @param tgt  The target bitset.
 * @param src  The source bitset.
 * @return the tgt set.
 */
static inline void bitset_xor(bitset_t *tgt, bitset_t const *src)
{
	assert(tgt->size == src->size);
	rbitset_xor(tgt->data, src->data, src->size);
}

/**
 * Copy a raw bitset into an bitset.
 */
static inline void rbitset_copy_to_bitset(const unsigned *rbitset,
                                          bitset_t *bitset)
{
	rbitset_copy(bitset->data, rbitset, bitset->size);
}

#endif