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];
30 static INLINE void cnt_inc(counter_t *cnt)
34 for (i = 0; i < STAT_CNT_NUM; ++i) {
43 static INLINE void cnt_dec(counter_t *cnt)
47 for (i = 0; i < STAT_CNT_NUM; ++i) {
48 if (--cnt->cnt[i] != -1)
54 * set a counter to zero
56 static INLINE void cnt_clr(counter_t *cnt)
58 memset(cnt->cnt, 0, sizeof(cnt->cnt));
62 * add a counter to another
64 static INLINE void cnt_add(counter_t *dst, const counter_t *src)
68 for (i = 0; i < STAT_CNT_NUM; ++i) {
69 unsigned x = dst->cnt[i];
70 unsigned y = src->cnt[i];
71 unsigned a = x + y + carry;
73 carry = (int)((x & y) | ((x | y) & ~a)) < 0 ? 1 : 0;
80 * add an (positive) integer to an counter
82 static INLINE void cnt_add_i(counter_t *dst, int src)
87 for (i = 0; i < STAT_CNT_NUM; ++i) {
88 unsigned a = dst->cnt[i] + carry;
90 carry = a < dst->cnt[i];
100 * compare two counter
102 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
107 for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
116 return va < vb ? -1 : 1;
121 * convert a counter into a double
123 static INLINE double cnt_to_dbl(const counter_t *a)
126 double res = 0.0, scale = 1.0, tmp;
128 i = (1 << (sizeof(a->cnt[i]) * 4));
129 tmp = ((double)i) * ((double)i);
131 for (i = 0; i < STAT_CNT_NUM; ++i) {
132 res += scale * (double)a->cnt[i];
140 * check, if a counter is equal to an unsigned
142 static INLINE int cnt_eq(const counter_t *a, unsigned value)
146 for (i = 1; i < STAT_CNT_NUM; ++i)
150 return a->cnt[0] == value;
154 * check, if a counter as greater than an unsigned
156 static INLINE int cnt_gt(const counter_t *a, unsigned value)
160 for (i = 1; i < STAT_CNT_NUM; ++i)
164 return a->cnt[0] > value;
168 #endif /* _COUNTER_H_ */