/*
- * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
+ * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
*
* This file is part of libFirm.
*
* PURPOSE.
*/
-/*
- * Project: libFIRM
- * File name: ir/ir/distrib.c
- * Purpose: Statistics for Firm. Distribution tables.
- * Author: Michael Beck
- * Created:
- * CVS-ID: $Id$
- * Copyright: (c) 2004 Universität Karlsruhe
+/**
+ * @file
+ * @brief Statistics for Firm. Distribution tables.
+ * @author Michael Beck
*/
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
#include "hashptr.h"
-#include "irtools.h"
+#include "util.h"
#include "xmalloc.h"
#include "firmstat_t.h"
*/
static unsigned addr_hash(const void *object)
{
- return HASH_PTR(object);
+ return hash_ptr(object);
}
/**
*/
static int int_cmp_fun(const void *elt, const void *key)
{
- const distrib_entry_t *p1 = elt;
- const distrib_entry_t *p2 = key;
+ const distrib_entry_t *p1 = (const distrib_entry_t*)elt;
+ const distrib_entry_t *p2 = (const distrib_entry_t*)key;
- return (char *)p1->object - (char *)p2->object;
+ if (p1->object == p2->object)
+ return 0;
+ return p1->object < p2->object ? -1 : 1;
}
/*
*/
distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
{
- distrib_tbl_t *res;
-
- res = xmalloc(sizeof(*res));
+ distrib_tbl_t *res = XMALLOC(distrib_tbl_t);
obstack_init(&res->cnts);
key.object = object;
- elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
+ elem = (distrib_entry_t*)pset_find(tbl->hash_map, &key, tbl->hash_func(object));
if (elem)
return elem;
- elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
+ elem = OALLOC(&tbl->cnts, distrib_entry_t);
/* clear counter */
cnt_clr(&elem->cnt);
elem->object = object;
- return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
+ return (distrib_entry_t*)pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
}
/*
*/
void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
{
- stat_add_distrib_tbl(tbl, (const void *)key, cnt);
+ stat_add_distrib_tbl(tbl, INT_TO_PTR(key), cnt);
}
/*
*/
void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key)
{
- stat_inc_distrib_tbl(tbl, (const void *)key);
+ stat_inc_distrib_tbl(tbl, INT_TO_PTR(key));
}
/*
*/
void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key)
{
- stat_insert_distrib_tbl(tbl, (const void *)key);
+ stat_insert_distrib_tbl(tbl, INT_TO_PTR(key));
}
/*
distrib_entry_t *entry;
counter_t cnt = ZERO_CNT;
- foreach_pset(tbl->hash_map, entry)
+ foreach_pset(tbl->hash_map, distrib_entry_t*, entry)
cnt_add(&cnt, &entry->cnt);
return cnt_to_uint(&cnt);
}
double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
{
distrib_entry_t *entry;
- unsigned count;
+ size_t count;
double sum;
if (tbl->int_dist) {
/* integer distribution, need min, max */
int min, max;
- entry = pset_first(tbl->hash_map);
+ entry = (distrib_entry_t*)pset_first(tbl->hash_map);
if (! entry)
return 0.0;
min =
- max = (int)entry->object;
+ max = PTR_TO_INT(entry->object);
sum = cnt_to_dbl(&entry->cnt);
- for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
- int value = (int)entry->object;
+ for (entry = (distrib_entry_t*)pset_next(tbl->hash_map); entry != NULL;
+ entry = (distrib_entry_t*)pset_next(tbl->hash_map)) {
+ int value = PTR_TO_INT(entry->object);
if (value < min)
min = value;
- if (value > max);
+ if (value > max)
max = value;
sum += cnt_to_dbl(&entry->cnt);
}
count = max - min + 1;
- }
- else {
+ } else {
sum = 0.0;
count = 0;
- foreach_pset(tbl->hash_map, entry) {
+ foreach_pset(tbl->hash_map, distrib_entry_t*, entry) {
sum += cnt_to_dbl(&entry->cnt);
++count;
}
double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl)
{
distrib_entry_t *entry;
- unsigned count = 0;
+ size_t count = 0;
double sum = 0.0;
if (tbl->int_dist) {
if (pset_count(tbl->hash_map) <= 0)
return 0.0;
- foreach_pset(tbl->hash_map, entry) {
- sum += cnt_to_dbl(&entry->cnt) * (int)entry->object;
+ foreach_pset(tbl->hash_map, distrib_entry_t*, entry) {
+ sum += cnt_to_dbl(&entry->cnt) * PTR_TO_INT(entry->object);
count += cnt_to_uint(&entry->cnt);
}
- }
- else {
- foreach_pset(tbl->hash_map, entry) {
+ } else {
+ foreach_pset(tbl->hash_map, distrib_entry_t*, entry) {
sum += cnt_to_dbl(&entry->cnt);
++count;
}
{
distrib_entry_t *entry;
- foreach_pset(tbl->hash_map, entry)
+ foreach_pset(tbl->hash_map, distrib_entry_t*, entry)
eval(entry, env);
}