aboutsummaryrefslogtreecommitdiff
path: root/src/bitset.h
blob: 2b46090d7205813fe47cac83ab660af338ef0f38 (plain) (blame)
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
#ifndef ORYX_BITSET_H
#define ORYX_BITSET_H

#include <limits.h>
#include <stdbool.h>

#include "alloc.h"
#include "common.h"

typedef unsigned char bitset_t;

#define _BITSET_BITS (sizeof(bitset_t) * CHAR_BIT)
#define _BITSLOT(x)  ((x) / _BITSET_BITS)
#define _BITMASK(x)  (1 << ((x) % _BITSET_BITS))

/* Set- or test the bit as position X in BS */
#define SETBIT(bs, x)  ((bs)[_BITSLOT(x)] |= _BITMASK(x))
#define TESTBIT(bs, x) ((bool)((bs)[_BITSLOT(x)] & _BITMASK(x)))

/* Set the bit DST in BS if the bit SRC is set */
#define SET_DST_IF_SRC(bs, dst, src)                                           \
	do {                                                                       \
		if (TESTBIT(bs, src))                                                  \
			SETBIT(bs, dst);                                                   \
	} while (false)

/* Allocate a new bitset capable of holding N bits */
static inline bitset_t *mkbitset(arena_t *, size_t n)
	__attribute__((always_inline, returns_nonnull, nonnull, malloc));

bitset_t *
mkbitset(arena_t *a, size_t n)
{
	size_t bits = CHAR_BIT * sizeof(bitset_t);
	return arena_new(a, bitset_t, (n + bits - 1) / bits);
}

#endif /* !ORYX_BITSET_H */