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.
17 #include "firmstat_t.h"
20 * calculates a hash value for an address
22 static unsigned addr_hash(const void *object)
24 return HASH_PTR(object);
28 * calculates a hash value for an integer
30 static unsigned int_hash(const void *object)
32 return (unsigned)object;
36 * compare function for integer distribution tables
38 static int int_cmp_fun(const void *elt, const void *key)
47 * create a new distribution table
49 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
53 res = xmalloc(sizeof(*res));
55 obstack_init(&res->cnts);
57 /* create the hash-table */
58 res->hash_map = new_pset(cmp_func, 8);
59 res->hash_func = hash_func ? hash_func : addr_hash;
66 * create a new distribution table for an integer distribution
68 distrib_tbl_t *stat_new_int_distrib_tbl(void)
70 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
79 * destroy a distribution table
81 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
84 /* free all entries */
85 obstack_free(&tbl->cnts, NULL);
87 /* delete the hash table */
88 del_pset(tbl->hash_map);
93 * Returns the associates distrib_entry_t for an object
95 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
98 distrib_entry_t *elem;
102 elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
106 elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
111 elem->object = object;
113 return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
117 * adds a new object count into the distribution table
119 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
121 distrib_entry_t *elem = distrib_get_entry(tbl, object);
123 cnt_add(&elem->cnt, cnt);
127 * adds a new key count into the integer distribution table
129 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
131 stat_add_distrib_tbl(tbl, (const void *)key, cnt);
135 * calculates the mean value of a distribution
137 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
139 distrib_entry_t *entry;
144 /* integer distribution, need min, max */
147 entry = pset_first(tbl->hash_map);
153 max = (int)entry->object;
154 sum = cnt_to_dbl(&entry->cnt);
157 for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
158 int value = (int)entry->object;
165 sum += cnt_to_dbl(&entry->cnt);
167 count = max - min + 1;
172 for (entry = pset_first(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
173 sum += cnt_to_dbl(&entry->cnt);
178 return count ? sum / (double)count : 0.0;
182 * iterates over all entries in a distribution table
184 void stat_iterate_distrib_tbl(distrib_tbl_t *tbl, eval_distrib_entry_fun eval)
186 distrib_entry_t *entry;
188 for (entry = pset_first(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {