* decl.c (compute_array_index_type): Use type_dependent_expression_p.
[official-gcc.git] / gcc / sbitmap.c
blob6aac459f8267253f0d13a3493c5ddb3e9429b352
1 /* Simple bitmaps.
2 Copyright (C) 1999-2012 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "sbitmap.h"
25 /* This suffices for roughly 99% of the hosts we run on, and the rest
26 don't have 256 bit integers. */
27 #if SBITMAP_ELT_BITS > 255
28 #error Need to increase size of datatype used for popcount
29 #endif
31 #if GCC_VERSION >= 3400
32 # if SBITMAP_ELT_BITS == HOST_BITS_PER_LONG
33 # define do_popcount(x) __builtin_popcountl(x)
34 # elif SBITMAP_ELT_BITS == HOST_BITS_PER_LONGLONG
35 # define do_popcount(x) __builtin_popcountll(x)
36 # else
37 # error "internal error: sbitmap.h and hwint.h are inconsistent"
38 # endif
39 #else
40 static unsigned long sbitmap_elt_popcount (SBITMAP_ELT_TYPE);
41 # define do_popcount(x) sbitmap_elt_popcount((x))
42 #endif
44 typedef SBITMAP_ELT_TYPE *sbitmap_ptr;
45 typedef const SBITMAP_ELT_TYPE *const_sbitmap_ptr;
47 /* This macro controls debugging that is as expensive as the
48 operations it verifies. */
50 /* #define BITMAP_DEBUGGING */
51 #ifdef BITMAP_DEBUGGING
53 /* Verify the population count of sbitmap A matches the cached value,
54 if there is a cached value. */
56 void
57 sbitmap_verify_popcount (const_sbitmap a)
59 unsigned ix;
60 unsigned int lastword;
62 if (!a->popcount)
63 return;
65 lastword = a->size;
66 for (ix = 0; ix < lastword; ix++)
67 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
69 #endif
71 /* Bitmap manipulation routines. */
73 /* Allocate a simple bitmap of N_ELMS bits. */
75 sbitmap
76 sbitmap_alloc (unsigned int n_elms)
78 unsigned int bytes, size, amt;
79 sbitmap bmap;
81 size = SBITMAP_SET_SIZE (n_elms);
82 bytes = size * sizeof (SBITMAP_ELT_TYPE);
83 amt = (sizeof (struct simple_bitmap_def)
84 + bytes - sizeof (SBITMAP_ELT_TYPE));
85 bmap = (sbitmap) xmalloc (amt);
86 bmap->n_bits = n_elms;
87 bmap->size = size;
88 bmap->popcount = NULL;
89 return bmap;
92 /* Allocate a simple bitmap of N_ELMS bits, and a popcount array. */
94 sbitmap
95 sbitmap_alloc_with_popcount (unsigned int n_elms)
97 sbitmap const bmap = sbitmap_alloc (n_elms);
98 bmap->popcount = XNEWVEC (unsigned char, bmap->size);
99 return bmap;
102 /* Resize a simple bitmap BMAP to N_ELMS bits. If increasing the
103 size of BMAP, clear the new bits to zero if the DEF argument
104 is zero, and set them to one otherwise. */
106 sbitmap
107 sbitmap_resize (sbitmap bmap, unsigned int n_elms, int def)
109 unsigned int bytes, size, amt;
110 unsigned int last_bit;
112 size = SBITMAP_SET_SIZE (n_elms);
113 bytes = size * sizeof (SBITMAP_ELT_TYPE);
114 if (bytes > SBITMAP_SIZE_BYTES (bmap))
116 amt = (sizeof (struct simple_bitmap_def)
117 + bytes - sizeof (SBITMAP_ELT_TYPE));
118 bmap = (sbitmap) xrealloc (bmap, amt);
119 if (bmap->popcount)
120 bmap->popcount = XRESIZEVEC (unsigned char, bmap->popcount, size);
123 if (n_elms > bmap->n_bits)
125 if (def)
127 memset (bmap->elms + bmap->size, -1,
128 bytes - SBITMAP_SIZE_BYTES (bmap));
130 /* Set the new bits if the original last element. */
131 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
132 if (last_bit)
133 bmap->elms[bmap->size - 1]
134 |= ~((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
136 /* Clear the unused bit in the new last element. */
137 last_bit = n_elms % SBITMAP_ELT_BITS;
138 if (last_bit)
139 bmap->elms[size - 1]
140 &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
142 else
144 memset (bmap->elms + bmap->size, 0,
145 bytes - SBITMAP_SIZE_BYTES (bmap));
146 if (bmap->popcount)
147 memset (bmap->popcount + bmap->size, 0,
148 (size * sizeof (unsigned char))
149 - (bmap->size * sizeof (unsigned char)));
153 else if (n_elms < bmap->n_bits)
155 /* Clear the surplus bits in the last word. */
156 last_bit = n_elms % SBITMAP_ELT_BITS;
157 if (last_bit)
159 bmap->elms[size - 1]
160 &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
161 if (bmap->popcount)
162 bmap->popcount[size - 1] = do_popcount (bmap->elms[size - 1]);
166 bmap->n_bits = n_elms;
167 bmap->size = size;
168 return bmap;
171 /* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
173 sbitmap
174 sbitmap_realloc (sbitmap src, unsigned int n_elms)
176 unsigned int bytes, size, amt;
177 sbitmap bmap;
179 size = SBITMAP_SET_SIZE (n_elms);
180 bytes = size * sizeof (SBITMAP_ELT_TYPE);
181 amt = (sizeof (struct simple_bitmap_def)
182 + bytes - sizeof (SBITMAP_ELT_TYPE));
184 if (SBITMAP_SIZE_BYTES (src) >= bytes)
186 src->n_bits = n_elms;
187 return src;
190 bmap = (sbitmap) xrealloc (src, amt);
191 bmap->n_bits = n_elms;
192 bmap->size = size;
193 return bmap;
196 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
198 sbitmap *
199 sbitmap_vector_alloc (unsigned int n_vecs, unsigned int n_elms)
201 unsigned int i, bytes, offset, elm_bytes, size, amt, vector_bytes;
202 sbitmap *bitmap_vector;
204 size = SBITMAP_SET_SIZE (n_elms);
205 bytes = size * sizeof (SBITMAP_ELT_TYPE);
206 elm_bytes = (sizeof (struct simple_bitmap_def)
207 + bytes - sizeof (SBITMAP_ELT_TYPE));
208 vector_bytes = n_vecs * sizeof (sbitmap *);
210 /* Round up `vector_bytes' to account for the alignment requirements
211 of an sbitmap. One could allocate the vector-table and set of sbitmaps
212 separately, but that requires maintaining two pointers or creating
213 a cover struct to hold both pointers (so our result is still just
214 one pointer). Neither is a bad idea, but this is simpler for now. */
216 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
217 struct { char x; SBITMAP_ELT_TYPE y; } align;
218 int alignment = (char *) & align.y - & align.x;
219 vector_bytes = (vector_bytes + alignment - 1) & ~ (alignment - 1);
222 amt = vector_bytes + (n_vecs * elm_bytes);
223 bitmap_vector = (sbitmap *) xmalloc (amt);
225 for (i = 0, offset = vector_bytes; i < n_vecs; i++, offset += elm_bytes)
227 sbitmap b = (sbitmap) ((char *) bitmap_vector + offset);
229 bitmap_vector[i] = b;
230 b->n_bits = n_elms;
231 b->size = size;
232 b->popcount = NULL;
235 return bitmap_vector;
238 /* Copy sbitmap SRC to DST. */
240 void
241 sbitmap_copy (sbitmap dst, const_sbitmap src)
243 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * dst->size);
244 if (dst->popcount)
245 memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * dst->size);
248 /* Copy the first N elements of sbitmap SRC to DST. */
250 void
251 sbitmap_copy_n (sbitmap dst, const_sbitmap src, unsigned int n)
253 memcpy (dst->elms, src->elms, sizeof (SBITMAP_ELT_TYPE) * n);
254 if (dst->popcount)
255 memcpy (dst->popcount, src->popcount, sizeof (unsigned char) * n);
258 /* Determine if a == b. */
260 sbitmap_equal (const_sbitmap a, const_sbitmap b)
262 return !memcmp (a->elms, b->elms, sizeof (SBITMAP_ELT_TYPE) * a->size);
265 /* Return true if the bitmap is empty. */
267 bool
268 sbitmap_empty_p (const_sbitmap bmap)
270 unsigned int i;
271 for (i=0; i<bmap->size; i++)
272 if (bmap->elms[i])
273 return false;
275 return true;
278 /* Return false if any of the N bits are set in MAP starting at
279 START. */
281 bool
282 sbitmap_range_empty_p (const_sbitmap bmap, unsigned int start, unsigned int n)
284 unsigned int i = start / SBITMAP_ELT_BITS;
285 SBITMAP_ELT_TYPE elm;
286 unsigned int shift = start % SBITMAP_ELT_BITS;
288 gcc_assert (bmap->n_bits >= start + n);
290 elm = bmap->elms[i];
291 elm = elm >> shift;
293 if (shift + n <= SBITMAP_ELT_BITS)
295 /* The bits are totally contained in a single element. */
296 if (shift + n < SBITMAP_ELT_BITS)
297 elm &= ((1 << n) - 1);
298 return (elm == 0);
301 if (elm)
302 return false;
304 n -= SBITMAP_ELT_BITS - shift;
305 i++;
307 /* Deal with full elts. */
308 while (n >= SBITMAP_ELT_BITS)
310 if (bmap->elms[i])
311 return false;
312 i++;
313 n -= SBITMAP_ELT_BITS;
316 /* The leftover bits. */
317 if (n)
319 elm = bmap->elms[i];
320 elm &= ((1 << n) - 1);
321 return (elm == 0);
324 return true;
329 /* Zero all elements in a bitmap. */
331 void
332 sbitmap_zero (sbitmap bmap)
334 memset (bmap->elms, 0, SBITMAP_SIZE_BYTES (bmap));
335 if (bmap->popcount)
336 memset (bmap->popcount, 0, bmap->size * sizeof (unsigned char));
339 /* Set all elements in a bitmap to ones. */
341 void
342 sbitmap_ones (sbitmap bmap)
344 unsigned int last_bit;
346 memset (bmap->elms, -1, SBITMAP_SIZE_BYTES (bmap));
347 if (bmap->popcount)
348 memset (bmap->popcount, -1, bmap->size * sizeof (unsigned char));
350 last_bit = bmap->n_bits % SBITMAP_ELT_BITS;
351 if (last_bit)
353 bmap->elms[bmap->size - 1]
354 = (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit);
355 if (bmap->popcount)
356 bmap->popcount[bmap->size - 1]
357 = do_popcount (bmap->elms[bmap->size - 1]);
361 /* Zero a vector of N_VECS bitmaps. */
363 void
364 sbitmap_vector_zero (sbitmap *bmap, unsigned int n_vecs)
366 unsigned int i;
368 for (i = 0; i < n_vecs; i++)
369 sbitmap_zero (bmap[i]);
372 /* Set a vector of N_VECS bitmaps to ones. */
374 void
375 sbitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
377 unsigned int i;
379 for (i = 0; i < n_vecs; i++)
380 sbitmap_ones (bmap[i]);
383 /* Set DST to be A union (B - C).
384 DST = A | (B & ~C).
385 Returns true if any change is made. */
387 bool
388 sbitmap_union_of_diff_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
390 unsigned int i, n = dst->size;
391 sbitmap_ptr dstp = dst->elms;
392 const_sbitmap_ptr ap = a->elms;
393 const_sbitmap_ptr bp = b->elms;
394 const_sbitmap_ptr cp = c->elms;
395 SBITMAP_ELT_TYPE changed = 0;
397 gcc_assert (!dst->popcount);
399 for (i = 0; i < n; i++)
401 const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & ~*cp++);
402 changed |= *dstp ^ tmp;
403 *dstp++ = tmp;
406 return changed != 0;
409 void
410 sbitmap_union_of_diff (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
412 unsigned int i, n = dst->size;
413 sbitmap_ptr dstp = dst->elms;
414 const_sbitmap_ptr ap = a->elms;
415 const_sbitmap_ptr bp = b->elms;
416 const_sbitmap_ptr cp = c->elms;
418 gcc_assert (!dst->popcount && !a->popcount
419 && !b->popcount && !c->popcount);
421 for (i = 0; i < n; i++)
422 *dstp++ = *ap++ | (*bp++ & ~*cp++);
425 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
427 void
428 sbitmap_not (sbitmap dst, const_sbitmap src)
430 unsigned int i, n = dst->size;
431 sbitmap_ptr dstp = dst->elms;
432 const_sbitmap_ptr srcp = src->elms;
433 unsigned int last_bit;
435 gcc_assert (!dst->popcount);
437 for (i = 0; i < n; i++)
438 *dstp++ = ~*srcp++;
440 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
441 last_bit = src->n_bits % SBITMAP_ELT_BITS;
442 if (last_bit)
443 dst->elms[n-1] = dst->elms[n-1]
444 & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
447 /* Set the bits in DST to be the difference between the bits
448 in A and the bits in B. i.e. dst = a & (~b). */
450 void
451 sbitmap_difference (sbitmap dst, const_sbitmap a, const_sbitmap b)
453 unsigned int i, dst_size = dst->size;
454 unsigned int min_size = dst->size;
455 sbitmap_ptr dstp = dst->elms;
456 const_sbitmap_ptr ap = a->elms;
457 const_sbitmap_ptr bp = b->elms;
459 gcc_assert (!dst->popcount);
461 /* A should be at least as large as DEST, to have a defined source. */
462 gcc_assert (a->size >= dst_size);
463 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
464 only copy the subtrahend into dest. */
465 if (b->size < min_size)
466 min_size = b->size;
467 for (i = 0; i < min_size; i++)
468 *dstp++ = *ap++ & (~*bp++);
469 /* Now fill the rest of dest from A, if B was too short.
470 This makes sense only when destination and A differ. */
471 if (dst != a && i != dst_size)
472 for (; i < dst_size; i++)
473 *dstp++ = *ap++;
476 /* Return true if there are any bits set in A are also set in B.
477 Return false otherwise. */
479 bool
480 sbitmap_any_common_bits (const_sbitmap a, const_sbitmap b)
482 const_sbitmap_ptr ap = a->elms;
483 const_sbitmap_ptr bp = b->elms;
484 unsigned int i, n;
486 n = MIN (a->size, b->size);
487 for (i = 0; i < n; i++)
488 if ((*ap++ & *bp++) != 0)
489 return true;
491 return false;
494 /* Set DST to be (A and B).
495 Return nonzero if any change is made. */
497 bool
498 sbitmap_a_and_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
500 unsigned int i, n = dst->size;
501 sbitmap_ptr dstp = dst->elms;
502 const_sbitmap_ptr ap = a->elms;
503 const_sbitmap_ptr bp = b->elms;
504 SBITMAP_ELT_TYPE changed = 0;
506 gcc_assert (!dst->popcount);
508 for (i = 0; i < n; i++)
510 const SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
511 changed |= *dstp ^ tmp;
512 *dstp++ = tmp;
515 return changed != 0;
518 void
519 sbitmap_a_and_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
521 unsigned int i, n = dst->size;
522 sbitmap_ptr dstp = dst->elms;
523 const_sbitmap_ptr ap = a->elms;
524 const_sbitmap_ptr bp = b->elms;
525 bool has_popcount = dst->popcount != NULL;
526 unsigned char *popcountp = dst->popcount;
528 for (i = 0; i < n; i++)
530 const SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
531 if (has_popcount)
533 bool wordchanged = (*dstp ^ tmp) != 0;
534 if (wordchanged)
535 *popcountp = do_popcount (tmp);
536 popcountp++;
538 *dstp++ = tmp;
540 #ifdef BITMAP_DEBUGGING
541 if (has_popcount)
542 sbitmap_verify_popcount (dst);
543 #endif
546 /* Set DST to be (A xor B)).
547 Return nonzero if any change is made. */
549 bool
550 sbitmap_a_xor_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
552 unsigned int i, n = dst->size;
553 sbitmap_ptr dstp = dst->elms;
554 const_sbitmap_ptr ap = a->elms;
555 const_sbitmap_ptr bp = b->elms;
556 SBITMAP_ELT_TYPE changed = 0;
558 gcc_assert (!dst->popcount);
560 for (i = 0; i < n; i++)
562 const SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
563 changed |= *dstp ^ tmp;
564 *dstp++ = tmp;
567 return changed != 0;
570 void
571 sbitmap_a_xor_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
573 unsigned int i, n = dst->size;
574 sbitmap_ptr dstp = dst->elms;
575 const_sbitmap_ptr ap = a->elms;
576 const_sbitmap_ptr bp = b->elms;
577 bool has_popcount = dst->popcount != NULL;
578 unsigned char *popcountp = dst->popcount;
580 for (i = 0; i < n; i++)
582 const SBITMAP_ELT_TYPE tmp = *ap++ ^ *bp++;
583 if (has_popcount)
585 bool wordchanged = (*dstp ^ tmp) != 0;
586 if (wordchanged)
587 *popcountp = do_popcount (tmp);
588 popcountp++;
590 *dstp++ = tmp;
592 #ifdef BITMAP_DEBUGGING
593 if (has_popcount)
594 sbitmap_verify_popcount (dst);
595 #endif
598 /* Set DST to be (A or B)).
599 Return nonzero if any change is made. */
601 bool
602 sbitmap_a_or_b_cg (sbitmap dst, const_sbitmap a, const_sbitmap b)
604 unsigned int i, n = dst->size;
605 sbitmap_ptr dstp = dst->elms;
606 const_sbitmap_ptr ap = a->elms;
607 const_sbitmap_ptr bp = b->elms;
608 SBITMAP_ELT_TYPE changed = 0;
610 gcc_assert (!dst->popcount);
612 for (i = 0; i < n; i++)
614 const SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
615 changed |= *dstp ^ tmp;
616 *dstp++ = tmp;
619 return changed != 0;
622 void
623 sbitmap_a_or_b (sbitmap dst, const_sbitmap a, const_sbitmap b)
625 unsigned int i, n = dst->size;
626 sbitmap_ptr dstp = dst->elms;
627 const_sbitmap_ptr ap = a->elms;
628 const_sbitmap_ptr bp = b->elms;
629 bool has_popcount = dst->popcount != NULL;
630 unsigned char *popcountp = dst->popcount;
632 for (i = 0; i < n; i++)
634 const SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
635 if (has_popcount)
637 bool wordchanged = (*dstp ^ tmp) != 0;
638 if (wordchanged)
639 *popcountp = do_popcount (tmp);
640 popcountp++;
642 *dstp++ = tmp;
644 #ifdef BITMAP_DEBUGGING
645 if (has_popcount)
646 sbitmap_verify_popcount (dst);
647 #endif
650 /* Return nonzero if A is a subset of B. */
652 bool
653 sbitmap_a_subset_b_p (const_sbitmap a, const_sbitmap b)
655 unsigned int i, n = a->size;
656 const_sbitmap_ptr ap, bp;
658 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
659 if ((*ap | *bp) != *bp)
660 return false;
662 return true;
665 /* Set DST to be (A or (B and C)).
666 Return nonzero if any change is made. */
668 bool
669 sbitmap_a_or_b_and_c_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
671 unsigned int i, n = dst->size;
672 sbitmap_ptr dstp = dst->elms;
673 const_sbitmap_ptr ap = a->elms;
674 const_sbitmap_ptr bp = b->elms;
675 const_sbitmap_ptr cp = c->elms;
676 SBITMAP_ELT_TYPE changed = 0;
678 gcc_assert (!dst->popcount);
680 for (i = 0; i < n; i++)
682 const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
683 changed |= *dstp ^ tmp;
684 *dstp++ = tmp;
687 return changed != 0;
690 void
691 sbitmap_a_or_b_and_c (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
693 unsigned int i, n = dst->size;
694 sbitmap_ptr dstp = dst->elms;
695 const_sbitmap_ptr ap = a->elms;
696 const_sbitmap_ptr bp = b->elms;
697 const_sbitmap_ptr cp = c->elms;
699 gcc_assert (!dst->popcount);
701 for (i = 0; i < n; i++)
702 *dstp++ = *ap++ | (*bp++ & *cp++);
705 /* Set DST to be (A and (B or C)).
706 Return nonzero if any change is made. */
708 bool
709 sbitmap_a_and_b_or_c_cg (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
711 unsigned int i, n = dst->size;
712 sbitmap_ptr dstp = dst->elms;
713 const_sbitmap_ptr ap = a->elms;
714 const_sbitmap_ptr bp = b->elms;
715 const_sbitmap_ptr cp = c->elms;
716 SBITMAP_ELT_TYPE changed = 0;
718 gcc_assert (!dst->popcount);
720 for (i = 0; i < n; i++)
722 const SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
723 changed |= *dstp ^ tmp;
724 *dstp++ = tmp;
727 return changed != 0;
730 void
731 sbitmap_a_and_b_or_c (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
733 unsigned int i, n = dst->size;
734 sbitmap_ptr dstp = dst->elms;
735 const_sbitmap_ptr ap = a->elms;
736 const_sbitmap_ptr bp = b->elms;
737 const_sbitmap_ptr cp = c->elms;
739 for (i = 0; i < n; i++)
740 *dstp++ = *ap++ & (*bp++ | *cp++);
743 /* Return number of first bit set in the bitmap, -1 if none. */
746 sbitmap_first_set_bit (const_sbitmap bmap)
748 unsigned int n = 0;
749 sbitmap_iterator sbi;
751 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, sbi)
752 return n;
753 return -1;
756 /* Return number of last bit set in the bitmap, -1 if none. */
759 sbitmap_last_set_bit (const_sbitmap bmap)
761 int i;
762 const SBITMAP_ELT_TYPE *const ptr = bmap->elms;
764 for (i = bmap->size - 1; i >= 0; i--)
766 const SBITMAP_ELT_TYPE word = ptr[i];
768 if (word != 0)
770 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
771 SBITMAP_ELT_TYPE mask
772 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
774 while (1)
776 if ((word & mask) != 0)
777 return index;
779 mask >>= 1;
780 index--;
785 return -1;
788 void
789 dump_sbitmap (FILE *file, const_sbitmap bmap)
791 unsigned int i, n, j;
792 unsigned int set_size = bmap->size;
793 unsigned int total_bits = bmap->n_bits;
795 fprintf (file, " ");
796 for (i = n = 0; i < set_size && n < total_bits; i++)
797 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
799 if (n != 0 && n % 10 == 0)
800 fprintf (file, " ");
802 fprintf (file, "%d",
803 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
806 fprintf (file, "\n");
809 void
810 dump_sbitmap_file (FILE *file, const_sbitmap bmap)
812 unsigned int i, pos;
814 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
816 for (pos = 30, i = 0; i < bmap->n_bits; i++)
817 if (TEST_BIT (bmap, i))
819 if (pos > 70)
821 fprintf (file, "\n ");
822 pos = 0;
825 fprintf (file, "%d ", i);
826 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
829 fprintf (file, "}\n");
832 DEBUG_FUNCTION void
833 debug_sbitmap (const_sbitmap bmap)
835 dump_sbitmap_file (stderr, bmap);
838 void
839 dump_sbitmap_vector (FILE *file, const char *title, const char *subtitle,
840 sbitmap *bmaps, int n_maps)
842 int i;
844 fprintf (file, "%s\n", title);
845 for (i = 0; i < n_maps; i++)
847 fprintf (file, "%s %d\n", subtitle, i);
848 dump_sbitmap (file, bmaps[i]);
851 fprintf (file, "\n");
854 #if GCC_VERSION < 3400
855 /* Table of number of set bits in a character, indexed by value of char. */
856 static const unsigned char popcount_table[] =
858 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,
859 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,
860 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,
861 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,
862 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,
863 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,
864 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,
865 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,
868 /* Count the bits in an SBITMAP element A. */
870 static unsigned long
871 sbitmap_elt_popcount (SBITMAP_ELT_TYPE a)
873 unsigned long ret = 0;
874 unsigned i;
876 if (a == 0)
877 return 0;
879 /* Just do this the table way for now */
880 for (i = 0; i < SBITMAP_ELT_BITS; i += 8)
881 ret += popcount_table[(a >> i) & 0xff];
882 return ret;
884 #endif
886 /* Count the number of bits in SBITMAP a, up to bit MAXBIT. */
888 unsigned long
889 sbitmap_popcount (const_sbitmap a, unsigned long maxbit)
891 unsigned long count = 0;
892 unsigned ix;
893 unsigned int lastword;
895 if (maxbit == 0)
896 return 0;
898 if (maxbit >= a->n_bits)
899 maxbit = a->n_bits;
901 /* Count the bits in the full word. */
902 lastword = MIN (a->size, SBITMAP_SET_SIZE (maxbit + 1) - 1);
903 for (ix = 0; ix < lastword; ix++)
905 if (a->popcount)
907 count += a->popcount[ix];
908 #ifdef BITMAP_DEBUGGING
909 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
910 #endif
912 else
913 count += do_popcount (a->elms[ix]);
916 /* Count the remaining bits. */
917 if (lastword < a->size)
919 unsigned int bitindex;
920 SBITMAP_ELT_TYPE theword = a->elms[lastword];
922 bitindex = maxbit % SBITMAP_ELT_BITS;
923 if (bitindex != 0)
925 theword &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - bitindex);
926 count += do_popcount (theword);
929 return count;