3 * File name: ir/ir/counter.h
4 * Purpose: Statistics for Firm. Counter implementation.
8 * Copyright: (c) 2004 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 * 32 bit should be enough for now
19 #define STAT_CNT_NUM 1
21 typedef struct _counter_t {
22 unsigned cnt[STAT_CNT_NUM];
28 static INLINE void cnt_inc(counter_t *cnt)
32 for (i = 0; i < STAT_CNT_NUM; ++i) {
41 static INLINE void cnt_dec(counter_t *cnt)
45 for (i = 0; i < STAT_CNT_NUM; ++i) {
46 if (--cnt->cnt[i] != -1)
52 * set a counter to zero
54 static INLINE void cnt_clr(counter_t *cnt)
56 memset(cnt->cnt, 0, sizeof(cnt->cnt));
60 * add a counter to another
62 static INLINE void cnt_add(counter_t *dst, const counter_t *src)
66 for (i = 0; i < STAT_CNT_NUM; ++i) {
67 unsigned a = dst->cnt[i] + src->cnt[i] + carry;
70 carry = a <= dst->cnt[i];
72 carry = a < dst->cnt[i];
82 * add an integer to an counter
84 static INLINE void cnt_add_i(counter_t *dst, int src)
87 unsigned a = dst->cnt[0] + src;
88 unsigned carry = a < dst->cnt[0];
94 for (i = 1; i < STAT_CNT_NUM; ++i) {
95 unsigned a = dst->cnt[i] + carry;
97 carry = a < dst->cnt[i];
107 * compare two counter
109 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
114 for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
123 return va < vb ? -1 : 1;
128 * convert a counter into a double
130 static INLINE double cnt_to_dbl(const counter_t *a)
133 double res = 0.0, scale = 1.0, tmp;
135 i = (1 << (sizeof(a->cnt[i]) * 4));
136 tmp = ((double)i) * ((double)i);
138 for (i = 0; i < STAT_CNT_NUM; ++i) {
139 res += scale * (double)a->cnt[i];
147 * check, if a counter is equal to an unsigned
149 static INLINE int cnt_eq(const counter_t *a, unsigned value)
153 for (i = 1; i < STAT_CNT_NUM; ++i)
157 return a->cnt[0] == value;
161 * check, if a counter as greater than an unsigned
163 static INLINE int cnt_gt(const counter_t *a, unsigned value)
167 for (i = 1; i < STAT_CNT_NUM; ++i)
171 return a->cnt[0] > value;
175 #endif /* _COUNTER_H_ */