summaryrefslogtreecommitdiff
path: root/ccan/bitmap/bitmap.h
blob: 9e6c2bbc51912d797eec56000963756598d48592 (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
/* Licensed under LGPLv2+ - see LICENSE file for details */
#ifndef CCAN_BITMAP_H_
#define CCAN_BITMAP_H_

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

#include <ccan/endian/endian.h>

typedef unsigned long bitmap_word;

#define BITMAP_WORD_BITS	(sizeof(bitmap_word) * CHAR_BIT)
#define BITMAP_NWORDS(_n)	\
	(((_n) + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)

/*
 * We wrap each word in a structure for type checking.
 */
typedef struct {
	bitmap_word w;
} bitmap;

#define BITMAP_DECLARE(_name, _nbits) \
	bitmap (_name)[BITMAP_NWORDS(_nbits)]

static inline size_t bitmap_sizeof(unsigned long nbits)
{
	return BITMAP_NWORDS(nbits) * sizeof(bitmap_word);
}

static inline bitmap_word bitmap_bswap(bitmap_word w)
{
	if (BITMAP_WORD_BITS == 32)
		return (ENDIAN_CAST bitmap_word)cpu_to_be32(w);
	else if (BITMAP_WORD_BITS == 64)
		return (ENDIAN_CAST bitmap_word)cpu_to_be64(w);
}

#define BITMAP_WORD(_bm, _n)	((_bm)[(_n) / BITMAP_WORD_BITS].w)
#define BITMAP_WORDBIT(_n) 	\
	(bitmap_bswap(1UL << (BITMAP_WORD_BITS - ((_n) % BITMAP_WORD_BITS) - 1)))

#define BITMAP_HEADWORDS(_nbits) \
	((_nbits) / BITMAP_WORD_BITS)
#define BITMAP_HEADBYTES(_nbits) \
	(BITMAP_HEADWORDS(_nbits) * sizeof(bitmap_word))

#define BITMAP_TAILWORD(_bm, _nbits) \
	((_bm)[BITMAP_HEADWORDS(_nbits)].w)
#define BITMAP_HASTAIL(_nbits)	(((_nbits) % BITMAP_WORD_BITS) != 0)
#define BITMAP_TAILBITS(_nbits)	\
	(bitmap_bswap(~(-1UL >> ((_nbits) % BITMAP_WORD_BITS))))
#define BITMAP_TAIL(_bm, _nbits) \
	(BITMAP_TAILWORD(_bm, _nbits) & BITMAP_TAILBITS(_nbits))

static inline void bitmap_set_bit(bitmap *bitmap, unsigned long n)
{
	BITMAP_WORD(bitmap, n) |= BITMAP_WORDBIT(n);
}

static inline void bitmap_clear_bit(bitmap *bitmap, unsigned long n)
{
	BITMAP_WORD(bitmap, n) &= ~BITMAP_WORDBIT(n);
}

static inline void bitmap_change_bit(bitmap *bitmap, unsigned long n)
{
	BITMAP_WORD(bitmap, n) ^= BITMAP_WORDBIT(n);
}

static inline bool bitmap_test_bit(const bitmap *bitmap, unsigned long n)
{
	return !!(BITMAP_WORD(bitmap, n) & BITMAP_WORDBIT(n));
}

void bitmap_zero_range(bitmap *bitmap, unsigned long n, unsigned long m);
void bitmap_fill_range(bitmap *bitmap, unsigned long n, unsigned long m);

static inline void bitmap_zero(bitmap *bitmap, unsigned long nbits)
{
	memset(bitmap, 0, bitmap_sizeof(nbits));
}

static inline void bitmap_fill(bitmap *bitmap, unsigned long nbits)
{
	memset(bitmap, 0xff, bitmap_sizeof(nbits));
}

static inline void bitmap_copy(bitmap *dst, const bitmap *src,
			       unsigned long nbits)
{
	memcpy(dst, src, bitmap_sizeof(nbits));
}

#define BITMAP_DEF_BINOP(_name, _op) \
	static inline void bitmap_##_name(bitmap *dst, bitmap *src1, bitmap *src2, \
					  unsigned long nbits)		\
	{ \
		unsigned long i = 0; \
		for (i = 0; i < BITMAP_NWORDS(nbits); i++) { \
			dst[i].w = src1[i].w _op src2[i].w; \
		} \
	}

BITMAP_DEF_BINOP(and, &)
BITMAP_DEF_BINOP(or, |)
BITMAP_DEF_BINOP(xor, ^)
BITMAP_DEF_BINOP(andnot, & ~)

#undef BITMAP_DEF_BINOP

static inline void bitmap_complement(bitmap *dst, const bitmap *src,
				     unsigned long nbits)
{
	unsigned long i;

	for (i = 0; i < BITMAP_NWORDS(nbits); i++)
		dst[i].w = ~src[i].w;
}

static inline bool bitmap_equal(const bitmap *src1, const bitmap *src2,
				unsigned long nbits)
{
	return (memcmp(src1, src2, BITMAP_HEADBYTES(nbits)) == 0)
		&& (!BITMAP_HASTAIL(nbits)
		    || (BITMAP_TAIL(src1, nbits) == BITMAP_TAIL(src2, nbits)));
}

static inline bool bitmap_intersects(const bitmap *src1, const bitmap *src2,
				     unsigned long nbits)
{
	unsigned long i;

	for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) {
		if (src1[i].w & src2[i].w)
			return true;
	}
	if (BITMAP_HASTAIL(nbits) &&
	    (BITMAP_TAIL(src1, nbits) & BITMAP_TAIL(src2, nbits)))
		return true;
	return false;
}

