1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Sparse bit array
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2018, Google LLC.
6*4882a593Smuzhiyun * Copyright (C) 2018, Red Hat, Inc. (code style cleanup and fuzzing driver)
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * This library provides functions to support a memory efficient bit array,
9*4882a593Smuzhiyun * with an index size of 2^64. A sparsebit array is allocated through
10*4882a593Smuzhiyun * the use sparsebit_alloc() and free'd via sparsebit_free(),
11*4882a593Smuzhiyun * such as in the following:
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * struct sparsebit *s;
14*4882a593Smuzhiyun * s = sparsebit_alloc();
15*4882a593Smuzhiyun * sparsebit_free(&s);
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * The struct sparsebit type resolves down to a struct sparsebit.
18*4882a593Smuzhiyun * Note that, sparsebit_free() takes a pointer to the sparsebit
19*4882a593Smuzhiyun * structure. This is so that sparsebit_free() is able to poison
20*4882a593Smuzhiyun * the pointer (e.g. set it to NULL) to the struct sparsebit before
21*4882a593Smuzhiyun * returning to the caller.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Between the return of sparsebit_alloc() and the call of
24*4882a593Smuzhiyun * sparsebit_free(), there are multiple query and modifying operations
25*4882a593Smuzhiyun * that can be performed on the allocated sparsebit array. All of
26*4882a593Smuzhiyun * these operations take as a parameter the value returned from
27*4882a593Smuzhiyun * sparsebit_alloc() and most also take a bit index. Frequently
28*4882a593Smuzhiyun * used routines include:
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * ---- Query Operations
31*4882a593Smuzhiyun * sparsebit_is_set(s, idx)
32*4882a593Smuzhiyun * sparsebit_is_clear(s, idx)
33*4882a593Smuzhiyun * sparsebit_any_set(s)
34*4882a593Smuzhiyun * sparsebit_first_set(s)
35*4882a593Smuzhiyun * sparsebit_next_set(s, prev_idx)
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * ---- Modifying Operations
38*4882a593Smuzhiyun * sparsebit_set(s, idx)
39*4882a593Smuzhiyun * sparsebit_clear(s, idx)
40*4882a593Smuzhiyun * sparsebit_set_num(s, idx, num);
41*4882a593Smuzhiyun * sparsebit_clear_num(s, idx, num);
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * A common operation, is to itterate over all the bits set in a test
44*4882a593Smuzhiyun * sparsebit array. This can be done via code with the following structure:
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * sparsebit_idx_t idx;
47*4882a593Smuzhiyun * if (sparsebit_any_set(s)) {
48*4882a593Smuzhiyun * idx = sparsebit_first_set(s);
49*4882a593Smuzhiyun * do {
50*4882a593Smuzhiyun * ...
51*4882a593Smuzhiyun * idx = sparsebit_next_set(s, idx);
52*4882a593Smuzhiyun * } while (idx != 0);
53*4882a593Smuzhiyun * }
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * The index of the first bit set needs to be obtained via
56*4882a593Smuzhiyun * sparsebit_first_set(), because sparsebit_next_set(), needs
57*4882a593Smuzhiyun * the index of the previously set. The sparsebit_idx_t type is
58*4882a593Smuzhiyun * unsigned, so there is no previous index before 0 that is available.
59*4882a593Smuzhiyun * Also, the call to sparsebit_first_set() is not made unless there
60*4882a593Smuzhiyun * is at least 1 bit in the array set. This is because sparsebit_first_set()
61*4882a593Smuzhiyun * aborts if sparsebit_first_set() is called with no bits set.
62*4882a593Smuzhiyun * It is the callers responsibility to assure that the
63*4882a593Smuzhiyun * sparsebit array has at least a single bit set before calling
64*4882a593Smuzhiyun * sparsebit_first_set().
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * ==== Implementation Overview ====
67*4882a593Smuzhiyun * For the most part the internal implementation of sparsebit is
68*4882a593Smuzhiyun * opaque to the caller. One important implementation detail that the
69*4882a593Smuzhiyun * caller may need to be aware of is the spatial complexity of the
70*4882a593Smuzhiyun * implementation. This implementation of a sparsebit array is not
71*4882a593Smuzhiyun * only sparse, in that it uses memory proportional to the number of bits
72*4882a593Smuzhiyun * set. It is also efficient in memory usage when most of the bits are
73*4882a593Smuzhiyun * set.
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * At a high-level the state of the bit settings are maintained through
76*4882a593Smuzhiyun * the use of a binary-search tree, where each node contains at least
77*4882a593Smuzhiyun * the following members:
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * typedef uint64_t sparsebit_idx_t;
80*4882a593Smuzhiyun * typedef uint64_t sparsebit_num_t;
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * sparsebit_idx_t idx;
83*4882a593Smuzhiyun * uint32_t mask;
84*4882a593Smuzhiyun * sparsebit_num_t num_after;
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * The idx member contains the bit index of the first bit described by this
87*4882a593Smuzhiyun * node, while the mask member stores the setting of the first 32-bits.
88*4882a593Smuzhiyun * The setting of the bit at idx + n, where 0 <= n < 32, is located in the
89*4882a593Smuzhiyun * mask member at 1 << n.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * Nodes are sorted by idx and the bits described by two nodes will never
92*4882a593Smuzhiyun * overlap. The idx member is always aligned to the mask size, i.e. a
93*4882a593Smuzhiyun * multiple of 32.
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * Beyond a typical implementation, the nodes in this implementation also
96*4882a593Smuzhiyun * contains a member named num_after. The num_after member holds the
97*4882a593Smuzhiyun * number of bits immediately after the mask bits that are contiguously set.
98*4882a593Smuzhiyun * The use of the num_after member allows this implementation to efficiently
99*4882a593Smuzhiyun * represent cases where most bits are set. For example, the case of all
100*4882a593Smuzhiyun * but the last two bits set, is represented by the following two nodes:
101*4882a593Smuzhiyun *
102*4882a593Smuzhiyun * node 0 - idx: 0x0 mask: 0xffffffff num_after: 0xffffffffffffffc0
103*4882a593Smuzhiyun * node 1 - idx: 0xffffffffffffffe0 mask: 0x3fffffff num_after: 0
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * ==== Invariants ====
106*4882a593Smuzhiyun * This implementation usses the following invariants:
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * + Node are only used to represent bits that are set.
109*4882a593Smuzhiyun * Nodes with a mask of 0 and num_after of 0 are not allowed.
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * + Sum of bits set in all the nodes is equal to the value of
112*4882a593Smuzhiyun * the struct sparsebit_pvt num_set member.
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * + The setting of at least one bit is always described in a nodes
115*4882a593Smuzhiyun * mask (mask >= 1).
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * + A node with all mask bits set only occurs when the last bit
118*4882a593Smuzhiyun * described by the previous node is not equal to this nodes
119*4882a593Smuzhiyun * starting index - 1. All such occurences of this condition are
120*4882a593Smuzhiyun * avoided by moving the setting of the nodes mask bits into
121*4882a593Smuzhiyun * the previous nodes num_after setting.
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun * + Node starting index is evenly divisible by the number of bits
124*4882a593Smuzhiyun * within a nodes mask member.
125*4882a593Smuzhiyun *
126*4882a593Smuzhiyun * + Nodes never represent a range of bits that wrap around the
127*4882a593Smuzhiyun * highest supported index.
128*4882a593Smuzhiyun *
129*4882a593Smuzhiyun * (idx + MASK_BITS + num_after - 1) <= ((sparsebit_idx_t) 0) - 1)
130*4882a593Smuzhiyun *
131*4882a593Smuzhiyun * As a consequence of the above, the num_after member of a node
132*4882a593Smuzhiyun * will always be <=:
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * maximum_index - nodes_starting_index - number_of_mask_bits
135*4882a593Smuzhiyun *
136*4882a593Smuzhiyun * + Nodes within the binary search tree are sorted based on each
137*4882a593Smuzhiyun * nodes starting index.
138*4882a593Smuzhiyun *
139*4882a593Smuzhiyun * + The range of bits described by any two nodes do not overlap. The
140*4882a593Smuzhiyun * range of bits described by a single node is:
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * start: node->idx
143*4882a593Smuzhiyun * end (inclusive): node->idx + MASK_BITS + node->num_after - 1;
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * Note, at times these invariants are temporarily violated for a
146*4882a593Smuzhiyun * specific portion of the code. For example, when setting a mask
147*4882a593Smuzhiyun * bit, there is a small delay between when the mask bit is set and the
148*4882a593Smuzhiyun * value in the struct sparsebit_pvt num_set member is updated. Other
149*4882a593Smuzhiyun * temporary violations occur when node_split() is called with a specified
150*4882a593Smuzhiyun * index and assures that a node where its mask represents the bit
151*4882a593Smuzhiyun * at the specified index exists. At times to do this node_split()
152*4882a593Smuzhiyun * must split an existing node into two nodes or create a node that
153*4882a593Smuzhiyun * has no bits set. Such temporary violations must be corrected before
154*4882a593Smuzhiyun * returning to the caller. These corrections are typically performed
155*4882a593Smuzhiyun * by the local function node_reduce().
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun #include "test_util.h"
159*4882a593Smuzhiyun #include "sparsebit.h"
160*4882a593Smuzhiyun #include <limits.h>
161*4882a593Smuzhiyun #include <assert.h>
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun #define DUMP_LINE_MAX 100 /* Does not include indent amount */
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun typedef uint32_t mask_t;
166*4882a593Smuzhiyun #define MASK_BITS (sizeof(mask_t) * CHAR_BIT)
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun struct node {
169*4882a593Smuzhiyun struct node *parent;
170*4882a593Smuzhiyun struct node *left;
171*4882a593Smuzhiyun struct node *right;
172*4882a593Smuzhiyun sparsebit_idx_t idx; /* index of least-significant bit in mask */
173*4882a593Smuzhiyun sparsebit_num_t num_after; /* num contiguously set after mask */
174*4882a593Smuzhiyun mask_t mask;
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun struct sparsebit {
178*4882a593Smuzhiyun /*
179*4882a593Smuzhiyun * Points to root node of the binary search
180*4882a593Smuzhiyun * tree. Equal to NULL when no bits are set in
181*4882a593Smuzhiyun * the entire sparsebit array.
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun struct node *root;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /*
186*4882a593Smuzhiyun * A redundant count of the total number of bits set. Used for
187*4882a593Smuzhiyun * diagnostic purposes and to change the time complexity of
188*4882a593Smuzhiyun * sparsebit_num_set() from O(n) to O(1).
189*4882a593Smuzhiyun * Note: Due to overflow, a value of 0 means none or all set.
190*4882a593Smuzhiyun */
191*4882a593Smuzhiyun sparsebit_num_t num_set;
192*4882a593Smuzhiyun };
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* Returns the number of set bits described by the settings
195*4882a593Smuzhiyun * of the node pointed to by nodep.
196*4882a593Smuzhiyun */
node_num_set(struct node * nodep)197*4882a593Smuzhiyun static sparsebit_num_t node_num_set(struct node *nodep)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun return nodep->num_after + __builtin_popcount(nodep->mask);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /* Returns a pointer to the node that describes the
203*4882a593Smuzhiyun * lowest bit index.
204*4882a593Smuzhiyun */
node_first(struct sparsebit * s)205*4882a593Smuzhiyun static struct node *node_first(struct sparsebit *s)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct node *nodep;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun for (nodep = s->root; nodep && nodep->left; nodep = nodep->left)
210*4882a593Smuzhiyun ;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun return nodep;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* Returns a pointer to the node that describes the
216*4882a593Smuzhiyun * lowest bit index > the index of the node pointed to by np.
217*4882a593Smuzhiyun * Returns NULL if no node with a higher index exists.
218*4882a593Smuzhiyun */
node_next(struct sparsebit * s,struct node * np)219*4882a593Smuzhiyun static struct node *node_next(struct sparsebit *s, struct node *np)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun struct node *nodep = np;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun /*
224*4882a593Smuzhiyun * If current node has a right child, next node is the left-most
225*4882a593Smuzhiyun * of the right child.
226*4882a593Smuzhiyun */
227*4882a593Smuzhiyun if (nodep->right) {
228*4882a593Smuzhiyun for (nodep = nodep->right; nodep->left; nodep = nodep->left)
229*4882a593Smuzhiyun ;
230*4882a593Smuzhiyun return nodep;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun /*
234*4882a593Smuzhiyun * No right child. Go up until node is left child of a parent.
235*4882a593Smuzhiyun * That parent is then the next node.
236*4882a593Smuzhiyun */
237*4882a593Smuzhiyun while (nodep->parent && nodep == nodep->parent->right)
238*4882a593Smuzhiyun nodep = nodep->parent;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun return nodep->parent;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /* Searches for and returns a pointer to the node that describes the
244*4882a593Smuzhiyun * highest index < the index of the node pointed to by np.
245*4882a593Smuzhiyun * Returns NULL if no node with a lower index exists.
246*4882a593Smuzhiyun */
node_prev(struct sparsebit * s,struct node * np)247*4882a593Smuzhiyun static struct node *node_prev(struct sparsebit *s, struct node *np)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun struct node *nodep = np;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /*
252*4882a593Smuzhiyun * If current node has a left child, next node is the right-most
253*4882a593Smuzhiyun * of the left child.
254*4882a593Smuzhiyun */
255*4882a593Smuzhiyun if (nodep->left) {
256*4882a593Smuzhiyun for (nodep = nodep->left; nodep->right; nodep = nodep->right)
257*4882a593Smuzhiyun ;
258*4882a593Smuzhiyun return (struct node *) nodep;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /*
262*4882a593Smuzhiyun * No left child. Go up until node is right child of a parent.
263*4882a593Smuzhiyun * That parent is then the next node.
264*4882a593Smuzhiyun */
265*4882a593Smuzhiyun while (nodep->parent && nodep == nodep->parent->left)
266*4882a593Smuzhiyun nodep = nodep->parent;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun return (struct node *) nodep->parent;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /* Allocates space to hold a copy of the node sub-tree pointed to by
273*4882a593Smuzhiyun * subtree and duplicates the bit settings to the newly allocated nodes.
274*4882a593Smuzhiyun * Returns the newly allocated copy of subtree.
275*4882a593Smuzhiyun */
node_copy_subtree(struct node * subtree)276*4882a593Smuzhiyun static struct node *node_copy_subtree(struct node *subtree)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun struct node *root;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun /* Duplicate the node at the root of the subtree */
281*4882a593Smuzhiyun root = calloc(1, sizeof(*root));
282*4882a593Smuzhiyun if (!root) {
283*4882a593Smuzhiyun perror("calloc");
284*4882a593Smuzhiyun abort();
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun root->idx = subtree->idx;
288*4882a593Smuzhiyun root->mask = subtree->mask;
289*4882a593Smuzhiyun root->num_after = subtree->num_after;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /* As needed, recursively duplicate the left and right subtrees */
292*4882a593Smuzhiyun if (subtree->left) {
293*4882a593Smuzhiyun root->left = node_copy_subtree(subtree->left);
294*4882a593Smuzhiyun root->left->parent = root;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun if (subtree->right) {
298*4882a593Smuzhiyun root->right = node_copy_subtree(subtree->right);
299*4882a593Smuzhiyun root->right->parent = root;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun return root;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /* Searches for and returns a pointer to the node that describes the setting
306*4882a593Smuzhiyun * of the bit given by idx. A node describes the setting of a bit if its
307*4882a593Smuzhiyun * index is within the bits described by the mask bits or the number of
308*4882a593Smuzhiyun * contiguous bits set after the mask. Returns NULL if there is no such node.
309*4882a593Smuzhiyun */
node_find(struct sparsebit * s,sparsebit_idx_t idx)310*4882a593Smuzhiyun static struct node *node_find(struct sparsebit *s, sparsebit_idx_t idx)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun struct node *nodep;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /* Find the node that describes the setting of the bit at idx */
315*4882a593Smuzhiyun for (nodep = s->root; nodep;
316*4882a593Smuzhiyun nodep = nodep->idx > idx ? nodep->left : nodep->right) {
317*4882a593Smuzhiyun if (idx >= nodep->idx &&
318*4882a593Smuzhiyun idx <= nodep->idx + MASK_BITS + nodep->num_after - 1)
319*4882a593Smuzhiyun break;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun return nodep;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /* Entry Requirements:
326*4882a593Smuzhiyun * + A node that describes the setting of idx is not already present.
327*4882a593Smuzhiyun *
328*4882a593Smuzhiyun * Adds a new node to describe the setting of the bit at the index given
329*4882a593Smuzhiyun * by idx. Returns a pointer to the newly added node.
330*4882a593Smuzhiyun *
331*4882a593Smuzhiyun * TODO(lhuemill): Degenerate cases causes the tree to get unbalanced.
332*4882a593Smuzhiyun */
node_add(struct sparsebit * s,sparsebit_idx_t idx)333*4882a593Smuzhiyun static struct node *node_add(struct sparsebit *s, sparsebit_idx_t idx)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun struct node *nodep, *parentp, *prev;
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /* Allocate and initialize the new node. */
338*4882a593Smuzhiyun nodep = calloc(1, sizeof(*nodep));
339*4882a593Smuzhiyun if (!nodep) {
340*4882a593Smuzhiyun perror("calloc");
341*4882a593Smuzhiyun abort();
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun nodep->idx = idx & -MASK_BITS;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /* If no nodes, set it up as the root node. */
347*4882a593Smuzhiyun if (!s->root) {
348*4882a593Smuzhiyun s->root = nodep;
349*4882a593Smuzhiyun return nodep;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun /*
353*4882a593Smuzhiyun * Find the parent where the new node should be attached
354*4882a593Smuzhiyun * and add the node there.
355*4882a593Smuzhiyun */
356*4882a593Smuzhiyun parentp = s->root;
357*4882a593Smuzhiyun while (true) {
358*4882a593Smuzhiyun if (idx < parentp->idx) {
359*4882a593Smuzhiyun if (!parentp->left) {
360*4882a593Smuzhiyun parentp->left = nodep;
361*4882a593Smuzhiyun nodep->parent = parentp;
362*4882a593Smuzhiyun break;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun parentp = parentp->left;
365*4882a593Smuzhiyun } else {
366*4882a593Smuzhiyun assert(idx > parentp->idx + MASK_BITS + parentp->num_after - 1);
367*4882a593Smuzhiyun if (!parentp->right) {
368*4882a593Smuzhiyun parentp->right = nodep;
369*4882a593Smuzhiyun nodep->parent = parentp;
370*4882a593Smuzhiyun break;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun parentp = parentp->right;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /*
377*4882a593Smuzhiyun * Does num_after bits of previous node overlap with the mask
378*4882a593Smuzhiyun * of the new node? If so set the bits in the new nodes mask
379*4882a593Smuzhiyun * and reduce the previous nodes num_after.
380*4882a593Smuzhiyun */
381*4882a593Smuzhiyun prev = node_prev(s, nodep);
382*4882a593Smuzhiyun while (prev && prev->idx + MASK_BITS + prev->num_after - 1 >= nodep->idx) {
383*4882a593Smuzhiyun unsigned int n1 = (prev->idx + MASK_BITS + prev->num_after - 1)
384*4882a593Smuzhiyun - nodep->idx;
385*4882a593Smuzhiyun assert(prev->num_after > 0);
386*4882a593Smuzhiyun assert(n1 < MASK_BITS);
387*4882a593Smuzhiyun assert(!(nodep->mask & (1 << n1)));
388*4882a593Smuzhiyun nodep->mask |= (1 << n1);
389*4882a593Smuzhiyun prev->num_after--;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun return nodep;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun /* Returns whether all the bits in the sparsebit array are set. */
sparsebit_all_set(struct sparsebit * s)396*4882a593Smuzhiyun bool sparsebit_all_set(struct sparsebit *s)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun /*
399*4882a593Smuzhiyun * If any nodes there must be at least one bit set. Only case
400*4882a593Smuzhiyun * where a bit is set and total num set is 0, is when all bits
401*4882a593Smuzhiyun * are set.
402*4882a593Smuzhiyun */
403*4882a593Smuzhiyun return s->root && s->num_set == 0;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun /* Clears all bits described by the node pointed to by nodep, then
407*4882a593Smuzhiyun * removes the node.
408*4882a593Smuzhiyun */
node_rm(struct sparsebit * s,struct node * nodep)409*4882a593Smuzhiyun static void node_rm(struct sparsebit *s, struct node *nodep)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun struct node *tmp;
412*4882a593Smuzhiyun sparsebit_num_t num_set;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun num_set = node_num_set(nodep);
415*4882a593Smuzhiyun assert(s->num_set >= num_set || sparsebit_all_set(s));
416*4882a593Smuzhiyun s->num_set -= node_num_set(nodep);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun /* Have both left and right child */
419*4882a593Smuzhiyun if (nodep->left && nodep->right) {
420*4882a593Smuzhiyun /*
421*4882a593Smuzhiyun * Move left children to the leftmost leaf node
422*4882a593Smuzhiyun * of the right child.
423*4882a593Smuzhiyun */
424*4882a593Smuzhiyun for (tmp = nodep->right; tmp->left; tmp = tmp->left)
425*4882a593Smuzhiyun ;
426*4882a593Smuzhiyun tmp->left = nodep->left;
427*4882a593Smuzhiyun nodep->left = NULL;
428*4882a593Smuzhiyun tmp->left->parent = tmp;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun /* Left only child */
432*4882a593Smuzhiyun if (nodep->left) {
433*4882a593Smuzhiyun if (!nodep->parent) {
434*4882a593Smuzhiyun s->root = nodep->left;
435*4882a593Smuzhiyun nodep->left->parent = NULL;
436*4882a593Smuzhiyun } else {
437*4882a593Smuzhiyun nodep->left->parent = nodep->parent;
438*4882a593Smuzhiyun if (nodep == nodep->parent->left)
439*4882a593Smuzhiyun nodep->parent->left = nodep->left;
440*4882a593Smuzhiyun else {
441*4882a593Smuzhiyun assert(nodep == nodep->parent->right);
442*4882a593Smuzhiyun nodep->parent->right = nodep->left;
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun nodep->parent = nodep->left = nodep->right = NULL;
447*4882a593Smuzhiyun free(nodep);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun return;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /* Right only child */
454*4882a593Smuzhiyun if (nodep->right) {
455*4882a593Smuzhiyun if (!nodep->parent) {
456*4882a593Smuzhiyun s->root = nodep->right;
457*4882a593Smuzhiyun nodep->right->parent = NULL;
458*4882a593Smuzhiyun } else {
459*4882a593Smuzhiyun nodep->right->parent = nodep->parent;
460*4882a593Smuzhiyun if (nodep == nodep->parent->left)
461*4882a593Smuzhiyun nodep->parent->left = nodep->right;
462*4882a593Smuzhiyun else {
463*4882a593Smuzhiyun assert(nodep == nodep->parent->right);
464*4882a593Smuzhiyun nodep->parent->right = nodep->right;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun nodep->parent = nodep->left = nodep->right = NULL;
469*4882a593Smuzhiyun free(nodep);
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun return;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /* Leaf Node */
475*4882a593Smuzhiyun if (!nodep->parent) {
476*4882a593Smuzhiyun s->root = NULL;
477*4882a593Smuzhiyun } else {
478*4882a593Smuzhiyun if (nodep->parent->left == nodep)
479*4882a593Smuzhiyun nodep->parent->left = NULL;
480*4882a593Smuzhiyun else {
481*4882a593Smuzhiyun assert(nodep == nodep->parent->right);
482*4882a593Smuzhiyun nodep->parent->right = NULL;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun nodep->parent = nodep->left = nodep->right = NULL;
487*4882a593Smuzhiyun free(nodep);
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun return;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /* Splits the node containing the bit at idx so that there is a node
493*4882a593Smuzhiyun * that starts at the specified index. If no such node exists, a new
494*4882a593Smuzhiyun * node at the specified index is created. Returns the new node.
495*4882a593Smuzhiyun *
496*4882a593Smuzhiyun * idx must start of a mask boundary.
497*4882a593Smuzhiyun */
node_split(struct sparsebit * s,sparsebit_idx_t idx)498*4882a593Smuzhiyun static struct node *node_split(struct sparsebit *s, sparsebit_idx_t idx)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun struct node *nodep1, *nodep2;
501*4882a593Smuzhiyun sparsebit_idx_t offset;
502*4882a593Smuzhiyun sparsebit_num_t orig_num_after;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun assert(!(idx % MASK_BITS));
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /*
507*4882a593Smuzhiyun * Is there a node that describes the setting of idx?
508*4882a593Smuzhiyun * If not, add it.
509*4882a593Smuzhiyun */
510*4882a593Smuzhiyun nodep1 = node_find(s, idx);
511*4882a593Smuzhiyun if (!nodep1)
512*4882a593Smuzhiyun return node_add(s, idx);
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun /*
515*4882a593Smuzhiyun * All done if the starting index of the node is where the
516*4882a593Smuzhiyun * split should occur.
517*4882a593Smuzhiyun */
518*4882a593Smuzhiyun if (nodep1->idx == idx)
519*4882a593Smuzhiyun return nodep1;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun /*
522*4882a593Smuzhiyun * Split point not at start of mask, so it must be part of
523*4882a593Smuzhiyun * bits described by num_after.
524*4882a593Smuzhiyun */
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun * Calculate offset within num_after for where the split is
528*4882a593Smuzhiyun * to occur.
529*4882a593Smuzhiyun */
530*4882a593Smuzhiyun offset = idx - (nodep1->idx + MASK_BITS);
531*4882a593Smuzhiyun orig_num_after = nodep1->num_after;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /*
534*4882a593Smuzhiyun * Add a new node to describe the bits starting at
535*4882a593Smuzhiyun * the split point.
536*4882a593Smuzhiyun */
537*4882a593Smuzhiyun nodep1->num_after = offset;
538*4882a593Smuzhiyun nodep2 = node_add(s, idx);
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun /* Move bits after the split point into the new node */
541*4882a593Smuzhiyun nodep2->num_after = orig_num_after - offset;
542*4882a593Smuzhiyun if (nodep2->num_after >= MASK_BITS) {
543*4882a593Smuzhiyun nodep2->mask = ~(mask_t) 0;
544*4882a593Smuzhiyun nodep2->num_after -= MASK_BITS;
545*4882a593Smuzhiyun } else {
546*4882a593Smuzhiyun nodep2->mask = (1 << nodep2->num_after) - 1;
547*4882a593Smuzhiyun nodep2->num_after = 0;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun return nodep2;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun /* Iteratively reduces the node pointed to by nodep and its adjacent
554*4882a593Smuzhiyun * nodes into a more compact form. For example, a node with a mask with
555*4882a593Smuzhiyun * all bits set adjacent to a previous node, will get combined into a
556*4882a593Smuzhiyun * single node with an increased num_after setting.
557*4882a593Smuzhiyun *
558*4882a593Smuzhiyun * After each reduction, a further check is made to see if additional
559*4882a593Smuzhiyun * reductions are possible with the new previous and next nodes. Note,
560*4882a593Smuzhiyun * a search for a reduction is only done across the nodes nearest nodep
561*4882a593Smuzhiyun * and those that became part of a reduction. Reductions beyond nodep
562*4882a593Smuzhiyun * and the adjacent nodes that are reduced are not discovered. It is the
563*4882a593Smuzhiyun * responsibility of the caller to pass a nodep that is within one node
564*4882a593Smuzhiyun * of each possible reduction.
565*4882a593Smuzhiyun *
566*4882a593Smuzhiyun * This function does not fix the temporary violation of all invariants.
567*4882a593Smuzhiyun * For example it does not fix the case where the bit settings described
568*4882a593Smuzhiyun * by two or more nodes overlap. Such a violation introduces the potential
569*4882a593Smuzhiyun * complication of a bit setting for a specific index having different settings
570*4882a593Smuzhiyun * in different nodes. This would then introduce the further complication
571*4882a593Smuzhiyun * of which node has the correct setting of the bit and thus such conditions
572*4882a593Smuzhiyun * are not allowed.
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * This function is designed to fix invariant violations that are introduced
575*4882a593Smuzhiyun * by node_split() and by changes to the nodes mask or num_after members.
576*4882a593Smuzhiyun * For example, when setting a bit within a nodes mask, the function that
577*4882a593Smuzhiyun * sets the bit doesn't have to worry about whether the setting of that
578*4882a593Smuzhiyun * bit caused the mask to have leading only or trailing only bits set.
579*4882a593Smuzhiyun * Instead, the function can call node_reduce(), with nodep equal to the
580*4882a593Smuzhiyun * node address that it set a mask bit in, and node_reduce() will notice
581*4882a593Smuzhiyun * the cases of leading or trailing only bits and that there is an
582*4882a593Smuzhiyun * adjacent node that the bit settings could be merged into.
583*4882a593Smuzhiyun *
584*4882a593Smuzhiyun * This implementation specifically detects and corrects violation of the
585*4882a593Smuzhiyun * following invariants:
586*4882a593Smuzhiyun *
587*4882a593Smuzhiyun * + Node are only used to represent bits that are set.
588*4882a593Smuzhiyun * Nodes with a mask of 0 and num_after of 0 are not allowed.
589*4882a593Smuzhiyun *
590*4882a593Smuzhiyun * + The setting of at least one bit is always described in a nodes
591*4882a593Smuzhiyun * mask (mask >= 1).
592*4882a593Smuzhiyun *
593*4882a593Smuzhiyun * + A node with all mask bits set only occurs when the last bit
594*4882a593Smuzhiyun * described by the previous node is not equal to this nodes
595*4882a593Smuzhiyun * starting index - 1. All such occurences of this condition are
596*4882a593Smuzhiyun * avoided by moving the setting of the nodes mask bits into
597*4882a593Smuzhiyun * the previous nodes num_after setting.
598*4882a593Smuzhiyun */
node_reduce(struct sparsebit * s,struct node * nodep)599*4882a593Smuzhiyun static void node_reduce(struct sparsebit *s, struct node *nodep)
600*4882a593Smuzhiyun {
601*4882a593Smuzhiyun bool reduction_performed;
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun do {
604*4882a593Smuzhiyun reduction_performed = false;
605*4882a593Smuzhiyun struct node *prev, *next, *tmp;
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun /* 1) Potential reductions within the current node. */
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun /* Nodes with all bits cleared may be removed. */
610*4882a593Smuzhiyun if (nodep->mask == 0 && nodep->num_after == 0) {
611*4882a593Smuzhiyun /*
612*4882a593Smuzhiyun * About to remove the node pointed to by
613*4882a593Smuzhiyun * nodep, which normally would cause a problem
614*4882a593Smuzhiyun * for the next pass through the reduction loop,
615*4882a593Smuzhiyun * because the node at the starting point no longer
616*4882a593Smuzhiyun * exists. This potential problem is handled
617*4882a593Smuzhiyun * by first remembering the location of the next
618*4882a593Smuzhiyun * or previous nodes. Doesn't matter which, because
619*4882a593Smuzhiyun * once the node at nodep is removed, there will be
620*4882a593Smuzhiyun * no other nodes between prev and next.
621*4882a593Smuzhiyun *
622*4882a593Smuzhiyun * Note, the checks performed on nodep against both
623*4882a593Smuzhiyun * both prev and next both check for an adjacent
624*4882a593Smuzhiyun * node that can be reduced into a single node. As
625*4882a593Smuzhiyun * such, after removing the node at nodep, doesn't
626*4882a593Smuzhiyun * matter whether the nodep for the next pass
627*4882a593Smuzhiyun * through the loop is equal to the previous pass
628*4882a593Smuzhiyun * prev or next node. Either way, on the next pass
629*4882a593Smuzhiyun * the one not selected will become either the
630*4882a593Smuzhiyun * prev or next node.
631*4882a593Smuzhiyun */
632*4882a593Smuzhiyun tmp = node_next(s, nodep);
633*4882a593Smuzhiyun if (!tmp)
634*4882a593Smuzhiyun tmp = node_prev(s, nodep);
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun node_rm(s, nodep);
637*4882a593Smuzhiyun nodep = NULL;
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun nodep = tmp;
640*4882a593Smuzhiyun reduction_performed = true;
641*4882a593Smuzhiyun continue;
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /*
645*4882a593Smuzhiyun * When the mask is 0, can reduce the amount of num_after
646*4882a593Smuzhiyun * bits by moving the initial num_after bits into the mask.
647*4882a593Smuzhiyun */
648*4882a593Smuzhiyun if (nodep->mask == 0) {
649*4882a593Smuzhiyun assert(nodep->num_after != 0);
650*4882a593Smuzhiyun assert(nodep->idx + MASK_BITS > nodep->idx);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun nodep->idx += MASK_BITS;
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun if (nodep->num_after >= MASK_BITS) {
655*4882a593Smuzhiyun nodep->mask = ~0;
656*4882a593Smuzhiyun nodep->num_after -= MASK_BITS;
657*4882a593Smuzhiyun } else {
658*4882a593Smuzhiyun nodep->mask = (1u << nodep->num_after) - 1;
659*4882a593Smuzhiyun nodep->num_after = 0;
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun reduction_performed = true;
663*4882a593Smuzhiyun continue;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun /*
667*4882a593Smuzhiyun * 2) Potential reductions between the current and
668*4882a593Smuzhiyun * previous nodes.
669*4882a593Smuzhiyun */
670*4882a593Smuzhiyun prev = node_prev(s, nodep);
671*4882a593Smuzhiyun if (prev) {
672*4882a593Smuzhiyun sparsebit_idx_t prev_highest_bit;
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /* Nodes with no bits set can be removed. */
675*4882a593Smuzhiyun if (prev->mask == 0 && prev->num_after == 0) {
676*4882a593Smuzhiyun node_rm(s, prev);
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun reduction_performed = true;
679*4882a593Smuzhiyun continue;
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun /*
683*4882a593Smuzhiyun * All mask bits set and previous node has
684*4882a593Smuzhiyun * adjacent index.
685*4882a593Smuzhiyun */
686*4882a593Smuzhiyun if (nodep->mask + 1 == 0 &&
687*4882a593Smuzhiyun prev->idx + MASK_BITS == nodep->idx) {
688*4882a593Smuzhiyun prev->num_after += MASK_BITS + nodep->num_after;
689*4882a593Smuzhiyun nodep->mask = 0;
690*4882a593Smuzhiyun nodep->num_after = 0;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun reduction_performed = true;
693*4882a593Smuzhiyun continue;
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun /*
697*4882a593Smuzhiyun * Is node adjacent to previous node and the node
698*4882a593Smuzhiyun * contains a single contiguous range of bits
699*4882a593Smuzhiyun * starting from the beginning of the mask?
700*4882a593Smuzhiyun */
701*4882a593Smuzhiyun prev_highest_bit = prev->idx + MASK_BITS - 1 + prev->num_after;
702*4882a593Smuzhiyun if (prev_highest_bit + 1 == nodep->idx &&
703*4882a593Smuzhiyun (nodep->mask | (nodep->mask >> 1)) == nodep->mask) {
704*4882a593Smuzhiyun /*
705*4882a593Smuzhiyun * How many contiguous bits are there?
706*4882a593Smuzhiyun * Is equal to the total number of set
707*4882a593Smuzhiyun * bits, due to an earlier check that
708*4882a593Smuzhiyun * there is a single contiguous range of
709*4882a593Smuzhiyun * set bits.
710*4882a593Smuzhiyun */
711*4882a593Smuzhiyun unsigned int num_contiguous
712*4882a593Smuzhiyun = __builtin_popcount(nodep->mask);
713*4882a593Smuzhiyun assert((num_contiguous > 0) &&
714*4882a593Smuzhiyun ((1ULL << num_contiguous) - 1) == nodep->mask);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun prev->num_after += num_contiguous;
717*4882a593Smuzhiyun nodep->mask = 0;
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun /*
720*4882a593Smuzhiyun * For predictable performance, handle special
721*4882a593Smuzhiyun * case where all mask bits are set and there
722*4882a593Smuzhiyun * is a non-zero num_after setting. This code
723*4882a593Smuzhiyun * is functionally correct without the following
724*4882a593Smuzhiyun * conditionalized statements, but without them
725*4882a593Smuzhiyun * the value of num_after is only reduced by
726*4882a593Smuzhiyun * the number of mask bits per pass. There are
727*4882a593Smuzhiyun * cases where num_after can be close to 2^64.
728*4882a593Smuzhiyun * Without this code it could take nearly
729*4882a593Smuzhiyun * (2^64) / 32 passes to perform the full
730*4882a593Smuzhiyun * reduction.
731*4882a593Smuzhiyun */
732*4882a593Smuzhiyun if (num_contiguous == MASK_BITS) {
733*4882a593Smuzhiyun prev->num_after += nodep->num_after;
734*4882a593Smuzhiyun nodep->num_after = 0;
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun reduction_performed = true;
738*4882a593Smuzhiyun continue;
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun /*
743*4882a593Smuzhiyun * 3) Potential reductions between the current and
744*4882a593Smuzhiyun * next nodes.
745*4882a593Smuzhiyun */
746*4882a593Smuzhiyun next = node_next(s, nodep);
747*4882a593Smuzhiyun if (next) {
748*4882a593Smuzhiyun /* Nodes with no bits set can be removed. */
749*4882a593Smuzhiyun if (next->mask == 0 && next->num_after == 0) {
750*4882a593Smuzhiyun node_rm(s, next);
751*4882a593Smuzhiyun reduction_performed = true;
752*4882a593Smuzhiyun continue;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun /*
756*4882a593Smuzhiyun * Is next node index adjacent to current node
757*4882a593Smuzhiyun * and has a mask with all bits set?
758*4882a593Smuzhiyun */
759*4882a593Smuzhiyun if (next->idx == nodep->idx + MASK_BITS + nodep->num_after &&
760*4882a593Smuzhiyun next->mask == ~(mask_t) 0) {
761*4882a593Smuzhiyun nodep->num_after += MASK_BITS;
762*4882a593Smuzhiyun next->mask = 0;
763*4882a593Smuzhiyun nodep->num_after += next->num_after;
764*4882a593Smuzhiyun next->num_after = 0;
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun node_rm(s, next);
767*4882a593Smuzhiyun next = NULL;
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun reduction_performed = true;
770*4882a593Smuzhiyun continue;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun } while (nodep && reduction_performed);
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun /* Returns whether the bit at the index given by idx, within the
777*4882a593Smuzhiyun * sparsebit array is set or not.
778*4882a593Smuzhiyun */
sparsebit_is_set(struct sparsebit * s,sparsebit_idx_t idx)779*4882a593Smuzhiyun bool sparsebit_is_set(struct sparsebit *s, sparsebit_idx_t idx)
780*4882a593Smuzhiyun {
781*4882a593Smuzhiyun struct node *nodep;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun /* Find the node that describes the setting of the bit at idx */
784*4882a593Smuzhiyun for (nodep = s->root; nodep;
785*4882a593Smuzhiyun nodep = nodep->idx > idx ? nodep->left : nodep->right)
786*4882a593Smuzhiyun if (idx >= nodep->idx &&
787*4882a593Smuzhiyun idx <= nodep->idx + MASK_BITS + nodep->num_after - 1)
788*4882a593Smuzhiyun goto have_node;
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun return false;
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun have_node:
793*4882a593Smuzhiyun /* Bit is set if it is any of the bits described by num_after */
794*4882a593Smuzhiyun if (nodep->num_after && idx >= nodep->idx + MASK_BITS)
795*4882a593Smuzhiyun return true;
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun /* Is the corresponding mask bit set */
798*4882a593Smuzhiyun assert(idx >= nodep->idx && idx - nodep->idx < MASK_BITS);
799*4882a593Smuzhiyun return !!(nodep->mask & (1 << (idx - nodep->idx)));
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun /* Within the sparsebit array pointed to by s, sets the bit
803*4882a593Smuzhiyun * at the index given by idx.
804*4882a593Smuzhiyun */
bit_set(struct sparsebit * s,sparsebit_idx_t idx)805*4882a593Smuzhiyun static void bit_set(struct sparsebit *s, sparsebit_idx_t idx)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun struct node *nodep;
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun /* Skip bits that are already set */
810*4882a593Smuzhiyun if (sparsebit_is_set(s, idx))
811*4882a593Smuzhiyun return;
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun /*
814*4882a593Smuzhiyun * Get a node where the bit at idx is described by the mask.
815*4882a593Smuzhiyun * The node_split will also create a node, if there isn't
816*4882a593Smuzhiyun * already a node that describes the setting of bit.
817*4882a593Smuzhiyun */
818*4882a593Smuzhiyun nodep = node_split(s, idx & -MASK_BITS);
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun /* Set the bit within the nodes mask */
821*4882a593Smuzhiyun assert(idx >= nodep->idx && idx <= nodep->idx + MASK_BITS - 1);
822*4882a593Smuzhiyun assert(!(nodep->mask & (1 << (idx - nodep->idx))));
823*4882a593Smuzhiyun nodep->mask |= 1 << (idx - nodep->idx);
824*4882a593Smuzhiyun s->num_set++;
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun node_reduce(s, nodep);
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun /* Within the sparsebit array pointed to by s, clears the bit
830*4882a593Smuzhiyun * at the index given by idx.
831*4882a593Smuzhiyun */
bit_clear(struct sparsebit * s,sparsebit_idx_t idx)832*4882a593Smuzhiyun static void bit_clear(struct sparsebit *s, sparsebit_idx_t idx)
833*4882a593Smuzhiyun {
834*4882a593Smuzhiyun struct node *nodep;
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun /* Skip bits that are already cleared */
837*4882a593Smuzhiyun if (!sparsebit_is_set(s, idx))
838*4882a593Smuzhiyun return;
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun /* Is there a node that describes the setting of this bit? */
841*4882a593Smuzhiyun nodep = node_find(s, idx);
842*4882a593Smuzhiyun if (!nodep)
843*4882a593Smuzhiyun return;
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun /*
846*4882a593Smuzhiyun * If a num_after bit, split the node, so that the bit is
847*4882a593Smuzhiyun * part of a node mask.
848*4882a593Smuzhiyun */
849*4882a593Smuzhiyun if (idx >= nodep->idx + MASK_BITS)
850*4882a593Smuzhiyun nodep = node_split(s, idx & -MASK_BITS);
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun /*
853*4882a593Smuzhiyun * After node_split above, bit at idx should be within the mask.
854*4882a593Smuzhiyun * Clear that bit.
855*4882a593Smuzhiyun */
856*4882a593Smuzhiyun assert(idx >= nodep->idx && idx <= nodep->idx + MASK_BITS - 1);
857*4882a593Smuzhiyun assert(nodep->mask & (1 << (idx - nodep->idx)));
858*4882a593Smuzhiyun nodep->mask &= ~(1 << (idx - nodep->idx));
859*4882a593Smuzhiyun assert(s->num_set > 0 || sparsebit_all_set(s));
860*4882a593Smuzhiyun s->num_set--;
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun node_reduce(s, nodep);
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun /* Recursively dumps to the FILE stream given by stream the contents
866*4882a593Smuzhiyun * of the sub-tree of nodes pointed to by nodep. Each line of output
867*4882a593Smuzhiyun * is prefixed by the number of spaces given by indent. On each
868*4882a593Smuzhiyun * recursion, the indent amount is increased by 2. This causes nodes
869*4882a593Smuzhiyun * at each level deeper into the binary search tree to be displayed
870*4882a593Smuzhiyun * with a greater indent.
871*4882a593Smuzhiyun */
dump_nodes(FILE * stream,struct node * nodep,unsigned int indent)872*4882a593Smuzhiyun static void dump_nodes(FILE *stream, struct node *nodep,
873*4882a593Smuzhiyun unsigned int indent)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun char *node_type;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun /* Dump contents of node */
878*4882a593Smuzhiyun if (!nodep->parent)
879*4882a593Smuzhiyun node_type = "root";
880*4882a593Smuzhiyun else if (nodep == nodep->parent->left)
881*4882a593Smuzhiyun node_type = "left";
882*4882a593Smuzhiyun else {
883*4882a593Smuzhiyun assert(nodep == nodep->parent->right);
884*4882a593Smuzhiyun node_type = "right";
885*4882a593Smuzhiyun }
886*4882a593Smuzhiyun fprintf(stream, "%*s---- %s nodep: %p\n", indent, "", node_type, nodep);
887*4882a593Smuzhiyun fprintf(stream, "%*s parent: %p left: %p right: %p\n", indent, "",
888*4882a593Smuzhiyun nodep->parent, nodep->left, nodep->right);
889*4882a593Smuzhiyun fprintf(stream, "%*s idx: 0x%lx mask: 0x%x num_after: 0x%lx\n",
890*4882a593Smuzhiyun indent, "", nodep->idx, nodep->mask, nodep->num_after);
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun /* If present, dump contents of left child nodes */
893*4882a593Smuzhiyun if (nodep->left)
894*4882a593Smuzhiyun dump_nodes(stream, nodep->left, indent + 2);
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun /* If present, dump contents of right child nodes */
897*4882a593Smuzhiyun if (nodep->right)
898*4882a593Smuzhiyun dump_nodes(stream, nodep->right, indent + 2);
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun
node_first_set(struct node * nodep,int start)901*4882a593Smuzhiyun static inline sparsebit_idx_t node_first_set(struct node *nodep, int start)
902*4882a593Smuzhiyun {
903*4882a593Smuzhiyun mask_t leading = (mask_t)1 << start;
904*4882a593Smuzhiyun int n1 = __builtin_ctz(nodep->mask & -leading);
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun return nodep->idx + n1;
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun
node_first_clear(struct node * nodep,int start)909*4882a593Smuzhiyun static inline sparsebit_idx_t node_first_clear(struct node *nodep, int start)
910*4882a593Smuzhiyun {
911*4882a593Smuzhiyun mask_t leading = (mask_t)1 << start;
912*4882a593Smuzhiyun int n1 = __builtin_ctz(~nodep->mask & -leading);
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun return nodep->idx + n1;
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun /* Dumps to the FILE stream specified by stream, the implementation dependent
918*4882a593Smuzhiyun * internal state of s. Each line of output is prefixed with the number
919*4882a593Smuzhiyun * of spaces given by indent. The output is completely implementation
920*4882a593Smuzhiyun * dependent and subject to change. Output from this function should only
921*4882a593Smuzhiyun * be used for diagnostic purposes. For example, this function can be
922*4882a593Smuzhiyun * used by test cases after they detect an unexpected condition, as a means
923*4882a593Smuzhiyun * to capture diagnostic information.
924*4882a593Smuzhiyun */
sparsebit_dump_internal(FILE * stream,struct sparsebit * s,unsigned int indent)925*4882a593Smuzhiyun static void sparsebit_dump_internal(FILE *stream, struct sparsebit *s,
926*4882a593Smuzhiyun unsigned int indent)
927*4882a593Smuzhiyun {
928*4882a593Smuzhiyun /* Dump the contents of s */
929*4882a593Smuzhiyun fprintf(stream, "%*sroot: %p\n", indent, "", s->root);
930*4882a593Smuzhiyun fprintf(stream, "%*snum_set: 0x%lx\n", indent, "", s->num_set);
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun if (s->root)
933*4882a593Smuzhiyun dump_nodes(stream, s->root, indent);
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun /* Allocates and returns a new sparsebit array. The initial state
937*4882a593Smuzhiyun * of the newly allocated sparsebit array has all bits cleared.
938*4882a593Smuzhiyun */
sparsebit_alloc(void)939*4882a593Smuzhiyun struct sparsebit *sparsebit_alloc(void)
940*4882a593Smuzhiyun {
941*4882a593Smuzhiyun struct sparsebit *s;
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun /* Allocate top level structure. */
944*4882a593Smuzhiyun s = calloc(1, sizeof(*s));
945*4882a593Smuzhiyun if (!s) {
946*4882a593Smuzhiyun perror("calloc");
947*4882a593Smuzhiyun abort();
948*4882a593Smuzhiyun }
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun return s;
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun /* Frees the implementation dependent data for the sparsebit array
954*4882a593Smuzhiyun * pointed to by s and poisons the pointer to that data.
955*4882a593Smuzhiyun */
sparsebit_free(struct sparsebit ** sbitp)956*4882a593Smuzhiyun void sparsebit_free(struct sparsebit **sbitp)
957*4882a593Smuzhiyun {
958*4882a593Smuzhiyun struct sparsebit *s = *sbitp;
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun if (!s)
961*4882a593Smuzhiyun return;
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun sparsebit_clear_all(s);
964*4882a593Smuzhiyun free(s);
965*4882a593Smuzhiyun *sbitp = NULL;
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun /* Makes a copy of the sparsebit array given by s, to the sparsebit
969*4882a593Smuzhiyun * array given by d. Note, d must have already been allocated via
970*4882a593Smuzhiyun * sparsebit_alloc(). It can though already have bits set, which
971*4882a593Smuzhiyun * if different from src will be cleared.
972*4882a593Smuzhiyun */
sparsebit_copy(struct sparsebit * d,struct sparsebit * s)973*4882a593Smuzhiyun void sparsebit_copy(struct sparsebit *d, struct sparsebit *s)
974*4882a593Smuzhiyun {
975*4882a593Smuzhiyun /* First clear any bits already set in the destination */
976*4882a593Smuzhiyun sparsebit_clear_all(d);
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun if (s->root) {
979*4882a593Smuzhiyun d->root = node_copy_subtree(s->root);
980*4882a593Smuzhiyun d->num_set = s->num_set;
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun /* Returns whether num consecutive bits starting at idx are all set. */
sparsebit_is_set_num(struct sparsebit * s,sparsebit_idx_t idx,sparsebit_num_t num)985*4882a593Smuzhiyun bool sparsebit_is_set_num(struct sparsebit *s,
986*4882a593Smuzhiyun sparsebit_idx_t idx, sparsebit_num_t num)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun sparsebit_idx_t next_cleared;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun assert(num > 0);
991*4882a593Smuzhiyun assert(idx + num - 1 >= idx);
992*4882a593Smuzhiyun
993*4882a593Smuzhiyun /* With num > 0, the first bit must be set. */
994*4882a593Smuzhiyun if (!sparsebit_is_set(s, idx))
995*4882a593Smuzhiyun return false;
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun /* Find the next cleared bit */
998*4882a593Smuzhiyun next_cleared = sparsebit_next_clear(s, idx);
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun /*
1001*4882a593Smuzhiyun * If no cleared bits beyond idx, then there are at least num
1002*4882a593Smuzhiyun * set bits. idx + num doesn't wrap. Otherwise check if
1003*4882a593Smuzhiyun * there are enough set bits between idx and the next cleared bit.
1004*4882a593Smuzhiyun */
1005*4882a593Smuzhiyun return next_cleared == 0 || next_cleared - idx >= num;
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun /* Returns whether the bit at the index given by idx. */
sparsebit_is_clear(struct sparsebit * s,sparsebit_idx_t idx)1009*4882a593Smuzhiyun bool sparsebit_is_clear(struct sparsebit *s,
1010*4882a593Smuzhiyun sparsebit_idx_t idx)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun return !sparsebit_is_set(s, idx);
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun /* Returns whether num consecutive bits starting at idx are all cleared. */
sparsebit_is_clear_num(struct sparsebit * s,sparsebit_idx_t idx,sparsebit_num_t num)1016*4882a593Smuzhiyun bool sparsebit_is_clear_num(struct sparsebit *s,
1017*4882a593Smuzhiyun sparsebit_idx_t idx, sparsebit_num_t num)
1018*4882a593Smuzhiyun {
1019*4882a593Smuzhiyun sparsebit_idx_t next_set;
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun assert(num > 0);
1022*4882a593Smuzhiyun assert(idx + num - 1 >= idx);
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun /* With num > 0, the first bit must be cleared. */
1025*4882a593Smuzhiyun if (!sparsebit_is_clear(s, idx))
1026*4882a593Smuzhiyun return false;
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun /* Find the next set bit */
1029*4882a593Smuzhiyun next_set = sparsebit_next_set(s, idx);
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun /*
1032*4882a593Smuzhiyun * If no set bits beyond idx, then there are at least num
1033*4882a593Smuzhiyun * cleared bits. idx + num doesn't wrap. Otherwise check if
1034*4882a593Smuzhiyun * there are enough cleared bits between idx and the next set bit.
1035*4882a593Smuzhiyun */
1036*4882a593Smuzhiyun return next_set == 0 || next_set - idx >= num;
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun /* Returns the total number of bits set. Note: 0 is also returned for
1040*4882a593Smuzhiyun * the case of all bits set. This is because with all bits set, there
1041*4882a593Smuzhiyun * is 1 additional bit set beyond what can be represented in the return
1042*4882a593Smuzhiyun * value. Use sparsebit_any_set(), instead of sparsebit_num_set() > 0,
1043*4882a593Smuzhiyun * to determine if the sparsebit array has any bits set.
1044*4882a593Smuzhiyun */
sparsebit_num_set(struct sparsebit * s)1045*4882a593Smuzhiyun sparsebit_num_t sparsebit_num_set(struct sparsebit *s)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun return s->num_set;
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun /* Returns whether any bit is set in the sparsebit array. */
sparsebit_any_set(struct sparsebit * s)1051*4882a593Smuzhiyun bool sparsebit_any_set(struct sparsebit *s)
1052*4882a593Smuzhiyun {
1053*4882a593Smuzhiyun /*
1054*4882a593Smuzhiyun * Nodes only describe set bits. If any nodes then there
1055*4882a593Smuzhiyun * is at least 1 bit set.
1056*4882a593Smuzhiyun */
1057*4882a593Smuzhiyun if (!s->root)
1058*4882a593Smuzhiyun return false;
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun /*
1061*4882a593Smuzhiyun * Every node should have a non-zero mask. For now will
1062*4882a593Smuzhiyun * just assure that the root node has a non-zero mask,
1063*4882a593Smuzhiyun * which is a quick check that at least 1 bit is set.
1064*4882a593Smuzhiyun */
1065*4882a593Smuzhiyun assert(s->root->mask != 0);
1066*4882a593Smuzhiyun assert(s->num_set > 0 ||
1067*4882a593Smuzhiyun (s->root->num_after == ((sparsebit_num_t) 0) - MASK_BITS &&
1068*4882a593Smuzhiyun s->root->mask == ~(mask_t) 0));
1069*4882a593Smuzhiyun
1070*4882a593Smuzhiyun return true;
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun /* Returns whether all the bits in the sparsebit array are cleared. */
sparsebit_all_clear(struct sparsebit * s)1074*4882a593Smuzhiyun bool sparsebit_all_clear(struct sparsebit *s)
1075*4882a593Smuzhiyun {
1076*4882a593Smuzhiyun return !sparsebit_any_set(s);
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun /* Returns whether all the bits in the sparsebit array are set. */
sparsebit_any_clear(struct sparsebit * s)1080*4882a593Smuzhiyun bool sparsebit_any_clear(struct sparsebit *s)
1081*4882a593Smuzhiyun {
1082*4882a593Smuzhiyun return !sparsebit_all_set(s);
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun /* Returns the index of the first set bit. Abort if no bits are set.
1086*4882a593Smuzhiyun */
sparsebit_first_set(struct sparsebit * s)1087*4882a593Smuzhiyun sparsebit_idx_t sparsebit_first_set(struct sparsebit *s)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun struct node *nodep;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun /* Validate at least 1 bit is set */
1092*4882a593Smuzhiyun assert(sparsebit_any_set(s));
1093*4882a593Smuzhiyun
1094*4882a593Smuzhiyun nodep = node_first(s);
1095*4882a593Smuzhiyun return node_first_set(nodep, 0);
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun
1098*4882a593Smuzhiyun /* Returns the index of the first cleared bit. Abort if
1099*4882a593Smuzhiyun * no bits are cleared.
1100*4882a593Smuzhiyun */
sparsebit_first_clear(struct sparsebit * s)1101*4882a593Smuzhiyun sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s)
1102*4882a593Smuzhiyun {
1103*4882a593Smuzhiyun struct node *nodep1, *nodep2;
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun /* Validate at least 1 bit is cleared. */
1106*4882a593Smuzhiyun assert(sparsebit_any_clear(s));
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun /* If no nodes or first node index > 0 then lowest cleared is 0 */
1109*4882a593Smuzhiyun nodep1 = node_first(s);
1110*4882a593Smuzhiyun if (!nodep1 || nodep1->idx > 0)
1111*4882a593Smuzhiyun return 0;
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun /* Does the mask in the first node contain any cleared bits. */
1114*4882a593Smuzhiyun if (nodep1->mask != ~(mask_t) 0)
1115*4882a593Smuzhiyun return node_first_clear(nodep1, 0);
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun /*
1118*4882a593Smuzhiyun * All mask bits set in first node. If there isn't a second node
1119*4882a593Smuzhiyun * then the first cleared bit is the first bit after the bits
1120*4882a593Smuzhiyun * described by the first node.
1121*4882a593Smuzhiyun */
1122*4882a593Smuzhiyun nodep2 = node_next(s, nodep1);
1123*4882a593Smuzhiyun if (!nodep2) {
1124*4882a593Smuzhiyun /*
1125*4882a593Smuzhiyun * No second node. First cleared bit is first bit beyond
1126*4882a593Smuzhiyun * bits described by first node.
1127*4882a593Smuzhiyun */
1128*4882a593Smuzhiyun assert(nodep1->mask == ~(mask_t) 0);
1129*4882a593Smuzhiyun assert(nodep1->idx + MASK_BITS + nodep1->num_after != (sparsebit_idx_t) 0);
1130*4882a593Smuzhiyun return nodep1->idx + MASK_BITS + nodep1->num_after;
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun
1133*4882a593Smuzhiyun /*
1134*4882a593Smuzhiyun * There is a second node.
1135*4882a593Smuzhiyun * If it is not adjacent to the first node, then there is a gap
1136*4882a593Smuzhiyun * of cleared bits between the nodes, and the first cleared bit
1137*4882a593Smuzhiyun * is the first bit within the gap.
1138*4882a593Smuzhiyun */
1139*4882a593Smuzhiyun if (nodep1->idx + MASK_BITS + nodep1->num_after != nodep2->idx)
1140*4882a593Smuzhiyun return nodep1->idx + MASK_BITS + nodep1->num_after;
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun /*
1143*4882a593Smuzhiyun * Second node is adjacent to the first node.
1144*4882a593Smuzhiyun * Because it is adjacent, its mask should be non-zero. If all
1145*4882a593Smuzhiyun * its mask bits are set, then with it being adjacent, it should
1146*4882a593Smuzhiyun * have had the mask bits moved into the num_after setting of the
1147*4882a593Smuzhiyun * previous node.
1148*4882a593Smuzhiyun */
1149*4882a593Smuzhiyun return node_first_clear(nodep2, 0);
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun /* Returns index of next bit set within s after the index given by prev.
1153*4882a593Smuzhiyun * Returns 0 if there are no bits after prev that are set.
1154*4882a593Smuzhiyun */
sparsebit_next_set(struct sparsebit * s,sparsebit_idx_t prev)1155*4882a593Smuzhiyun sparsebit_idx_t sparsebit_next_set(struct sparsebit *s,
1156*4882a593Smuzhiyun sparsebit_idx_t prev)
1157*4882a593Smuzhiyun {
1158*4882a593Smuzhiyun sparsebit_idx_t lowest_possible = prev + 1;
1159*4882a593Smuzhiyun sparsebit_idx_t start;
1160*4882a593Smuzhiyun struct node *nodep;
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun /* A bit after the highest index can't be set. */
1163*4882a593Smuzhiyun if (lowest_possible == 0)
1164*4882a593Smuzhiyun return 0;
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun /*
1167*4882a593Smuzhiyun * Find the leftmost 'candidate' overlapping or to the right
1168*4882a593Smuzhiyun * of lowest_possible.
1169*4882a593Smuzhiyun */
1170*4882a593Smuzhiyun struct node *candidate = NULL;
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun /* True iff lowest_possible is within candidate */
1173*4882a593Smuzhiyun bool contains = false;
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun /*
1176*4882a593Smuzhiyun * Find node that describes setting of bit at lowest_possible.
1177*4882a593Smuzhiyun * If such a node doesn't exist, find the node with the lowest
1178*4882a593Smuzhiyun * starting index that is > lowest_possible.
1179*4882a593Smuzhiyun */
1180*4882a593Smuzhiyun for (nodep = s->root; nodep;) {
1181*4882a593Smuzhiyun if ((nodep->idx + MASK_BITS + nodep->num_after - 1)
1182*4882a593Smuzhiyun >= lowest_possible) {
1183*4882a593Smuzhiyun candidate = nodep;
1184*4882a593Smuzhiyun if (candidate->idx <= lowest_possible) {
1185*4882a593Smuzhiyun contains = true;
1186*4882a593Smuzhiyun break;
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun nodep = nodep->left;
1189*4882a593Smuzhiyun } else {
1190*4882a593Smuzhiyun nodep = nodep->right;
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun }
1193*4882a593Smuzhiyun if (!candidate)
1194*4882a593Smuzhiyun return 0;
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun assert(candidate->mask != 0);
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun /* Does the candidate node describe the setting of lowest_possible? */
1199*4882a593Smuzhiyun if (!contains) {
1200*4882a593Smuzhiyun /*
1201*4882a593Smuzhiyun * Candidate doesn't describe setting of bit at lowest_possible.
1202*4882a593Smuzhiyun * Candidate points to the first node with a starting index
1203*4882a593Smuzhiyun * > lowest_possible.
1204*4882a593Smuzhiyun */
1205*4882a593Smuzhiyun assert(candidate->idx > lowest_possible);
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun return node_first_set(candidate, 0);
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun /*
1211*4882a593Smuzhiyun * Candidate describes setting of bit at lowest_possible.
1212*4882a593Smuzhiyun * Note: although the node describes the setting of the bit
1213*4882a593Smuzhiyun * at lowest_possible, its possible that its setting and the
1214*4882a593Smuzhiyun * setting of all latter bits described by this node are 0.
1215*4882a593Smuzhiyun * For now, just handle the cases where this node describes
1216*4882a593Smuzhiyun * a bit at or after an index of lowest_possible that is set.
1217*4882a593Smuzhiyun */
1218*4882a593Smuzhiyun start = lowest_possible - candidate->idx;
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun if (start < MASK_BITS && candidate->mask >= (1 << start))
1221*4882a593Smuzhiyun return node_first_set(candidate, start);
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun if (candidate->num_after) {
1224*4882a593Smuzhiyun sparsebit_idx_t first_num_after_idx = candidate->idx + MASK_BITS;
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun return lowest_possible < first_num_after_idx
1227*4882a593Smuzhiyun ? first_num_after_idx : lowest_possible;
1228*4882a593Smuzhiyun }
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun /*
1231*4882a593Smuzhiyun * Although candidate node describes setting of bit at
1232*4882a593Smuzhiyun * the index of lowest_possible, all bits at that index and
1233*4882a593Smuzhiyun * latter that are described by candidate are cleared. With
1234*4882a593Smuzhiyun * this, the next bit is the first bit in the next node, if
1235*4882a593Smuzhiyun * such a node exists. If a next node doesn't exist, then
1236*4882a593Smuzhiyun * there is no next set bit.
1237*4882a593Smuzhiyun */
1238*4882a593Smuzhiyun candidate = node_next(s, candidate);
1239*4882a593Smuzhiyun if (!candidate)
1240*4882a593Smuzhiyun return 0;
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun return node_first_set(candidate, 0);
1243*4882a593Smuzhiyun }
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun /* Returns index of next bit cleared within s after the index given by prev.
1246*4882a593Smuzhiyun * Returns 0 if there are no bits after prev that are cleared.
1247*4882a593Smuzhiyun */
sparsebit_next_clear(struct sparsebit * s,sparsebit_idx_t prev)1248*4882a593Smuzhiyun sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s,
1249*4882a593Smuzhiyun sparsebit_idx_t prev)
1250*4882a593Smuzhiyun {
1251*4882a593Smuzhiyun sparsebit_idx_t lowest_possible = prev + 1;
1252*4882a593Smuzhiyun sparsebit_idx_t idx;
1253*4882a593Smuzhiyun struct node *nodep1, *nodep2;
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun /* A bit after the highest index can't be set. */
1256*4882a593Smuzhiyun if (lowest_possible == 0)
1257*4882a593Smuzhiyun return 0;
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun /*
1260*4882a593Smuzhiyun * Does a node describing the setting of lowest_possible exist?
1261*4882a593Smuzhiyun * If not, the bit at lowest_possible is cleared.
1262*4882a593Smuzhiyun */
1263*4882a593Smuzhiyun nodep1 = node_find(s, lowest_possible);
1264*4882a593Smuzhiyun if (!nodep1)
1265*4882a593Smuzhiyun return lowest_possible;
1266*4882a593Smuzhiyun
1267*4882a593Smuzhiyun /* Does a mask bit in node 1 describe the next cleared bit. */
1268*4882a593Smuzhiyun for (idx = lowest_possible - nodep1->idx; idx < MASK_BITS; idx++)
1269*4882a593Smuzhiyun if (!(nodep1->mask & (1 << idx)))
1270*4882a593Smuzhiyun return nodep1->idx + idx;
1271*4882a593Smuzhiyun
1272*4882a593Smuzhiyun /*
1273*4882a593Smuzhiyun * Next cleared bit is not described by node 1. If there
1274*4882a593Smuzhiyun * isn't a next node, then next cleared bit is described
1275*4882a593Smuzhiyun * by bit after the bits described by the first node.
1276*4882a593Smuzhiyun */
1277*4882a593Smuzhiyun nodep2 = node_next(s, nodep1);
1278*4882a593Smuzhiyun if (!nodep2)
1279*4882a593Smuzhiyun return nodep1->idx + MASK_BITS + nodep1->num_after;
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun /*
1282*4882a593Smuzhiyun * There is a second node.
1283*4882a593Smuzhiyun * If it is not adjacent to the first node, then there is a gap
1284*4882a593Smuzhiyun * of cleared bits between the nodes, and the next cleared bit
1285*4882a593Smuzhiyun * is the first bit within the gap.
1286*4882a593Smuzhiyun */
1287*4882a593Smuzhiyun if (nodep1->idx + MASK_BITS + nodep1->num_after != nodep2->idx)
1288*4882a593Smuzhiyun return nodep1->idx + MASK_BITS + nodep1->num_after;
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun /*
1291*4882a593Smuzhiyun * Second node is adjacent to the first node.
1292*4882a593Smuzhiyun * Because it is adjacent, its mask should be non-zero. If all
1293*4882a593Smuzhiyun * its mask bits are set, then with it being adjacent, it should
1294*4882a593Smuzhiyun * have had the mask bits moved into the num_after setting of the
1295*4882a593Smuzhiyun * previous node.
1296*4882a593Smuzhiyun */
1297*4882a593Smuzhiyun return node_first_clear(nodep2, 0);
1298*4882a593Smuzhiyun }
1299*4882a593Smuzhiyun
1300*4882a593Smuzhiyun /* Starting with the index 1 greater than the index given by start, finds
1301*4882a593Smuzhiyun * and returns the index of the first sequence of num consecutively set
1302*4882a593Smuzhiyun * bits. Returns a value of 0 of no such sequence exists.
1303*4882a593Smuzhiyun */
sparsebit_next_set_num(struct sparsebit * s,sparsebit_idx_t start,sparsebit_num_t num)1304*4882a593Smuzhiyun sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s,
1305*4882a593Smuzhiyun sparsebit_idx_t start, sparsebit_num_t num)
1306*4882a593Smuzhiyun {
1307*4882a593Smuzhiyun sparsebit_idx_t idx;
1308*4882a593Smuzhiyun
1309*4882a593Smuzhiyun assert(num >= 1);
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun for (idx = sparsebit_next_set(s, start);
1312*4882a593Smuzhiyun idx != 0 && idx + num - 1 >= idx;
1313*4882a593Smuzhiyun idx = sparsebit_next_set(s, idx)) {
1314*4882a593Smuzhiyun assert(sparsebit_is_set(s, idx));
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun /*
1317*4882a593Smuzhiyun * Does the sequence of bits starting at idx consist of
1318*4882a593Smuzhiyun * num set bits?
1319*4882a593Smuzhiyun */
1320*4882a593Smuzhiyun if (sparsebit_is_set_num(s, idx, num))
1321*4882a593Smuzhiyun return idx;
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun /*
1324*4882a593Smuzhiyun * Sequence of set bits at idx isn't large enough.
1325*4882a593Smuzhiyun * Skip this entire sequence of set bits.
1326*4882a593Smuzhiyun */
1327*4882a593Smuzhiyun idx = sparsebit_next_clear(s, idx);
1328*4882a593Smuzhiyun if (idx == 0)
1329*4882a593Smuzhiyun return 0;
1330*4882a593Smuzhiyun }
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun return 0;
1333*4882a593Smuzhiyun }
1334*4882a593Smuzhiyun
1335*4882a593Smuzhiyun /* Starting with the index 1 greater than the index given by start, finds
1336*4882a593Smuzhiyun * and returns the index of the first sequence of num consecutively cleared
1337*4882a593Smuzhiyun * bits. Returns a value of 0 of no such sequence exists.
1338*4882a593Smuzhiyun */
sparsebit_next_clear_num(struct sparsebit * s,sparsebit_idx_t start,sparsebit_num_t num)1339*4882a593Smuzhiyun sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *s,
1340*4882a593Smuzhiyun sparsebit_idx_t start, sparsebit_num_t num)
1341*4882a593Smuzhiyun {
1342*4882a593Smuzhiyun sparsebit_idx_t idx;
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun assert(num >= 1);
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun for (idx = sparsebit_next_clear(s, start);
1347*4882a593Smuzhiyun idx != 0 && idx + num - 1 >= idx;
1348*4882a593Smuzhiyun idx = sparsebit_next_clear(s, idx)) {
1349*4882a593Smuzhiyun assert(sparsebit_is_clear(s, idx));
1350*4882a593Smuzhiyun
1351*4882a593Smuzhiyun /*
1352*4882a593Smuzhiyun * Does the sequence of bits starting at idx consist of
1353*4882a593Smuzhiyun * num cleared bits?
1354*4882a593Smuzhiyun */
1355*4882a593Smuzhiyun if (sparsebit_is_clear_num(s, idx, num))
1356*4882a593Smuzhiyun return idx;
1357*4882a593Smuzhiyun
1358*4882a593Smuzhiyun /*
1359*4882a593Smuzhiyun * Sequence of cleared bits at idx isn't large enough.
1360*4882a593Smuzhiyun * Skip this entire sequence of cleared bits.
1361*4882a593Smuzhiyun */
1362*4882a593Smuzhiyun idx = sparsebit_next_set(s, idx);
1363*4882a593Smuzhiyun if (idx == 0)
1364*4882a593Smuzhiyun return 0;
1365*4882a593Smuzhiyun }
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun return 0;
1368*4882a593Smuzhiyun }
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun /* Sets the bits * in the inclusive range idx through idx + num - 1. */
sparsebit_set_num(struct sparsebit * s,sparsebit_idx_t start,sparsebit_num_t num)1371*4882a593Smuzhiyun void sparsebit_set_num(struct sparsebit *s,
1372*4882a593Smuzhiyun sparsebit_idx_t start, sparsebit_num_t num)
1373*4882a593Smuzhiyun {
1374*4882a593Smuzhiyun struct node *nodep, *next;
1375*4882a593Smuzhiyun unsigned int n1;
1376*4882a593Smuzhiyun sparsebit_idx_t idx;
1377*4882a593Smuzhiyun sparsebit_num_t n;
1378*4882a593Smuzhiyun sparsebit_idx_t middle_start, middle_end;
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun assert(num > 0);
1381*4882a593Smuzhiyun assert(start + num - 1 >= start);
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun /*
1384*4882a593Smuzhiyun * Leading - bits before first mask boundary.
1385*4882a593Smuzhiyun *
1386*4882a593Smuzhiyun * TODO(lhuemill): With some effort it may be possible to
1387*4882a593Smuzhiyun * replace the following loop with a sequential sequence
1388*4882a593Smuzhiyun * of statements. High level sequence would be:
1389*4882a593Smuzhiyun *
1390*4882a593Smuzhiyun * 1. Use node_split() to force node that describes setting
1391*4882a593Smuzhiyun * of idx to be within the mask portion of a node.
1392*4882a593Smuzhiyun * 2. Form mask of bits to be set.
1393*4882a593Smuzhiyun * 3. Determine number of mask bits already set in the node
1394*4882a593Smuzhiyun * and store in a local variable named num_already_set.
1395*4882a593Smuzhiyun * 4. Set the appropriate mask bits within the node.
1396*4882a593Smuzhiyun * 5. Increment struct sparsebit_pvt num_set member
1397*4882a593Smuzhiyun * by the number of bits that were actually set.
1398*4882a593Smuzhiyun * Exclude from the counts bits that were already set.
1399*4882a593Smuzhiyun * 6. Before returning to the caller, use node_reduce() to
1400*4882a593Smuzhiyun * handle the multiple corner cases that this method
1401*4882a593Smuzhiyun * introduces.
1402*4882a593Smuzhiyun */
1403*4882a593Smuzhiyun for (idx = start, n = num; n > 0 && idx % MASK_BITS != 0; idx++, n--)
1404*4882a593Smuzhiyun bit_set(s, idx);
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun /* Middle - bits spanning one or more entire mask */
1407*4882a593Smuzhiyun middle_start = idx;
1408*4882a593Smuzhiyun middle_end = middle_start + (n & -MASK_BITS) - 1;
1409*4882a593Smuzhiyun if (n >= MASK_BITS) {
1410*4882a593Smuzhiyun nodep = node_split(s, middle_start);
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun /*
1413*4882a593Smuzhiyun * As needed, split just after end of middle bits.
1414*4882a593Smuzhiyun * No split needed if end of middle bits is at highest
1415*4882a593Smuzhiyun * supported bit index.
1416*4882a593Smuzhiyun */
1417*4882a593Smuzhiyun if (middle_end + 1 > middle_end)
1418*4882a593Smuzhiyun (void) node_split(s, middle_end + 1);
1419*4882a593Smuzhiyun
1420*4882a593Smuzhiyun /* Delete nodes that only describe bits within the middle. */
1421*4882a593Smuzhiyun for (next = node_next(s, nodep);
1422*4882a593Smuzhiyun next && (next->idx < middle_end);
1423*4882a593Smuzhiyun next = node_next(s, nodep)) {
1424*4882a593Smuzhiyun assert(next->idx + MASK_BITS + next->num_after - 1 <= middle_end);
1425*4882a593Smuzhiyun node_rm(s, next);
1426*4882a593Smuzhiyun next = NULL;
1427*4882a593Smuzhiyun }
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun /* As needed set each of the mask bits */
1430*4882a593Smuzhiyun for (n1 = 0; n1 < MASK_BITS; n1++) {
1431*4882a593Smuzhiyun if (!(nodep->mask & (1 << n1))) {
1432*4882a593Smuzhiyun nodep->mask |= 1 << n1;
1433*4882a593Smuzhiyun s->num_set++;
1434*4882a593Smuzhiyun }
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun
1437*4882a593Smuzhiyun s->num_set -= nodep->num_after;
1438*4882a593Smuzhiyun nodep->num_after = middle_end - middle_start + 1 - MASK_BITS;
1439*4882a593Smuzhiyun s->num_set += nodep->num_after;
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun node_reduce(s, nodep);
1442*4882a593Smuzhiyun }
1443*4882a593Smuzhiyun idx = middle_end + 1;
1444*4882a593Smuzhiyun n -= middle_end - middle_start + 1;
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun /* Trailing - bits at and beyond last mask boundary */
1447*4882a593Smuzhiyun assert(n < MASK_BITS);
1448*4882a593Smuzhiyun for (; n > 0; idx++, n--)
1449*4882a593Smuzhiyun bit_set(s, idx);
1450*4882a593Smuzhiyun }
1451*4882a593Smuzhiyun
1452*4882a593Smuzhiyun /* Clears the bits * in the inclusive range idx through idx + num - 1. */
sparsebit_clear_num(struct sparsebit * s,sparsebit_idx_t start,sparsebit_num_t num)1453*4882a593Smuzhiyun void sparsebit_clear_num(struct sparsebit *s,
1454*4882a593Smuzhiyun sparsebit_idx_t start, sparsebit_num_t num)
1455*4882a593Smuzhiyun {
1456*4882a593Smuzhiyun struct node *nodep, *next;
1457*4882a593Smuzhiyun unsigned int n1;
1458*4882a593Smuzhiyun sparsebit_idx_t idx;
1459*4882a593Smuzhiyun sparsebit_num_t n;
1460*4882a593Smuzhiyun sparsebit_idx_t middle_start, middle_end;
1461*4882a593Smuzhiyun
1462*4882a593Smuzhiyun assert(num > 0);
1463*4882a593Smuzhiyun assert(start + num - 1 >= start);
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun /* Leading - bits before first mask boundary */
1466*4882a593Smuzhiyun for (idx = start, n = num; n > 0 && idx % MASK_BITS != 0; idx++, n--)
1467*4882a593Smuzhiyun bit_clear(s, idx);
1468*4882a593Smuzhiyun
1469*4882a593Smuzhiyun /* Middle - bits spanning one or more entire mask */
1470*4882a593Smuzhiyun middle_start = idx;
1471*4882a593Smuzhiyun middle_end = middle_start + (n & -MASK_BITS) - 1;
1472*4882a593Smuzhiyun if (n >= MASK_BITS) {
1473*4882a593Smuzhiyun nodep = node_split(s, middle_start);
1474*4882a593Smuzhiyun
1475*4882a593Smuzhiyun /*
1476*4882a593Smuzhiyun * As needed, split just after end of middle bits.
1477*4882a593Smuzhiyun * No split needed if end of middle bits is at highest
1478*4882a593Smuzhiyun * supported bit index.
1479*4882a593Smuzhiyun */
1480*4882a593Smuzhiyun if (middle_end + 1 > middle_end)
1481*4882a593Smuzhiyun (void) node_split(s, middle_end + 1);
1482*4882a593Smuzhiyun
1483*4882a593Smuzhiyun /* Delete nodes that only describe bits within the middle. */
1484*4882a593Smuzhiyun for (next = node_next(s, nodep);
1485*4882a593Smuzhiyun next && (next->idx < middle_end);
1486*4882a593Smuzhiyun next = node_next(s, nodep)) {
1487*4882a593Smuzhiyun assert(next->idx + MASK_BITS + next->num_after - 1 <= middle_end);
1488*4882a593Smuzhiyun node_rm(s, next);
1489*4882a593Smuzhiyun next = NULL;
1490*4882a593Smuzhiyun }
1491*4882a593Smuzhiyun
1492*4882a593Smuzhiyun /* As needed clear each of the mask bits */
1493*4882a593Smuzhiyun for (n1 = 0; n1 < MASK_BITS; n1++) {
1494*4882a593Smuzhiyun if (nodep->mask & (1 << n1)) {
1495*4882a593Smuzhiyun nodep->mask &= ~(1 << n1);
1496*4882a593Smuzhiyun s->num_set--;
1497*4882a593Smuzhiyun }
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun
1500*4882a593Smuzhiyun /* Clear any bits described by num_after */
1501*4882a593Smuzhiyun s->num_set -= nodep->num_after;
1502*4882a593Smuzhiyun nodep->num_after = 0;
1503*4882a593Smuzhiyun
1504*4882a593Smuzhiyun /*
1505*4882a593Smuzhiyun * Delete the node that describes the beginning of
1506*4882a593Smuzhiyun * the middle bits and perform any allowed reductions
1507*4882a593Smuzhiyun * with the nodes prev or next of nodep.
1508*4882a593Smuzhiyun */
1509*4882a593Smuzhiyun node_reduce(s, nodep);
1510*4882a593Smuzhiyun nodep = NULL;
1511*4882a593Smuzhiyun }
1512*4882a593Smuzhiyun idx = middle_end + 1;
1513*4882a593Smuzhiyun n -= middle_end - middle_start + 1;
1514*4882a593Smuzhiyun
1515*4882a593Smuzhiyun /* Trailing - bits at and beyond last mask boundary */
1516*4882a593Smuzhiyun assert(n < MASK_BITS);
1517*4882a593Smuzhiyun for (; n > 0; idx++, n--)
1518*4882a593Smuzhiyun bit_clear(s, idx);
1519*4882a593Smuzhiyun }
1520*4882a593Smuzhiyun
1521*4882a593Smuzhiyun /* Sets the bit at the index given by idx. */
sparsebit_set(struct sparsebit * s,sparsebit_idx_t idx)1522*4882a593Smuzhiyun void sparsebit_set(struct sparsebit *s, sparsebit_idx_t idx)
1523*4882a593Smuzhiyun {
1524*4882a593Smuzhiyun sparsebit_set_num(s, idx, 1);
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun /* Clears the bit at the index given by idx. */
sparsebit_clear(struct sparsebit * s,sparsebit_idx_t idx)1528*4882a593Smuzhiyun void sparsebit_clear(struct sparsebit *s, sparsebit_idx_t idx)
1529*4882a593Smuzhiyun {
1530*4882a593Smuzhiyun sparsebit_clear_num(s, idx, 1);
1531*4882a593Smuzhiyun }
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun /* Sets the bits in the entire addressable range of the sparsebit array. */
sparsebit_set_all(struct sparsebit * s)1534*4882a593Smuzhiyun void sparsebit_set_all(struct sparsebit *s)
1535*4882a593Smuzhiyun {
1536*4882a593Smuzhiyun sparsebit_set(s, 0);
1537*4882a593Smuzhiyun sparsebit_set_num(s, 1, ~(sparsebit_idx_t) 0);
1538*4882a593Smuzhiyun assert(sparsebit_all_set(s));
1539*4882a593Smuzhiyun }
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun /* Clears the bits in the entire addressable range of the sparsebit array. */
sparsebit_clear_all(struct sparsebit * s)1542*4882a593Smuzhiyun void sparsebit_clear_all(struct sparsebit *s)
1543*4882a593Smuzhiyun {
1544*4882a593Smuzhiyun sparsebit_clear(s, 0);
1545*4882a593Smuzhiyun sparsebit_clear_num(s, 1, ~(sparsebit_idx_t) 0);
1546*4882a593Smuzhiyun assert(!sparsebit_any_set(s));
1547*4882a593Smuzhiyun }
1548*4882a593Smuzhiyun
display_range(FILE * stream,sparsebit_idx_t low,sparsebit_idx_t high,bool prepend_comma_space)1549*4882a593Smuzhiyun static size_t display_range(FILE *stream, sparsebit_idx_t low,
1550*4882a593Smuzhiyun sparsebit_idx_t high, bool prepend_comma_space)
1551*4882a593Smuzhiyun {
1552*4882a593Smuzhiyun char *fmt_str;
1553*4882a593Smuzhiyun size_t sz;
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun /* Determine the printf format string */
1556*4882a593Smuzhiyun if (low == high)
1557*4882a593Smuzhiyun fmt_str = prepend_comma_space ? ", 0x%lx" : "0x%lx";
1558*4882a593Smuzhiyun else
1559*4882a593Smuzhiyun fmt_str = prepend_comma_space ? ", 0x%lx:0x%lx" : "0x%lx:0x%lx";
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun /*
1562*4882a593Smuzhiyun * When stream is NULL, just determine the size of what would
1563*4882a593Smuzhiyun * have been printed, else print the range.
1564*4882a593Smuzhiyun */
1565*4882a593Smuzhiyun if (!stream)
1566*4882a593Smuzhiyun sz = snprintf(NULL, 0, fmt_str, low, high);
1567*4882a593Smuzhiyun else
1568*4882a593Smuzhiyun sz = fprintf(stream, fmt_str, low, high);
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun return sz;
1571*4882a593Smuzhiyun }
1572*4882a593Smuzhiyun
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun /* Dumps to the FILE stream given by stream, the bit settings
1575*4882a593Smuzhiyun * of s. Each line of output is prefixed with the number of
1576*4882a593Smuzhiyun * spaces given by indent. The length of each line is implementation
1577*4882a593Smuzhiyun * dependent and does not depend on the indent amount. The following
1578*4882a593Smuzhiyun * is an example output of a sparsebit array that has bits:
1579*4882a593Smuzhiyun *
1580*4882a593Smuzhiyun * 0x5, 0x8, 0xa:0xe, 0x12
1581*4882a593Smuzhiyun *
1582*4882a593Smuzhiyun * This corresponds to a sparsebit whose bits 5, 8, 10, 11, 12, 13, 14, 18
1583*4882a593Smuzhiyun * are set. Note that a ':', instead of a '-' is used to specify a range of
1584*4882a593Smuzhiyun * contiguous bits. This is done because '-' is used to specify command-line
1585*4882a593Smuzhiyun * options, and sometimes ranges are specified as command-line arguments.
1586*4882a593Smuzhiyun */
sparsebit_dump(FILE * stream,struct sparsebit * s,unsigned int indent)1587*4882a593Smuzhiyun void sparsebit_dump(FILE *stream, struct sparsebit *s,
1588*4882a593Smuzhiyun unsigned int indent)
1589*4882a593Smuzhiyun {
1590*4882a593Smuzhiyun size_t current_line_len = 0;
1591*4882a593Smuzhiyun size_t sz;
1592*4882a593Smuzhiyun struct node *nodep;
1593*4882a593Smuzhiyun
1594*4882a593Smuzhiyun if (!sparsebit_any_set(s))
1595*4882a593Smuzhiyun return;
1596*4882a593Smuzhiyun
1597*4882a593Smuzhiyun /* Display initial indent */
1598*4882a593Smuzhiyun fprintf(stream, "%*s", indent, "");
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun /* For each node */
1601*4882a593Smuzhiyun for (nodep = node_first(s); nodep; nodep = node_next(s, nodep)) {
1602*4882a593Smuzhiyun unsigned int n1;
1603*4882a593Smuzhiyun sparsebit_idx_t low, high;
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun /* For each group of bits in the mask */
1606*4882a593Smuzhiyun for (n1 = 0; n1 < MASK_BITS; n1++) {
1607*4882a593Smuzhiyun if (nodep->mask & (1 << n1)) {
1608*4882a593Smuzhiyun low = high = nodep->idx + n1;
1609*4882a593Smuzhiyun
1610*4882a593Smuzhiyun for (; n1 < MASK_BITS; n1++) {
1611*4882a593Smuzhiyun if (nodep->mask & (1 << n1))
1612*4882a593Smuzhiyun high = nodep->idx + n1;
1613*4882a593Smuzhiyun else
1614*4882a593Smuzhiyun break;
1615*4882a593Smuzhiyun }
1616*4882a593Smuzhiyun
1617*4882a593Smuzhiyun if ((n1 == MASK_BITS) && nodep->num_after)
1618*4882a593Smuzhiyun high += nodep->num_after;
1619*4882a593Smuzhiyun
1620*4882a593Smuzhiyun /*
1621*4882a593Smuzhiyun * How much room will it take to display
1622*4882a593Smuzhiyun * this range.
1623*4882a593Smuzhiyun */
1624*4882a593Smuzhiyun sz = display_range(NULL, low, high,
1625*4882a593Smuzhiyun current_line_len != 0);
1626*4882a593Smuzhiyun
1627*4882a593Smuzhiyun /*
1628*4882a593Smuzhiyun * If there is not enough room, display
1629*4882a593Smuzhiyun * a newline plus the indent of the next
1630*4882a593Smuzhiyun * line.
1631*4882a593Smuzhiyun */
1632*4882a593Smuzhiyun if (current_line_len + sz > DUMP_LINE_MAX) {
1633*4882a593Smuzhiyun fputs("\n", stream);
1634*4882a593Smuzhiyun fprintf(stream, "%*s", indent, "");
1635*4882a593Smuzhiyun current_line_len = 0;
1636*4882a593Smuzhiyun }
1637*4882a593Smuzhiyun
1638*4882a593Smuzhiyun /* Display the range */
1639*4882a593Smuzhiyun sz = display_range(stream, low, high,
1640*4882a593Smuzhiyun current_line_len != 0);
1641*4882a593Smuzhiyun current_line_len += sz;
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun }
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun /*
1646*4882a593Smuzhiyun * If num_after and most significant-bit of mask is not
1647*4882a593Smuzhiyun * set, then still need to display a range for the bits
1648*4882a593Smuzhiyun * described by num_after.
1649*4882a593Smuzhiyun */
1650*4882a593Smuzhiyun if (!(nodep->mask & (1 << (MASK_BITS - 1))) && nodep->num_after) {
1651*4882a593Smuzhiyun low = nodep->idx + MASK_BITS;
1652*4882a593Smuzhiyun high = nodep->idx + MASK_BITS + nodep->num_after - 1;
1653*4882a593Smuzhiyun
1654*4882a593Smuzhiyun /*
1655*4882a593Smuzhiyun * How much room will it take to display
1656*4882a593Smuzhiyun * this range.
1657*4882a593Smuzhiyun */
1658*4882a593Smuzhiyun sz = display_range(NULL, low, high,
1659*4882a593Smuzhiyun current_line_len != 0);
1660*4882a593Smuzhiyun
1661*4882a593Smuzhiyun /*
1662*4882a593Smuzhiyun * If there is not enough room, display
1663*4882a593Smuzhiyun * a newline plus the indent of the next
1664*4882a593Smuzhiyun * line.
1665*4882a593Smuzhiyun */
1666*4882a593Smuzhiyun if (current_line_len + sz > DUMP_LINE_MAX) {
1667*4882a593Smuzhiyun fputs("\n", stream);
1668*4882a593Smuzhiyun fprintf(stream, "%*s", indent, "");
1669*4882a593Smuzhiyun current_line_len = 0;
1670*4882a593Smuzhiyun }
1671*4882a593Smuzhiyun
1672*4882a593Smuzhiyun /* Display the range */
1673*4882a593Smuzhiyun sz = display_range(stream, low, high,
1674*4882a593Smuzhiyun current_line_len != 0);
1675*4882a593Smuzhiyun current_line_len += sz;
1676*4882a593Smuzhiyun }
1677*4882a593Smuzhiyun }
1678*4882a593Smuzhiyun fputs("\n", stream);
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun
1681*4882a593Smuzhiyun /* Validates the internal state of the sparsebit array given by
1682*4882a593Smuzhiyun * s. On error, diagnostic information is printed to stderr and
1683*4882a593Smuzhiyun * abort is called.
1684*4882a593Smuzhiyun */
sparsebit_validate_internal(struct sparsebit * s)1685*4882a593Smuzhiyun void sparsebit_validate_internal(struct sparsebit *s)
1686*4882a593Smuzhiyun {
1687*4882a593Smuzhiyun bool error_detected = false;
1688*4882a593Smuzhiyun struct node *nodep, *prev = NULL;
1689*4882a593Smuzhiyun sparsebit_num_t total_bits_set = 0;
1690*4882a593Smuzhiyun unsigned int n1;
1691*4882a593Smuzhiyun
1692*4882a593Smuzhiyun /* For each node */
1693*4882a593Smuzhiyun for (nodep = node_first(s); nodep;
1694*4882a593Smuzhiyun prev = nodep, nodep = node_next(s, nodep)) {
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun /*
1697*4882a593Smuzhiyun * Increase total bits set by the number of bits set
1698*4882a593Smuzhiyun * in this node.
1699*4882a593Smuzhiyun */
1700*4882a593Smuzhiyun for (n1 = 0; n1 < MASK_BITS; n1++)
1701*4882a593Smuzhiyun if (nodep->mask & (1 << n1))
1702*4882a593Smuzhiyun total_bits_set++;
1703*4882a593Smuzhiyun
1704*4882a593Smuzhiyun total_bits_set += nodep->num_after;
1705*4882a593Smuzhiyun
1706*4882a593Smuzhiyun /*
1707*4882a593Smuzhiyun * Arbitrary choice as to whether a mask of 0 is allowed
1708*4882a593Smuzhiyun * or not. For diagnostic purposes it is beneficial to
1709*4882a593Smuzhiyun * have only one valid means to represent a set of bits.
1710*4882a593Smuzhiyun * To support this an arbitrary choice has been made
1711*4882a593Smuzhiyun * to not allow a mask of zero.
1712*4882a593Smuzhiyun */
1713*4882a593Smuzhiyun if (nodep->mask == 0) {
1714*4882a593Smuzhiyun fprintf(stderr, "Node mask of zero, "
1715*4882a593Smuzhiyun "nodep: %p nodep->mask: 0x%x",
1716*4882a593Smuzhiyun nodep, nodep->mask);
1717*4882a593Smuzhiyun error_detected = true;
1718*4882a593Smuzhiyun break;
1719*4882a593Smuzhiyun }
1720*4882a593Smuzhiyun
1721*4882a593Smuzhiyun /*
1722*4882a593Smuzhiyun * Validate num_after is not greater than the max index
1723*4882a593Smuzhiyun * - the number of mask bits. The num_after member
1724*4882a593Smuzhiyun * uses 0-based indexing and thus has no value that
1725*4882a593Smuzhiyun * represents all bits set. This limitation is handled
1726*4882a593Smuzhiyun * by requiring a non-zero mask. With a non-zero mask,
1727*4882a593Smuzhiyun * MASK_BITS worth of bits are described by the mask,
1728*4882a593Smuzhiyun * which makes the largest needed num_after equal to:
1729*4882a593Smuzhiyun *
1730*4882a593Smuzhiyun * (~(sparsebit_num_t) 0) - MASK_BITS + 1
1731*4882a593Smuzhiyun */
1732*4882a593Smuzhiyun if (nodep->num_after
1733*4882a593Smuzhiyun > (~(sparsebit_num_t) 0) - MASK_BITS + 1) {
1734*4882a593Smuzhiyun fprintf(stderr, "num_after too large, "
1735*4882a593Smuzhiyun "nodep: %p nodep->num_after: 0x%lx",
1736*4882a593Smuzhiyun nodep, nodep->num_after);
1737*4882a593Smuzhiyun error_detected = true;
1738*4882a593Smuzhiyun break;
1739*4882a593Smuzhiyun }
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun /* Validate node index is divisible by the mask size */
1742*4882a593Smuzhiyun if (nodep->idx % MASK_BITS) {
1743*4882a593Smuzhiyun fprintf(stderr, "Node index not divisible by "
1744*4882a593Smuzhiyun "mask size,\n"
1745*4882a593Smuzhiyun " nodep: %p nodep->idx: 0x%lx "
1746*4882a593Smuzhiyun "MASK_BITS: %lu\n",
1747*4882a593Smuzhiyun nodep, nodep->idx, MASK_BITS);
1748*4882a593Smuzhiyun error_detected = true;
1749*4882a593Smuzhiyun break;
1750*4882a593Smuzhiyun }
1751*4882a593Smuzhiyun
1752*4882a593Smuzhiyun /*
1753*4882a593Smuzhiyun * Validate bits described by node don't wrap beyond the
1754*4882a593Smuzhiyun * highest supported index.
1755*4882a593Smuzhiyun */
1756*4882a593Smuzhiyun if ((nodep->idx + MASK_BITS + nodep->num_after - 1) < nodep->idx) {
1757*4882a593Smuzhiyun fprintf(stderr, "Bits described by node wrap "
1758*4882a593Smuzhiyun "beyond highest supported index,\n"
1759*4882a593Smuzhiyun " nodep: %p nodep->idx: 0x%lx\n"
1760*4882a593Smuzhiyun " MASK_BITS: %lu nodep->num_after: 0x%lx",
1761*4882a593Smuzhiyun nodep, nodep->idx, MASK_BITS, nodep->num_after);
1762*4882a593Smuzhiyun error_detected = true;
1763*4882a593Smuzhiyun break;
1764*4882a593Smuzhiyun }
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun /* Check parent pointers. */
1767*4882a593Smuzhiyun if (nodep->left) {
1768*4882a593Smuzhiyun if (nodep->left->parent != nodep) {
1769*4882a593Smuzhiyun fprintf(stderr, "Left child parent pointer "
1770*4882a593Smuzhiyun "doesn't point to this node,\n"
1771*4882a593Smuzhiyun " nodep: %p nodep->left: %p "
1772*4882a593Smuzhiyun "nodep->left->parent: %p",
1773*4882a593Smuzhiyun nodep, nodep->left,
1774*4882a593Smuzhiyun nodep->left->parent);
1775*4882a593Smuzhiyun error_detected = true;
1776*4882a593Smuzhiyun break;
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun }
1779*4882a593Smuzhiyun
1780*4882a593Smuzhiyun if (nodep->right) {
1781*4882a593Smuzhiyun if (nodep->right->parent != nodep) {
1782*4882a593Smuzhiyun fprintf(stderr, "Right child parent pointer "
1783*4882a593Smuzhiyun "doesn't point to this node,\n"
1784*4882a593Smuzhiyun " nodep: %p nodep->right: %p "
1785*4882a593Smuzhiyun "nodep->right->parent: %p",
1786*4882a593Smuzhiyun nodep, nodep->right,
1787*4882a593Smuzhiyun nodep->right->parent);
1788*4882a593Smuzhiyun error_detected = true;
1789*4882a593Smuzhiyun break;
1790*4882a593Smuzhiyun }
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun if (!nodep->parent) {
1794*4882a593Smuzhiyun if (s->root != nodep) {
1795*4882a593Smuzhiyun fprintf(stderr, "Unexpected root node, "
1796*4882a593Smuzhiyun "s->root: %p nodep: %p",
1797*4882a593Smuzhiyun s->root, nodep);
1798*4882a593Smuzhiyun error_detected = true;
1799*4882a593Smuzhiyun break;
1800*4882a593Smuzhiyun }
1801*4882a593Smuzhiyun }
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun if (prev) {
1804*4882a593Smuzhiyun /*
1805*4882a593Smuzhiyun * Is index of previous node before index of
1806*4882a593Smuzhiyun * current node?
1807*4882a593Smuzhiyun */
1808*4882a593Smuzhiyun if (prev->idx >= nodep->idx) {
1809*4882a593Smuzhiyun fprintf(stderr, "Previous node index "
1810*4882a593Smuzhiyun ">= current node index,\n"
1811*4882a593Smuzhiyun " prev: %p prev->idx: 0x%lx\n"
1812*4882a593Smuzhiyun " nodep: %p nodep->idx: 0x%lx",
1813*4882a593Smuzhiyun prev, prev->idx, nodep, nodep->idx);
1814*4882a593Smuzhiyun error_detected = true;
1815*4882a593Smuzhiyun break;
1816*4882a593Smuzhiyun }
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun /*
1819*4882a593Smuzhiyun * Nodes occur in asscending order, based on each
1820*4882a593Smuzhiyun * nodes starting index.
1821*4882a593Smuzhiyun */
1822*4882a593Smuzhiyun if ((prev->idx + MASK_BITS + prev->num_after - 1)
1823*4882a593Smuzhiyun >= nodep->idx) {
1824*4882a593Smuzhiyun fprintf(stderr, "Previous node bit range "
1825*4882a593Smuzhiyun "overlap with current node bit range,\n"
1826*4882a593Smuzhiyun " prev: %p prev->idx: 0x%lx "
1827*4882a593Smuzhiyun "prev->num_after: 0x%lx\n"
1828*4882a593Smuzhiyun " nodep: %p nodep->idx: 0x%lx "
1829*4882a593Smuzhiyun "nodep->num_after: 0x%lx\n"
1830*4882a593Smuzhiyun " MASK_BITS: %lu",
1831*4882a593Smuzhiyun prev, prev->idx, prev->num_after,
1832*4882a593Smuzhiyun nodep, nodep->idx, nodep->num_after,
1833*4882a593Smuzhiyun MASK_BITS);
1834*4882a593Smuzhiyun error_detected = true;
1835*4882a593Smuzhiyun break;
1836*4882a593Smuzhiyun }
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun /*
1839*4882a593Smuzhiyun * When the node has all mask bits set, it shouldn't
1840*4882a593Smuzhiyun * be adjacent to the last bit described by the
1841*4882a593Smuzhiyun * previous node.
1842*4882a593Smuzhiyun */
1843*4882a593Smuzhiyun if (nodep->mask == ~(mask_t) 0 &&
1844*4882a593Smuzhiyun prev->idx + MASK_BITS + prev->num_after == nodep->idx) {
1845*4882a593Smuzhiyun fprintf(stderr, "Current node has mask with "
1846*4882a593Smuzhiyun "all bits set and is adjacent to the "
1847*4882a593Smuzhiyun "previous node,\n"
1848*4882a593Smuzhiyun " prev: %p prev->idx: 0x%lx "
1849*4882a593Smuzhiyun "prev->num_after: 0x%lx\n"
1850*4882a593Smuzhiyun " nodep: %p nodep->idx: 0x%lx "
1851*4882a593Smuzhiyun "nodep->num_after: 0x%lx\n"
1852*4882a593Smuzhiyun " MASK_BITS: %lu",
1853*4882a593Smuzhiyun prev, prev->idx, prev->num_after,
1854*4882a593Smuzhiyun nodep, nodep->idx, nodep->num_after,
1855*4882a593Smuzhiyun MASK_BITS);
1856*4882a593Smuzhiyun
1857*4882a593Smuzhiyun error_detected = true;
1858*4882a593Smuzhiyun break;
1859*4882a593Smuzhiyun }
1860*4882a593Smuzhiyun }
1861*4882a593Smuzhiyun }
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun if (!error_detected) {
1864*4882a593Smuzhiyun /*
1865*4882a593Smuzhiyun * Is sum of bits set in each node equal to the count
1866*4882a593Smuzhiyun * of total bits set.
1867*4882a593Smuzhiyun */
1868*4882a593Smuzhiyun if (s->num_set != total_bits_set) {
1869*4882a593Smuzhiyun fprintf(stderr, "Number of bits set missmatch,\n"
1870*4882a593Smuzhiyun " s->num_set: 0x%lx total_bits_set: 0x%lx",
1871*4882a593Smuzhiyun s->num_set, total_bits_set);
1872*4882a593Smuzhiyun
1873*4882a593Smuzhiyun error_detected = true;
1874*4882a593Smuzhiyun }
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun
1877*4882a593Smuzhiyun if (error_detected) {
1878*4882a593Smuzhiyun fputs(" dump_internal:\n", stderr);
1879*4882a593Smuzhiyun sparsebit_dump_internal(stderr, s, 4);
1880*4882a593Smuzhiyun abort();
1881*4882a593Smuzhiyun }
1882*4882a593Smuzhiyun }
1883*4882a593Smuzhiyun
1884*4882a593Smuzhiyun
1885*4882a593Smuzhiyun #ifdef FUZZ
1886*4882a593Smuzhiyun /* A simple but effective fuzzing driver. Look for bugs with the help
1887*4882a593Smuzhiyun * of some invariants and of a trivial representation of sparsebit.
1888*4882a593Smuzhiyun * Just use 512 bytes of /dev/zero and /dev/urandom as inputs, and let
1889*4882a593Smuzhiyun * afl-fuzz do the magic. :)
1890*4882a593Smuzhiyun */
1891*4882a593Smuzhiyun
1892*4882a593Smuzhiyun #include <stdlib.h>
1893*4882a593Smuzhiyun #include <assert.h>
1894*4882a593Smuzhiyun
1895*4882a593Smuzhiyun struct range {
1896*4882a593Smuzhiyun sparsebit_idx_t first, last;
1897*4882a593Smuzhiyun bool set;
1898*4882a593Smuzhiyun };
1899*4882a593Smuzhiyun
1900*4882a593Smuzhiyun struct sparsebit *s;
1901*4882a593Smuzhiyun struct range ranges[1000];
1902*4882a593Smuzhiyun int num_ranges;
1903*4882a593Smuzhiyun
get_value(sparsebit_idx_t idx)1904*4882a593Smuzhiyun static bool get_value(sparsebit_idx_t idx)
1905*4882a593Smuzhiyun {
1906*4882a593Smuzhiyun int i;
1907*4882a593Smuzhiyun
1908*4882a593Smuzhiyun for (i = num_ranges; --i >= 0; )
1909*4882a593Smuzhiyun if (ranges[i].first <= idx && idx <= ranges[i].last)
1910*4882a593Smuzhiyun return ranges[i].set;
1911*4882a593Smuzhiyun
1912*4882a593Smuzhiyun return false;
1913*4882a593Smuzhiyun }
1914*4882a593Smuzhiyun
operate(int code,sparsebit_idx_t first,sparsebit_idx_t last)1915*4882a593Smuzhiyun static void operate(int code, sparsebit_idx_t first, sparsebit_idx_t last)
1916*4882a593Smuzhiyun {
1917*4882a593Smuzhiyun sparsebit_num_t num;
1918*4882a593Smuzhiyun sparsebit_idx_t next;
1919*4882a593Smuzhiyun
1920*4882a593Smuzhiyun if (first < last) {
1921*4882a593Smuzhiyun num = last - first + 1;
1922*4882a593Smuzhiyun } else {
1923*4882a593Smuzhiyun num = first - last + 1;
1924*4882a593Smuzhiyun first = last;
1925*4882a593Smuzhiyun last = first + num - 1;
1926*4882a593Smuzhiyun }
1927*4882a593Smuzhiyun
1928*4882a593Smuzhiyun switch (code) {
1929*4882a593Smuzhiyun case 0:
1930*4882a593Smuzhiyun sparsebit_set(s, first);
1931*4882a593Smuzhiyun assert(sparsebit_is_set(s, first));
1932*4882a593Smuzhiyun assert(!sparsebit_is_clear(s, first));
1933*4882a593Smuzhiyun assert(sparsebit_any_set(s));
1934*4882a593Smuzhiyun assert(!sparsebit_all_clear(s));
1935*4882a593Smuzhiyun if (get_value(first))
1936*4882a593Smuzhiyun return;
1937*4882a593Smuzhiyun if (num_ranges == 1000)
1938*4882a593Smuzhiyun exit(0);
1939*4882a593Smuzhiyun ranges[num_ranges++] = (struct range)
1940*4882a593Smuzhiyun { .first = first, .last = first, .set = true };
1941*4882a593Smuzhiyun break;
1942*4882a593Smuzhiyun case 1:
1943*4882a593Smuzhiyun sparsebit_clear(s, first);
1944*4882a593Smuzhiyun assert(!sparsebit_is_set(s, first));
1945*4882a593Smuzhiyun assert(sparsebit_is_clear(s, first));
1946*4882a593Smuzhiyun assert(sparsebit_any_clear(s));
1947*4882a593Smuzhiyun assert(!sparsebit_all_set(s));
1948*4882a593Smuzhiyun if (!get_value(first))
1949*4882a593Smuzhiyun return;
1950*4882a593Smuzhiyun if (num_ranges == 1000)
1951*4882a593Smuzhiyun exit(0);
1952*4882a593Smuzhiyun ranges[num_ranges++] = (struct range)
1953*4882a593Smuzhiyun { .first = first, .last = first, .set = false };
1954*4882a593Smuzhiyun break;
1955*4882a593Smuzhiyun case 2:
1956*4882a593Smuzhiyun assert(sparsebit_is_set(s, first) == get_value(first));
1957*4882a593Smuzhiyun assert(sparsebit_is_clear(s, first) == !get_value(first));
1958*4882a593Smuzhiyun break;
1959*4882a593Smuzhiyun case 3:
1960*4882a593Smuzhiyun if (sparsebit_any_set(s))
1961*4882a593Smuzhiyun assert(get_value(sparsebit_first_set(s)));
1962*4882a593Smuzhiyun if (sparsebit_any_clear(s))
1963*4882a593Smuzhiyun assert(!get_value(sparsebit_first_clear(s)));
1964*4882a593Smuzhiyun sparsebit_set_all(s);
1965*4882a593Smuzhiyun assert(!sparsebit_any_clear(s));
1966*4882a593Smuzhiyun assert(sparsebit_all_set(s));
1967*4882a593Smuzhiyun num_ranges = 0;
1968*4882a593Smuzhiyun ranges[num_ranges++] = (struct range)
1969*4882a593Smuzhiyun { .first = 0, .last = ~(sparsebit_idx_t)0, .set = true };
1970*4882a593Smuzhiyun break;
1971*4882a593Smuzhiyun case 4:
1972*4882a593Smuzhiyun if (sparsebit_any_set(s))
1973*4882a593Smuzhiyun assert(get_value(sparsebit_first_set(s)));
1974*4882a593Smuzhiyun if (sparsebit_any_clear(s))
1975*4882a593Smuzhiyun assert(!get_value(sparsebit_first_clear(s)));
1976*4882a593Smuzhiyun sparsebit_clear_all(s);
1977*4882a593Smuzhiyun assert(!sparsebit_any_set(s));
1978*4882a593Smuzhiyun assert(sparsebit_all_clear(s));
1979*4882a593Smuzhiyun num_ranges = 0;
1980*4882a593Smuzhiyun break;
1981*4882a593Smuzhiyun case 5:
1982*4882a593Smuzhiyun next = sparsebit_next_set(s, first);
1983*4882a593Smuzhiyun assert(next == 0 || next > first);
1984*4882a593Smuzhiyun assert(next == 0 || get_value(next));
1985*4882a593Smuzhiyun break;
1986*4882a593Smuzhiyun case 6:
1987*4882a593Smuzhiyun next = sparsebit_next_clear(s, first);
1988*4882a593Smuzhiyun assert(next == 0 || next > first);
1989*4882a593Smuzhiyun assert(next == 0 || !get_value(next));
1990*4882a593Smuzhiyun break;
1991*4882a593Smuzhiyun case 7:
1992*4882a593Smuzhiyun next = sparsebit_next_clear(s, first);
1993*4882a593Smuzhiyun if (sparsebit_is_set_num(s, first, num)) {
1994*4882a593Smuzhiyun assert(next == 0 || next > last);
1995*4882a593Smuzhiyun if (first)
1996*4882a593Smuzhiyun next = sparsebit_next_set(s, first - 1);
1997*4882a593Smuzhiyun else if (sparsebit_any_set(s))
1998*4882a593Smuzhiyun next = sparsebit_first_set(s);
1999*4882a593Smuzhiyun else
2000*4882a593Smuzhiyun return;
2001*4882a593Smuzhiyun assert(next == first);
2002*4882a593Smuzhiyun } else {
2003*4882a593Smuzhiyun assert(sparsebit_is_clear(s, first) || next <= last);
2004*4882a593Smuzhiyun }
2005*4882a593Smuzhiyun break;
2006*4882a593Smuzhiyun case 8:
2007*4882a593Smuzhiyun next = sparsebit_next_set(s, first);
2008*4882a593Smuzhiyun if (sparsebit_is_clear_num(s, first, num)) {
2009*4882a593Smuzhiyun assert(next == 0 || next > last);
2010*4882a593Smuzhiyun if (first)
2011*4882a593Smuzhiyun next = sparsebit_next_clear(s, first - 1);
2012*4882a593Smuzhiyun else if (sparsebit_any_clear(s))
2013*4882a593Smuzhiyun next = sparsebit_first_clear(s);
2014*4882a593Smuzhiyun else
2015*4882a593Smuzhiyun return;
2016*4882a593Smuzhiyun assert(next == first);
2017*4882a593Smuzhiyun } else {
2018*4882a593Smuzhiyun assert(sparsebit_is_set(s, first) || next <= last);
2019*4882a593Smuzhiyun }
2020*4882a593Smuzhiyun break;
2021*4882a593Smuzhiyun case 9:
2022*4882a593Smuzhiyun sparsebit_set_num(s, first, num);
2023*4882a593Smuzhiyun assert(sparsebit_is_set_num(s, first, num));
2024*4882a593Smuzhiyun assert(!sparsebit_is_clear_num(s, first, num));
2025*4882a593Smuzhiyun assert(sparsebit_any_set(s));
2026*4882a593Smuzhiyun assert(!sparsebit_all_clear(s));
2027*4882a593Smuzhiyun if (num_ranges == 1000)
2028*4882a593Smuzhiyun exit(0);
2029*4882a593Smuzhiyun ranges[num_ranges++] = (struct range)
2030*4882a593Smuzhiyun { .first = first, .last = last, .set = true };
2031*4882a593Smuzhiyun break;
2032*4882a593Smuzhiyun case 10:
2033*4882a593Smuzhiyun sparsebit_clear_num(s, first, num);
2034*4882a593Smuzhiyun assert(!sparsebit_is_set_num(s, first, num));
2035*4882a593Smuzhiyun assert(sparsebit_is_clear_num(s, first, num));
2036*4882a593Smuzhiyun assert(sparsebit_any_clear(s));
2037*4882a593Smuzhiyun assert(!sparsebit_all_set(s));
2038*4882a593Smuzhiyun if (num_ranges == 1000)
2039*4882a593Smuzhiyun exit(0);
2040*4882a593Smuzhiyun ranges[num_ranges++] = (struct range)
2041*4882a593Smuzhiyun { .first = first, .last = last, .set = false };
2042*4882a593Smuzhiyun break;
2043*4882a593Smuzhiyun case 11:
2044*4882a593Smuzhiyun sparsebit_validate_internal(s);
2045*4882a593Smuzhiyun break;
2046*4882a593Smuzhiyun default:
2047*4882a593Smuzhiyun break;
2048*4882a593Smuzhiyun }
2049*4882a593Smuzhiyun }
2050*4882a593Smuzhiyun
get8(void)2051*4882a593Smuzhiyun unsigned char get8(void)
2052*4882a593Smuzhiyun {
2053*4882a593Smuzhiyun int ch;
2054*4882a593Smuzhiyun
2055*4882a593Smuzhiyun ch = getchar();
2056*4882a593Smuzhiyun if (ch == EOF)
2057*4882a593Smuzhiyun exit(0);
2058*4882a593Smuzhiyun return ch;
2059*4882a593Smuzhiyun }
2060*4882a593Smuzhiyun
get64(void)2061*4882a593Smuzhiyun uint64_t get64(void)
2062*4882a593Smuzhiyun {
2063*4882a593Smuzhiyun uint64_t x;
2064*4882a593Smuzhiyun
2065*4882a593Smuzhiyun x = get8();
2066*4882a593Smuzhiyun x = (x << 8) | get8();
2067*4882a593Smuzhiyun x = (x << 8) | get8();
2068*4882a593Smuzhiyun x = (x << 8) | get8();
2069*4882a593Smuzhiyun x = (x << 8) | get8();
2070*4882a593Smuzhiyun x = (x << 8) | get8();
2071*4882a593Smuzhiyun x = (x << 8) | get8();
2072*4882a593Smuzhiyun return (x << 8) | get8();
2073*4882a593Smuzhiyun }
2074*4882a593Smuzhiyun
main(void)2075*4882a593Smuzhiyun int main(void)
2076*4882a593Smuzhiyun {
2077*4882a593Smuzhiyun s = sparsebit_alloc();
2078*4882a593Smuzhiyun for (;;) {
2079*4882a593Smuzhiyun uint8_t op = get8() & 0xf;
2080*4882a593Smuzhiyun uint64_t first = get64();
2081*4882a593Smuzhiyun uint64_t last = get64();
2082*4882a593Smuzhiyun
2083*4882a593Smuzhiyun operate(op, first, last);
2084*4882a593Smuzhiyun }
2085*4882a593Smuzhiyun }
2086*4882a593Smuzhiyun #endif
2087