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.
18 * 32 bit should be enough for most cases
21 #define STAT_CNT_NUM 1
24 typedef struct _counter_t {
25 unsigned cnt[STAT_CNT_NUM];
28 /** initializes a counter with zero */
29 #define ZERO_CNT { { 0 } }
34 static INLINE void cnt_inc(counter_t *cnt)
38 for (i = 0; i < STAT_CNT_NUM; ++i) {
47 static INLINE void cnt_dec(counter_t *cnt)
51 for (i = 0; i < STAT_CNT_NUM; ++i) {
52 if (--cnt->cnt[i] != -1)
58 * set a counter to zero
60 static INLINE void cnt_clr(counter_t *cnt)
62 memset(cnt->cnt, 0, sizeof(cnt->cnt));
66 * add a counter to another
68 static INLINE void cnt_add(counter_t *dst, const counter_t *src)
72 for (i = 0; i < STAT_CNT_NUM; ++i) {
73 unsigned x = dst->cnt[i];
74 unsigned y = src->cnt[i];
75 unsigned a = x + y + carry;
77 carry = (int)((x & y) | ((x | y) & ~a)) < 0 ? 1 : 0;
84 * add an (positive) integer to an counter
86 static INLINE void cnt_add_i(counter_t *dst, int src)
91 for (i = 0; i < STAT_CNT_NUM; ++i) {
92 unsigned a = dst->cnt[i] + carry;
94 carry = a < dst->cnt[i];
104 * compare two counter
106 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
111 for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
120 return va < vb ? -1 : 1;
125 * convert a counter into a double
127 static INLINE double cnt_to_dbl(const counter_t *a)
130 double res = 0.0, scale = 1.0, tmp;
132 i = (1 << (sizeof(a->cnt[0]) * 4));
133 tmp = ((double)i) * ((double)i);
135 for (i = 0; i < STAT_CNT_NUM; ++i) {
136 res += scale * (double)a->cnt[i];
144 * convert a counter into an unsigned
146 static INLINE unsigned cnt_to_uint(const counter_t *a)
150 for (i = 1; i < STAT_CNT_NUM; ++i)
158 * check, if a counter is equal to an unsigned
160 static INLINE int cnt_eq(const counter_t *a, unsigned value)
164 for (i = 1; i < STAT_CNT_NUM; ++i)
168 return a->cnt[0] == value;
172 * check, if a counter as greater than an unsigned
174 static INLINE int cnt_gt(const counter_t *a, unsigned value)
178 for (i = 1; i < STAT_CNT_NUM; ++i)
182 return a->cnt[0] > value;
185 #endif /* _COUNTER_H_ */