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"
20 static const counter_t _cnt_0 = { { 0 } };
23 * calculates a hash value for an address
25 static unsigned addr_hash(const void *object)
27 return HASH_PTR(object);
31 * calculates a hash value for an integer
33 static unsigned int_hash(const void *object)
35 return (unsigned)PTR_TO_INT(object);
39 * compare function for integer distribution tables
41 static int int_cmp_fun(const void *elt, const void *key)
43 const distrib_entry_t *p1 = elt;
44 const distrib_entry_t *p2 = key;
46 return (char *)p1->object - (char *)p2->object;
50 * create a new distribution table
52 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
56 res = xmalloc(sizeof(*res));
58 obstack_init(&res->cnts);
60 /* create the hash-table */
61 res->hash_map = new_pset(cmp_func, 8);
62 res->hash_func = hash_func ? hash_func : addr_hash;
69 * create a new distribution table for an integer distribution
71 distrib_tbl_t *stat_new_int_distrib_tbl(void)
73 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
82 * destroy a distribution table
84 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
87 /* free all entries */
88 obstack_free(&tbl->cnts, NULL);
90 /* delete the hash table */
91 del_pset(tbl->hash_map);
96 * Returns the associates distrib_entry_t for an object
98 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
101 distrib_entry_t *elem;
105 elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
109 elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
114 elem->object = object;
116 return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
120 * adds a new object count into the distribution table
122 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
124 distrib_entry_t *elem = distrib_get_entry(tbl, object);
126 cnt_add(&elem->cnt, cnt);
130 * adds a new key count into the integer distribution table
132 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
134 stat_add_distrib_tbl(tbl, (const void *)key, cnt);
138 * increases object count by one
140 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object)
142 distrib_entry_t *elem = distrib_get_entry(tbl, object);
148 * increases key count by one
150 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key)
152 stat_inc_distrib_tbl(tbl, (const void *)key);
157 * inserts a new object with count 0 into the distribution table
158 * if object is already present, nothing happens
160 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object)
162 distrib_entry_t *elem = distrib_get_entry(tbl, object);
164 cnt_add(&elem->cnt, &_cnt_0);
168 * inserts a new key with count 0 into the integer distribution table
169 * if key is already present, nothing happens
171 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key)
173 stat_insert_distrib_tbl(tbl, (const void *)key);
177 * returns the sum over all counters in a distribution table
179 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl)
181 distrib_entry_t *entry;
184 foreach_pset(tbl->hash_map, entry)
185 sum += cnt_to_uint(&entry->cnt);
190 * calculates the mean value of a distribution
192 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
194 distrib_entry_t *entry;
199 /* integer distribution, need min, max */
202 entry = pset_first(tbl->hash_map);
208 max = (int)entry->object;
209 sum = cnt_to_dbl(&entry->cnt);
212 for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
213 int value = (int)entry->object;
220 sum += cnt_to_dbl(&entry->cnt);
222 count = max - min + 1;
227 foreach_pset(tbl->hash_map, entry) {
228 sum += cnt_to_dbl(&entry->cnt);
233 return count ? sum / (double)count : 0.0;
237 * calculates the average value of a distribution
239 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl)
241 distrib_entry_t *entry;
246 if (pset_count(tbl->hash_map) <= 0)
249 foreach_pset(tbl->hash_map, entry) {
250 sum += cnt_to_dbl(&entry->cnt) * (int)entry->object;
251 count += cnt_to_uint(&entry->cnt);
255 foreach_pset(tbl->hash_map, entry) {
256 sum += cnt_to_dbl(&entry->cnt);
261 return count ? sum / (double)count : 0.0;
265 * iterates over all entries in a distribution table
267 void stat_iterate_distrib_tbl(distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env)
269 distrib_entry_t *entry;
271 foreach_pset(tbl->hash_map, entry)