3 * File name: ir/ir/distrib.c
4 * Purpose: Statistics for Firm. Distribution tables.
8 * Copyright: (c) 2004 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
18 #include "firmstat_t.h"
21 * calculates a hash value for an address
23 static unsigned addr_hash(const void *object)
25 return HASH_PTR(object);
29 * calculates a hash value for an integer
31 static unsigned int_hash(const void *object)
33 return (unsigned)PTR_TO_INT(object);
37 * compare function for integer distribution tables
39 static int int_cmp_fun(const void *elt, const void *key)
48 * create a new distribution table
50 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
54 res = xmalloc(sizeof(*res));
56 obstack_init(&res->cnts);
58 /* create the hash-table */
59 res->hash_map = new_pset(cmp_func, 8);
60 res->hash_func = hash_func ? hash_func : addr_hash;
67 * create a new distribution table for an integer distribution
69 distrib_tbl_t *stat_new_int_distrib_tbl(void)
71 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
80 * destroy a distribution table
82 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
85 /* free all entries */
86 obstack_free(&tbl->cnts, NULL);
88 /* delete the hash table */
89 del_pset(tbl->hash_map);
94 * Returns the associates distrib_entry_t for an object
96 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
99 distrib_entry_t *elem;
103 elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
107 elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
112 elem->object = object;
114 return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
118 * adds a new object count into the distribution table
120 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
122 distrib_entry_t *elem = distrib_get_entry(tbl, object);
124 cnt_add(&elem->cnt, cnt);
128 * adds a new key count into the integer distribution table
130 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
132 stat_add_distrib_tbl(tbl, (const void *)key, cnt);
136 * calculates the mean value of a distribution
138 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
140 distrib_entry_t *entry;
145 /* integer distribution, need min, max */
148 entry = pset_first(tbl->hash_map);
154 max = (int)entry->object;
155 sum = cnt_to_dbl(&entry->cnt);
158 for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
159 int value = (int)entry->object;
166 sum += cnt_to_dbl(&entry->cnt);
168 count = max - min + 1;
173 for (entry = pset_first(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
174 sum += cnt_to_dbl(&entry->cnt);
179 return count ? sum / (double)count : 0.0;
183 * iterates over all entries in a distribution table
185 void stat_iterate_distrib_tbl(distrib_tbl_t *tbl, eval_distrib_entry_fun eval)
187 distrib_entry_t *entry;
189 for (entry = pset_first(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {