2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 
   3    Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz). 
   5    This program is free software; you can redistribute it and/or modify 
   6    it under the terms of the GNU General Public License as published by 
   7    the Free Software Foundation; either version 2 of the License, or 
   8    (at your option) any later version. 
  10    This program is distributed in the hope that it will be useful, 
  11    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13    GNU General Public License for more details. 
  15    You should have received a copy of the GNU General Public License 
  16    along with this program; if not, write to the Free Software 
  17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */ 
  30 #include "bitset_stats.h" 
  33 const char * const bitset_type_names
[] = BITSET_TYPE_NAMES
; 
  36 /* Return number of bytes required to create a N_BIT bitset 
  37    of TYPE.  The bitset may grow to require more bytes than this.  */ 
  39 bitset_bytes (enum bitset_type type
, bitset_bindex n_bits
) 
  43   if (bitset_stats_enabled
) 
  44     return bitset_stats_bytes (); 
  49       bytes 
= abitset_bytes (n_bits
); 
  53       bytes 
= lbitset_bytes (n_bits
); 
  57       bytes 
= ebitset_bytes (n_bits
); 
  61       bytes 
= vbitset_bytes (n_bits
); 
  72 /* Initialise bitset BSET of TYPE for N_BITS.  */ 
  74 bitset_init (bitset bset
, bitset_bindex n_bits
, enum bitset_type type
) 
  76   if (bitset_stats_enabled
) 
  77     return bitset_stats_init (bset
, n_bits
, type
); 
  82       return abitset_init (bset
, n_bits
); 
  85       return lbitset_init (bset
, n_bits
); 
  88       return ebitset_init (bset
, n_bits
); 
  91       return vbitset_init (bset
, n_bits
); 
  99 /* Select a bitset type for a set of N_BITS and with attribute hints 
 100    specified by ATTR.  For variable size bitsets, N_BITS is only a 
 101    hint and may be zero.  */ 
 103 bitset_type_choose (bitset_bindex n_bits ATTRIBUTE_UNUSED
, unsigned int attr
) 
 105   /* Check attributes.  */ 
 106   if (attr 
& BITSET_FIXED 
&& attr 
& BITSET_VARIABLE
) 
 108   if (attr 
& BITSET_SPARSE 
&& attr 
& BITSET_DENSE
) 
 111   /* Choose the type of bitset.  Note that sometimes we will be asked 
 112   for a zero length fixed size bitset.  */ 
 115   /* If no attributes selected, choose a good compromise.  */ 
 117     return BITSET_VARRAY
; 
 119   if (attr 
& BITSET_SPARSE
) 
 122   if (attr 
& BITSET_FIXED
) 
 125   if (attr 
& BITSET_GREEDY
) 
 128   return BITSET_VARRAY
; 
 132 /* Create a bitset of N_BITS of type TYPE.  */ 
 134 bitset_alloc (bitset_bindex n_bits
, enum bitset_type type
) 
 139   bytes 
= bitset_bytes (type
, n_bits
); 
 141   bset 
= xcalloc (1, bytes
); 
 143   /* The cache is disabled until some elements are allocated.  If we 
 144      have variable length arrays, then we may need to allocate a dummy 
 147   return bitset_init (bset
, n_bits
, type
); 
 151 /* Create a bitset of N_BITS of type TYPE.  */ 
 153 bitset_obstack_alloc (struct obstack 
*bobstack
, 
 154                       bitset_bindex n_bits
, enum bitset_type type
) 
 159   bytes 
= bitset_bytes (type
, n_bits
); 
 161   bset 
= obstack_alloc (bobstack
, bytes
); 
 162   memset (bset
, 0, bytes
); 
 164   return bitset_init (bset
, n_bits
, type
); 
 168 /* Create a bitset of N_BITS and with attribute hints specified by 
 171 bitset_create (bitset_bindex n_bits
, unsigned int attr
) 
 173   enum bitset_type type
; 
 175   type 
= bitset_type_choose (n_bits
, attr
); 
 177   return bitset_alloc (n_bits
, type
); 
 181 /* Free bitset BSET.  */ 
 183 bitset_free (bitset bset
) 
 190 /* Free bitset BSET allocated on obstack.  */ 
 192 bitset_obstack_free (bitset bset
) 
 198 /* Return bitset type.  */ 
 200 bitset_type_get (bitset bset
) 
 202    enum bitset_type type
; 
 204    type 
= BITSET_TYPE_ (bset
); 
 205    if (type 
!= BITSET_STATS
) 
 208    return bitset_stats_type_get (bset
); 
 212 /* Return name of bitset type.  */ 
 214 bitset_type_name_get (bitset bset
) 
 216   enum bitset_type type
; 
 218   type 
= bitset_type_get (bset
); 
 220   return bitset_type_names
[type
]; 
 224 /* Find next bit set in SRC starting from and including BITNO. 
 225    Return BITSET_BINDEX_MAX if SRC empty.  */ 
 227 bitset_next (bitset src
, bitset_bindex bitno
) 
 230   bitset_bindex next 
= bitno
; 
 232   if (!bitset_list (src
, &val
, 1, &next
)) 
 233     return BITSET_BINDEX_MAX
; 
 238 /* Return true if both bitsets are of the same type and size.  */ 
 240 bitset_compatible_p (bitset bset1
, bitset bset2
) 
 242     return BITSET_COMPATIBLE_ (bset1
, bset2
); 
 246 /* Find previous bit set in SRC starting from and including BITNO. 
 247    Return BITSET_BINDEX_MAX if SRC empty.  */ 
 249 bitset_prev (bitset src
, bitset_bindex bitno
) 
 252   bitset_bindex next 
