summaryrefslogtreecommitdiff
path: root/bitmap.c
blob: 78034fdab965a4ef5583862b95bc213d326dd50b (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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef uint32_t unset_t;

#define BITS_PER_WORD	(sizeof(unset_t) * 8)
#define bits_unset(x)	(BITS_PER_WORD - __builtin_popcount(x))

#define TREE_ARY	16
#define BITS_PER_LEAF	(TREE_ARY * BITS_PER_WORD)

struct bitmap_tree {
	struct {
		uint64_t	size;
		uint64_t	nr_unset;
		size_t		first_leaf;
	} __attribute__((aligned(64)));

	struct bitmap_tree_node {
		unset_t nr_unset[TREE_ARY];
	} tree[];
};

static void bitmap_tree_init(struct bitmap_tree *tree, size_t i,
			     uint64_t nr_unset, uint64_t nr_this_level)
{
	struct bitmap_tree_node *n = tree->tree + i;
	unsigned j = 0;

	if (i < tree->first_leaf) {
		i = TREE_ARY * i + 1;

		nr_this_level /= TREE_ARY;

		while (nr_unset) {
			n->nr_unset[j] = nr_unset < nr_this_level
				? nr_unset : nr_this_level;

			bitmap_tree_init(tree, i + j, n->nr_unset[j],
					 nr_this_level);

			nr_unset -= n->nr_unset[j];
			j++;
		}
	} else {
		memset(n, 0, sizeof(*n));
	}
}

struct bitmap_tree *bitmap_tree_alloc(uint64_t size)
{
	struct bitmap_tree *tree;
	size_t nr_leaf_nodes = (size + BITS_PER_LEAF - 1) / BITS_PER_LEAF;
	size_t nr_nodes = 0;
	size_t nr_cur_level = 1;

	while (1) {
		if (nr_cur_level >= nr_leaf_nodes)
			break;

		nr_nodes += nr_cur_level;
		nr_cur_level *= TREE_ARY;
	}

	nr_nodes += nr_leaf_nodes;

	tree = malloc(sizeof(struct bitmap_tree) +
		      sizeof(struct bitmap_tree_node) * nr_nodes);

	tree->size = size;
	tree->nr_unset = size;
	tree->first_leaf = nr_nodes - nr_leaf_nodes;

	bitmap_tree_init(tree, 0, tree->nr_unset,
			 nr_cur_level * BITS_PER_LEAF);

	return tree;
}

static uint64_t set_nth_unset_bit_recurse(struct bitmap_tree *tree, size_t i,
					  uint64_t bit_to_set)
{
	struct bitmap_tree_node *n = tree->tree + i;
	unsigned j = 0, bit = 0;

	if (i < tree->first_leaf) {
		i = TREE_ARY * i + 1;
		__builtin_prefetch(tree->tree + i);

		while (bit_to_set > n->nr_unset[j]) {
			bit_to_set -= n->nr_unset[j];
			j++;
		}

		n->nr_unset[j]--;

		return set_nth_unset_bit_recurse(tree, i + j, bit_to_set);
	} else {
		while (bit_to_set > bits_unset(n->nr_unset[j])) {
			bit_to_set -= bits_unset(n->nr_unset[j]);
			j++;
		}

		do {
			while (n->nr_unset[j] & (1ULL << bit))
				bit++;
		} while (bit_to_set--);

		n->nr_unset[j] &= ~(1ULL << bit);

		return ((i - tree->first_leaf) * TREE_ARY + j) * BITS_PER_WORD + bit;
	}
}

uint64_t set_nth_unset_bit(struct bitmap_tree *tree, uint64_t bit)
{
	bit %= tree->nr_unset;
	tree->nr_unset--;

	return set_nth_unset_bit_recurse(tree, 0, bit);
}

int main()
{
	struct bitmap_tree *tree = bitmap_tree_alloc(123320987);

	while (tree->nr_unset)
		set_nth_unset_bit(tree, lrand48());

	return 0;
}