]> git.saurik.com Git - bison.git/blame - lib/bitset_stats.c
DJGPP specific file.
[bison.git] / lib / bitset_stats.c
CommitLineData
613f5e1a 1/* Bitset statistics.
aacbb77a 2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
613f5e1a
AD
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
4
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.
9
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.
14
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
0fb669f9 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
613f5e1a
AD
18*/
19
20/* This file is a wrapper bitset implementation for the other bitset
21 implementations. It provides bitset compatibility checking and
22 statistics gathering without having to instrument the bitset
23 implementations. When statistics gathering is enabled, the bitset
24 operations get vectored through here and we then call the appropriate
d65ec44e 25 routines.
613f5e1a
AD
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "bbitset.h"
33#include "abitset.h"
34#include "ebitset.h"
35#include "lbitset.h"
eaef5507 36#include "vbitset.h"
613f5e1a
AD
37#include "bitset_stats.h"
38#include <stdlib.h>
39#include <string.h>
40#include <stdio.h>
41
c131cbff
PE
42#include "gettext.h"
43#define _(Msgid) gettext (Msgid)
613f5e1a
AD
44
45/* Configuration macros. */
46#define BITSET_STATS_FILE "bitset.dat"
47#define BITSET_LOG_COUNT_BINS 10
48#define BITSET_LOG_SIZE_BINS 16
49#define BITSET_DENSITY_BINS 20
50
51
52/* Accessor macros. */
04098407 53#define BITSET_STATS_ALLOCS_INC(TYPE) \
613f5e1a 54 bitset_stats_info->types[(TYPE)].allocs++
04098407 55#define BITSET_STATS_FREES_INC(BSET) \
613f5e1a 56 bitset_stats_info->types[BITSET_TYPE_ (BSET)].frees++
04098407 57#define BITSET_STATS_SETS_INC(BSET) \
613f5e1a 58 bitset_stats_info->types[BITSET_TYPE_ (BSET)].sets++
04098407 59#define BITSET_STATS_CACHE_SETS_INC(BSET) \
613f5e1a 60 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_sets++
04098407 61#define BITSET_STATS_RESETS_INC(BSET) \
613f5e1a 62 bitset_stats_info->types[BITSET_TYPE_ (BSET)].resets++
04098407 63#define BITSET_STATS_CACHE_RESETS_INC(BSET) \
613f5e1a 64 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_resets++
04098407 65#define BITSET_STATS_TESTS_INC(BSET) \
613f5e1a 66 bitset_stats_info->types[BITSET_TYPE_ (BSET)].tests++
04098407 67#define BITSET_STATS_CACHE_TESTS_INC(BSET) \
613f5e1a 68 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_tests++
04098407 69#define BITSET_STATS_LISTS_INC(BSET) \
613f5e1a 70 bitset_stats_info->types[BITSET_TYPE_ (BSET)].lists++
04098407 71#define BITSET_STATS_LIST_COUNTS_INC(BSET, I) \
613f5e1a 72 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_counts[(I)]++
04098407 73#define BITSET_STATS_LIST_SIZES_INC(BSET, I) \
613f5e1a 74 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_sizes[(I)]++
04098407 75#define BITSET_STATS_LIST_DENSITY_INC(BSET, I) \
613f5e1a
AD
76 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_density[(I)]++
77
78
613f5e1a
AD
79struct bitset_type_info_struct
80{
81 unsigned int allocs;
82 unsigned int frees;
83 unsigned int lists;
84 unsigned int sets;
85 unsigned int cache_sets;
86 unsigned int resets;
87 unsigned int cache_resets;
88 unsigned int tests;
89 unsigned int cache_tests;
90 unsigned int list_counts[BITSET_LOG_COUNT_BINS];
91 unsigned int list_sizes[BITSET_LOG_SIZE_BINS];
92 unsigned int list_density[BITSET_DENSITY_BINS];
93};
94
95struct bitset_stats_info_struct
96{
97 unsigned int runs;
98 struct bitset_type_info_struct types[BITSET_TYPE_NUM];
99};
100
101
102struct bitset_stats_info_struct bitset_stats_info_data;
103struct bitset_stats_info_struct *bitset_stats_info;
d0829076 104bool bitset_stats_enabled = false;
613f5e1a
AD
105
106
613f5e1a
AD
107/* Print a percentage histogram with message MSG to FILE. */
108static void
d65ec44e
PE
109bitset_percent_histogram_print (FILE *file, const char *name, const char *msg,
110 unsigned int n_bins, unsigned int *bins)
613f5e1a
AD
111{
112 unsigned int i;
113 unsigned int total;
114
115 total = 0;
116 for (i = 0; i < n_bins; i++)
117 total += bins[i];
118
119 if (!total)
120 return;
121
122 fprintf (file, "%s %s", name, msg);
123 for (i = 0; i < n_bins; i++)
2d382ea8 124 fprintf (file, "%.0f-%.0f%%\t%8u (%5.1f%%)\n",
613f5e1a
AD
125 i * 100.0 / n_bins,
126 (i + 1) * 100.0 / n_bins, bins[i],
127 (100.0 * bins[i]) / total);
128}
129
130
131/* Print a log histogram with message MSG to FILE. */
132static void
d65ec44e
PE
133bitset_log_histogram_print (FILE *file, const char *name, const char *msg,
134 unsigned int n_bins, unsigned int *bins)
613f5e1a
AD
135{
136 unsigned int i;
137 unsigned int total;
138 unsigned int max_width;
613f5e1a
AD
139
140 total = 0;
141 for (i = 0; i < n_bins; i++)
142 total += bins[i];
143
144 if (!total)
145 return;
146
6aa452a6 147 /* Determine number of useful bins. */
613f5e1a
AD
148 for (i = n_bins; i > 3 && ! bins[i - 1]; i--)
149 continue;
6aa452a6 150 n_bins = i;
613f5e1a
AD
151
152 /* 2 * ceil (log10 (2) * (N - 1)) + 1. */
153 max_width = 2 * (unsigned int) (0.30103 * (n_bins - 1) + 0.9999) + 1;
154
155 fprintf (file, "%s %s", name, msg);
156 for (i = 0; i < 2; i++)
2d382ea8 157 fprintf (file, "%*d\t%8u (%5.1f%%)\n",
613f5e1a
AD
158 max_width, i, bins[i], 100.0 * bins[i] / total);
159
6aa452a6 160 for (; i < n_bins; i++)
2d382ea8 161 fprintf (file, "%*lu-%lu\t%8u (%5.1f%%)\n",
613f5e1a 162 max_width - ((unsigned int) (0.30103 * (i) + 0.9999) + 1),
779e7ceb
PE
163 1UL << (i - 1),
164 (1UL << i) - 1,
2d382ea8 165 bins[i],
613f5e1a
AD
166 (100.0 * bins[i]) / total);
167}
168
169
170/* Print bitset statistics to FILE. */
171static void
d65ec44e
PE
172bitset_stats_print_1 (FILE *file, const char *name,
173 struct bitset_type_info_struct *stats)
613f5e1a
AD
174{
175 if (!stats)
176 return;
d65ec44e 177
613f5e1a 178 fprintf (file, "%s:\n", name);
2d382ea8 179 fprintf (file, _("%u bitset_allocs, %u freed (%.2f%%).\n"),
613f5e1a
AD
180 stats->allocs, stats->frees,
181 stats->allocs ? 100.0 * stats->frees / stats->allocs : 0);
2d382ea8 182 fprintf (file, _("%u bitset_sets, %u cached (%.2f%%)\n"),
613f5e1a
AD
183 stats->sets, stats->cache_sets,
184 stats->sets ? 100.0 * stats->cache_sets / stats->sets : 0);
2d382ea8 185 fprintf (file, _("%u bitset_resets, %u cached (%.2f%%)\n"),
613f5e1a
AD
186 stats->resets, stats->cache_resets,
187 stats->resets ? 100.0 * stats->cache_resets / stats->resets : 0);
2d382ea8 188 fprintf (file, _("%u bitset_tests, %u cached (%.2f%%)\n"),
613f5e1a
AD
189 stats->tests, stats->cache_tests,
190 stats->tests ? 100.0 * stats->cache_tests / stats->tests : 0);
191
2d382ea8 192 fprintf (file, _("%u bitset_lists\n"), stats->lists);
613f5e1a 193
c131cbff 194 bitset_log_histogram_print (file, name, _("count log histogram\n"),
613f5e1a
AD
195 BITSET_LOG_COUNT_BINS, stats->list_counts);
196
c131cbff 197 bitset_log_histogram_print (file, name, _("size log histogram\n"),
613f5e1a
AD
198 BITSET_LOG_SIZE_BINS, stats->list_sizes);
199
c131cbff 200 bitset_percent_histogram_print (file, name, _("density histogram\n"),
613f5e1a
AD
201 BITSET_DENSITY_BINS, stats->list_density);
202}
203
204
205/* Print all bitset statistics to FILE. */
206static void
d0829076 207bitset_stats_print (FILE *file, bool verbose ATTRIBUTE_UNUSED)
613f5e1a
AD
208{
209 int i;
613f5e1a
AD
210
211 if (!bitset_stats_info)
212 return;
213
c131cbff 214 fprintf (file, _("Bitset statistics:\n\n"));
613f5e1a
AD
215
216 if (bitset_stats_info->runs > 1)
2d382ea8 217 fprintf (file, _("Accumulated runs = %u\n"), bitset_stats_info->runs);
613f5e1a
AD
218
219 for (i = 0; i < BITSET_TYPE_NUM; i++)
d65ec44e 220 bitset_stats_print_1 (file, bitset_type_names[i],
6aa452a6 221 &bitset_stats_info->types[i]);
613f5e1a
AD
222}
223
224
225/* Initialise bitset statistics logging. */
226void
d65ec44e 227bitset_stats_enable (void)
613f5e1a
AD
228{
229 if (!bitset_stats_info)
230 bitset_stats_info = &bitset_stats_info_data;
d0829076 231 bitset_stats_enabled = true;
613f5e1a
AD
232}
233
234
235void
d65ec44e 236bitset_stats_disable (void)
613f5e1a 237{
d0829076 238 bitset_stats_enabled = false;
613f5e1a
AD
239}
240
241
242/* Read bitset statistics file. */
243void
48b16bbc 244bitset_stats_read (const char *file_name)
613f5e1a
AD
245{
246 FILE *file;
247
248 if (!bitset_stats_info)
249 return;
d65ec44e 250
48b16bbc
PE
251 if (!file_name)
252 file_name = BITSET_STATS_FILE;
613f5e1a 253
48b16bbc 254 file = fopen (file_name, "r");
613f5e1a
AD
255 if (file)
256 {
257 if (fread (&bitset_stats_info_data, sizeof (bitset_stats_info_data),
258 1, file) != 1)
259 {
260 if (ferror (file))
c131cbff 261 perror (_("Could not read stats file."));
613f5e1a 262 else
c131cbff 263 fprintf (stderr, _("Bad stats file size.\n"));
613f5e1a 264 }
11a71262
PE
265 if (fclose (file) != 0)
266 perror (_("Could not read stats file."));
613f5e1a
AD
267 }
268 bitset_stats_info_data.runs++;
269}
270
271
272/* Write bitset statistics file. */
273void
48b16bbc 274bitset_stats_write (const char *file_name)
613f5e1a
AD
275{
276 FILE *file;
277
278 if (!bitset_stats_info)
279 return;
280
48b16bbc
PE
281 if (!file_name)
282 file_name = BITSET_STATS_FILE;
613f5e1a 283
48b16bbc 284 file = fopen (file_name, "w");
613f5e1a
AD
285 if (file)
286 {
287 if (fwrite (&bitset_stats_info_data, sizeof (bitset_stats_info_data),
288 1, file) != 1)
c131cbff 289 perror (_("Could not write stats file."));
11a71262
PE
290 if (fclose (file) != 0)
291 perror (_("Could not write stats file."));
613f5e1a
AD
292 }
293 else
c131cbff 294 perror (_("Could not open stats file for writing."));
613f5e1a
AD
295}
296
297
298/* Dump bitset statistics to FILE. */
299void
d65ec44e 300bitset_stats_dump (FILE *file)
613f5e1a 301{
d0829076 302 bitset_stats_print (file, false);
613f5e1a
AD
303}
304
305
306/* Function to be called from debugger to print bitset stats. */
307void
308debug_bitset_stats (void)
309{
d0829076 310 bitset_stats_print (stderr, true);
613f5e1a
AD
311}
312
313
314static void
d65ec44e 315bitset_stats_set (bitset dst, bitset_bindex bitno)
613f5e1a
AD
316{
317 bitset bset = dst->s.bset;
c131cbff
PE
318 bitset_windex wordno = bitno / BITSET_WORD_BITS;
319 bitset_windex offset = wordno - bset->b.cindex;
d65ec44e 320
613f5e1a
AD
321 BITSET_STATS_SETS_INC (bset);
322
323 if (offset < bset->b.csize)
324 {
c131cbff 325 bset->b.cdata[offset] |= (bitset_word) 1 << (bitno % BITSET_WORD_BITS);
613f5e1a
AD
326 BITSET_STATS_CACHE_SETS_INC (bset);
327 }
328 else
329 BITSET_SET_ (bset, bitno);
330}
331
332
333static void
d65ec44e 334bitset_stats_reset (bitset dst, bitset_bindex bitno)
613f5e1a
AD
335{
336 bitset bset = dst->s.bset;
c131cbff
PE
337 bitset_windex wordno = bitno / BITSET_WORD_BITS;
338 bitset_windex offset = wordno - bset->b.cindex;
d65ec44e 339
613f5e1a
AD
340 BITSET_STATS_RESETS_INC (bset);
341
342 if (offset < bset->b.csize)
343 {
c131cbff
PE
344 bset->b.cdata[offset] &=
345 ~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
613f5e1a
AD
346 BITSET_STATS_CACHE_RESETS_INC (bset);
347 }
348 else
349 BITSET_RESET_ (bset, bitno);
350}
351
352
d0829076 353static bool
d65ec44e 354bitset_stats_toggle (bitset src, bitset_bindex bitno)
6aa452a6
AD
355{
356 return BITSET_TOGGLE_ (src->s.bset, bitno);
357}
358
359
d0829076 360static bool
d65ec44e 361bitset_stats_test (bitset src, bitset_bindex bitno)
613f5e1a
AD
362{
363 bitset bset = src->s.bset;
c131cbff
PE
364 bitset_windex wordno = bitno / BITSET_WORD_BITS;
365 bitset_windex offset = wordno - bset->b.cindex;
613f5e1a
AD
366
367 BITSET_STATS_TESTS_INC (bset);
d65ec44e 368
613f5e1a
AD
369 if (offset < bset->b.csize)
370 {
371 BITSET_STATS_CACHE_TESTS_INC (bset);
372 return (bset->b.cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1;
373 }
374 else
375 return BITSET_TEST_ (bset, bitno);
376}
377
378
eaef5507
PE
379static bitset_bindex
380bitset_stats_resize (bitset src, bitset_bindex size)
381{
382 return BITSET_RESIZE_ (src->s.bset, size);
383}
384
385
2d382ea8 386static bitset_bindex
d65ec44e 387bitset_stats_size (bitset src)
613f5e1a
AD
388{
389 return BITSET_SIZE_ (src->s.bset);
390}
391
392
2d382ea8 393static bitset_bindex
d65ec44e 394bitset_stats_count (bitset src)
6aa452a6
AD
395{
396 return BITSET_COUNT_ (src->s.bset);
397}
398
399
d0829076 400static bool
d65ec44e 401bitset_stats_empty_p (bitset dst)
6aa452a6
AD
402{
403 return BITSET_EMPTY_P_ (dst->s.bset);
404}
405
406
407static void
d65ec44e 408bitset_stats_ones (bitset dst)
6aa452a6
AD
409{
410 BITSET_ONES_ (dst->s.bset);
411}
412
413
414static void
d65ec44e 415bitset_stats_zero (bitset dst)
6aa452a6
AD
416{
417 BITSET_ZERO_ (dst->s.bset);
418}
419
420
421static void
d65ec44e 422bitset_stats_copy (bitset dst, bitset src)
6aa452a6
AD
423{
424 BITSET_CHECK2_ (dst, src);
425 BITSET_COPY_ (dst->s.bset, src->s.bset);
426}
427
428
d0829076 429static bool
d65ec44e 430bitset_stats_disjoint_p (bitset dst, bitset src)
613f5e1a 431{
6aa452a6
AD
432 BITSET_CHECK2_ (dst, src);
433 return BITSET_DISJOINT_P_ (dst->s.bset, src->s.bset);
613f5e1a
AD
434}
435
436
d0829076 437static bool
d65ec44e 438bitset_stats_equal_p (bitset dst, bitset src)
613f5e1a
AD
439{
440 BITSET_CHECK2_ (dst, src);
6aa452a6
AD
441 return BITSET_EQUAL_P_ (dst->s.bset, src->s.bset);
442}
443
444
445static void
d65ec44e 446bitset_stats_not (bitset dst, bitset src)
6aa452a6
AD
447{
448 BITSET_CHECK2_ (dst, src);
449 BITSET_NOT_ (dst->s.bset, src->s.bset);
450}
451
452
d0829076 453static bool
d65ec44e 454bitset_stats_subset_p (bitset dst, bitset src)
6aa452a6
AD
455{
456 BITSET_CHECK2_ (dst, src);
457 return BITSET_SUBSET_P_ (dst->s.bset, src->s.bset);
458}
459
460
461static void
d65ec44e 462bitset_stats_and (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
463{
464 BITSET_CHECK3_ (dst, src1, src2);
465 BITSET_AND_ (dst->s.bset, src1->s.bset, src2->s.bset);
466}
467
468
d0829076 469static bool
d65ec44e 470bitset_stats_and_cmp (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
471{
472 BITSET_CHECK3_ (dst, src1, src2);
473 return BITSET_AND_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
474}
475
476
477static void
d65ec44e 478bitset_stats_andn (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
479{
480 BITSET_CHECK3_ (dst, src1, src2);
481 BITSET_ANDN_ (dst->s.bset, src1->s.bset, src2->s.bset);
482}
483
484
d0829076 485static bool
d65ec44e 486bitset_stats_andn_cmp (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
487{
488 BITSET_CHECK3_ (dst, src1, src2);
489 return BITSET_ANDN_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
490}
491
492
493static void
d65ec44e 494bitset_stats_or (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
495{
496 BITSET_CHECK3_ (dst, src1, src2);
497 BITSET_OR_ (dst->s.bset, src1->s.bset, src2->s.bset);
498}
499
500
d0829076 501static bool
d65ec44e 502bitset_stats_or_cmp (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
503{
504 BITSET_CHECK3_ (dst, src1, src2);
505 return BITSET_OR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
506}
507
508
509static void
d65ec44e 510bitset_stats_xor (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
511{
512 BITSET_CHECK3_ (dst, src1, src2);
513 BITSET_XOR_ (dst->s.bset, src1->s.bset, src2->s.bset);
613f5e1a
AD
514}
515
516
d0829076 517static bool
d65ec44e 518bitset_stats_xor_cmp (bitset dst, bitset src1, bitset src2)
613f5e1a
AD
519{
520 BITSET_CHECK3_ (dst, src1, src2);
6aa452a6
AD
521 return BITSET_XOR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
522}
523
524
525static void
d65ec44e 526bitset_stats_and_or (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
527{
528 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6 529 BITSET_AND_OR_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
613f5e1a
AD
530}
531
532
d0829076 533static bool
d65ec44e 534bitset_stats_and_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
613f5e1a
AD
535{
536 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6
AD
537 return BITSET_AND_OR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
538}
539
540
541static void
d65ec44e 542bitset_stats_andn_or (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
543{
544 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6
AD
545 BITSET_ANDN_OR_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
546}
547
548
d0829076 549static bool
d65ec44e 550bitset_stats_andn_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
551{
552 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6
AD
553 return BITSET_ANDN_OR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
554}
555
556
557static void
d65ec44e 558bitset_stats_or_and (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
559{
560 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6
AD
561 BITSET_OR_AND_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
562}
563
564
d0829076 565static bool
d65ec44e 566bitset_stats_or_and_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
567{
568 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6 569 return BITSET_OR_AND_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
613f5e1a
AD
570}
571
572
2d382ea8 573static bitset_bindex
d65ec44e
PE
574bitset_stats_list (bitset bset, bitset_bindex *list,
575 bitset_bindex num, bitset_bindex *next)
613f5e1a
AD
576{
577 bitset_bindex count;
578 bitset_bindex tmp;
579 bitset_bindex size;
580 bitset_bindex i;
eaef5507 581 enum bitset_type type;
613f5e1a
AD
582
583 count = BITSET_LIST_ (bset->s.bset, list, num, next);
d65ec44e 584
eaef5507 585 type = BITSET_TYPE_ (bset->s.bset);
613f5e1a 586 BITSET_STATS_LISTS_INC (bset->s.bset);
d65ec44e 587
613f5e1a
AD
588 /* Log histogram of number of set bits. */
589 for (i = 0, tmp = count; tmp; tmp >>= 1, i++)
590 continue;
591 if (i >= BITSET_LOG_COUNT_BINS)
592 i = BITSET_LOG_COUNT_BINS - 1;
593 BITSET_STATS_LIST_COUNTS_INC (bset->s.bset, i);
d65ec44e 594
613f5e1a
AD
595 /* Log histogram of number of bits in set. */
596 size = BITSET_SIZE_ (bset->s.bset);
597 for (i = 0, tmp = size; tmp; tmp >>= 1, i++)
598 continue;
599 if (i >= BITSET_LOG_SIZE_BINS)
600 i = BITSET_LOG_SIZE_BINS - 1;
601 BITSET_STATS_LIST_SIZES_INC (bset->s.bset, i);
d65ec44e 602
613f5e1a
AD
603 /* Histogram of fraction of bits set. */
604 i = size ? (count * BITSET_DENSITY_BINS) / size : 0;
605 if (i >= BITSET_DENSITY_BINS)
606 i = BITSET_DENSITY_BINS - 1;
607 BITSET_STATS_LIST_DENSITY_INC (bset->s.bset, i);
608 return count;
609}
610
611
2d382ea8 612static bitset_bindex
d65ec44e
PE
613bitset_stats_list_reverse (bitset bset, bitset_bindex *list,
614 bitset_bindex num, bitset_bindex *next)
613f5e1a 615{
6aa452a6 616 return BITSET_LIST_REVERSE_ (bset->s.bset, list, num, next);
613f5e1a
AD
617}
618
619
620static void
d65ec44e 621bitset_stats_free (bitset bset)
613f5e1a
AD
622{
623 BITSET_STATS_FREES_INC (bset->s.bset);
624 BITSET_FREE_ (bset->s.bset);
625}
626
627
6aa452a6 628struct bitset_vtable bitset_stats_vtable = {
613f5e1a
AD
629 bitset_stats_set,
630 bitset_stats_reset,
6aa452a6 631 bitset_stats_toggle,
613f5e1a 632 bitset_stats_test,
eaef5507 633 bitset_stats_resize,
613f5e1a 634 bitset_stats_size,
6aa452a6
AD
635 bitset_stats_count,
636 bitset_stats_empty_p,
637 bitset_stats_ones,
638 bitset_stats_zero,
639 bitset_stats_copy,
640 bitset_stats_disjoint_p,
641 bitset_stats_equal_p,
642 bitset_stats_not,
643 bitset_stats_subset_p,
644 bitset_stats_and,
645 bitset_stats_and_cmp,
646 bitset_stats_andn,
647 bitset_stats_andn_cmp,
648 bitset_stats_or,
649 bitset_stats_or_cmp,
650 bitset_stats_xor,
651 bitset_stats_xor_cmp,
652 bitset_stats_and_or,
653 bitset_stats_and_or_cmp,
654 bitset_stats_andn_or,
655 bitset_stats_andn_or_cmp,
656 bitset_stats_or_and,
657 bitset_stats_or_and_cmp,
613f5e1a 658 bitset_stats_list,
6aa452a6 659 bitset_stats_list_reverse,
613f5e1a
AD
660 bitset_stats_free,
661 BITSET_STATS
662};
663
664
665/* Return enclosed bitset type. */
666enum bitset_type
d65ec44e 667bitset_stats_type_get (bitset bset)
613f5e1a
AD
668{
669 return BITSET_TYPE_ (bset->s.bset);
670}
671
672
2d382ea8
PE
673size_t
674bitset_stats_bytes (void)
613f5e1a 675{
55e728eb 676 return sizeof (struct bitset_stats_struct);
613f5e1a
AD
677}
678
679
680bitset
d65ec44e 681bitset_stats_init (bitset bset, bitset_bindex n_bits, enum bitset_type type)
613f5e1a 682{
2d382ea8 683 size_t bytes;
613f5e1a
AD
684 bitset sbset;
685
6aa452a6 686 bset->b.vtable = &bitset_stats_vtable;
613f5e1a
AD
687
688 /* Disable cache. */
689 bset->b.cindex = 0;
690 bset->b.csize = 0;
691 bset->b.cdata = 0;
692
eaef5507
PE
693 BITSET_NBITS_ (bset) = n_bits;
694
613f5e1a
AD
695 /* Set up the actual bitset implementation that
696 we are a wrapper over. */
697 switch (type)
698 {
699 case BITSET_ARRAY:
700 bytes = abitset_bytes (n_bits);
89519adf 701 sbset = xcalloc (1, bytes);
613f5e1a
AD
702 abitset_init (sbset, n_bits);
703 break;
704
705 case BITSET_LIST:
706 bytes = lbitset_bytes (n_bits);
89519adf 707 sbset = xcalloc (1, bytes);
613f5e1a
AD
708 lbitset_init (sbset, n_bits);
709 break;
710
711 case BITSET_TABLE:
712 bytes = ebitset_bytes (n_bits);
89519adf 713 sbset = xcalloc (1, bytes);
613f5e1a
AD
714 ebitset_init (sbset, n_bits);
715 break;
716
eaef5507
PE
717 case BITSET_VARRAY:
718 bytes = vbitset_bytes (n_bits);
89519adf 719 sbset = xcalloc (1, bytes);
eaef5507
PE
720 vbitset_init (sbset, n_bits);
721 break;
722
613f5e1a
AD
723 default:
724 abort ();
725 }
726
727 bset->s.bset = sbset;
728
729 BITSET_STATS_ALLOCS_INC (type);
d65ec44e 730
613f5e1a
AD
731 return bset;
732}