vector.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /* Variable array bitsets.
  2. Copyright (C) 2002-2006, 2009-2015, 2018-2020 Free Software Foundation, Inc.
  3. Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "bitset/vector.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "xalloc.h"
  19. /* This file implements variable size bitsets stored as a variable
  20. length array of words. Any unused bits in the last word must be
  21. zero.
  22. Note that binary or ternary operations assume that each bitset operand
  23. has the same size.
  24. */
  25. static void vbitset_unused_clear (bitset);
  26. static void vbitset_set (bitset, bitset_bindex);
  27. static void vbitset_reset (bitset, bitset_bindex);
  28. static bool vbitset_test (bitset, bitset_bindex);
  29. static bitset_bindex vbitset_list (bitset, bitset_bindex *,
  30. bitset_bindex, bitset_bindex *);
  31. static bitset_bindex vbitset_list_reverse (bitset, bitset_bindex *,
  32. bitset_bindex, bitset_bindex *);
  33. #define VBITSET_N_WORDS(N) (((N) + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
  34. #define VBITSET_WORDS(X) ((X)->b.cdata)
  35. #define VBITSET_SIZE(X) ((X)->b.csize)
  36. #define VBITSET_ASIZE(X) ((X)->v.size)
  37. #undef min
  38. #undef max
  39. #define min(a, b) ((a) > (b) ? (b) : (a))
  40. #define max(a, b) ((a) > (b) ? (a) : (b))
  41. static bitset_bindex
  42. vbitset_resize (bitset src, bitset_bindex n_bits)
  43. {
  44. if (n_bits == BITSET_NBITS_ (src))
  45. return n_bits;
  46. bitset_windex oldsize = VBITSET_SIZE (src);
  47. bitset_windex newsize = VBITSET_N_WORDS (n_bits);
  48. if (oldsize < newsize)
  49. {
  50. /* The bitset needs to grow. If we already have enough memory
  51. allocated, then just zero what we need. */
  52. if (newsize > VBITSET_ASIZE (src))
  53. {
  54. /* We need to allocate more memory. When oldsize is
  55. non-zero this means that we are changing the size, so
  56. grow the bitset 25% larger than requested to reduce
  57. number of reallocations. */
  58. bitset_windex size = oldsize == 0 ? newsize : newsize + newsize / 4;
  59. VBITSET_WORDS (src)
  60. = xrealloc (VBITSET_WORDS (src), size * sizeof (bitset_word));
  61. VBITSET_ASIZE (src) = size;
  62. }
  63. memset (VBITSET_WORDS (src) + oldsize, 0,
  64. (newsize - oldsize) * sizeof (bitset_word));
  65. }
  66. else
  67. {
  68. /* The bitset needs to shrink. There's no point deallocating
  69. the memory unless it is shrinking by a reasonable amount. */
  70. if ((oldsize - newsize) >= oldsize / 2)
  71. {
  72. void *p
  73. = realloc (VBITSET_WORDS (src), newsize * sizeof (bitset_word));
  74. if (p)
  75. {
  76. VBITSET_WORDS (src) = p;
  77. VBITSET_ASIZE (src) = newsize;
  78. }
  79. }
  80. /* Need to prune any excess bits. FIXME. */
  81. }
  82. VBITSET_SIZE (src) = newsize;
  83. BITSET_NBITS_ (src) = n_bits;
  84. return n_bits;
  85. }
  86. /* Set bit BITNO in bitset DST. */
  87. static void
  88. vbitset_set (bitset dst, bitset_bindex bitno)
  89. {
  90. bitset_windex windex = bitno / BITSET_WORD_BITS;
  91. /* Perhaps we should abort. The user should explicitly call
  92. bitset_resize since this will not catch the case when we set a
  93. bit larger than the current size but smaller than the allocated
  94. size. */
  95. vbitset_resize (dst, bitno);
  96. dst->b.cdata[windex - dst->b.cindex] |=
  97. (bitset_word) 1 << (bitno % BITSET_WORD_BITS);
  98. }
  99. /* Reset bit BITNO in bitset DST. */
  100. static void
  101. vbitset_reset (bitset dst MAYBE_UNUSED, bitset_bindex bitno MAYBE_UNUSED)
  102. {
  103. /* We must be accessing outside the cache so the bit is
  104. zero anyway. */
  105. }
  106. /* Test bit BITNO in bitset SRC. */
  107. static bool
  108. vbitset_test (bitset src MAYBE_UNUSED,
  109. bitset_bindex bitno MAYBE_UNUSED)
  110. {
  111. /* We must be accessing outside the cache so the bit is
  112. zero anyway. */
  113. return false;
  114. }
  115. /* Find list of up to NUM bits set in BSET in reverse order, starting
  116. from and including NEXT and store in array LIST. Return with
  117. actual number of bits found and with *NEXT indicating where search
  118. stopped. */
  119. static bitset_bindex
  120. vbitset_list_reverse (bitset src, bitset_bindex *list,
  121. bitset_bindex num, bitset_bindex *next)
  122. {
  123. bitset_word *srcp = VBITSET_WORDS (src);
  124. bitset_bindex n_bits = BITSET_SIZE_ (src);
  125. bitset_bindex rbitno = *next;
  126. /* If num is 1, we could speed things up with a binary search
  127. of the word of interest. */
  128. if (rbitno >= n_bits)
  129. return 0;
  130. bitset_bindex count = 0;
  131. bitset_bindex bitno = n_bits - (rbitno + 1);
  132. bitset_windex windex = bitno / BITSET_WORD_BITS;
  133. unsigned bitcnt = bitno % BITSET_WORD_BITS;
  134. bitset_bindex bitoff = windex * BITSET_WORD_BITS;
  135. do
  136. {
  137. bitset_word word = srcp[windex] << (BITSET_WORD_BITS - 1 - bitcnt);
  138. for (; word; bitcnt--)
  139. {
  140. if (word & BITSET_MSB)
  141. {
  142. list[count++] = bitoff + bitcnt;
  143. if (count >= num)
  144. {
  145. *next = n_bits - (bitoff + bitcnt);
  146. return count;
  147. }
  148. }
  149. word <<= 1;
  150. }
  151. bitoff -= BITSET_WORD_BITS;
  152. bitcnt = BITSET_WORD_BITS - 1;
  153. }
  154. while (windex--);
  155. *next = n_bits - (bitoff + 1);
  156. return count;
  157. }
  158. /* Find list of up to NUM bits set in BSET starting from and including
  159. *NEXT and store in array LIST. Return with actual number of bits
  160. found and with *NEXT indicating where search stopped. */
  161. static bitset_bindex
  162. vbitset_list (bitset src, bitset_bindex *list,
  163. bitset_bindex num, bitset_bindex *next)
  164. {
  165. bitset_windex size = VBITSET_SIZE (src);
  166. bitset_word *srcp = VBITSET_WORDS (src);
  167. bitset_bindex bitno = *next;
  168. bitset_bindex count = 0;
  169. bitset_windex windex;
  170. bitset_bindex bitoff;
  171. bitset_word word;
  172. if (!bitno)
  173. {
  174. /* Many bitsets are zero, so make this common case fast. */
  175. for (windex = 0; windex < size && !srcp[windex]; windex++)
  176. continue;
  177. if (windex >= size)
  178. return 0;
  179. /* If num is 1, we could speed things up with a binary search
  180. of the current word. */
  181. bitoff = windex * BITSET_WORD_BITS;
  182. }
  183. else
  184. {
  185. if (bitno >= BITSET_SIZE_ (src))
  186. return 0;
  187. windex = bitno / BITSET_WORD_BITS;
  188. bitno = bitno % BITSET_WORD_BITS;
  189. if (bitno)
  190. {
  191. /* Handle the case where we start within a word.
  192. Most often, this is executed with large bitsets
  193. with many set bits where we filled the array
  194. on the previous call to this function. */
  195. bitoff = windex * BITSET_WORD_BITS;
  196. word = srcp[windex] >> bitno;
  197. for (bitno = bitoff + bitno; word; bitno++)
  198. {
  199. if (word & 1)
  200. {
  201. list[count++] = bitno;
  202. if (count >= num)
  203. {
  204. *next = bitno + 1;
  205. return count;
  206. }
  207. }
  208. word >>= 1;
  209. }
  210. windex++;
  211. }
  212. bitoff = windex * BITSET_WORD_BITS;
  213. }
  214. for (; windex < size; windex++, bitoff += BITSET_WORD_BITS)
  215. {
  216. if (!(word = srcp[windex]))
  217. continue;
  218. if ((count + BITSET_WORD_BITS) < num)
  219. {
  220. for (bitno = bitoff; word; bitno++)
  221. {
  222. if (word & 1)
  223. list[count++] = bitno;
  224. word >>= 1;
  225. }
  226. }
  227. else
  228. {
  229. for (bitno = bitoff; word; bitno++)
  230. {
  231. if (word & 1)
  232. {
  233. list[count++] = bitno;
  234. if (count >= num)
  235. {
  236. *next = bitno + 1;
  237. return count;
  238. }
  239. }
  240. word >>= 1;
  241. }
  242. }
  243. }
  244. *next = bitoff;
  245. return count;
  246. }
  247. /* Ensure that any unused bits within the last word are clear. */
  248. static inline void
  249. vbitset_unused_clear (bitset dst)
  250. {
  251. unsigned last_bit = BITSET_SIZE_ (dst) % BITSET_WORD_BITS;
  252. if (last_bit)
  253. VBITSET_WORDS (dst)[VBITSET_SIZE (dst) - 1] &=
  254. ((bitset_word) 1 << last_bit) - 1;
  255. }
  256. static void
  257. vbitset_ones (bitset dst)
  258. {
  259. bitset_word *dstp = VBITSET_WORDS (dst);
  260. unsigned bytes = sizeof (bitset_word) * VBITSET_SIZE (dst);
  261. memset (dstp, -1, bytes);
  262. vbitset_unused_clear (dst);
  263. }
  264. static void
  265. vbitset_zero (bitset dst)
  266. {
  267. bitset_word *dstp = VBITSET_WORDS (dst);
  268. unsigned bytes = sizeof (bitset_word) * VBITSET_SIZE (dst);
  269. memset (dstp, 0, bytes);
  270. }
  271. static bool
  272. vbitset_empty_p (bitset dst)
  273. {
  274. bitset_word *dstp = VBITSET_WORDS (dst);
  275. for (unsigned i = 0; i < VBITSET_SIZE (dst); i++)
  276. if (dstp[i])
  277. return false;
  278. return true;
  279. }
  280. static void
  281. vbitset_copy1 (bitset dst, bitset src)
  282. {
  283. if (src == dst)
  284. return;
  285. vbitset_resize (dst, BITSET_SIZE_ (src));
  286. bitset_word *srcp = VBITSET_WORDS (src);
  287. bitset_word *dstp = VBITSET_WORDS (dst);
  288. bitset_windex ssize = VBITSET_SIZE (src);
  289. bitset_windex dsize = VBITSET_SIZE (dst);
  290. memcpy (dstp, srcp, sizeof (bitset_word) * ssize);
  291. memset (dstp + sizeof (bitset_word) * ssize, 0,
  292. sizeof (bitset_word) * (dsize - ssize));
  293. }
  294. static void
  295. vbitset_not (bitset dst, bitset src)
  296. {
  297. vbitset_resize (dst, BITSET_SIZE_ (src));
  298. bitset_word *srcp = VBITSET_WORDS (src);
  299. bitset_word *dstp = VBITSET_WORDS (dst);
  300. bitset_windex ssize = VBITSET_SIZE (src);
  301. bitset_windex dsize = VBITSET_SIZE (dst);
  302. for (unsigned i = 0; i < ssize; i++)
  303. *dstp++ = ~(*srcp++);
  304. vbitset_unused_clear (dst);
  305. memset (dstp + sizeof (bitset_word) * ssize, 0,
  306. sizeof (bitset_word) * (dsize - ssize));
  307. }
  308. static bool
  309. vbitset_equal_p (bitset dst, bitset src)
  310. {
  311. bitset_word *srcp = VBITSET_WORDS (src);
  312. bitset_word *dstp = VBITSET_WORDS (dst);
  313. bitset_windex ssize = VBITSET_SIZE (src);
  314. bitset_windex dsize = VBITSET_SIZE (dst);
  315. unsigned i;
  316. for (i = 0; i < min (ssize, dsize); i++)
  317. if (*srcp++ != *dstp++)
  318. return false;
  319. if (ssize > dsize)
  320. {
  321. for (; i < ssize; i++)
  322. if (*srcp++)
  323. return false;
  324. }
  325. else
  326. {
  327. for (; i < dsize; i++)
  328. if (*dstp++)
  329. return false;
  330. }
  331. return true;
  332. }
  333. static bool
  334. vbitset_subset_p (bitset dst, bitset src)
  335. {
  336. bitset_word *srcp = VBITSET_WORDS (src);
  337. bitset_word *dstp = VBITSET_WORDS (dst);
  338. bitset_windex ssize = VBITSET_SIZE (src);
  339. bitset_windex dsize = VBITSET_SIZE (dst);
  340. unsigned i;
  341. for (i = 0; i < min (ssize, dsize); i++, dstp++, srcp++)
  342. if (*dstp != (*srcp | *dstp))
  343. return false;
  344. if (ssize > dsize)
  345. for (; i < ssize; i++)
  346. if (*srcp++)
  347. return false;
  348. return true;
  349. }
  350. static bool
  351. vbitset_disjoint_p (bitset dst, bitset src)
  352. {
  353. bitset_word *srcp = VBITSET_WORDS (src);
  354. bitset_word *dstp = VBITSET_WORDS (dst);
  355. bitset_windex ssize = VBITSET_SIZE (src);
  356. bitset_windex dsize = VBITSET_SIZE (dst);
  357. for (unsigned i = 0; i < min (ssize, dsize); i++)
  358. if (*srcp++ & *dstp++)
  359. return false;
  360. return true;
  361. }
  362. static void
  363. vbitset_and (bitset dst, bitset src1, bitset src2)
  364. {
  365. vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
  366. bitset_windex dsize = VBITSET_SIZE (dst);
  367. bitset_windex ssize1 = VBITSET_SIZE (src1);
  368. bitset_windex ssize2 = VBITSET_SIZE (src2);
  369. bitset_word *dstp = VBITSET_WORDS (dst);
  370. bitset_word *src1p = VBITSET_WORDS (src1);
  371. bitset_word *src2p = VBITSET_WORDS (src2);
  372. for (unsigned i = 0; i < min (ssize1, ssize2); i++)
  373. *dstp++ = *src1p++ & *src2p++;
  374. memset (dstp, 0, sizeof (bitset_word) * (dsize - min (ssize1, ssize2)));
  375. }
  376. static bool
  377. vbitset_and_cmp (bitset dst, bitset src1, bitset src2)
  378. {
  379. vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
  380. bitset_windex dsize = VBITSET_SIZE (dst);
  381. bitset_windex ssize1 = VBITSET_SIZE (src1);
  382. bitset_windex ssize2 = VBITSET_SIZE (src2);
  383. bitset_word *dstp = VBITSET_WORDS (dst);
  384. bitset_word *src1p = VBITSET_WORDS (src1);
  385. bitset_word *src2p = VBITSET_WORDS (src2);
  386. bool changed = false;
  387. unsigned i;
  388. for (i = 0; i < min (ssize1, ssize2); i++, dstp++)
  389. {
  390. bitset_word tmp = *src1p++ & *src2p++;
  391. if (*dstp != tmp)
  392. {
  393. changed = true;
  394. *dstp = tmp;
  395. }
  396. }
  397. if (ssize2 > ssize1)
  398. {
  399. src1p = src2p;
  400. ssize1 = ssize2;
  401. }
  402. for (; i < ssize1; i++, dstp++)
  403. {
  404. if (*dstp != 0)
  405. {
  406. changed = true;
  407. *dstp = 0;
  408. }
  409. }
  410. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize1));
  411. return changed;
  412. }
  413. static void
  414. vbitset_andn (bitset dst, bitset src1, bitset src2)
  415. {
  416. vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
  417. bitset_windex dsize = VBITSET_SIZE (dst);
  418. bitset_windex ssize1 = VBITSET_SIZE (src1);
  419. bitset_windex ssize2 = VBITSET_SIZE (src2);
  420. bitset_word *dstp = VBITSET_WORDS (dst);
  421. bitset_word *src1p = VBITSET_WORDS (src1);
  422. bitset_word *src2p = VBITSET_WORDS (src2);
  423. unsigned i;
  424. for (i = 0; i < min (ssize1, ssize2); i++)
  425. *dstp++ = *src1p++ & ~(*src2p++);
  426. if (ssize2 > ssize1)
  427. {
  428. for (; i < ssize2; i++)
  429. *dstp++ = 0;
  430. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize2));
  431. }
  432. else
  433. {
  434. for (; i < ssize1; i++)
  435. *dstp++ = *src1p++;
  436. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize1));
  437. }
  438. }
  439. static bool
  440. vbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
  441. {
  442. vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
  443. bitset_windex dsize = VBITSET_SIZE (dst);
  444. bitset_windex ssize1 = VBITSET_SIZE (src1);
  445. bitset_windex ssize2 = VBITSET_SIZE (src2);
  446. bitset_word *dstp = VBITSET_WORDS (dst);
  447. bitset_word *src1p = VBITSET_WORDS (src1);
  448. bitset_word *src2p = VBITSET_WORDS (src2);
  449. bool changed = false;
  450. unsigned i;
  451. for (i = 0; i < min (ssize1, ssize2); i++, dstp++)
  452. {
  453. bitset_word tmp = *src1p++ & ~(*src2p++);
  454. if (*dstp != tmp)
  455. {
  456. changed = true;
  457. *dstp = tmp;
  458. }
  459. }
  460. if (ssize2 > ssize1)
  461. {
  462. for (; i < ssize2; i++, dstp++)
  463. {
  464. if (*dstp != 0)
  465. {
  466. changed = true;
  467. *dstp = 0;
  468. }
  469. }
  470. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize2));
  471. }
  472. else
  473. {
  474. for (; i < ssize1; i++, dstp++)
  475. {
  476. bitset_word tmp = *src1p++;
  477. if (*dstp != tmp)
  478. {
  479. changed = true;
  480. *dstp = tmp;
  481. }
  482. }
  483. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize1));
  484. }
  485. return changed;
  486. }
  487. static void
  488. vbitset_or (bitset dst, bitset src1, bitset src2)
  489. {
  490. vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
  491. bitset_windex dsize = VBITSET_SIZE (dst);
  492. bitset_windex ssize1 = VBITSET_SIZE (src1);
  493. bitset_windex ssize2 = VBITSET_SIZE (src2);
  494. bitset_word *dstp = VBITSET_WORDS (dst);
  495. bitset_word *src1p = VBITSET_WORDS (src1);
  496. bitset_word *src2p = VBITSET_WORDS (src2);
  497. unsigned i;
  498. for (i = 0; i < min (ssize1, ssize2); i++)
  499. *dstp++ = *src1p++ | *src2p++;
  500. if (ssize2 > ssize1)
  501. {
  502. src1p = src2p;
  503. ssize1 = ssize2;
  504. }
  505. for (; i < ssize1; i++)
  506. *dstp++ = *src1p++;
  507. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize1));
  508. }
  509. static bool
  510. vbitset_or_cmp (bitset dst, bitset src1, bitset src2)
  511. {
  512. vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
  513. bitset_windex dsize = VBITSET_SIZE (dst);
  514. bitset_windex ssize1 = VBITSET_SIZE (src1);
  515. bitset_windex ssize2 = VBITSET_SIZE (src2);
  516. bitset_word *dstp = VBITSET_WORDS (dst);
  517. bitset_word *src1p = VBITSET_WORDS (src1);
  518. bitset_word *src2p = VBITSET_WORDS (src2);
  519. bool changed = false;
  520. unsigned i;
  521. for (i = 0; i < min (ssize1, ssize2); i++, dstp++)
  522. {
  523. bitset_word tmp = *src1p++ | *src2p++;
  524. if (*dstp != tmp)
  525. {
  526. changed = true;
  527. *dstp = tmp;
  528. }
  529. }
  530. if (ssize2 > ssize1)
  531. {
  532. src1p = src2p;
  533. ssize1 = ssize2;
  534. }
  535. for (; i < ssize1; i++, dstp++)
  536. {
  537. bitset_word tmp = *src1p++;
  538. if (*dstp != tmp)
  539. {
  540. changed = true;
  541. *dstp = tmp;
  542. }
  543. }
  544. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize1));
  545. return changed;
  546. }
  547. static void
  548. vbitset_xor (bitset dst, bitset src1, bitset src2)
  549. {
  550. vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
  551. bitset_windex dsize = VBITSET_SIZE (dst);
  552. bitset_windex ssize1 = VBITSET_SIZE (src1);
  553. bitset_windex ssize2 = VBITSET_SIZE (src2);
  554. bitset_word *dstp = VBITSET_WORDS (dst);
  555. bitset_word *src1p = VBITSET_WORDS (src1);
  556. bitset_word *src2p = VBITSET_WORDS (src2);
  557. unsigned i;
  558. for (i = 0; i < min (ssize1, ssize2); i++)
  559. *dstp++ = *src1p++ ^ *src2p++;
  560. if (ssize2 > ssize1)
  561. {
  562. src1p = src2p;
  563. ssize1 = ssize2;
  564. }
  565. for (; i < ssize1; i++)
  566. *dstp++ = *src1p++;
  567. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize1));
  568. }
  569. static bool
  570. vbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
  571. {
  572. vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
  573. bitset_windex dsize = VBITSET_SIZE (dst);
  574. bitset_windex ssize1 = VBITSET_SIZE (src1);
  575. bitset_windex ssize2 = VBITSET_SIZE (src2);
  576. bitset_word *dstp = VBITSET_WORDS (dst);
  577. bitset_word *src1p = VBITSET_WORDS (src1);
  578. bitset_word *src2p = VBITSET_WORDS (src2);
  579. bool changed = false;
  580. unsigned i;
  581. for (i = 0; i < min (ssize1, ssize2); i++, dstp++)
  582. {
  583. bitset_word tmp = *src1p++ ^ *src2p++;
  584. if (*dstp != tmp)
  585. {
  586. changed = true;
  587. *dstp = tmp;
  588. }
  589. }
  590. if (ssize2 > ssize1)
  591. {
  592. src1p = src2p;
  593. ssize1 = ssize2;
  594. }
  595. for (; i < ssize1; i++, dstp++)
  596. {
  597. bitset_word tmp = *src1p++;
  598. if (*dstp != tmp)
  599. {
  600. changed = true;
  601. *dstp = tmp;
  602. }
  603. }
  604. memset (dstp, 0, sizeof (bitset_word) * (dsize - ssize1));
  605. return changed;
  606. }
  607. /* FIXME, these operations need fixing for different size
  608. bitsets. */
  609. static void
  610. vbitset_and_or (bitset dst, bitset src1, bitset src2, bitset src3)
  611. {
  612. if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
  613. || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
  614. {
  615. bitset_and_or_ (dst, src1, src2, src3);
  616. return;
  617. }
  618. vbitset_resize (dst, BITSET_NBITS_ (src1));
  619. bitset_word *src1p = VBITSET_WORDS (src1);
  620. bitset_word *src2p = VBITSET_WORDS (src2);
  621. bitset_word *src3p = VBITSET_WORDS (src3);
  622. bitset_word *dstp = VBITSET_WORDS (dst);
  623. bitset_windex size = VBITSET_SIZE (dst);
  624. for (unsigned i = 0; i < size; i++)
  625. *dstp++ = (*src1p++ & *src2p++) | *src3p++;
  626. }
  627. static bool
  628. vbitset_and_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
  629. {
  630. if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
  631. || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
  632. return bitset_and_or_cmp_ (dst, src1, src2, src3);
  633. vbitset_resize (dst, BITSET_NBITS_ (src1));
  634. bitset_word *src1p = VBITSET_WORDS (src1);
  635. bitset_word *src2p = VBITSET_WORDS (src2);
  636. bitset_word *src3p = VBITSET_WORDS (src3);
  637. bitset_word *dstp = VBITSET_WORDS (dst);
  638. bitset_windex size = VBITSET_SIZE (dst);
  639. bool changed = false;
  640. for (unsigned i = 0; i < size; i++, dstp++)
  641. {
  642. bitset_word tmp = (*src1p++ & *src2p++) | *src3p++;
  643. if (*dstp != tmp)
  644. {
  645. changed = true;
  646. *dstp = tmp;
  647. }
  648. }
  649. return changed;
  650. }
  651. static void
  652. vbitset_andn_or (bitset dst, bitset src1, bitset src2, bitset src3)
  653. {
  654. if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
  655. || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
  656. {
  657. bitset_andn_or_ (dst, src1, src2, src3);
  658. return;
  659. }
  660. vbitset_resize (dst, BITSET_NBITS_ (src1));
  661. bitset_word *src1p = VBITSET_WORDS (src1);
  662. bitset_word *src2p = VBITSET_WORDS (src2);
  663. bitset_word *src3p = VBITSET_WORDS (src3);
  664. bitset_word *dstp = VBITSET_WORDS (dst);
  665. bitset_windex size = VBITSET_SIZE (dst);
  666. for (unsigned i = 0; i < size; i++)
  667. *dstp++ = (*src1p++ & ~(*src2p++)) | *src3p++;
  668. }
  669. static bool
  670. vbitset_andn_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
  671. {
  672. if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
  673. || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
  674. return bitset_andn_or_cmp_ (dst, src1, src2, src3);
  675. vbitset_resize (dst, BITSET_NBITS_ (src1));
  676. bitset_word *src1p = VBITSET_WORDS (src1);
  677. bitset_word *src2p = VBITSET_WORDS (src2);
  678. bitset_word *src3p = VBITSET_WORDS (src3);
  679. bitset_word *dstp = VBITSET_WORDS (dst);
  680. bitset_windex size = VBITSET_SIZE (dst);
  681. bool changed = false;
  682. for (unsigned i = 0; i < size; i++, dstp++)
  683. {
  684. bitset_word tmp = (*src1p++ & ~(*src2p++)) | *src3p++;
  685. if (*dstp != tmp)
  686. {
  687. changed = true;
  688. *dstp = tmp;
  689. }
  690. }
  691. return changed;
  692. }
  693. static void
  694. vbitset_or_and (bitset dst, bitset src1, bitset src2, bitset src3)
  695. {
  696. if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
  697. || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
  698. {
  699. bitset_or_and_ (dst, src1, src2, src3);
  700. return;
  701. }
  702. vbitset_resize (dst, BITSET_NBITS_ (src1));
  703. bitset_word *src1p = VBITSET_WORDS (src1);
  704. bitset_word *src2p = VBITSET_WORDS (src2);
  705. bitset_word *src3p = VBITSET_WORDS (src3);
  706. bitset_word *dstp = VBITSET_WORDS (dst);
  707. bitset_windex size = VBITSET_SIZE (dst);
  708. for (unsigned i = 0; i < size; i++)
  709. *dstp++ = (*src1p++ | *src2p++) & *src3p++;
  710. }
  711. static bool
  712. vbitset_or_and_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
  713. {
  714. if (BITSET_NBITS_ (src1) != BITSET_NBITS_ (src2)
  715. || BITSET_NBITS_ (src1) != BITSET_NBITS_ (src3))
  716. return bitset_or_and_cmp_ (dst, src1, src2, src3);
  717. vbitset_resize (dst, BITSET_NBITS_ (src1));
  718. bitset_word *src1p = VBITSET_WORDS (src1);
  719. bitset_word *src2p = VBITSET_WORDS (src2);
  720. bitset_word *src3p = VBITSET_WORDS (src3);
  721. bitset_word *dstp = VBITSET_WORDS (dst);
  722. bitset_windex size = VBITSET_SIZE (dst);
  723. bool changed = false;
  724. unsigned i;
  725. for (i = 0; i < size; i++, dstp++)
  726. {
  727. bitset_word tmp = (*src1p++ | *src2p++) & *src3p++;
  728. if (*dstp != tmp)
  729. {
  730. changed = true;
  731. *dstp = tmp;
  732. }
  733. }
  734. return changed;
  735. }
  736. static void
  737. vbitset_copy (bitset dst, bitset src)
  738. {
  739. if (BITSET_COMPATIBLE_ (dst, src))
  740. vbitset_copy1 (dst, src);
  741. else
  742. bitset_copy_ (dst, src);
  743. }
  744. static void
  745. vbitset_free (bitset bset)
  746. {
  747. free (VBITSET_WORDS (bset));
  748. }
  749. /* Vector of operations for multiple word bitsets. */
  750. struct bitset_vtable vbitset_vtable = {
  751. vbitset_set,
  752. vbitset_reset,
  753. bitset_toggle_,
  754. vbitset_test,
  755. vbitset_resize,
  756. bitset_size_,
  757. bitset_count_,
  758. vbitset_empty_p,
  759. vbitset_ones,
  760. vbitset_zero,
  761. vbitset_copy,
  762. vbitset_disjoint_p,
  763. vbitset_equal_p,
  764. vbitset_not,
  765. vbitset_subset_p,
  766. vbitset_and,
  767. vbitset_and_cmp,
  768. vbitset_andn,
  769. vbitset_andn_cmp,
  770. vbitset_or,
  771. vbitset_or_cmp,
  772. vbitset_xor,
  773. vbitset_xor_cmp,
  774. vbitset_and_or,
  775. vbitset_and_or_cmp,
  776. vbitset_andn_or,
  777. vbitset_andn_or_cmp,
  778. vbitset_or_and,
  779. vbitset_or_and_cmp,
  780. vbitset_list,
  781. vbitset_list_reverse,
  782. vbitset_free,
  783. BITSET_VECTOR
  784. };
  785. size_t
  786. vbitset_bytes (bitset_bindex n_bits MAYBE_UNUSED)
  787. {
  788. return sizeof (struct vbitset_struct);
  789. }
  790. bitset
  791. vbitset_init (bitset bset, bitset_bindex n_bits)
  792. {
  793. bset->b.vtable = &vbitset_vtable;
  794. bset->b.cindex = 0;
  795. VBITSET_SIZE (bset) = 0;
  796. vbitset_resize (bset, n_bits);
  797. return bset;
  798. }