Remove the unused parameter const arch_env_t *env from arch_set_irn_register().
[libfirm] / ir / stat / counter.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Statistics for Firm. Counter implementation.
23  * @author  Michael Beck
24  * @version $Id$
25  */
26 #ifndef FIRM_STAT_COUNTER_H
27 #define FIRM_STAT_COUNTER_H
28
29 #include <string.h>
30 #include <limits.h>
31
32 /*
33  * 32 bit should be enough for most cases
34  */
35 #ifndef STAT_CNT_NUM
36 #define STAT_CNT_NUM 1
37 #endif
38
39 typedef struct _counter_t {
40         unsigned cnt[STAT_CNT_NUM];
41 } counter_t;
42
43 /** initializes a counter with zero */
44 #define ZERO_CNT { { 0 } }
45
46 /**
47  * increase a counter
48  */
49 static INLINE void cnt_inc(counter_t *cnt)
50 {
51         int i;
52
53         for (i = 0; i < STAT_CNT_NUM; ++i) {
54                 if (++cnt->cnt[i])
55                         break;
56         }
57 }
58
59 /**
60  * decrease a counter
61  */
62 static INLINE void cnt_dec(counter_t *cnt)
63 {
64         int i;
65
66         for (i = 0; i < STAT_CNT_NUM; ++i) {
67                 if (--cnt->cnt[i] != (unsigned) -1)
68                         break;
69         }
70 }
71
72 /**
73  * set a counter to zero
74  */
75 static INLINE void cnt_clr(counter_t *cnt)
76 {
77         memset(cnt->cnt, 0, sizeof(cnt->cnt));
78 }
79
80 /**
81  * add a counter to another
82  */
83 static INLINE void cnt_add(counter_t *dst, const counter_t *src)
84 {
85         int i, carry = 0;
86
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;
91
92                 carry = (int)((x & y) | ((x | y) & ~a)) < 0 ? 1 : 0;
93
94                 dst->cnt[i] = a;
95         }
96 }
97
98 /**
99  * add an (positive) integer to an counter
100  */
101 static INLINE void cnt_add_i(counter_t *dst, int src)
102 {
103         int i;
104         unsigned carry = src;
105
106         for (i = 0; i < STAT_CNT_NUM; ++i) {
107                 unsigned a = dst->cnt[i] + carry;
108
109                 carry = a < dst->cnt[i];
110
111                 dst->cnt[i] = a;
112
113                 if (! carry)
114                         break;
115         }
116 }
117
118 /**
119  * compare two counter
120  */
121 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
122 {
123         int i;
124         unsigned va, vb;
125
126         for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
127                 va = a->cnt[i];
128                 vb = b->cnt[i];
129
130                 if (va != vb)
131                         break;
132         }
133
134         if (va != vb)
135                 return va < vb ? -1 : 1;
136         return 0;
137 }
138
139 /**
140  * convert a counter into a double
141  */
142 static INLINE double cnt_to_dbl(const counter_t *a)
143 {
144         int i;
145         double res = 0.0, scale = 1.0, tmp;
146
147         i = (1 << (sizeof(a->cnt[0]) * 4));
148         tmp = ((double)i) * ((double)i);
149
150         for (i = 0; i < STAT_CNT_NUM; ++i) {
151                 res += scale * (double)a->cnt[i];
152
153                 scale *= tmp;
154         }
155         return res;
156 }
157
158 /**
159  * convert a counter into an unsigned
160  */
161 static INLINE unsigned cnt_to_uint(const counter_t *a)
162 {
163         int i;
164
165         for (i = 1; i < STAT_CNT_NUM; ++i)
166                 if (a->cnt[i])
167                         return UINT_MAX;
168
169         return a->cnt[0];
170 }
171
172 /**
173  * check, if a counter is equal to an unsigned
174  */
175 static INLINE int cnt_eq(const counter_t *a, unsigned value)
176 {
177         int i;
178
179         for (i = 1; i < STAT_CNT_NUM; ++i)
180                 if (a->cnt[i])
181                         return 0;
182
183         return a->cnt[0] == value;
184 }
185
186 /**
187  * check, if a counter as greater than an unsigned
188  */
189 static INLINE int cnt_gt(const counter_t *a, unsigned value)
190 {
191         int i;
192
193         for (i = 1; i < STAT_CNT_NUM; ++i)
194                 if (a->cnt[i])
195                         return 1;
196
197         return a->cnt[0] > value;
198 }
199
200 #endif /* FIRM_STAT_COUNTER_H */