sbitmap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /* Simple bitmaps.
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "sbitmap.h"
  19. /* This suffices for roughly 99% of the hosts we run on, and the rest
  20. don't have 256 bit integers. */
  21. #if SBITMAP_ELT_BITS > 255
  22. #error Need to increase size of datatype used for popcount
  23. #endif
  24. #if GCC_VERSION >= 3400
  25. # if SBITMAP_ELT_BITS == HOST_BITS_PER_LONG
  26. # define do_popcount(x) __builtin_popcountl (x)
  27. # elif SBITMAP_ELT_BITS == HOST_BITS_PER_LONGLONG
  28. # define do_popcount(x) __builtin_popcountll (x)
  29. # else
  30. # error "internal error: sbitmap.h and hwint.h are inconsistent"
  31. # endif
  32. #else
  33. static unsigned long sbitmap_elt_popcount (SBITMAP_ELT_TYPE);
  34. # define do_popcount(x) sbitmap_elt_popcount (x)
  35. #endif
  36. typedef SBITMAP_ELT_TYPE *sbitmap_ptr;
  37. typedef const SBITMAP_ELT_TYPE *const_sbitmap_ptr;
  38. /* Return the size in bytes of a bitmap MAP. */
  39. static inline unsigned int sbitmap_size_bytes (const_sbitmap map)
  40. {
  41. return map->size * sizeof (SBITMAP_ELT_TYPE);
  42. }
  43. /* This macro controls debugging that is as expensive as the
  44. operations it verifies. */
  45. /* #define BITMAP_DEBUGGING */
  46. #ifdef BITMAP_DEBUGGING
  47. /* Verify the population count of sbitmap A matches the cached value,
  48. if there is a cached value. */
  49. static void
  50. sbitmap_verify_popcount (const_sbitmap a)
  51. {
  52. unsigned ix;
  53. unsigned int lastword;
  54. if (!a->popcount)
  55. return;
  56. lastword = a->size;
  57. for (ix = 0; ix < lastword; ix++)
  58. gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
  59. }
  60. #endif
  61. /* Bitmap manipulation routines. */
  62. /* Allocate a simple bitmap of N_ELMS bits. */
  63. sbitmap
  64. sbitmap_alloc (unsigned int n_elms)
  65. {
  66. unsigned int bytes, size, amt;
  67. sbitmap bmap;
  68. size = SBITMAP_SET_SIZE (n_elms);
  69. bytes = size * sizeof (SBITMAP_ELT_TYPE);
  70. amt = (sizeof (struct simple_bitmap_def)
  71. + bytes - sizeof (SBITMAP_ELT_TYPE));
  72. bmap = (sbitmap) xmalloc (amt);
  73. bmap->n_bits = n_elms;
  74. bmap->size = size;
  75. bmap->popcount = NULL;
  76. return bmap;
  77. }
  78. /* Allocate a simple bitmap of N_ELMS bits, and a popcount array. */
  79. sbitmap
  80. sbitmap_alloc_with_popcount (unsigned int n_elms)
  81. {
  82. sbitmap const bmap = sbitmap_alloc (n_elms);
  83. bmap->popcount = XNEWVEC (unsigned char, bmap->size);
  84. return bmap;
  85. }
  86. /* Resize a simple bitmap BMAP to N_ELMS bits. If increasing the
  87. size of BMAP, clear the new bits to zero if the DEF argument
  88. is zero, and set them to one otherwise. */
  89. sbitmap
  90. sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
  91. {
  92. unsigned int bytes, size, amt;
  93. unsigned int last_bit;
  94. size = SBITMAP_SET_SIZE (n_elms);
  95. bytes = size * sizeof (SBITMAP_ELT_TYPE);
  96. if (bytes > sbitmap_size_bytes (bmap))
  97. {
  98. amt = (sizeof (struct simple_bitmap_def)
  99. + bytes - sizeof (SBITMAP_ELT_TYPE));
  100. bmap = (sbitmap) xrealloc (bmap, amt);
  101. if (bmap->popcount)
  102. bmap->popcount = XRESIZEVEC (unsigned char, bmap->popcount, size);
  103. }
  104. if (n_elms > bmap->n_bits)
  105. {
  106. if (def)
  107. {
  108. memset (bmap->elms + bmap->size, -1,
  109. bytes - sbitmap_size_bytes (bmap));
  110. /* Set the new bits if the original last element. */
  111. last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
  112. if (last_bit)
  113. bmap->elms[bmap->size - 1]
  114. |= ~((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
  115. /* Clear the unused bit in the new last element. */
  116. last_bit = n_elms % SBITMAP_ELT_BITS;
  117. if (last_bit)
  118. bmap->elms[size - 1]
  119. &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
  120. }
  121. else
  122. {
  123. memset (bmap->elms + bmap->size, 0,
  124. bytes - sbitmap_size_bytes (bmap));
  125. if (bmap->popcount)
  126. memset (bmap->popcount + bmap->size, 0,
  127. (size * sizeof (unsigned char))
  128. - (bmap->size * sizeof (unsigned char)));
  129. }
  130. }
  131. else if (n_elms < bmap->n_bits)
  132. {
  133. /* Clear the surplus bits in the last word. */
  134. last_bit = n_elms % SBITMAP_ELT_BITS;
  135. if (last_bit)
  136. {
  137. bmap->elms[size - 1]
  138. &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
  139. if (bmap->popcount)
  140. bmap->popcount[size - 1] = do_popcount (bmap->elms[size - 1]);
  141. }
  142. }
  143. bmap->n_bits = n_elms;
  144. bmap->size = size;
  145. return bmap;
  146. }
  147. /* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
  148. sbitmap
  149. sbitmap_realloc (sbitmap src, unsigned int n_elms)
  150. {
  151. unsigned int bytes, size, amt;
  152. sbitmap bmap;
  153. size = SBITMAP_SET_SIZE (n_elms);
  154. bytes = size * sizeof (SBITMAP_ELT_TYPE);
  155. amt = (sizeof (struct simple_bitmap_def)
  156. + bytes - sizeof (SBITMAP_ELT_TYPE));
  157. if (sbitmap_size_bytes (src) >= bytes)
  158. {
  159. src->n_bits = n_elms;
  160. return src;
  161. }
  162. bmap = (sbitmap) xrealloc (src, amt);
  163. bmap->n_bits = n_elms;
  164. bmap->size = size;
  165. return bmap;
  166. }
  167. /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
  168. sbitmap *
  169. sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
  170. {
  171. unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
  172. sbitmap *bitmap_vector;
  173. size = SBITMAP_SET_SIZE (n_elms);
  174. bytes = size * sizeof (SBITMAP_ELT_TYPE);
  175. elm_bytes = (sizeof (struct simple_bitmap_def)
  176. + bytes - sizeof (SBITMAP_ELT_TYPE));
  177. vector_bytes = n_vecs * sizeof (sbitmap *);
  178. /* Round up `vector_bytes' to account for the alignment requirements
  179. of an sbitmap. One could allocate the vector-table and set of sbitmaps
  180. separately, but that requires maintaining two pointers or creating
  181. a cover struct to hold both pointers (so our result is still just
  182. one pointer). Neither is a bad idea, but this is simpler for now. */
  183. {
  184. /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
  185. struct { char x; SBITMAP_ELT_TYPE y; } align;
  186. int alignment = (char *) & align.y - & align.x;
  187. vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
  188. }
  189. amt = vector_bytes + (n_vecs * elm_bytes);
  190. bitmap_vector = (sbitmap *) xmalloc (amt);
  191. for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
  192. {
  193. sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
  194. bitmap_vector[i] = b;
  195. b->n_bits = n_elms;
  196. b->size = size;
  197. b->popcount = NULL;
  198. }
  199. return bitmap_vector;
  200. }
  201. /* Copy sbitmap SRC to DST. */
  202. void
  203. bitmap_copy (sbitmap dst, const_sbitmap src)
  204. {
  205. memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
  206. if (dst->popcount)
  207. memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * dst->size);
  208. }
  209. /* Determine if a == b. */
  210. int
  211. bitmap_equal_p (const_sbitmap a, const_sbitmap b)
  212. {
  213. return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
  214. }
  215. /* Return true if the bitmap is empty. */
  216. bool
  217. bitmap_empty_p (const_sbitmap bmap)
  218. {
  219. unsigned int i;
  220. for (i=0; i<bmap->size; i++)
  221. if (bmap->elms[i])
  222. return false;
  223. return true;
  224. }
  225. /* Zero all elements in a bitmap. */
  226. void
  227. bitmap_clear (sbitmap bmap)
  228. {
  229. memset (bmap->elms, 0, sbitmap_size_bytes (bmap));
  230. if (bmap->popcount)
  231. memset (bmap->popcount, 0, bmap->size * sizeof (unsigned char));
  232. }
  233. /* Set all elements in a bitmap to ones. */
  234. void
  235. bitmap_ones (sbitmap bmap)
  236. {
  237. unsigned int last_bit;
  238. memset (bmap->elms, -1, sbitmap_size_bytes (bmap));
  239. if (bmap->popcount)
  240. memset (bmap->popcount, -1, bmap->size * sizeof (unsigned char));
  241. last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
  242. if (last_bit)
  243. {
  244. bmap->elms[bmap->size - 1]
  245. = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
  246. if (bmap->popcount)
  247. bmap->popcount[bmap->size - 1]
  248. = do_popcount (bmap->elms[bmap->size - 1]);
  249. }
  250. }
  251. /* Zero a vector of N_VECS bitmaps. */
  252. void
  253. bitmap_vector_clear (sbitmap *bmap, unsigned int n_vecs)
  254. {
  255. unsigned int i;
  256. for (i = 0; i < n_vecs; i++)
  257. bitmap_clear (bmap[i]);
  258. }
  259. /* Set a vector of N_VECS bitmaps to ones. */
  260. void
  261. bitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
  262. {
  263. unsigned int i;
  264. for (i = 0; i < n_vecs; i++)
  265. bitmap_ones (bmap[i]);
  266. }
  267. /* Set DST to be A union (B - C).
  268. DST = A | (B & ~C).
  269. Returns true if any change is made. */
  270. bool
  271. bitmap_ior_and_compl (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
  272. {
  273. unsigned int i, n = dst->size;
  274. sbitmap_ptr dstp = dst->elms;
  275. const_sbitmap_ptr ap = a->elms;
  276. const_sbitmap_ptr bp = b->elms;
  277. const_sbitmap_ptr cp = c->elms;
  278. SBITMAP_ELT_TYPE changed = 0;
  279. gcc_assert (!dst->popcount);
  280. for (i = 0; i < n; i++)
  281. {
  282. const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
  283. changed |= *dstp ^ tmp;
  284. *dstp++ = tmp;
  285. }
  286. return changed != 0;
  287. }
  288. /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
  289. void
  290. bitmap_not (sbitmap dst, const_sbitmap src)
  291. {
  292. unsigned int i, n = dst->size;
  293. sbitmap_ptr dstp = dst->elms;
  294. const_sbitmap_ptr srcp = src->elms;
  295. unsigned int last_bit;
  296. gcc_assert (!dst->popcount);
  297. for (i = 0; i < n; i++)
  298. *dstp++ = ~*srcp++;
  299. /* Zero all bits past n_bits, by ANDing dst with bitmap_ones. */
  300. last_bit = src->n_bits % SBITMAP_ELT_BITS;
  301. if (last_bit)
  302. dst->elms[n-1] = dst->elms[n-1]
  303. & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
  304. }
  305. /* Set the bits in DST to be the difference between the bits
  306. in A and the bits in B. i.e. dst = a & (~b). */
  307. void
  308. bitmap_and_compl (sbitmap dst, const_sbitmap a, const_sbitmap b)
  309. {
  310. unsigned int i, dst_size = dst->size;
  311. unsigned int min_size = dst->size;
  312. sbitmap_ptr dstp = dst->elms;
  313. const_sbitmap_ptr ap = a->elms;
  314. const_sbitmap_ptr bp = b->elms;
  315. gcc_assert (!dst->popcount);
  316. /* A should be at least as large as DEST, to have a defined source. */
  317. gcc_assert (a->size >= dst_size);
  318. /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
  319. only copy the subtrahend into dest. */
  320. if (b->size < min_size)
  321. min_size = b->size;
  322. for (i = 0; i < min_size; i++)
  323. *dstp++ = *ap++ & (~*bp++);
  324. /* Now fill the rest of dest from A, if B was too short.
  325. This makes sense only when destination and A differ. */
  326. if (dst != a && i != dst_size)
  327. for (; i < dst_size; i++)
  328. *dstp++ = *ap++;
  329. }
  330. /* Return true if there are any bits set in A are also set in B.
  331. Return false otherwise. */
  332. bool
  333. bitmap_intersect_p (const_sbitmap a, const_sbitmap b)
  334. {
  335. const_sbitmap_ptr ap = a->elms;
  336. const_sbitmap_ptr bp = b->elms;
  337. unsigned int i, n;
  338. n = MIN (a->size, b->size);
  339. for (i = 0; i < n; i++)
  340. if ((*ap++ & *bp++) != 0)
  341. return true;
  342. return false;
  343. }
  344. /* Set DST to be (A and B).
  345. Return nonzero if any change is made. */
  346. bool
  347. bitmap_and (sbitmap dst, const_sbitmap a, const_sbitmap b)
  348. {
  349. unsigned int i, n = dst->size;
  350. sbitmap_ptr dstp = dst->elms;
  351. const_sbitmap_ptr ap = a->elms;
  352. const_sbitmap_ptr bp = b->elms;
  353. bool has_popcount = dst->popcount != NULL;
  354. unsigned char *popcountp = dst->popcount;
  355. SBITMAP_ELT_TYPE changed = 0;
  356. for (i = 0; i < n; i++)
  357. {
  358. const SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
  359. SBITMAP_ELT_TYPE wordchanged = *dstp ^ tmp;
  360. if (has_popcount)
  361. {
  362. if (wordchanged)
  363. *popcountp = do_popcount (tmp);
  364. popcountp++;
  365. }
  366. *dstp++ = tmp;
  367. changed |= wordchanged;
  368. }
  369. #ifdef BITMAP_DEBUGGING
  370. if (has_popcount)
  371. sbitmap_verify_popcount (dst);
  372. #endif
  373. return changed != 0;
  374. }
  375. /* Set DST to be (A xor B)).
  376. Return nonzero if any change is made. */
  377. bool
  378. bitmap_xor (sbitmap dst, const_sbitmap a, const_sbitmap b)
  379. {
  380. unsigned int i, n = dst->size;
  381. sbitmap_ptr dstp = dst->elms;
  382. const_sbitmap_ptr ap = a->elms;
  383. const_sbitmap_ptr bp = b->elms;
  384. bool has_popcount = dst->popcount != NULL;
  385. unsigned char *popcountp = dst->popcount;
  386. SBITMAP_ELT_TYPE changed = 0;
  387. for (i = 0; i < n; i++)
  388. {
  389. const SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
  390. SBITMAP_ELT_TYPE wordchanged = *dstp ^ tmp;
  391. if (has_popcount)
  392. {
  393. if (wordchanged)
  394. *popcountp = do_popcount (tmp);
  395. popcountp++;
  396. }
  397. *dstp++ = tmp;
  398. changed |= wordchanged;
  399. }
  400. #ifdef BITMAP_DEBUGGING
  401. if (has_popcount)
  402. sbitmap_verify_popcount (dst);
  403. #endif
  404. return changed != 0;
  405. }
  406. /* Set DST to be (A or B)).
  407. Return nonzero if any change is made. */
  408. bool
  409. bitmap_ior (sbitmap dst, const_sbitmap a, const_sbitmap b)
  410. {
  411. unsigned int i, n = dst->size;
  412. sbitmap_ptr dstp = dst->elms;
  413. const_sbitmap_ptr ap = a->elms;
  414. const_sbitmap_ptr bp = b->elms;
  415. bool has_popcount = dst->popcount != NULL;
  416. unsigned char *popcountp = dst->popcount;
  417. SBITMAP_ELT_TYPE changed = 0;
  418. for (i = 0; i < n; i++)
  419. {
  420. const SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
  421. SBITMAP_ELT_TYPE wordchanged = *dstp ^ tmp;
  422. if (has_popcount)
  423. {
  424. if (wordchanged)
  425. *popcountp = do_popcount (tmp);
  426. popcountp++;
  427. }
  428. *dstp++ = tmp;
  429. changed |= wordchanged;
  430. }
  431. #ifdef BITMAP_DEBUGGING
  432. if (has_popcount)
  433. sbitmap_verify_popcount (dst);
  434. #endif
  435. return changed != 0;
  436. }
  437. /* Return nonzero if A is a subset of B. */
  438. bool
  439. bitmap_subset_p (const_sbitmap a, const_sbitmap b)
  440. {
  441. unsigned int i, n = a->size;
  442. const_sbitmap_ptr ap, bp;
  443. for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
  444. if ((*ap | *bp) != *bp)
  445. return false;
  446. return true;
  447. }
  448. /* Set DST to be (A or (B and C)).
  449. Return nonzero if any change is made. */
  450. bool
  451. bitmap_or_and (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
  452. {
  453. unsigned int i, n = dst->size;
  454. sbitmap_ptr dstp = dst->elms;
  455. const_sbitmap_ptr ap = a->elms;
  456. const_sbitmap_ptr bp = b->elms;
  457. const_sbitmap_ptr cp = c->elms;
  458. SBITMAP_ELT_TYPE changed = 0;
  459. gcc_assert (!dst->popcount);
  460. for (i = 0; i < n; i++)
  461. {
  462. const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
  463. changed |= *dstp ^ tmp;
  464. *dstp++ = tmp;
  465. }
  466. return changed != 0;
  467. }
  468. /* Set DST to be (A and (B or C)).
  469. Return nonzero if any change is made. */
  470. bool
  471. bitmap_and_or (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
  472. {
  473. unsigned int i, n = dst->size;
  474. sbitmap_ptr dstp = dst->elms;
  475. const_sbitmap_ptr ap = a->elms;
  476. const_sbitmap_ptr bp = b->elms;
  477. const_sbitmap_ptr cp = c->elms;
  478. SBITMAP_ELT_TYPE changed = 0;
  479. gcc_assert (!dst->popcount);
  480. for (i = 0; i < n; i++)
  481. {
  482. const SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
  483. changed |= *dstp ^ tmp;
  484. *dstp++ = tmp;
  485. }
  486. return changed != 0;
  487. }
  488. /* Return number of first bit set in the bitmap, -1 if none. */
  489. int
  490. bitmap_first_set_bit (const_sbitmap bmap)
  491. {
  492. unsigned int n = 0;
  493. sbitmap_iterator sbi;
  494. EXECUTE_IF_SET_IN_BITMAP (bmap, 0, n, sbi)
  495. return n;
  496. return -1;
  497. }
  498. /* Return number of last bit set in the bitmap, -1 if none. */
  499. int
  500. bitmap_last_set_bit (const_sbitmap bmap)
  501. {
  502. int i;
  503. const SBITMAP_ELT_TYPE *const ptr = bmap->elms;
  504. for (i = bmap->size - 1; i >= 0; i--)
  505. {
  506. const SBITMAP_ELT_TYPE word = ptr[i];
  507. if (word != 0)
  508. {
  509. unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
  510. SBITMAP_ELT_TYPE mask
  511. = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
  512. while (1)
  513. {
  514. if ((word & mask) != 0)
  515. return index;
  516. mask >>= 1;
  517. index--;
  518. }
  519. }
  520. }
  521. return -1;
  522. }
  523. void
  524. dump_bitmap (FILE *file, const_sbitmap bmap)
  525. {
  526. unsigned int i, n, j;
  527. unsigned int set_size = bmap->size;
  528. unsigned int total_bits = bmap->n_bits;
  529. fprintf (file, " ");
  530. for (i = n = 0; i < set_size && n < total_bits; i++)
  531. for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
  532. {
  533. if (n != 0 && n % 10 == 0)
  534. fprintf (file, " ");
  535. fprintf (file, "%d",
  536. (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
  537. }
  538. fprintf (file, "\n");
  539. }
  540. DEBUG_FUNCTION void
  541. debug_raw (simple_bitmap_def &ref)
  542. {
  543. dump_bitmap (stderr, &ref);
  544. }
  545. DEBUG_FUNCTION void
  546. debug_raw (simple_bitmap_def *ptr)
  547. {
  548. if (ptr)
  549. debug_raw (*ptr);
  550. else
  551. fprintf (stderr, "<nil>\n");
  552. }
  553. void
  554. dump_bitmap_file (FILE *file, const_sbitmap bmap)
  555. {
  556. unsigned int i, pos;
  557. fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
  558. for (pos = 30, i = 0; i < bmap->n_bits; i++)
  559. if (bitmap_bit_p (bmap, i))
  560. {
  561. if (pos > 70)
  562. {
  563. fprintf (file, "\n ");
  564. pos = 0;
  565. }
  566. fprintf (file, "%d ", i);
  567. pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
  568. }
  569. fprintf (file, "}\n");
  570. }
  571. DEBUG_FUNCTION void
  572. debug_bitmap (const_sbitmap bmap)
  573. {
  574. dump_bitmap_file (stderr, bmap);
  575. }
  576. DEBUG_FUNCTION void
  577. debug (simple_bitmap_def &ref)
  578. {
  579. dump_bitmap_file (stderr, &ref);
  580. }
  581. DEBUG_FUNCTION void
  582. debug (simple_bitmap_def *ptr)
  583. {
  584. if (ptr)
  585. debug (*ptr);
  586. else
  587. fprintf (stderr, "<nil>\n");
  588. }
  589. void
  590. dump_bitmap_vector (FILE *file, const char *title, const char *subtitle,
  591. sbitmap *bmaps, int n_maps)
  592. {
  593. int i;
  594. fprintf (file, "%s\n", title);
  595. for (i = 0; i < n_maps; i++)
  596. {
  597. fprintf (file, "%s %d\n", subtitle, i);
  598. dump_bitmap (file, bmaps[i]);
  599. }
  600. fprintf (file, "\n");
  601. }
  602. #if GCC_VERSION < 3400
  603. /* Table of number of set bits in a character, indexed by value of char. */
  604. static const unsigned char popcount_table[] =
  605. {
  606. 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
  607. 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
  608. 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
  609. 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
  610. 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
  611. 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
  612. 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
  613. 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
  614. };
  615. /* Count the bits in an SBITMAP element A. */
  616. static unsigned long
  617. sbitmap_elt_popcount (SBITMAP_ELT_TYPE a)
  618. {
  619. unsigned long ret = 0;
  620. unsigned i;
  621. if (a == 0)
  622. return 0;
  623. /* Just do this the table way for now */
  624. for (i = 0; i < SBITMAP_ELT_BITS; i += 8)
  625. ret += popcount_table[(a >> i) & 0xff];
  626. return ret;
  627. }
  628. #endif