2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Statistics for Firm. Counter implementation.
11 #ifndef FIRM_STAT_COUNTER_H
12 #define FIRM_STAT_COUNTER_H
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] != (unsigned) -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)
112 for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
121 return va < vb ? -1 : 1;
126 * convert a counter into a double
128 static inline double cnt_to_dbl(const counter_t *a)
131 double res = 0.0, scale = 1.0, tmp;
133 i = (1 << (sizeof(a->cnt[0]) * 4));
134 tmp = ((double)i) * ((double)i);
136 for (i = 0; i < STAT_CNT_NUM; ++i) {
137 res += scale * (double)a->cnt[i];
145 * convert a counter into an unsigned
147 static inline unsigned cnt_to_uint(const counter_t *a)
151 for (i = 1; i < STAT_CNT_NUM; ++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;
186 #endif /* FIRM_STAT_COUNTER_H */