2012-10-31 Jonathan Yong <jon_y@users.sourceforge.net>
[official-gcc.git] / gcc / sbitmap.c
blob737b0cd7fa78e1676d38581984b35f6c625e7644
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 bitmap_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 bitmap_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 bitmap_equal_p (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 bitmap_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 bitmap_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 bitmap_clear (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 bitmap_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 bitmap_vector_clear (sbitmap *bmap, unsigned int n_vecs)
366 unsigned int i;
368 for (i = 0; i < n_vecs; i++)
369 bitmap_clear (bmap[i]);
372 /* Set a vector of N_VECS bitmaps to ones. */
374 void
375 bitmap_vector_ones (sbitmap *bmap, unsigned int n_vecs)
377 unsigned int i;
379 for (i = 0; i < n_vecs; i++)
380 bitmap_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 bitmap_ior_and_compl (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 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
411 void
412 bitmap_not (sbitmap dst, const_sbitmap src)
414 unsigned int i, n = dst->size;
415 sbitmap_ptr dstp = dst->elms;
416 const_sbitmap_ptr srcp = src->elms;
417 unsigned int last_bit;
419 gcc_assert (!dst->popcount);
421 for (i = 0; i < n; i++)
422 *dstp++ = ~*srcp++;
424 /* Zero all bits past n_bits, by ANDing dst with bitmap_ones. */
425 last_bit = src->n_bits % SBITMAP_ELT_BITS;
426 if (last_bit)
427 dst->elms[n-1] = dst->elms[n-1]
428 & ((SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - last_bit));
431 /* Set the bits in DST to be the difference between the bits
432 in A and the bits in B. i.e. dst = a & (~b). */
434 void
435 bitmap_and_compl (sbitmap dst, const_sbitmap a, const_sbitmap b)
437 unsigned int i, dst_size = dst->size;
438 unsigned int min_size = dst->size;
439 sbitmap_ptr dstp = dst->elms;
440 const_sbitmap_ptr ap = a->elms;
441 const_sbitmap_ptr bp = b->elms;
443 gcc_assert (!dst->popcount);
445 /* A should be at least as large as DEST, to have a defined source. */
446 gcc_assert (a->size >= dst_size);
447 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
448 only copy the subtrahend into dest. */
449 if (b->size < min_size)
450 min_size = b->size;
451 for (i = 0; i < min_size; i++)
452 *dstp++ = *ap++ & (~*bp++);
453 /* Now fill the rest of dest from A, if B was too short.
454 This makes sense only when destination and A differ. */
455 if (dst != a && i != dst_size)
456 for (; i < dst_size; i++)
457 *dstp++ = *ap++;
460 /* Return true if there are any bits set in A are also set in B.
461 Return false otherwise. */
463 bool
464 bitmap_intersect_p (const_sbitmap a, const_sbitmap b)
466 const_sbitmap_ptr ap = a->elms;
467 const_sbitmap_ptr bp = b->elms;
468 unsigned int i, n;
470 n = MIN (a->size, b->size);
471 for (i = 0; i < n; i++)
472 if ((*ap++ & *bp++) != 0)
473 return true;
475 return false;
478 /* Set DST to be (A and B).
479 Return nonzero if any change is made. */
481 bool
482 bitmap_and (sbitmap dst, const_sbitmap a, const_sbitmap b)
484 unsigned int i, n = dst->size;
485 sbitmap_ptr dstp = dst->elms;
486 const_sbitmap_ptr ap = a->elms;
487 const_sbitmap_ptr bp = b->elms;
488 bool has_popcount = dst->popcount != NULL;
489 unsigned char *popcountp = dst->popcount;
490 bool anychange = false;
492 for (i = 0; i < n; i++)
494 const SBITMAP_ELT_TYPE tmp = *ap++ & *bp++;
495 if (has_popcount)
497 bool wordchanged = (*dstp ^ tmp) != 0;
498 if (wordchanged)
500 *popcountp = do_popcount (tmp);
501 anychange = true;
503 popcountp++;
505 *dstp++ = tmp;
507 #ifdef BITMAP_DEBUGGING
508 if (has_popcount)
509 sbitmap_verify_popcount (dst);
510 #endif
511 return anychange;
514 /* Set DST to be (A xor B)).
515 Return nonzero if any change is made. */
517 bool
518 bitmap_xor (sbitmap dst, const_sbitmap a, const_sbitmap b)
520 unsigned int i, n = dst->size;
521 sbitmap_ptr dstp = dst->elms;
522 const_sbitmap_ptr ap = a->elms;
523 const_sbitmap_ptr bp = b->elms;
524 bool has_popcount = dst->popcount != NULL;
525 unsigned char *popcountp = dst->popcount;
526 bool anychange = false;
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)
536 *popcountp = do_popcount (tmp);
537 anychange = true;
539 popcountp++;
541 *dstp++ = tmp;
543 #ifdef BITMAP_DEBUGGING
544 if (has_popcount)
545 sbitmap_verify_popcount (dst);
546 #endif
547 return anychange;
550 /* Set DST to be (A or B)).
551 Return nonzero if any change is made. */
553 bool
554 bitmap_ior (sbitmap dst, const_sbitmap a, const_sbitmap b)
556 unsigned int i, n = dst->size;
557 sbitmap_ptr dstp = dst->elms;
558 const_sbitmap_ptr ap = a->elms;
559 const_sbitmap_ptr bp = b->elms;
560 bool has_popcount = dst->popcount != NULL;
561 unsigned char *popcountp = dst->popcount;
562 bool anychange = false;
564 for (i = 0; i < n; i++)
566 const SBITMAP_ELT_TYPE tmp = *ap++ | *bp++;
567 if (has_popcount)
569 bool wordchanged = (*dstp ^ tmp) != 0;
570 if (wordchanged)
572 *popcountp = do_popcount (tmp);
573 anychange = true;
575 popcountp++;
577 *dstp++ = tmp;
579 #ifdef BITMAP_DEBUGGING
580 if (has_popcount)
581 sbitmap_verify_popcount (dst);
582 #endif
583 return anychange;
586 /* Return nonzero if A is a subset of B. */
588 bool
589 bitmap_subset_p (const_sbitmap a, const_sbitmap b)
591 unsigned int i, n = a->size;
592 const_sbitmap_ptr ap, bp;
594 for (ap = a->elms, bp = b->elms, i = 0; i < n; i++, ap++, bp++)
595 if ((*ap | *bp) != *bp)
596 return false;
598 return true;
601 /* Set DST to be (A or (B and C)).
602 Return nonzero if any change is made. */
604 bool
605 bitmap_or_and (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
607 unsigned int i, n = dst->size;
608 sbitmap_ptr dstp = dst->elms;
609 const_sbitmap_ptr ap = a->elms;
610 const_sbitmap_ptr bp = b->elms;
611 const_sbitmap_ptr cp = c->elms;
612 SBITMAP_ELT_TYPE changed = 0;
614 gcc_assert (!dst->popcount);
616 for (i = 0; i < n; i++)
618 const SBITMAP_ELT_TYPE tmp = *ap++ | (*bp++ & *cp++);
619 changed |= *dstp ^ tmp;
620 *dstp++ = tmp;
623 return changed != 0;
626 /* Set DST to be (A and (B or C)).
627 Return nonzero if any change is made. */
629 bool
630 bitmap_and_or (sbitmap dst, const_sbitmap a, const_sbitmap b, const_sbitmap c)
632 unsigned int i, n = dst->size;
633 sbitmap_ptr dstp = dst->elms;
634 const_sbitmap_ptr ap = a->elms;
635 const_sbitmap_ptr bp = b->elms;
636 const_sbitmap_ptr cp = c->elms;
637 SBITMAP_ELT_TYPE changed = 0;
639 gcc_assert (!dst->popcount);
641 for (i = 0; i < n; i++)
643 const SBITMAP_ELT_TYPE tmp = *ap++ & (*bp++ | *cp++);
644 changed |= *dstp ^ tmp;
645 *dstp++ = tmp;
648 return changed != 0;
651 /* Return number of first bit set in the bitmap, -1 if none. */
654 bitmap_first_set_bit (const_sbitmap bmap)
656 unsigned int n = 0;
657 sbitmap_iterator sbi;
659 EXECUTE_IF_SET_IN_SBITMAP (bmap, 0, n, sbi)
660 return n;
661 return -1;
664 /* Return number of last bit set in the bitmap, -1 if none. */
667 bitmap_last_set_bit (const_sbitmap bmap)
669 int i;
670 const SBITMAP_ELT_TYPE *const ptr = bmap->elms;
672 for (i = bmap->size - 1; i >= 0; i--)
674 const SBITMAP_ELT_TYPE word = ptr[i];
676 if (word != 0)
678 unsigned int index = (i + 1) * SBITMAP_ELT_BITS - 1;
679 SBITMAP_ELT_TYPE mask
680 = (SBITMAP_ELT_TYPE) 1 << (SBITMAP_ELT_BITS - 1);
682 while (1)
684 if ((word & mask) != 0)
685 return index;
687 mask >>= 1;
688 index--;
693 return -1;
696 void
697 dump_bitmap (FILE *file, const_sbitmap bmap)
699 unsigned int i, n, j;
700 unsigned int set_size = bmap->size;
701 unsigned int total_bits = bmap->n_bits;
703 fprintf (file, " ");
704 for (i = n = 0; i < set_size && n < total_bits; i++)
705 for (j = 0; j < SBITMAP_ELT_BITS && n < total_bits; j++, n++)
707 if (n != 0 && n % 10 == 0)
708 fprintf (file, " ");
710 fprintf (file, "%d",
711 (bmap->elms[i] & ((SBITMAP_ELT_TYPE) 1 << j)) != 0);
714 fprintf (file, "\n");
717 void
718 dump_bitmap_file (FILE *file, const_sbitmap bmap)
720 unsigned int i, pos;
722 fprintf (file, "n_bits = %d, set = {", bmap->n_bits);
724 for (pos = 30, i = 0; i < bmap->n_bits; i++)
725 if (TEST_BIT (bmap, i))
727 if (pos > 70)
729 fprintf (file, "\n ");
730 pos = 0;
733 fprintf (file, "%d ", i);
734 pos += 2 + (i >= 10) + (i >= 100) + (i >= 1000);
737 fprintf (file, "}\n");
740 DEBUG_FUNCTION void
741 debug_bitmap (const_sbitmap bmap)
743 dump_bitmap_file (stderr, bmap);
746 void
747 dump_bitmap_vector (FILE *file, const char *title, const char *subtitle,
748 sbitmap *bmaps, int n_maps)
750 int i;
752 fprintf (file, "%s\n", title);
753 for (i = 0; i < n_maps; i++)
755 fprintf (file, "%s %d\n", subtitle, i);
756 dump_bitmap (file, bmaps[i]);
759 fprintf (file, "\n");
762 #if GCC_VERSION < 3400
763 /* Table of number of set bits in a character, indexed by value of char. */
764 static const unsigned char popcount_table[] =
766 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,
767 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,
768 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,
769 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,
770 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,
771 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,
772 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,
773 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,
776 /* Count the bits in an SBITMAP element A. */
778 static unsigned long
779 sbitmap_elt_popcount (SBITMAP_ELT_TYPE a)
781 unsigned long ret = 0;
782 unsigned i;
784 if (a == 0)
785 return 0;
787 /* Just do this the table way for now */
788 for (i = 0; i < SBITMAP_ELT_BITS; i += 8)
789 ret += popcount_table[(a >> i) & 0xff];
790 return ret;
792 #endif
794 /* Count the number of bits in SBITMAP a, up to bit MAXBIT. */
796 unsigned long
797 sbitmap_popcount (const_sbitmap a, unsigned long maxbit)
799 unsigned long count = 0;
800 unsigned ix;
801 unsigned int lastword;
803 if (maxbit == 0)
804 return 0;
806 if (maxbit >= a->n_bits)
807 maxbit = a->n_bits;
809 /* Count the bits in the full word. */
810 lastword = MIN (a->size, SBITMAP_SET_SIZE (maxbit + 1) - 1);
811 for (ix = 0; ix < lastword; ix++)
813 if (a->popcount)
815 count += a->popcount[ix];
816 #ifdef BITMAP_DEBUGGING
817 gcc_assert (a->popcount[ix] == do_popcount (a->elms[ix]));
818 #endif
820 else
821 count += do_popcount (a->elms[ix]);
824 /* Count the remaining bits. */
825 if (lastword < a->size)
827 unsigned int bitindex;
828 SBITMAP_ELT_TYPE theword = a->elms[lastword];
830 bitindex = maxbit % SBITMAP_ELT_BITS;
831 if (bitindex != 0)
833 theword &= (SBITMAP_ELT_TYPE)-1 >> (SBITMAP_ELT_BITS - bitindex);
834 count += do_popcount (theword);
837 return count;