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 most cases
20 #define STAT_CNT_NUM 1
23 typedef struct _counter_t {
24 unsigned cnt[STAT_CNT_NUM];
27 static const counter_t _cnt_0 = { { 0 } };
28 static const counter_t _cnt_1 = { { 1 } };
31 * returns a counter representing value 0
33 static INLINE const counter_t *cnt_get_0()
39 * returns a counter representing value 1
41 static INLINE const counter_t *cnt_get_1()
49 static INLINE void cnt_inc(counter_t *cnt)
53 for (i = 0; i < STAT_CNT_NUM; ++i) {
62 static INLINE void cnt_dec(counter_t *cnt)
66 for (i = 0; i < STAT_CNT_NUM; ++i) {
67 if (--cnt->cnt[i] != -1)
73 * set a counter to zero
75 static INLINE void cnt_clr(counter_t *cnt)
77 memset(cnt->cnt, 0, sizeof(cnt->cnt));
81 * add a counter to another
83 static INLINE void cnt_add(counter_t *dst, const counter_t *src)
87 for (i = 0; i < STAT_CNT_NUM; ++i) {
88 unsigned x = dst->cnt[i];
89 unsigned y = src->cnt[i];
90 unsigned a = x + y + carry;
92 carry = (int)((x & y) | ((x | y) & ~a)) < 0 ? 1 : 0;
99 * add an (positive) integer to an counter
101 static INLINE void cnt_add_i(counter_t *dst, int src)
104 unsigned carry = src;
106 for (i = 0; i < STAT_CNT_NUM; ++i) {
107 unsigned a = dst->cnt[i] + carry;
109 carry = a < dst->cnt[i];
119 * compare two counter
121 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
126 for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
135 return va < vb ? -1 : 1;
140 * convert a counter into a double
142 static INLINE double cnt_to_dbl(const counter_t *a)
145 double res = 0.0, scale = 1.0, tmp;
147 i = (1 << (sizeof(a->cnt[i]) * 4));
148 tmp = ((double)i) * ((double)i);
150 for (i = 0; i < STAT_CNT_NUM; ++i) {
151 res += scale * (double)a->cnt[i];
159 * check, if a counter is equal to an unsigned
161 static INLINE int cnt_eq(const counter_t *a, unsigned value)
165 for (i = 1; i < STAT_CNT_NUM; ++i)
169 return a->cnt[0] == value;
173 * check, if a counter as greater than an unsigned
175 static INLINE int cnt_gt(const counter_t *a, unsigned value)
179 for (i = 1; i < STAT_CNT_NUM; ++i)
183 return a->cnt[0] > value;
187 #endif /* _COUNTER_H_ */