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 x = dst->cnt[i];
68 unsigned y = src->cnt[i];
69 unsigned a = x + y + carry;
71 carry = (int)((x & y) | ((x | y) & ~a)) < 0 ? 1 : 0;
78 * add an (positive) integer to an counter
80 static INLINE void cnt_add_i(counter_t *dst, int src)
85 for (i = 0; i < STAT_CNT_NUM; ++i) {
86 unsigned a = dst->cnt[i] + carry;
88 carry = a < dst->cnt[i];
100 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
105 for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
114 return va < vb ? -1 : 1;
119 * convert a counter into a double
121 static INLINE double cnt_to_dbl(const counter_t *a)
124 double res = 0.0, scale = 1.0, tmp;
126 i = (1 << (sizeof(a->cnt[i]) * 4));
127 tmp = ((double)i) * ((double)i);
129 for (i = 0; i < STAT_CNT_NUM; ++i) {
130 res += scale * (double)a->cnt[i];
138 * check, if a counter is equal to an unsigned
140 static INLINE int cnt_eq(const counter_t *a, unsigned value)
144 for (i = 1; i < STAT_CNT_NUM; ++i)
148 return a->cnt[0] == value;
152 * check, if a counter as greater than an unsigned
154 static INLINE int cnt_gt(const counter_t *a, unsigned value)
158 for (i = 1; i < STAT_CNT_NUM; ++i)
162 return a->cnt[0] > value;
166 #endif /* _COUNTER_H_ */