sparsebit.c 58 KB

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