stat_get_count_distrib_tbl(): uses now counter_t for calculating the values
[libfirm] / ir / stat / distrib.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/distrib.c
4  * Purpose:     Statistics for Firm. Distribution tables.
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 2004 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11 #ifdef HAVE_CONFIG_H
12 # include "config.h"
13 #endif
14
15 #include "hashptr.h"
16 #include "irtools.h"
17 #include "xmalloc.h"
18 #include "firmstat_t.h"
19
20 /**
21  * calculates a hash value for an address
22  */
23 static unsigned addr_hash(const void *object)
24 {
25         return HASH_PTR(object);
26 }
27
28 /**
29  * calculates a hash value for an integer
30  */
31 static unsigned int_hash(const void *object)
32 {
33         return (unsigned)PTR_TO_INT(object);
34 }
35
36 /**
37  * compare function for integer distribution tables
38  */
39 static int int_cmp_fun(const void *elt, const void *key)
40 {
41         const distrib_entry_t *p1 = elt;
42         const distrib_entry_t *p2 = key;
43
44         return (char *)p1->object - (char *)p2->object;
45 }
46
47 /*
48  * create a new distribution table
49  */
50 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
51 {
52         distrib_tbl_t *res;
53
54         res = xmalloc(sizeof(*res));
55
56         obstack_init(&res->cnts);
57
58         /* create the hash-table */
59         res->hash_map  = new_pset(cmp_func, 8);
60         res->hash_func = hash_func ? hash_func : addr_hash;
61         res->int_dist  = 0;
62
63         return res;
64 }
65
66 /*
67  * create a new distribution table for an integer distribution
68  */
69 distrib_tbl_t *stat_new_int_distrib_tbl(void)
70 {
71         distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
72
73         if (res)
74                 res->int_dist = 1;
75
76         return res;
77 }
78
79 /*
80  * destroy a distribution table
81  */
82 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
83 {
84         if (tbl) {
85                 /* free all entries */
86                 obstack_free(&tbl->cnts, NULL);
87
88                 /* delete the hash table */
89                 del_pset(tbl->hash_map);
90         }
91 }
92
93 /**
94  * Returns the associates distrib_entry_t for an object
95  */
96 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
97 {
98         distrib_entry_t key;
99         distrib_entry_t *elem;
100
101         key.object = object;
102
103         elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
104         if (elem)
105                 return elem;
106
107         elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
108
109         /* clear counter */
110         cnt_clr(&elem->cnt);
111
112         elem->object = object;
113
114         return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
115 }
116
117 /*
118  * adds a new object count into the distribution table
119  */
120 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
121 {
122         distrib_entry_t *elem = distrib_get_entry(tbl, object);
123
124         cnt_add(&elem->cnt, cnt);
125 }
126
127 /*
128  * adds a new key count into the integer distribution table
129  */
130 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
131 {
132         stat_add_distrib_tbl(tbl, (const void *)key, cnt);
133 }
134
135 /*
136  * increases object count by one
137  */
138 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object)
139 {
140         distrib_entry_t *elem = distrib_get_entry(tbl, object);
141
142         cnt_inc(&elem->cnt);
143 }
144
145 /*
146  * increases key count by one
147  */
148 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key)
149 {
150         stat_inc_distrib_tbl(tbl, (const void *)key);
151 }
152
153 /*
154  * inserts a new object with count 0 into the distribution table
155  * if object is already present, nothing happens
156  */
157 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object)
158 {
159         /* executed for side effect */
160         (void)distrib_get_entry(tbl, object);
161 }
162
163 /*
164  * inserts a new key with count 0 into the integer distribution table
165  * if key is already present, nothing happens
166  */
167 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key)
168 {
169         stat_insert_distrib_tbl(tbl, (const void *)key);
170 }
171
172 /*
173  * returns the sum over all counters in a distribution table
174  */
175 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl)
176 {
177         distrib_entry_t *entry;
178         counter_t cnt = ZERO_CNT;
179
180         foreach_pset(tbl->hash_map, entry)
181                 cnt_add(&cnt, &entry->cnt);
182         return cnt_to_uint(&cnt);
183 }
184
185 /*
186  * calculates the mean value of a distribution
187  */
188 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
189 {
190         distrib_entry_t *entry;
191         unsigned count;
192         double sum;
193
194         if (tbl->int_dist) {
195                 /* integer distribution, need min, max */
196                 int min, max;
197
198                 entry = pset_first(tbl->hash_map);
199
200                 if (! entry)
201                         return 0.0;
202
203                 min =
204                 max = (int)entry->object;
205                 sum = cnt_to_dbl(&entry->cnt);
206
207
208                 for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
209                         int value = (int)entry->object;
210
211                         if (value < min)
212                                 min = value;
213                         if (value > max);
214                                 max = value;
215
216                         sum += cnt_to_dbl(&entry->cnt);
217                 }
218                 count = max - min + 1;
219         }
220         else {
221                 sum = 0.0;
222                 count = 0;
223                 foreach_pset(tbl->hash_map, entry) {
224                         sum += cnt_to_dbl(&entry->cnt);
225                         ++count;
226                 }
227         }
228
229         return count ? sum / (double)count : 0.0;
230 }
231
232 /*
233  * calculates the average value of a distribution
234  */
235 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl)
236 {
237         distrib_entry_t *entry;
238         unsigned        count = 0;
239         double          sum   = 0.0;
240
241         if (tbl->int_dist) {
242                 if (pset_count(tbl->hash_map) <= 0)
243                         return 0.0;
244
245                 foreach_pset(tbl->hash_map, entry) {
246                         sum   += cnt_to_dbl(&entry->cnt) * (int)entry->object;
247                         count += cnt_to_uint(&entry->cnt);
248                 }
249         }
250         else {
251                 foreach_pset(tbl->hash_map, entry) {
252                         sum += cnt_to_dbl(&entry->cnt);
253                         ++count;
254                 }
255         }
256
257         return count ? sum / (double)count : 0.0;
258 }
259
260 /**
261  * iterates over all entries in a distribution table
262  */
263 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env)
264 {
265         distrib_entry_t *entry;
266
267         foreach_pset(tbl->hash_map, entry)
268                 eval(entry, env);
269 }