2 Copyright (C) 1999-2021 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"
26 typedef SBITMAP_ELT_TYPE
*sbitmap_ptr
;
27 typedef const SBITMAP_ELT_TYPE
*const_sbitmap_ptr
;
29 /* Return the size in bytes of a bitmap MAP. */
31 static inline unsigned int sbitmap_size_bytes (const_sbitmap map
)
33 return map
->size
* sizeof (SBITMAP_ELT_TYPE
);
37 /* Bitmap manipulation routines. */
39 /* Allocate a simple bitmap of N_ELMS bits. */
42 sbitmap_alloc (unsigned int n_elms
)
44 unsigned int bytes
, size
, amt
;
47 size
= SBITMAP_SET_SIZE (n_elms
);
48 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
49 amt
= (sizeof (struct simple_bitmap_def
)
50 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
51 bmap
= (sbitmap
) xmalloc (amt
);
52 bmap
->n_bits
= n_elms
;
57 /* Resize a simple bitmap BMAP to N_ELMS bits. If increasing the
58 size of BMAP, clear the new bits to zero if the DEF argument
59 is zero, and set them to one otherwise. */
62 sbitmap_resize (sbitmap bmap
, unsigned int n_elms
, int def
)
64 unsigned int bytes
, size
, amt
;
65 unsigned int last_bit
;
67 size
= SBITMAP_SET_SIZE (n_elms
);
68 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
69 if (bytes
> sbitmap_size_bytes (bmap
))
71 amt
= (sizeof (struct simple_bitmap_def
)
72 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
73 bmap
= (sbitmap
) xrealloc (bmap
, amt
);
76 if (n_elms
> bmap
->n_bits
)
80 memset (bmap
->elms
+ bmap
->size
, -1,
81 bytes
- sbitmap_size_bytes (bmap
));
83 /* Set the new bits if the original last element. */
84 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
86 bmap
->elms
[bmap
->size
- 1]
87 |= ~((SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
));
89 /* Clear the unused bit in the new last element. */
90 last_bit
= n_elms
% SBITMAP_ELT_BITS
;
93 &= (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
96 memset (bmap
->elms
+ bmap
->size
, 0, bytes
- sbitmap_size_bytes (bmap
));
98 else if (n_elms
< bmap
->n_bits
)
100 /* Clear the surplus bits in the last word. */
101 last_bit
= n_elms
% SBITMAP_ELT_BITS
;
104 &= (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
107 bmap
->n_bits
= n_elms
;
112 /* Re-allocate a simple bitmap of N_ELMS bits. New storage is uninitialized. */
115 sbitmap_realloc (sbitmap src
, unsigned int n_elms
)
117 unsigned int bytes
, size
, amt
;
120 size
= SBITMAP_SET_SIZE (n_elms
);
121 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
122 amt
= (sizeof (struct simple_bitmap_def
)
123 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
125 if (sbitmap_size_bytes (src
) >= bytes
)
127 src
->n_bits
= n_elms
;
131 bmap
= (sbitmap
) xrealloc (src
, amt
);
132 bmap
->n_bits
= n_elms
;
137 /* Allocate a vector of N_VECS bitmaps of N_ELMS bits. */
140 sbitmap_vector_alloc (unsigned int n_vecs
, unsigned int n_elms
)
142 unsigned int i
, size
;
143 size_t amt
, bytes
, vector_bytes
, elm_bytes
, offset
;
144 sbitmap
*bitmap_vector
;
146 size
= SBITMAP_SET_SIZE (n_elms
);
147 bytes
= size
* sizeof (SBITMAP_ELT_TYPE
);
148 elm_bytes
= (sizeof (struct simple_bitmap_def
)
149 + bytes
- sizeof (SBITMAP_ELT_TYPE
));
150 vector_bytes
= n_vecs
* sizeof (sbitmap
*);
152 /* Round up `vector_bytes' to account for the alignment requirements
153 of an sbitmap. One could allocate the vector-table and set of sbitmaps
154 separately, but that requires maintaining two pointers or creating
155 a cover struct to hold both pointers (so our result is still just
156 one pointer). Neither is a bad idea, but this is simpler for now. */
158 /* Based on DEFAULT_ALIGNMENT computation in obstack.c. */
159 struct { char x
; SBITMAP_ELT_TYPE y
; } align
;
160 int alignment
= (char *) & align
.y
- & align
.x
;
161 vector_bytes
= (vector_bytes
+ alignment
- 1) & ~ (alignment
- 1);
164 amt
= vector_bytes
+ (n_vecs
* elm_bytes
);
165 bitmap_vector
= (sbitmap
*) xmalloc (amt
);
167 for (i
= 0, offset
= vector_bytes
; i
< n_vecs
; i
++, offset
+= elm_bytes
)
169 sbitmap b
= (sbitmap
) ((char *) bitmap_vector
+ offset
);
171 bitmap_vector
[i
] = b
;
176 return bitmap_vector
;
179 /* Copy sbitmap SRC to DST. */
182 bitmap_copy (sbitmap dst
, const_sbitmap src
)
184 gcc_checking_assert (src
->size
<= dst
->size
);
186 memcpy (dst
->elms
, src
->elms
, sizeof (SBITMAP_ELT_TYPE
) * dst
->size
);
189 /* Determine if a == b. */
191 bitmap_equal_p (const_sbitmap a
, const_sbitmap b
)
193 bitmap_check_sizes (a
, b
);
195 return !memcmp (a
->elms
, b
->elms
, sizeof (SBITMAP_ELT_TYPE
) * a
->size
);
198 /* Return true if the bitmap is empty. */
201 bitmap_empty_p (const_sbitmap bmap
)
204 for (i
=0; i
<bmap
->size
; i
++)
211 /* Clear COUNT bits from START in BMAP. */
214 bitmap_clear_range (sbitmap bmap
, unsigned int start
, unsigned int count
)
219 bitmap_check_index (bmap
, start
+ count
- 1);
221 unsigned int start_word
= start
/ SBITMAP_ELT_BITS
;
222 unsigned int start_bitno
= start
% SBITMAP_ELT_BITS
;
224 /* Clearing less than a full word, starting at the beginning of a word. */
225 if (start_bitno
== 0 && count
< SBITMAP_ELT_BITS
)
227 SBITMAP_ELT_TYPE mask
= ((SBITMAP_ELT_TYPE
)1 << count
) - 1;
228 bmap
->elms
[start_word
] &= ~mask
;
232 unsigned int end_word
= (start
+ count
) / SBITMAP_ELT_BITS
;
233 unsigned int end_bitno
= (start
+ count
) % SBITMAP_ELT_BITS
;
235 /* Clearing starts somewhere in the middle of the first word. Clear up to
236 the end of the first word or the end of the requested region, whichever
238 if (start_bitno
!= 0)
240 unsigned int nbits
= ((start_word
== end_word
)
241 ? end_bitno
- start_bitno
242 : SBITMAP_ELT_BITS
- start_bitno
);
243 SBITMAP_ELT_TYPE mask
= ((SBITMAP_ELT_TYPE
)1 << nbits
) - 1;
244 mask
<<= start_bitno
;
245 bmap
->elms
[start_word
] &= ~mask
;
253 /* Now clear words at a time until we hit a partial word. */
254 unsigned int nwords
= (end_word
- start_word
);
257 memset (&bmap
->elms
[start_word
], 0, nwords
* sizeof (SBITMAP_ELT_TYPE
));
258 count
-= nwords
* sizeof (SBITMAP_ELT_TYPE
) * BITS_PER_UNIT
;
259 start_word
+= nwords
;
265 /* Now handle residuals in the last word. */
266 SBITMAP_ELT_TYPE mask
= ((SBITMAP_ELT_TYPE
)1 << count
) - 1;
267 bmap
->elms
[start_word
] &= ~mask
;
270 /* Set COUNT bits from START in BMAP. */
272 bitmap_set_range (sbitmap bmap
, unsigned int start
, unsigned int count
)
277 bitmap_check_index (bmap
, start
+ count
- 1);
279 unsigned int start_word
= start
/ SBITMAP_ELT_BITS
;
280 unsigned int start_bitno
= start
% SBITMAP_ELT_BITS
;
282 /* Setting less than a full word, starting at the beginning of a word. */
283 if (start_bitno
== 0 && count
< SBITMAP_ELT_BITS
)
285 SBITMAP_ELT_TYPE mask
= ((SBITMAP_ELT_TYPE
)1 << count
) - 1;
286 bmap
->elms
[start_word
] |= mask
;
290 unsigned int end_word
= (start
+ count
) / SBITMAP_ELT_BITS
;
291 unsigned int end_bitno
= (start
+ count
) % SBITMAP_ELT_BITS
;
293 /* Setting starts somewhere in the middle of the first word. Set up to
294 the end of the first word or the end of the requested region, whichever
296 if (start_bitno
!= 0)
298 unsigned int nbits
= ((start_word
== end_word
)
299 ? end_bitno
- start_bitno
300 : SBITMAP_ELT_BITS
- start_bitno
);
301 SBITMAP_ELT_TYPE mask
= ((SBITMAP_ELT_TYPE
)1 << nbits
) - 1;
302 mask
<<= start_bitno
;
303 bmap
->elms
[start_word
] |= mask
;
311 /* Now set words at a time until we hit a partial word. */
312 unsigned int nwords
= (end_word
- start_word
);
315 memset (&bmap
->elms
[start_word
], 0xff,
316 nwords
* sizeof (SBITMAP_ELT_TYPE
));
317 count
-= nwords
* sizeof (SBITMAP_ELT_TYPE
) * BITS_PER_UNIT
;
318 start_word
+= nwords
;
324 /* Now handle residuals in the last word. */
325 SBITMAP_ELT_TYPE mask
= ((SBITMAP_ELT_TYPE
)1 << count
) - 1;
326 bmap
->elms
[start_word
] |= mask
;
329 /* Return TRUE if any bit between START and END inclusive is set within
330 the simple bitmap BMAP. Return FALSE otherwise. */
333 bitmap_bit_in_range_p (const_sbitmap bmap
, unsigned int start
, unsigned int end
)
335 gcc_checking_assert (start
<= end
);
336 bitmap_check_index (bmap
, end
);
338 unsigned int start_word
= start
/ SBITMAP_ELT_BITS
;
339 unsigned int start_bitno
= start
% SBITMAP_ELT_BITS
;
341 unsigned int end_word
= end
/ SBITMAP_ELT_BITS
;
342 unsigned int end_bitno
= end
% SBITMAP_ELT_BITS
;
344 /* Check beginning of first word if different from zero. */
345 if (start_bitno
!= 0)
347 SBITMAP_ELT_TYPE high_mask
= ~(SBITMAP_ELT_TYPE
)0;
348 if (start_word
== end_word
&& end_bitno
+ 1 < SBITMAP_ELT_BITS
)
349 high_mask
= ((SBITMAP_ELT_TYPE
)1 << (end_bitno
+ 1)) - 1;
351 SBITMAP_ELT_TYPE low_mask
= ((SBITMAP_ELT_TYPE
)1 << start_bitno
) - 1;
352 SBITMAP_ELT_TYPE mask
= high_mask
- low_mask
;
353 if (bmap
->elms
[start_word
] & mask
)
358 if (start_word
> end_word
)
361 /* Now test words at a time until we hit a partial word. */
362 unsigned int nwords
= (end_word
- start_word
);
365 if (bmap
->elms
[start_word
])
371 /* Now handle residuals in the last word. */
372 SBITMAP_ELT_TYPE mask
= ~(SBITMAP_ELT_TYPE
)0;
373 if (end_bitno
+ 1 < SBITMAP_ELT_BITS
)
374 mask
= ((SBITMAP_ELT_TYPE
)1 << (end_bitno
+ 1)) - 1;
375 return (bmap
->elms
[start_word
] & mask
) != 0;
378 #if GCC_VERSION < 3400
379 /* Table of number of set bits in a character, indexed by value of char. */
380 static const unsigned char popcount_table
[] =
382 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,
383 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,
384 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,
385 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,
386 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,
387 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,
388 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,
389 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,
393 sbitmap_popcount (SBITMAP_ELT_TYPE a
)
395 unsigned long ret
= 0;
398 /* Just do this the table way for now */
399 for (i
= 0; i
< HOST_BITS_PER_WIDEST_FAST_INT
; i
+= 8)
400 ret
+= popcount_table
[(a
>> i
) & 0xff];
405 /* Count and return the number of bits set in the bitmap BMAP. */
408 bitmap_count_bits (const_sbitmap bmap
)
410 unsigned int count
= 0;
411 for (unsigned int i
= 0; i
< bmap
->size
; i
++)
414 #if GCC_VERSION < 3400
415 count
+= sbitmap_popcount (bmap
->elms
[i
]);
417 # if HOST_BITS_PER_WIDEST_FAST_INT == HOST_BITS_PER_LONG
418 count
+= __builtin_popcountl (bmap
->elms
[i
]);
419 # elif HOST_BITS_PER_WIDEST_FAST_INT == HOST_BITS_PER_LONGLONG
420 count
+= __builtin_popcountll (bmap
->elms
[i
]);
422 count
+= __builtin_popcount (bmap
->elms
[i
]);
429 /* Zero all elements in a bitmap. */
432 bitmap_clear (sbitmap bmap
)
434 memset (bmap
->elms
, 0, sbitmap_size_bytes (bmap
));
437 /* Set all elements in a bitmap to ones. */
440 bitmap_ones (sbitmap bmap
)
442 unsigned int last_bit
;
444 memset (bmap
->elms
, -1, sbitmap_size_bytes (bmap
));
446 last_bit
= bmap
->n_bits
% SBITMAP_ELT_BITS
;
448 bmap
->elms
[bmap
->size
- 1]
449 = (SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
);
452 /* Zero a vector of N_VECS bitmaps. */
455 bitmap_vector_clear (sbitmap
*bmap
, unsigned int n_vecs
)
459 for (i
= 0; i
< n_vecs
; i
++)
460 bitmap_clear (bmap
[i
]);
463 /* Set a vector of N_VECS bitmaps to ones. */
466 bitmap_vector_ones (sbitmap
*bmap
, unsigned int n_vecs
)
470 for (i
= 0; i
< n_vecs
; i
++)
471 bitmap_ones (bmap
[i
]);
474 /* Set DST to be A union (B - C).
476 Returns true if any change is made. */
479 bitmap_ior_and_compl (sbitmap dst
, const_sbitmap a
, const_sbitmap b
, const_sbitmap c
)
481 bitmap_check_sizes (a
, b
);
482 bitmap_check_sizes (b
, c
);
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 const_sbitmap_ptr cp
= c
->elms
;
489 SBITMAP_ELT_TYPE changed
= 0;
491 for (i
= 0; i
< n
; i
++)
493 const SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & ~*cp
++);
494 changed
|= *dstp
^ tmp
;
501 /* Set bitmap DST to the bitwise negation of the bitmap SRC. */
504 bitmap_not (sbitmap dst
, const_sbitmap src
)
506 bitmap_check_sizes (src
, dst
);
508 unsigned int i
, n
= dst
->size
;
509 sbitmap_ptr dstp
= dst
->elms
;
510 const_sbitmap_ptr srcp
= src
->elms
;
511 unsigned int last_bit
;
513 for (i
= 0; i
< n
; i
++)
516 /* Zero all bits past n_bits, by ANDing dst with bitmap_ones. */
517 last_bit
= src
->n_bits
% SBITMAP_ELT_BITS
;
519 dst
->elms
[n
-1] = dst
->elms
[n
-1]
520 & ((SBITMAP_ELT_TYPE
)-1 >> (SBITMAP_ELT_BITS
- last_bit
));
523 /* Set the bits in DST to be the difference between the bits
524 in A and the bits in B. i.e. dst = a & (~b). */
527 bitmap_and_compl (sbitmap dst
, const_sbitmap a
, const_sbitmap b
)
529 bitmap_check_sizes (a
, b
);
530 bitmap_check_sizes (b
, dst
);
532 unsigned int i
, dst_size
= dst
->size
;
533 unsigned int min_size
= dst
->size
;
534 sbitmap_ptr dstp
= dst
->elms
;
535 const_sbitmap_ptr ap
= a
->elms
;
536 const_sbitmap_ptr bp
= b
->elms
;
538 /* A should be at least as large as DEST, to have a defined source. */
539 gcc_assert (a
->size
>= dst_size
);
540 /* If minuend is smaller, we simply pretend it to be zero bits, i.e.
541 only copy the subtrahend into dest. */
542 if (b
->size
< min_size
)
544 for (i
= 0; i
< min_size
; i
++)
545 *dstp
++ = *ap
++ & (~*bp
++);
546 /* Now fill the rest of dest from A, if B was too short.
547 This makes sense only when destination and A differ. */
548 if (dst
!= a
&& i
!= dst_size
)
549 for (; i
< dst_size
; i
++)
553 /* Return true if there are any bits set in A are also set in B.
554 Return false otherwise. */
557 bitmap_intersect_p (const_sbitmap a
, const_sbitmap b
)
559 bitmap_check_sizes (a
, b
);
561 const_sbitmap_ptr ap
= a
->elms
;
562 const_sbitmap_ptr bp
= b
->elms
;
565 n
= MIN (a
->size
, b
->size
);
566 for (i
= 0; i
< n
; i
++)
567 if ((*ap
++ & *bp
++) != 0)
573 /* Set DST to be (A and B).
574 Return nonzero if any change is made. */
577 bitmap_and (sbitmap dst
, const_sbitmap a
, const_sbitmap b
)
579 bitmap_check_sizes (a
, b
);
580 bitmap_check_sizes (b
, dst
);
582 unsigned int i
, n
= dst
->size
;
583 sbitmap_ptr dstp
= dst
->elms
;
584 const_sbitmap_ptr ap
= a
->elms
;
585 const_sbitmap_ptr bp
= b
->elms
;
586 SBITMAP_ELT_TYPE changed
= 0;
588 for (i
= 0; i
< n
; i
++)
590 const SBITMAP_ELT_TYPE tmp
= *ap
++ & *bp
++;
591 SBITMAP_ELT_TYPE wordchanged
= *dstp
^ tmp
;
593 changed
|= wordchanged
;
598 /* Set DST to be (A xor B)).
599 Return nonzero if any change is made. */
602 bitmap_xor (sbitmap dst
, const_sbitmap a
, const_sbitmap b
)
604 bitmap_check_sizes (a
, b
);
605 bitmap_check_sizes (b
, dst
);
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 SBITMAP_ELT_TYPE changed
= 0;
613 for (i
= 0; i
< n
; i
++)
615 const SBITMAP_ELT_TYPE tmp
= *ap
++ ^ *bp
++;
616 SBITMAP_ELT_TYPE wordchanged
= *dstp
^ tmp
;
618 changed
|= wordchanged
;
623 /* Set DST to be (A or B)).
624 Return nonzero if any change is made. */
627 bitmap_ior (sbitmap dst
, const_sbitmap a
, const_sbitmap b
)
629 bitmap_check_sizes (a
, b
);
630 bitmap_check_sizes (b
, dst
);
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 SBITMAP_ELT_TYPE changed
= 0;
638 for (i
= 0; i
< n
; i
++)
640 const SBITMAP_ELT_TYPE tmp
= *ap
++ | *bp
++;
641 SBITMAP_ELT_TYPE wordchanged
= *dstp
^ tmp
;
643 changed
|= wordchanged
;
648 /* Return nonzero if A is a subset of B. */
651 bitmap_subset_p (const_sbitmap a
, const_sbitmap b
)
653 bitmap_check_sizes (a
, 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 bitmap_or_and (sbitmap dst
, const_sbitmap a
, const_sbitmap b
, const_sbitmap c
)
671 bitmap_check_sizes (a
, b
);
672 bitmap_check_sizes (b
, c
);
673 bitmap_check_sizes (c
, dst
);
675 unsigned int i
, n
= dst
->size
;
676 sbitmap_ptr dstp
= dst
->elms
;
677 const_sbitmap_ptr ap
= a
->elms
;
678 const_sbitmap_ptr bp
= b
->elms
;
679 const_sbitmap_ptr cp
= c
->elms
;
680 SBITMAP_ELT_TYPE changed
= 0;
682 for (i
= 0; i
< n
; i
++)
684 const SBITMAP_ELT_TYPE tmp
= *ap
++ | (*bp
++ & *cp
++);
685 changed
|= *dstp
^ tmp
;
692 /* Set DST to be (A and (B or C)).
693 Return nonzero if any change is made. */
696 bitmap_and_or (sbitmap dst
, const_sbitmap a
, const_sbitmap b
, const_sbitmap c
)
698 bitmap_check_sizes (a
, b
);
699 bitmap_check_sizes (b
, c
);
700 bitmap_check_sizes (c
, dst
);
702 unsigned int i
, n
= dst
->size
;
703 sbitmap_ptr dstp
= dst
->elms
;
704 const_sbitmap_ptr ap
= a
->elms
;
705 const_sbitmap_ptr bp
= b
->elms
;
706 const_sbitmap_ptr cp
= c
->elms
;
707 SBITMAP_ELT_TYPE changed
= 0;
709 for (i
= 0; i
< n
; i
++)
711 const SBITMAP_ELT_TYPE tmp
= *ap
++ & (*bp
++ | *cp
++);
712 changed
|= *dstp
^ tmp
;
719 /* Return number of first bit set in the bitmap, -1 if none. */
722 bitmap_first_set_bit (const_sbitmap bmap
)
725 sbitmap_iterator sbi
;
727 EXECUTE_IF_SET_IN_BITMAP (bmap
, 0, n
, sbi
)
732 /* Return number of last bit set in the bitmap, -1 if none. */
735 bitmap_last_set_bit (const_sbitmap bmap
)
738 const SBITMAP_ELT_TYPE
*const ptr
= bmap
->elms
;
740 for (i
= bmap
->size
- 1; i
>= 0; i
--)
742 const SBITMAP_ELT_TYPE word
= ptr
[i
];
746 unsigned int index
= (i
+ 1) * SBITMAP_ELT_BITS
- 1;
747 SBITMAP_ELT_TYPE mask
748 = (SBITMAP_ELT_TYPE
) 1 << (SBITMAP_ELT_BITS
- 1);
752 if ((word
& mask
) != 0)
765 dump_bitmap (FILE *file
, const_sbitmap bmap
)
767 unsigned int i
, n
, j
;
768 unsigned int set_size
= bmap
->size
;
769 unsigned int total_bits
= bmap
->n_bits
;
772 for (i
= n
= 0; i
< set_size
&& n
< total_bits
; i
++)
773 for (j
= 0; j
< SBITMAP_ELT_BITS
&& n
< total_bits
; j
++, n
++)
775 if (n
!= 0 && n
% 10 == 0)
779 (bmap
->elms
[i
] & ((SBITMAP_ELT_TYPE
) 1 << j
)) != 0);
782 fprintf (file
, "\n");
786 debug_raw (simple_bitmap_def
&ref
)
788 dump_bitmap (stderr
, &ref
);
792 debug_raw (simple_bitmap_def
*ptr
)
797 fprintf (stderr
, "<nil>\n");
801 dump_bitmap_file (FILE *file
, const_sbitmap bmap
)
805 fprintf (file
, "n_bits = %d, set = {", bmap
->n_bits
);
807 for (pos
= 30, i
= 0; i
< bmap
->n_bits
; i
++)
808 if (bitmap_bit_p (bmap
, i
))
812 fprintf (file
, "\n ");
816 fprintf (file
, "%d ", i
);
817 pos
+= 2 + (i
>= 10) + (i
>= 100) + (i
>= 1000);
820 fprintf (file
, "}\n");
824 debug_bitmap (const_sbitmap bmap
)
826 dump_bitmap_file (stderr
, bmap
);
830 debug (simple_bitmap_def
&ref
)
832 dump_bitmap_file (stderr
, &ref
);
836 debug (simple_bitmap_def
*ptr
)
841 fprintf (stderr
, "<nil>\n");
845 dump_bitmap_vector (FILE *file
, const char *title
, const char *subtitle
,
846 sbitmap
*bmaps
, int n_maps
)
850 fprintf (file
, "%s\n", title
);
851 for (i
= 0; i
< n_maps
; i
++)
853 fprintf (file
, "%s %d\n", subtitle
, i
);
854 dump_bitmap (file
, bmaps
[i
]);
857 fprintf (file
, "\n");
864 /* Selftests for sbitmaps. */
866 /* Checking function that uses both bitmap_bit_in_range_p and
867 loop of bitmap_bit_p and verifies consistent results. */
870 bitmap_bit_in_range_p_checking (sbitmap s
, unsigned int start
,
873 bool r1
= bitmap_bit_in_range_p (s
, start
, end
);
876 for (unsigned int i
= start
; i
<= end
; i
++)
877 if (bitmap_bit_p (s
, i
))
887 /* Verify bitmap_set_range functions for sbitmap. */
892 sbitmap s
= sbitmap_alloc (16);
895 bitmap_set_range (s
, 0, 1);
896 ASSERT_TRUE (bitmap_bit_in_range_p_checking (s
, 0, 0));
897 ASSERT_FALSE (bitmap_bit_in_range_p_checking (s
, 1, 15));
898 bitmap_set_range (s
, 15, 1);
899 ASSERT_FALSE (bitmap_bit_in_range_p_checking (s
, 1, 14));
900 ASSERT_TRUE (bitmap_bit_in_range_p_checking (s
, 15, 15));
903 s
= sbitmap_alloc (1024);
905 bitmap_set_range (s
, 512, 1);
906 ASSERT_FALSE (bitmap_bit_in_range_p_checking (s
, 0, 511));
907 ASSERT_FALSE (bitmap_bit_in_range_p_checking (s
, 513, 1023));
908 ASSERT_TRUE (bitmap_bit_in_range_p_checking (s
, 512, 512));
909 ASSERT_TRUE (bitmap_bit_in_range_p_checking (s
, 508, 512));
910 ASSERT_TRUE (bitmap_bit_in_range_p_checking (s
, 508, 513));
911 ASSERT_FALSE (bitmap_bit_in_range_p_checking (s
, 508, 511));
914 bitmap_set_range (s
, 512, 64);
915 ASSERT_FALSE (bitmap_bit_in_range_p_checking (s
, 0, 511));
916 ASSERT_FALSE (bitmap_bit_in_range_p_checking (s
, 512 + 64, 1023));
917 ASSERT_TRUE (bitmap_bit_in_range_p_checking (s
, 512, 512));
918 ASSERT_TRUE (bitmap_bit_in_range_p_checking (s
, 512 + 63, 512 + 63));
922 /* Verify bitmap_bit_in_range_p functions for sbitmap. */
927 sbitmap s
= sbitmap_alloc (1024);
930 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 512, 1023));
931 bitmap_set_bit (s
, 100);
933 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 512, 1023));
934 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 0, 99));
935 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 101, 1023));
936 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 1, 100));
937 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 64, 100));
938 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 100, 100));
939 ASSERT_TRUE (bitmap_bit_p (s
, 100));
943 s
= sbitmap_alloc (64);
945 bitmap_set_bit (s
, 63);
946 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 0, 63));
947 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 1, 63));
948 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 63, 63));
949 ASSERT_TRUE (bitmap_bit_p (s
, 63));
952 s
= sbitmap_alloc (1024);
954 bitmap_set_bit (s
, 128);
955 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 0, 127));
956 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 129, 1023));
958 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 0, 128));
959 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 1, 128));
960 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 128, 255));
961 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 128, 254));
962 ASSERT_TRUE (bitmap_bit_p (s
, 128));
965 bitmap_set_bit (s
, 8);
966 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 0, 8));
967 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 0, 12));
968 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 0, 63));
969 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 0, 127));
970 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 0, 512));
971 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 8, 8));
972 ASSERT_TRUE (bitmap_bit_p (s
, 8));
975 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 0, 0));
976 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 0, 8));
977 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 0, 63));
978 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 1, 63));
979 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 0, 256));
981 bitmap_set_bit (s
, 0);
982 bitmap_set_bit (s
, 16);
983 bitmap_set_bit (s
, 32);
984 bitmap_set_bit (s
, 48);
985 bitmap_set_bit (s
, 64);
986 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 0, 0));
987 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 1, 16));
988 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 48, 63));
989 ASSERT_TRUE (bitmap_bit_in_range_p (s
, 64, 64));
990 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 1, 15));
991 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 17, 31));
992 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 49, 63));
993 ASSERT_FALSE (bitmap_bit_in_range_p (s
, 65, 1023));
997 /* Run all of the selftests within this file. */
1003 test_bit_in_range ();
1006 } // namespace selftest
1007 #endif /* CHECKING_P */