2 Copyright (C) 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "bitset_stats.h"
31 const char * const bitset_type_names
[] = BITSET_TYPE_NAMES
;
33 static void bitset_print
PARAMS ((FILE *, bitset
, int));
34 static int bitset_op4_cmp
PARAMS ((bitset
, bitset
, bitset
, bitset
, int));
37 /* Return number of bytes required to create a N_BIT bitset
38 of TYPE. The bitset may grow to require more bytes than this. */
40 bitset_bytes (type
, n_bits
)
41 enum_bitset_type type
;
46 if (bitset_stats_enabled
)
47 return bitset_stats_bytes ();
52 bytes
= abitset_bytes (n_bits
);
56 bytes
= lbitset_bytes (n_bits
);
60 bytes
= ebitset_bytes (n_bits
);
71 /* Initialise bitset BSET of TYPE for N_BITS. */
73 bitset_init (bset
, n_bits
, type
)
76 enum_bitset_type type
;
78 if (bitset_stats_enabled
)
79 return bitset_stats_init (bset
, n_bits
, type
);
84 return abitset_init (bset
, n_bits
);
87 return lbitset_init (bset
, n_bits
);
90 return ebitset_init (bset
, n_bits
);
98 /* Select a bitset type for a set of N_BITS and with attribute hints
99 specified by ATTR. For variable size bitsets, N_BITS is only a
100 hint and may be zero. */
102 bitset_type_choose (n_bits
, attr
)
103 bitset_bindex n_bits ATTRIBUTE_UNUSED
;
106 enum bitset_type type
;
108 /* Check attributes. */
109 if (attr
& BITSET_FIXED
&& attr
& BITSET_VARIABLE
)
111 if (attr
& BITSET_SPARSE
&& attr
& BITSET_DENSE
)
114 /* Choose the type of bitset. Note that sometimes we will be asked
115 for a zero length fixed size bitset. */
118 /* Currently, the simple bitsets do not support a variable size. */
119 if (attr
& BITSET_VARIABLE
|| attr
& BITSET_SPARSE
)
122 if (attr
& BITSET_DENSE
|| attr
& BITSET_GREEDY
)
130 /* Create a bitset of N_BITS of type TYPE. */
132 bitset_alloc (n_bits
, type
)
133 bitset_bindex n_bits
;
134 enum_bitset_type type
;
139 bytes
= bitset_bytes (type
, n_bits
);
141 bset
= (bitset
) 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 (bobstack
, n_bits
, type
)
154 struct obstack
*bobstack
;
155 bitset_bindex n_bits
;
156 enum_bitset_type type
;
161 bytes
= bitset_bytes (type
, n_bits
);
163 bset
= obstack_alloc (bobstack
, bytes
);
164 memset (bset
, 0, bytes
);
166 return bitset_init (bset
, n_bits
, type
);
170 /* Create a bitset of N_BITS and with attribute hints specified by
173 bitset_create (n_bits
, attr
)
174 bitset_bindex n_bits
;
177 enum bitset_type type
;
179 type
= bitset_type_choose (n_bits
, attr
);
181 return bitset_alloc (n_bits
, type
);
185 /* Free bitset BSET. */
195 /* Free bitset BSET allocated on obstack. */
197 bitset_obstack_free (bset
)
204 /* Return bitset type. */
206 bitset_type_get (bset
)
209 enum bitset_type type
;
211 type
= BITSET_TYPE_ (bset
);
212 if (type
!= BITSET_STATS
)
215 return bitset_stats_type_get (bset
);
219 /* Return name of bitset type. */
221 bitset_type_name_get (bset
)
224 enum bitset_type type
;
226 type
= bitset_type_get (bset
);
228 return bitset_type_names
[type
];
232 /* Find next bit set in SRC starting from and including BITNO.
233 Return BITSET_BINDEX_MAX if SRC empty. */
235 bitset_next (src
, bitno
)
240 bitset_bindex next
= bitno
;
242 if (!bitset_list (src
, &val
, 1, &next
))
243 return BITSET_BINDEX_MAX
;
248 /* Find previous bit set in SRC starting from and including BITNO.
249 Return BITSET_BINDEX_MAX if SRC empty. */
251 bitset_prev (src
, bitno
)
256 bitset_bindex next
= bitno
;
258 if (!bitset_list_reverse (src
, &val
, 1, &next
))
259 return BITSET_BINDEX_MAX
;
264 /* Find first set bit. */
269 return bitset_next (src
, 0);
273 /* Find last set bit. */
278 return bitset_prev (src
, 0);
282 /* Return non-zero if BITNO in SRC is the only set bit. */
284 bitset_only_set_p (src
, bitno
)
288 bitset_bindex val
[2];
289 bitset_bindex next
= 0;
291 if (bitset_list (src
, val
, 2, &next
) != 1)
293 return val
[0] == bitno
;
297 /* Print contents of bitset BSET to FILE. */
299 bitset_print (file
, bset
, verbose
)
306 bitset_iterator iter
;
309 fprintf (file
, "n_bits = %lu, set = {",
310 (unsigned long) bitset_size (bset
));
313 BITSET_FOR_EACH (iter
, bset
, i
, 0)
317 fprintf (file
, "\n");
321 fprintf (file
, "%d ", i
);
322 pos
+= 1 + (i
>= 10) + (i
>= 100);
326 fprintf (file
, "}\n");
330 /* Dump bitset BSET to FILE. */
332 bitset_dump (file
, bset
)
336 bitset_print (file
, bset
, 0);
341 /* Release memory associated with bitsets. */
343 bitset_release_memory ()
345 lbitset_release_memory ();
346 ebitset_release_memory ();
351 /* Toggle bit BITNO in bitset BSET and return non-zero if not set. */
353 bitset_toggle_ (bset
, bitno
)
357 /* This routine is for completeness. It could be optimized if
359 if (bitset_test (bset
, bitno
))
361 bitset_reset (bset
, bitno
);
366 bitset_set (bset
, bitno
);
372 /* Return number of bits set in bitset SRC. */
377 bitset_bindex list
[BITSET_LIST_SIZE
];
382 /* This could be greatly sped up by adding a count method for each
383 bitset implementation that uses a direct technique (based on
384 masks) for counting the number of bits set in a word. */
387 for (count
= 0; (num
= bitset_list (src
, list
, BITSET_LIST_SIZE
, &next
));
395 /* DST = SRC. Return non-zero if DST != SRC.
396 This is a fallback for the case where SRC and DST are different
399 bitset_copy_ (dst
, src
)
404 bitset_iterator iter
;
406 /* Convert bitset types. We assume that the DST bitset
407 is large enough to hold the SRC bitset. */
409 BITSET_FOR_EACH (iter
, src
, i
, 0)
418 /* This is a fallback for implementations that do not support
419 four operand operations. */
421 bitset_op4_cmp (dst
, src1
, src2
, src3
, op
)
429 int stats_enabled_save
;
432 /* Create temporary bitset. */
433 stats_enabled_save
= bitset_stats_enabled
;
434 bitset_stats_enabled
= 0;
435 tmp
= bitset_alloc (0, bitset_type_get (dst
));
436 bitset_stats_enabled
= stats_enabled_save
;
440 case BITSET_OP_OR_AND
:
441 bitset_or (tmp
, src1
, src2
);
442 changed
= bitset_and_cmp (dst
, src3
, tmp
);
445 case BITSET_OP_AND_OR
:
446 bitset_and (tmp
, src1
, src2
);
447 changed
= bitset_or_cmp (dst
, src3
, tmp
);
450 case BITSET_OP_ANDN_OR
:
451 bitset_andn (tmp
, src1
, src2
);
452 changed
= bitset_or_cmp (dst
, src3
, tmp
);
464 /* DST = (SRC1 & SRC2) | SRC3. */
466 bitset_and_or_ (dst
, src1
, src2
, src3
)
472 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
);
476 /* DST = (SRC1 & SRC2) | SRC3. Return non-zero if
477 DST != (SRC1 & SRC2) | SRC3. */
479 bitset_and_or_cmp_ (dst
, src1
, src2
, src3
)
485 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_AND_OR
);
489 /* DST = (SRC1 & ~SRC2) | SRC3. */
491 bitset_andn_or_ (dst
, src1
, src2
, src3
)
497 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
);
501 /* DST = (SRC1 & ~SRC2) | SRC3. Return non-zero if
502 DST != (SRC1 & ~SRC2) | SRC3. */
504 bitset_andn_or_cmp_ (dst
, src1
, src2
, src3
)
510 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_ANDN_OR
);
514 /* DST = (SRC1 | SRC2) & SRC3. */
516 bitset_or_and_ (dst
, src1
, src2
, src3
)
522 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
);
526 /* DST = (SRC1 | SRC2) & SRC3. Return non-zero if
527 DST != (SRC1 | SRC2) & SRC3. */
529 bitset_or_and_cmp_ (dst
, src1
, src2
, src3
)
535 return bitset_op4_cmp (dst
, src1
, src2
, src3
, BITSET_OP_OR_AND
);
539 /* Function to be called from debugger to print bitset. */
545 bitset_print (stderr
, bset
, 1);