2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Statistics for Firm. Counter implementation.
23 * @author Michael Beck
26 #ifndef FIRM_STAT_COUNTER_H
27 #define FIRM_STAT_COUNTER_H
33 * 32 bit should be enough for most cases
36 #define STAT_CNT_NUM 1
39 typedef struct _counter_t {
40 unsigned cnt[STAT_CNT_NUM];
43 /** initializes a counter with zero */
44 #define ZERO_CNT { { 0 } }
49 static INLINE void cnt_inc(counter_t *cnt)
53 for (i = 0; i < STAT_CNT_NUM; ++i) {
62 static INLINE void cnt_dec(counter_t *cnt)
66 for (i = 0; i < STAT_CNT_NUM; ++i) {
67 if (--cnt->cnt[i] != -1)
73 * set a counter to zero
75 static INLINE void cnt_clr(counter_t *cnt)
77 memset(cnt->cnt, 0, sizeof(cnt->cnt));
81 * add a counter to another
83 static INLINE void cnt_add(counter_t *dst, const counter_t *src)
87 for (i = 0; i < STAT_CNT_NUM; ++i) {
88 unsigned x = dst->cnt[i];
89 unsigned y = src->cnt[i];
90 unsigned a = x + y + carry;
92 carry = (int)((x & y) | ((x | y) & ~a)) < 0 ? 1 : 0;
99 * add an (positive) integer to an counter
101 static INLINE void cnt_add_i(counter_t *dst, int src)
104 unsigned carry = src;
106 for (i = 0; i < STAT_CNT_NUM; ++i) {
107 unsigned a = dst->cnt[i] + carry;
109 carry = a < dst->cnt[i];
119 * compare two counter
121 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
126 for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
135 return va < vb ? -1 : 1;
140 * convert a counter into a double
142 static INLINE double cnt_to_dbl(const counter_t *a)
145 double res = 0.0, scale = 1.0, tmp;
147 i = (1 << (sizeof(a->cnt[0]) * 4));
148 tmp = ((double)i) * ((double)i);
150 for (i = 0; i < STAT_CNT_NUM; ++i) {
151 res += scale * (double)a->cnt[i];
159 * convert a counter into an unsigned
161 static INLINE unsigned cnt_to_uint(const counter_t *a)
165 for (i = 1; i < STAT_CNT_NUM; ++i)
173 * check, if a counter is equal to an unsigned
175 static INLINE int cnt_eq(const counter_t *a, unsigned value)
179 for (i = 1; i < STAT_CNT_NUM; ++i)
183 return a->cnt[0] == value;
187 * check, if a counter as greater than an unsigned
189 static INLINE int cnt_gt(const counter_t *a, unsigned value)
193 for (i = 1; i < STAT_CNT_NUM; ++i)
197 return a->cnt[0] > value;
200 #endif /* FIRM_STAT_COUNTER_H */