aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/include/odp_bitmap_internal.h
blob: 1be4d02873aced11cf7b86b0f328fa07e2ccc338 (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
/* Copyright (c) 2016, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/**
 * @file
 *
 * ODP generic bitmap types and operations.
 */

#ifndef ODP_BITMAP_INTERNAL_H_
#define ODP_BITMAP_INTERNAL_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <odp/api/hints.h>

/* Generate unique identifier for instantiated class */
#define TOKENIZE(template, line) \
	template ## _ ## line ## _ ## __COUNTER__

/* Array size in general */
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))

#define BITS_PER_BYTE	(8)
#define BITS_PER_LONG	__WORDSIZE
#define BYTES_PER_LONG	(BITS_PER_LONG / BITS_PER_BYTE)

#define BIT_WORD(nr)	((nr) / BITS_PER_LONG)
#define BITS_TO_LONGS(nr) BIT_WORD(nr + BITS_PER_LONG - 1)

#define BITMAP_FIRST_WORD_MASK(start) \
	(~0UL << ((start) & (BITS_PER_LONG - 1)))
#define BITMAP_LAST_WORD_MASK(nbits)  \
	(~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))

/* WAPL bitmap base class */
typedef struct {
	unsigned int  nwords;
	unsigned int  *pl;
	unsigned long *ul;
} wapl_bitmap_t;

/*
 * Word-Aligned Position List (WAPL) bitmap, which actually
 * is not a compression, but with an extra list of non-empty
 * word positions.
 *
 * WAPL accelerates bitwise operations and iterations by
 * applying only to non-empty positions instead of walking
 * through the whole bitmap.
 *
 * WAPL uses [1 ~ N] instead of [0 ~ N - 1] as position
 * values and an extra 0 as end indicator for position list.
 * This is the reason to allocate one extra room below.
 */
#define instantiate_wapl_bitmap(line, nbits)			\
	struct TOKENIZE(wapl_bitmap, line) {			\
		unsigned int pl[BITS_TO_LONGS(nbits) + 1];	\
		unsigned long ul[BITS_TO_LONGS(nbits) + 1];	\
	}

#define WAPL_BITMAP(nbits) instantiate_wapl_bitmap(__LINE__, nbits)

/*
 * Upcast any derived WAPL bitmap class to its base class
 */
#define __wapl_upcast(base, derived)				\
	do {							\
		__typeof__(derived) p = derived;		\
		base.pl = p->pl;				\
		base.ul = p->ul;				\
		base.nwords = ARRAY_SIZE(p->ul) - 1;		\
	} while (0)

/*
 * WAPL base class bitmap operations
 */
void __wapl_bitmap_and(wapl_bitmap_t *dst,
		       wapl_bitmap_t *src, wapl_bitmap_t *and);

void __wapl_bitmap_or(wapl_bitmap_t *dst, wapl_bitmap_t *or);

void __wapl_bitmap_set(wapl_bitmap_t *map, unsigned int bit);

void __wapl_bitmap_clear(wapl_bitmap_t *map, unsigned int bit);

/*
 * Generic WAPL bitmap operations
 */
#define wapl_bitmap_zero(map)					\
	({							\
		__typeof__(map) p = map;			\
		memset((void *)p, 0, sizeof(__typeof__(*p)));	\
	})

#define wapl_bitmap_copy(dst, src)				\
	({							\
		__typeof__(dst) d = dst;			\
		__typeof__(src) s = src;			\
		if (d != s)					\
			memcpy((void *)d, (void *)s,		\
				sizeof(__typeof__(*d)));	\
	})

#define wapl_bitmap_and(dst, src, and)				\
	({							\
		wapl_bitmap_t d, s, a;				\
		__wapl_upcast(d, dst);				\
		__wapl_upcast(s, src);				\
		__wapl_upcast(a, and);				\
		__wapl_bitmap_and(&d, &s, &a);			\
	})

#define wapl_bitmap_or(dst, src, or)				\
	({							\
		wapl_bitmap_t d, o;				\
		wapl_bitmap_copy(dst, src);			\
		__wapl_upcast(d, dst);				\
		__wapl_upcast(o, or);				\
		__wapl_bitmap_or(&d, &o);			\
	})

#define wapl_bitmap_set(map, bit)				\
	({							\
		wapl_bitmap_t b;				\
		__wapl_upcast(b, map);				\
		__wapl_bitmap_set(&b, bit);			\
	})

#define wapl_bitmap_clear(map, bit)				\
	({							\
		wapl_bitmap_t b;				\
		__wapl_upcast(b, map);				\
		__wapl_bitmap_clear(&b, bit);			\
	})

/*
 * Round robin iterator runs upon a WAPL bitmap:
 *
 * wapl_bitmap_iterator(iterator, WAPL bitmap);
 * for (iterator->start(); iterator->has_next(); ) {
 *	unsigned int bit_index = iterator->next();
 *	...operations on this bit index...
 * }
 */
