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