-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitmap.h
468 lines (395 loc) · 11.6 KB
/
bitmap.h
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2014 Intel Corporation
* Copyright(C) 2023 Marvell.
*/
#ifndef __OCT_BITMAP_H__
#define __OCT_BITMAP_H__
#include <stdint.h>
#include <string.h>
#include <util.h>
/* Slab */
#define PLT_BITMAP_SLAB_BIT_SIZE 64
#define PLT_BITMAP_SLAB_BIT_SIZE_LOG2 6
#define PLT_BITMAP_SLAB_BIT_MASK (PLT_BITMAP_SLAB_BIT_SIZE - 1)
/* Cache line (CL) */
#define PLT_BITMAP_CL_BIT_SIZE (PLT_CACHE_LINE_SIZE * 8)
#define PLT_BITMAP_CL_BIT_SIZE_LOG2 (PLT_CACHE_LINE_SIZE_LOG2 + 3)
#define PLT_BITMAP_CL_BIT_MASK (PLT_BITMAP_CL_BIT_SIZE - 1)
#define PLT_BITMAP_CL_SLAB_SIZE (PLT_BITMAP_CL_BIT_SIZE / PLT_BITMAP_SLAB_BIT_SIZE)
#define PLT_BITMAP_CL_SLAB_SIZE_LOG2 (PLT_BITMAP_CL_BIT_SIZE_LOG2 - PLT_BITMAP_SLAB_BIT_SIZE_LOG2)
#define PLT_BITMAP_CL_SLAB_MASK (PLT_BITMAP_CL_SLAB_SIZE - 1)
#define plt_bitmap_scan_init(bmp) __plt_bitmap_scan_init(bmp)
/** Bitmap data structure */
struct plt_bitmap {
/* Context for array1 and array2 */
uint64_t *array1; /**< Bitmap array1 */
uint64_t *array2; /**< Bitmap array2 */
uint32_t array1_size; /**< Number of 64-bit slabs in array1 that are
actually used */
uint32_t array2_size; /**< Number of 64-bit slabs in array2 */
/* Context for the "scan next" operation */
uint32_t index1; /**< Bitmap scan: Index of current array1 slab */
uint32_t offset1; /**< Bitmap scan: Offset of current bit within current
array1 slab */
uint32_t index2; /**< Bitmap scan: Index of current array2 slab */
uint32_t go2; /**< Bitmap scan: Go/stop condition for current array2
cache line */
/* Storage space for array1 and array2 */
uint8_t memory[];
};
static inline void
__plt_bitmap_index1_inc(struct plt_bitmap *bmp)
{
bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
}
static inline uint64_t
__plt_bitmap_mask1_get(struct plt_bitmap *bmp)
{
return (~1llu) << bmp->offset1;
}
static inline void
__plt_bitmap_index2_set(struct plt_bitmap *bmp)
{
bmp->index2 = (((bmp->index1 << PLT_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1)
<< PLT_BITMAP_CL_SLAB_SIZE_LOG2);
}
static inline uint32_t
__plt_bitmap_get_memory_footprint(uint32_t n_bits, uint32_t *array1_byte_offset,
uint32_t *array1_slabs, uint32_t *array2_byte_offset,
uint32_t *array2_slabs)
{
uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
uint32_t n_cache_lines_array2;
uint32_t n_bytes_total;
n_cache_lines_array2 = (n_bits + PLT_BITMAP_CL_BIT_SIZE - 1) / PLT_BITMAP_CL_BIT_SIZE;
n_slabs_array1 =
(n_cache_lines_array2 + PLT_BITMAP_SLAB_BIT_SIZE - 1) / PLT_BITMAP_SLAB_BIT_SIZE;
n_slabs_array1 = plt_align32pow2(n_slabs_array1);
n_slabs_context = (sizeof(struct plt_bitmap) + (PLT_BITMAP_SLAB_BIT_SIZE / 8) - 1) /
(PLT_BITMAP_SLAB_BIT_SIZE / 8);
n_cache_lines_context_and_array1 =
(n_slabs_context + n_slabs_array1 + PLT_BITMAP_CL_SLAB_SIZE - 1) /
PLT_BITMAP_CL_SLAB_SIZE;
n_bytes_total =
(n_cache_lines_context_and_array1 + n_cache_lines_array2) * PLT_CACHE_LINE_SIZE;
if (array1_byte_offset)
*array1_byte_offset = n_slabs_context * (PLT_BITMAP_SLAB_BIT_SIZE / 8);
if (array1_slabs)
*array1_slabs = n_slabs_array1;
if (array2_byte_offset)
*array2_byte_offset = n_cache_lines_context_and_array1 * PLT_CACHE_LINE_SIZE;
if (array2_slabs)
*array2_slabs = n_cache_lines_array2 * PLT_BITMAP_CL_SLAB_SIZE;
return n_bytes_total;
}
static inline void
__plt_bitmap_scan_init(struct plt_bitmap *bmp)
{
bmp->index1 = bmp->array1_size - 1;
bmp->offset1 = PLT_BITMAP_SLAB_BIT_SIZE - 1;
__plt_bitmap_index2_set(bmp);
bmp->index2 += PLT_BITMAP_CL_SLAB_SIZE;
bmp->go2 = 0;
}
/**
* Bitmap memory footprint calculation
*
* @param n_bits
* Number of bits in the bitmap
* @return
* Bitmap memory footprint measured in bytes on success, 0 on error
*/
static inline uint32_t
plt_bitmap_get_memory_footprint(uint32_t n_bits)
{
/* Check input arguments */
if (n_bits == 0)
return 0;
return __plt_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
}
/**
* Bitmap initialization
*
* @param n_bits
* Number of pre-allocated bits in array2.
* @param mem
* Base address of array1 and array2.
* @param mem_size
* Minimum expected size of bitmap.
* @return
* Handle to bitmap instance.
*/
static inline struct plt_bitmap *
plt_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
{
struct plt_bitmap *bmp;
uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
uint32_t size;
/* Check input arguments */
if (n_bits == 0)
return NULL;
if ((mem == NULL) || (((uintptr_t)mem) & PLT_CACHE_LINE_MASK))
return NULL;
size = __plt_bitmap_get_memory_footprint(n_bits, &array1_byte_offset, &array1_slabs,
&array2_byte_offset, &array2_slabs);
if (size < mem_size)
return NULL;
/* Setup bitmap */
memset(mem, 0, size);
bmp = (struct plt_bitmap *)mem;
bmp->array1 = (uint64_t *)&mem[array1_byte_offset];
bmp->array1_size = array1_slabs;
bmp->array2 = (uint64_t *)&mem[array2_byte_offset];
bmp->array2_size = array2_slabs;
__plt_bitmap_scan_init(bmp);
return bmp;
}
/**
* Bitmap free
*
* @param bmp
* Handle to bitmap instance
* @return
* 0 upon success, error code otherwise
*/
static inline int
plt_bitmap_free(struct plt_bitmap *bmp)
{
/* Check input arguments */
if (bmp == NULL)
return -1;
return 0;
}
/**
* Bitmap reset
*
* @param bmp
* Handle to bitmap instance
*/
static inline void
plt_bitmap_reset(struct plt_bitmap *bmp)
{
memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
__plt_bitmap_scan_init(bmp);
}
/**
* Bitmap location prefetch into CPU L1 cache
*
* @param bmp
* Handle to bitmap instance
* @param pos
* Bit position
* @return
* 0 upon success, error code otherwise
*/
static inline void
plt_bitmap_prefetch0(struct plt_bitmap *bmp, uint32_t pos)
{
uint64_t *slab2;
uint32_t index2;
index2 = pos >> PLT_BITMAP_SLAB_BIT_SIZE_LOG2;
slab2 = bmp->array2 + index2;
plt_prefetch0(slab2);
}
/**
* Bitmap bit get
*
* @param bmp
* Handle to bitmap instance
* @param pos
* Bit position
* @return
* 0 when bit is cleared, non-zero when bit is set
*/
static inline uint64_t
plt_bitmap_get(struct plt_bitmap *bmp, uint32_t pos)
{
uint64_t *slab2;
uint32_t index2, offset2;
index2 = pos >> PLT_BITMAP_SLAB_BIT_SIZE_LOG2;
offset2 = pos & PLT_BITMAP_SLAB_BIT_MASK;
slab2 = bmp->array2 + index2;
return ((*slab2) >> offset2) & 0x1;
}
/**
* Bitmap bit set
*
* @param bmp
* Handle to bitmap instance
* @param pos
* Bit position
*/
static inline void
plt_bitmap_set(struct plt_bitmap *bmp, uint32_t pos)
{
uint64_t *slab1, *slab2;
uint32_t index1, index2, offset1, offset2;
/* Set bit in array2 slab and set bit in array1 slab */
index2 = pos >> PLT_BITMAP_SLAB_BIT_SIZE_LOG2;
offset2 = pos & PLT_BITMAP_SLAB_BIT_MASK;
index1 = pos >> (PLT_BITMAP_SLAB_BIT_SIZE_LOG2 + PLT_BITMAP_CL_BIT_SIZE_LOG2);
offset1 = (pos >> PLT_BITMAP_CL_BIT_SIZE_LOG2) & PLT_BITMAP_SLAB_BIT_MASK;
slab2 = bmp->array2 + index2;
slab1 = bmp->array1 + index1;
*slab2 |= 1llu << offset2;
*slab1 |= 1llu << offset1;
}
/**
* Bitmap slab set
*
* @param bmp
* Handle to bitmap instance
* @param pos
* Bit position identifying the array2 slab
* @param slab
* Value to be assigned to the 64-bit slab in array2
*/
static inline void
plt_bitmap_set_slab(struct plt_bitmap *bmp, uint32_t pos, uint64_t slab)
{
uint64_t *slab1, *slab2;
uint32_t index1, index2, offset1;
/* Set bits in array2 slab and set bit in array1 slab */
index2 = pos >> PLT_BITMAP_SLAB_BIT_SIZE_LOG2;
index1 = pos >> (PLT_BITMAP_SLAB_BIT_SIZE_LOG2 + PLT_BITMAP_CL_BIT_SIZE_LOG2);
offset1 = (pos >> PLT_BITMAP_CL_BIT_SIZE_LOG2) & PLT_BITMAP_SLAB_BIT_MASK;
slab2 = bmp->array2 + index2;
slab1 = bmp->array1 + index1;
*slab2 |= slab;
*slab1 |= 1llu << offset1;
}
static inline uint64_t
__plt_bitmap_line_not_empty(uint64_t *slab2)
{
uint64_t v1, v2, v3, v4, v5, v6, v7, v8;
v1 = slab2[0] | slab2[1];
v2 = slab2[2] | slab2[3];
v3 = slab2[4] | slab2[5];
v4 = slab2[6] | slab2[7];
v1 |= v2;
v3 |= v4;
if (PLT_BITMAP_CL_SLAB_SIZE == 16)
{
v5 = slab2[8] | slab2[9];
v6 = slab2[10] | slab2[11];
v7 = slab2[12] | slab2[13];
v8 = slab2[14] | slab2[15];
v5 |= v6;
v7 |= v8;
return v1 | v3 | v5 | v7;
}
return v1 | v3;
}
/**
* Bitmap bit clear
*
* @param bmp
* Handle to bitmap instance
* @param pos
* Bit position
*/
static inline void
plt_bitmap_clear(struct plt_bitmap *bmp, uint32_t pos)
{
uint64_t *slab1, *slab2;
uint32_t index1, index2, offset1, offset2;
/* Clear bit in array2 slab */
index2 = pos >> PLT_BITMAP_SLAB_BIT_SIZE_LOG2;
offset2 = pos & PLT_BITMAP_SLAB_BIT_MASK;
slab2 = bmp->array2 + index2;
/* Return if array2 slab is not all-zeros */
*slab2 &= ~(1llu << offset2);
if (*slab2)
return;
/* Check the entire cache line of array2 for all-zeros */
index2 &= ~PLT_BITMAP_CL_SLAB_MASK;
slab2 = bmp->array2 + index2;
if (__plt_bitmap_line_not_empty(slab2))
return;
/* The array2 cache line is all-zeros, so clear bit in array1 slab */
index1 = pos >> (PLT_BITMAP_SLAB_BIT_SIZE_LOG2 + PLT_BITMAP_CL_BIT_SIZE_LOG2);
offset1 = (pos >> PLT_BITMAP_CL_BIT_SIZE_LOG2) & PLT_BITMAP_SLAB_BIT_MASK;
slab1 = bmp->array1 + index1;
*slab1 &= ~(1llu << offset1);
}
static inline int
__plt_bitmap_scan_search(struct plt_bitmap *bmp)
{
uint64_t value1;
uint32_t i;
/* Check current array1 slab */
value1 = bmp->array1[bmp->index1];
value1 &= __plt_bitmap_mask1_get(bmp);
if (plt_bsf64_safe(value1, &bmp->offset1))
return 1;
__plt_bitmap_index1_inc(bmp);
bmp->offset1 = 0;
/* Look for another array1 slab */
for (i = 0; i < bmp->array1_size; i++, __plt_bitmap_index1_inc(bmp)) {
value1 = bmp->array1[bmp->index1];
if (plt_bsf64_safe(value1, &bmp->offset1))
return 1;
}
return 0;
}
static inline void
__plt_bitmap_scan_read_init(struct plt_bitmap *bmp)
{
__plt_bitmap_index2_set(bmp);
bmp->go2 = 1;
plt_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
}
static inline int
__plt_bitmap_scan_read(struct plt_bitmap *bmp, uint32_t *pos, uint64_t *slab)
{
uint64_t *slab2;
slab2 = bmp->array2 + bmp->index2;
for (; bmp->go2; bmp->index2++, slab2++, bmp->go2 = bmp->index2 & PLT_BITMAP_CL_SLAB_MASK) {
if (*slab2) {
*pos = bmp->index2 << PLT_BITMAP_SLAB_BIT_SIZE_LOG2;
*slab = *slab2;
bmp->index2++;
slab2++;
bmp->go2 = bmp->index2 & PLT_BITMAP_CL_SLAB_MASK;
return 1;
}
}
return 0;
}
/**
* Bitmap scan (with automatic wrap-around)
*
* @param bmp
* Handle to bitmap instance
* @param pos
* When function call returns 1, pos contains the position of the next set
* bit, otherwise not modified
* @param slab
* When function call returns 1, slab contains the value of the entire 64-bit
* slab where the bit indicated by pos is located. Slabs are always 64-bit
* aligned, so the position of the first bit of the slab (this bit is not
* necessarily set) is pos / 64. Once a slab has been returned by the bitmap
* scan operation, the internal pointers of the bitmap are updated to point
* after this slab, so the same slab will not be returned again if it
* contains more than one bit which is set. When function call returns 0,
* slab is not modified.
* @return
* 0 if there is no bit set in the bitmap, 1 otherwise
*/
static inline int
plt_bitmap_scan(struct plt_bitmap *bmp, uint32_t *pos, uint64_t *slab)
{
/* Return data from current array2 line if available */
if (__plt_bitmap_scan_read(bmp, pos, slab))
return 1;
/* Look for non-empty array2 line */
if (__plt_bitmap_scan_search(bmp)) {
__plt_bitmap_scan_read_init(bmp);
__plt_bitmap_scan_read(bmp, pos, slab);
return 1;
}
/* Empty bitmap */
return 0;
}
#endif /* __OCT_BITMAP_H__ */