typedef struct wapl_bitmap_iterator {
	int _start, _next, _nbits;
	wapl_bitmap_t _base;

	void (*start)(struct wapl_bitmap_iterator *this);
	bool (*has_next)(struct wapl_bitmap_iterator *this);
	unsigned int (*next)(struct wapl_bitmap_iterator *this);
} wapl_bitmap_iterator_t;

/*
 * WAPL bitmap iterator constructor
 */
void __wapl_bitmap_iterator(wapl_bitmap_iterator_t *this);

/*
 * Generic constructor accepts any derived WAPL bitmap class
 */
#define wapl_bitmap_iterator(iterator, map)			\
	({							\
		__typeof__(iterator) __it = iterator;		\
		__wapl_upcast(__it->_base, map);		\
		__wapl_bitmap_iterator(__it);			\
	})

/* Sparse bitmap base class */
typedef struct {
	unsigned int nbits;
	unsigned int *last, *pl, *il;
} sparse_bitmap_t;

/*
 * Sparse bitmap, lists all bit indexes directly as an array.
 * Expected to be significantly straightforward iteration.
 */
#define instantiate_sparse_bitmap(line, nbits)			\
	struct TOKENIZE(sparse_bitmap, line) {			\
		unsigned int last;				\
		unsigned int pl[nbits];				\
		unsigned int il[nbits];				\
	}

#define SPARSE_BITMAP(nbits) instantiate_sparse_bitmap(__LINE__, nbits)

/*
 * Upcast any derived sparse bitmap class to its base class
 */
#define __sparse_upcast(base, derived)				\
	do {							\
		__typeof__(derived) p = derived;		\
		base.pl = p->pl;				\
		base.il = p->il;				\
		base.last = &p->last;				\
		base.nbits = ARRAY_SIZE(p->il);			\
	} while (0)

/*
 * Sparse base class bitmap operations
 */
void __sparse_bitmap_set(sparse_bitmap_t *map, unsigned int bit);

void __sparse_bitmap_clear(sparse_bitmap_t *map, unsigned int bit);

/*
 * Generic sparse bitmap operations
 */
#define sparse_bitmap_zero(map)					\
	({							\
		__typeof__(map) p = map;			\
		memset((void *)p, 0, sizeof(__typeof__(*p)));	\
	})

#define sparse_bitmap_set(map, bit)				\
	({							\
		sparse_bitmap_t b;				\
		__sparse_upcast(b, map);			\
		__sparse_bitmap_set(&b, bit);			\
	})

#define sparse_bitmap_clear(map, bit)				\
	({							\
		sparse_bitmap_t b;				\
		__sparse_upcast(b, map);			\
		__sparse_bitmap_clear(&b, bit);			\
	})

/*
 * Round robin iterator runs upon a sparse bitmap:
 *
 * sparse_bitmap_iterator(iterator, SPARSE bitmap);
 * for (iterator->start(); iterator->has_next(); ) {
 *	unsigned int bit_index = iterator->next();
 *	...operations on this bit index...
 * }
 */
typedef struct sparse_bitmap_iterator {
	int _start, _next, _nbits;
	sparse_bitmap_t _base;

	void (*start)(struct sparse_bitmap_iterator *this);
	bool (*has_next)(struct sparse_bitmap_iterator *this);
	unsigned int (*next)(struct sparse_bitmap_iterator *this);
} sparse_bitmap_iterator_t;

/*
 * Sparse bitmap iterator constructor
 */
void __sparse_bitmap_iterator(sparse_bitmap_iterator_t *this);

/*
 * Generic constructor accepts any derived sparse bitmap class.
 */
#define sparse_bitmap_iterator(iterator, map)			\
	({							\
		__typeof__(iterator) __it = iterator;		\
		__sparse_upcast(__it->_base, map);		\
		__sparse_bitmap_iterator(__it);			\
	})

/*
 * Raw bitmap atomic set and clear.
 */
void raw_bitmap_set(unsigned long *map, unsigned int bit);

void raw_bitmap_clear(unsigned long *map, unsigned int bit);

/*
 * It will enter infinite loop incase that all bits are zero,
 * so please make sure the bitmap at least has one set.
 */
static inline int __bitmap_wraparound_next(
	unsigned long *addr, unsigned int nbits, int start)
{
	unsigned long tmp;

	if (start >= (int)nbits)
		start = 0;

	tmp = addr[BIT_WORD(start)];

	/* Handle 1st word. */
	tmp &= BITMAP_FIRST_WORD_MASK(start);
	start = start & ~(BITS_PER_LONG - 1);

	while (!tmp) {
		start += BITS_PER_LONG;
		if (start >= (int)nbits)
			start = 0;

		tmp = addr[BIT_WORD(start)];
	}

	start += __builtin_ffsl(tmp) - 1;
	return start;
}

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif