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];
31 static INLINE void cnt_inc(counter_t *cnt)
35 for (i = 0; i < STAT_CNT_NUM; ++i) {
44 static INLINE void cnt_dec(counter_t *cnt)
48 for (i = 0; i < STAT_CNT_NUM; ++i) {
49 if (--cnt->cnt[i] != -1)
55 * set a counter to zero
57 static INLINE void cnt_clr(counter_t *cnt)
59 memset(cnt->cnt, 0, sizeof(cnt->cnt));
63 * add a counter to another
65 static INLINE void cnt_add(counter_t *dst, const counter_t *src)
69 for (i = 0; i < STAT_CNT_NUM; ++i) {
70 unsigned x = dst->cnt[i];
71 unsigned y = src->cnt[i];
72 unsigned a = x + y + carry;
74 carry = (int)((x & y) | ((x | y) & ~a)) < 0 ? 1 : 0;
81 * add an (positive) integer to an counter
83 static INLINE void cnt_add_i(counter_t *dst, int src)
88 for (i = 0; i < STAT_CNT_NUM; ++i) {
89 unsigned a = dst->cnt[i] + carry;
91 carry = a < dst->cnt[i];
101 * compare two counter
103 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
108 for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
117 return va < vb ? -1 : 1;
122 * convert a counter into a double
124 static INLINE double cnt_to_dbl(const counter_t *a)
127 double res = 0.0, scale = 1.0, tmp;
129 i = (1 << (sizeof(a->cnt[0]) * 4));
130 tmp = ((double)i) * ((double)i);
132 for (i = 0; i < STAT_CNT_NUM; ++i) {
133 res += scale * (double)a->cnt[i];
141 * convert a counter into an int
143 static INLINE unsigned cnt_to_int(const counter_t *a)
147 for (i = 1; i < STAT_CNT_NUM; ++i)
155 * check, if a counter is equal to an unsigned
157 static INLINE int cnt_eq(const counter_t *a, unsigned value)
161 for (i = 1; i < STAT_CNT_NUM; ++i)
165 return a->cnt[0] == value;
169 * check, if a counter as greater than an unsigned
171 static INLINE int cnt_gt(const counter_t *a, unsigned value)
175 for (i = 1; i < STAT_CNT_NUM; ++i)
179 return a->cnt[0] > value;
183 #endif /* _COUNTER_H_ */