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
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
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/>. */
22 #include "coretypes.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
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)
37 # error "internal error: sbitmap.h and hwint.h are inconsistent"
40 static unsigned long sbitmap_elt_popcount (SBITMAP_ELT_TYPE
);
41 # define do_popcount(x) sbitmap_elt_popcount((x))
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. */
57 sbitmap_verify_popcount (const_sbitmap a
)
60 unsigned int lastword
;
66 for (ix
= 0; ix
< lastword
; ix
++)
67 gcc_assert (a
->popcount
[ix
] == do_popcount (a
->elms
[ix
]));
71 /* Bitmap manipulation routines. */
73 /* Allocate a simple bitmap of N_ELMS bits. */
76 sbitmap_alloc (unsigned int n_elms
)
78 unsigned int bytes
, size
, amt
;
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
;
88 bmap
->popcount
= NULL
;
92 /* Allocate a simple bitmap of N_ELMS bits, and a popcount array. */
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
);
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. */
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
);
120 bmap
->popcount
= XRESIZEVEC (unsigned char, bmap
->popcount
, size
);
123 if (n_elms
> bmap
->n_bits
)
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
;
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
;
140 &= (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
144 memset (bmap
->elms
+ bmap
->size
, 0,
145 bytes
- SBITMAP_SIZE_BYTES (bmap
));
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
;
160 &= (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
162 bmap
->popcount
[size
- 1] = do_popcount (bmap
->elms
[size
- 1]);
166 bmap
->n_bits
= n_elms
;
171 /* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
174 sbitmap_realloc (sbitmap src
, unsigned int n_elms
)
176 unsigned int bytes
, size
, amt
;
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
;
190 bmap
= (sbitmap
) xrealloc (src
, amt
);
191 bmap
->n_bits
= n_elms
;
196 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
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
;
235 return bitmap_vector
;
238 /* Copy sbitmap SRC to DST. */
241 sbitmap_copy (sbitmap dst
, const_sbitmap src
)
243 memcpy (dst
->elms
, src
->elms
, sizeof (SBITMAP_ELT_TYPE
) * dst
->size
);
245 memcpy (dst
->popcount
, src
->popcount
, sizeof (unsigned char) * dst
->size
);
248 /* Copy the first N elements of sbitmap SRC to DST. */
251 sbitmap_copy_n (sbitmap dst
, const_sbitmap src
, unsigned int n
)
253 memcpy (dst
->elms
, src
->elms
, sizeof (SBITMAP_ELT_TYPE
) * n
);
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. */
268 sbitmap_empty_p (const_sbitmap bmap
)
271 for (i
=0; i
<bmap
->size
; i
++)
278 /* Return false if any of the N bits are set in MAP starting at
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
);
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);
304 n
-= SBITMAP_ELT_BITS
- shift
;
307 /* Deal with full elts. */
308 while (n
>= SBITMAP_ELT_BITS
)
313 n
-= SBITMAP_ELT_BITS
;
316 /* The leftover bits. */
320 elm
&= ((1 << n
) - 1);
329 /* Zero all elements in a bitmap. */
332 sbitmap_zero (sbitmap bmap
)
334 memset (bmap
->elms
, 0, SBITMAP_SIZE_BYTES (bmap
));
336 memset (bmap
->popcount
, 0, bmap
->size
* sizeof (unsigned char));
339 /* Set all elements in a bitmap to ones. */
342 sbitmap_ones (sbitmap bmap
)
344 unsigned int last_bit
;
346 memset (bmap
->elms
, -1, SBITMAP_SIZE_BYTES (bmap
));
348 memset (bmap
->popcount
, -1, bmap
->size
* sizeof (unsigned char));
350 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
353 bmap
->elms
[bmap
->size
- 1]
354 = (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
356 bmap
->popcount
[bmap
->size
- 1]
357 = do_popcount (bmap
->elms
[bmap
->size
- 1]);
361 /* Zero a vector of N_VECS bitmaps. */
364 sbitmap_vector_zero (sbitmap
*bmap
, unsigned int n_vecs
)
368 for (i
= 0; i
< n_vecs
; i
++)
369 sbitmap_zero (bmap
[i
]);
372 /* Set a vector of N_VECS bitmaps to ones. */
375 sbitmap_vector_ones (sbitmap
*bmap
, unsigned int n_vecs
)
379 for (i
= 0; i
< n_vecs
; i
++)
380 sbitmap_ones (bmap
[i
]);
383 /* Set DST to be A union (B - C).
385 Returns true if any change is made. */
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
;
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. */
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
++)
440 /* Zero all bits past n_bits, by ANDing dst with sbitmap_ones. */
441 last_bit
= src
->n_bits
% SBITMAP_ELT_BITS
;
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). */
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
)
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
++)
476 /* Return true if there are any bits set in A are also set in B.
477 Return false otherwise. */
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
;
486 n
= MIN (a
->size
, b
->size
);
487 for (i
= 0; i
< n
; i
++)
488 if ((*ap
++ & *bp
++) != 0)
494 /* Set DST to be (A and B).
495 Return nonzero if any change is made. */
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
;
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
++;
533 bool wordchanged
= (*dstp
^ tmp
) != 0;
535 *popcountp
= do_popcount (tmp
);
540 #ifdef BITMAP_DEBUGGING
542 sbitmap_verify_popcount (dst
);
546 /* Set DST to be (A xor B)).
547 Return nonzero if any change is made. */
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
;
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
++;
585 bool wordchanged
= (*dstp
^ tmp
) != 0;
587 *popcountp
= do_popcount (tmp
);
592 #ifdef BITMAP_DEBUGGING
594 sbitmap_verify_popcount (dst
);
598 /* Set DST to be (A or B)).
599 Return nonzero if any change is made. */
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
;
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
++;
637 bool wordchanged
= (*dstp
^ tmp
) != 0;
639 *popcountp
= do_popcount (tmp
);
644 #ifdef BITMAP_DEBUGGING
646 sbitmap_verify_popcount (dst
);
650 /* Return nonzero if A is a subset of B. */
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
)
665 /* Set DST to be (A or (B and C)).
666 Return nonzero if any change is made. */
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
;
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. */
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
;
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
)
749 sbitmap_iterator sbi
;
751 EXECUTE_IF_SET_IN_SBITMAP (bmap
, 0, n
, sbi
)
756 /* Return number of last bit set in the bitmap, -1 if none. */
759 sbitmap_last_set_bit (const_sbitmap bmap
)
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
];
770 unsigned int index
= (i
+ 1) * SBITMAP_ELT_BITS
- 1;
771 SBITMAP_ELT_TYPE mask
772 = (SBITMAP_ELT_TYPE
) 1 << (SBITMAP_ELT_BITS
- 1);
776 if ((word
& mask
) != 0)
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
;
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)
803 (bmap
->elms
[i
] & ((SBITMAP_ELT_TYPE
) 1 << j
)) != 0);
806 fprintf (file
, "\n");
810 dump_sbitmap_file (FILE *file
, const_sbitmap bmap
)
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
))
821 fprintf (file
, "\n ");
825 fprintf (file
, "%d ", i
);
826 pos
+= 2 + (i
>= 10) + (i
>= 100) + (i
>= 1000);
829 fprintf (file
, "}\n");
833 debug_sbitmap (const_sbitmap bmap
)
835 dump_sbitmap_file (stderr
, bmap
);
839 dump_sbitmap_vector (FILE *file
, const char *title
, const char *subtitle
,
840 sbitmap
*bmaps
, int n_maps
)
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. */
871 sbitmap_elt_popcount (SBITMAP_ELT_TYPE a
)
873 unsigned long ret
= 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];
886 /* Count the number of bits in SBITMAP a, up to bit MAXBIT. */
889 sbitmap_popcount (const_sbitmap a
, unsigned long maxbit
)
891 unsigned long count
= 0;
893 unsigned int lastword
;
898 if (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
++)
907 count
+= a
->popcount
[ix
];
908 #ifdef BITMAP_DEBUGGING
909 gcc_assert (a
->popcount
[ix
] == do_popcount (a
->elms
[ix
]));
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
;
925 theword
&= (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- bitindex
);
926 count
+= do_popcount (theword
);