= bitno
; 
 254   if (!bitset_list_reverse (src
, &val
, 1, &next
)) 
 255     return BITSET_BINDEX_MAX
; 
 260 /* Find first set bit.   */ 
 262 bitset_first (bitset src
) 
 264   return bitset_next (src
, 0); 
 268 /* Find last set bit.   */ 
 270 bitset_last (bitset src
) 
 272   return bitset_prev (src
, 0); 
 276 /* Is BITNO in SRC the only set bit?  */ 
 278 bitset_only_set_p (bitset src
, bitset_bindex bitno
) 
 280   bitset_bindex val
[2]; 
 281   bitset_bindex next 
= 0; 
 283   if (bitset_list (src
, val
, 2, &next
) != 1) 
 285   return val
[0] == bitno
; 
 289 /* Print contents of bitset BSET to FILE.   */ 
 291 bitset_print (FILE *file
, bitset bset
, bool verbose
) 
 295   bitset_iterator iter
; 
 298     fprintf (file
, "n_bits = %lu, set = {", 
 299              (unsigned long int) bitset_size (bset
)); 
 302   BITSET_FOR_EACH (iter
, bset
, i
, 0) 
 306         fprintf (file
, "\n"); 
 310     fprintf (file
, "%lu ", (unsigned long int) i
); 
 311     pos 
+= 1 + (i 
>= 10) + (i 
>= 100); 
 315     fprintf (file
, "}\n"); 
 319 /* Dump bitset BSET to FILE.  */ 
 321 bitset_dump (FILE *file
, bitset bset
) 
 323   bitset_print (file
, bset
, false); 
 327 /* Release memory associated with bitsets.  */ 
 329 bitset_release_memory (void) 
 331   lbitset_release_memory (); 
 332   ebitset_release_memory (); 
 336 /* Toggle bit BITNO in bitset BSET and the new value of the bit.  */ 
 338 bitset_toggle_ (bitset bset
, bitset_bindex bitno
) 
 340   /* This routine is for completeness.  It could be optimized if 
 342   if (bitset_test (bset
, bitno
)) 
 344       bitset_reset (bset
, bitno
); 
 349       bitset_set (bset
, bitno
); 
 355 /* Return number of bits in bitset SRC.  */ 
 357 bitset_size_ (bitset src
) 
 359     return BITSET_NBITS_ (src
); 
 363 /* Return number of bits set in bitset SRC.  */ 
 365 bitset_count_ (bitset src
) 
 367   bitset_bindex list
[BITSET_LIST_SIZE
]; 
 372   /* This could be greatly sped up by adding a count method for each 
 373      bitset implementation that uses a direct technique (based on 
 374      masks) for counting the number of bits set in a word.  */ 
 377   for (count 
= 0; (num 
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
)); 
 385 /* DST = SRC.  Return true if DST != SRC. 
 386    This is a fallback for the case where SRC and DST are different 
 389 bitset_copy_ (bitset dst
, bitset src
) 
 392   bitset_iterator iter
; 
 394   /* Convert bitset types.  We assume that the DST bitset 
 395      is large enough to hold the SRC bitset.  */ 
 397   BITSET_FOR_EACH (iter
, src
, i
, 0) 
 406 /* This is a fallback for implementations that do not support 
 407    four operand operations.  */ 
 409 bitset_op4_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
, 
 412   bool changed 
= false; 
 413   bool stats_enabled_save
; 
 416   /* Create temporary bitset.  */ 
 417   stats_enabled_save 
= bitset_stats_enabled
; 
 418   bitset_stats_enabled 
= false; 
 419   tmp 
= bitset_alloc (0, bitset_type_get (dst
)); 
 420   bitset_stats_enabled 
= stats_enabled_save
; 
 424     case BITSET_OP_OR_AND
: 
 425       bitset_or (tmp
, src1
, src2
); 
 426       changed 
= bitset_and_cmp (dst
, src3
, tmp
); 
 429     case BITSET_OP_AND_OR
: 
 430       bitset_and (tmp
, src1
, src2
); 
 431       changed 
= bitset_or_cmp (dst
, src3
, tmp
); 
 434     case BITSET_OP_ANDN_OR
: 
 435       bitset_andn (tmp
, src1
, src2
); 
 436       changed 
= bitset_or_cmp (dst
, src3
, tmp
); 
 448 /* DST = (SRC1 & SRC2) | SRC3.  */ 
 450 bitset_and_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
) 
 452   bitset_and_or_cmp_ (dst
, src1
, src2
, src3
); 
 456 /* DST = (SRC1 & SRC2) | SRC3.  Return non-zero if 
 457    DST != (SRC1 & SRC2) | SRC3.  */ 
 459 bitset_and_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
) 
 461   return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
); 
 465 /* DST = (SRC1 & ~SRC2) | SRC3.  */ 
 467 bitset_andn_or_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
) 
 469   bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
); 
 473 /* DST = (SRC1 & ~SRC2) | SRC3.  Return non-zero if 
 474    DST != (SRC1 & ~SRC2) | SRC3.  */ 
 476 bitset_andn_or_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
) 
 478   return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
); 
 482 /* DST = (SRC1 | SRC2) & SRC3.  */ 
 484 bitset_or_and_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
) 
 486   bitset_or_and_cmp_ (dst
, src1
, src2
, src3
); 
 490 /* DST = (SRC1 | SRC2) & SRC3.  Return non-zero if 
 491    DST != (SRC1 | SRC2) & SRC3.  */ 
 493 bitset_or_and_cmp_ (bitset dst
, bitset src1
, bitset src2
, bitset src3
) 
 495   return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
); 
 499 /* Function to be called from debugger to print bitset.  */ 
 501 debug_bitset (bitset bset
) 
 504     bitset_print (stderr
, bset
, true);