static inline bool bitmap_subset(const bitmap *src1, const bitmap *src2,
				 unsigned long nbits)
{
	unsigned long i;

	for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) {
		if (src1[i].w  & ~src2[i].w)
			return false;
	}
	if (BITMAP_HASTAIL(nbits) &&
	    (BITMAP_TAIL(src1, nbits) & ~BITMAP_TAIL(src2, nbits)))
		return false;
	return true;
}

static inline bool bitmap_full(const bitmap *bitmap, unsigned long nbits)
{
	unsigned long i;

	for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) {
		if (bitmap[i].w != -1UL)
			return false;
	}
	if (BITMAP_HASTAIL(nbits) &&
	    (BITMAP_TAIL(bitmap, nbits) != BITMAP_TAILBITS(nbits)))
		return false;

	return true;
}

static inline bool bitmap_empty(const bitmap *bitmap, unsigned long nbits)
{
	unsigned long i;

	for (i = 0; i < BITMAP_HEADWORDS(nbits); i++) {
		if (bitmap[i].w != 0)
			return false;
	}
	if (BITMAP_HASTAIL(nbits) && (BITMAP_TAIL(bitmap, nbits) != 0))
		return false;

	return true;
}

unsigned long bitmap_ffs(const bitmap *bitmap,
			 unsigned long n, unsigned long m);

/*
 * Allocation functions
 */
static inline bitmap *bitmap_alloc(unsigned long nbits)
{
	return malloc(bitmap_sizeof(nbits));
}

static inline bitmap *bitmap_alloc0(unsigned long nbits)
{
	bitmap *bitmap;

	bitmap = bitmap_alloc(nbits);
	if (bitmap)
		bitmap_zero(bitmap, nbits);
	return bitmap;
}

static inline bitmap *bitmap_alloc1(unsigned long nbits)
{
	bitmap *bitmap;

	bitmap = bitmap_alloc(nbits);
	if (bitmap)
		bitmap_fill(bitmap, nbits);
	return bitmap;
}

static inline bitmap *bitmap_realloc0(bitmap *bitmap,
				      unsigned long obits, unsigned long nbits)
{
	bitmap = realloc(bitmap, bitmap_sizeof(nbits));

	if ((nbits > obits) && bitmap)
		bitmap_zero_range(bitmap, obits, nbits);

	return bitmap;
}

static inline bitmap *bitmap_realloc1(bitmap *bitmap,
				      unsigned long obits, unsigned long nbits)
{
	bitmap = realloc(bitmap, bitmap_sizeof(nbits));

	if ((nbits > obits) && bitmap)
		bitmap_fill_range(bitmap, obits, nbits);

	return bitmap;
}

#endif /* CCAN_BITMAP_H_ */