055ea955655738b7b7fc4919fc62690f1cc0997a
[libfirm] / ir / stat / counter.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/counter.h
4  * Purpose:     Statistics for Firm. Counter implementation.
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 2004 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11 #ifndef _COUNTER_H_
12 #define _COUNTER_H_
13
14 /*
15  * 32 bit should be enough for now
16  */
17 #define STAT_CNT_NUM 1
18
19 typedef struct _counter_t {
20   unsigned cnt[STAT_CNT_NUM];
21 } counter_t;
22
23 /**
24  * increase a counter
25  */
26 static INLINE void cnt_inc(counter_t *cnt)
27 {
28   int i;
29
30   for (i = 0; i < STAT_CNT_NUM; ++i) {
31     if (++cnt->cnt[i])
32       break;
33   }
34 }
35
36 /**
37  * decreace a counter
38  */
39 static INLINE void cnt_dec(counter_t *cnt)
40 {
41   int i;
42
43   for (i = 0; i < STAT_CNT_NUM; ++i) {
44     if (--cnt->cnt[i] != -1)
45       break;
46   }
47 }
48
49 /**
50  * set a counter to zero
51  */
52 static INLINE void cnt_clr(counter_t *cnt)
53 {
54   memset(cnt->cnt, 0, sizeof(cnt->cnt));
55 }
56
57 /**
58  * add a counter to another
59  */
60 static INLINE void cnt_add(counter_t *dst, const counter_t *src)
61 {
62   int i, carry = 0;
63
64   for (i = 0; i < STAT_CNT_NUM; ++i) {
65     unsigned a = dst->cnt[i] + src->cnt[i] + carry;
66
67     if (carry)
68       carry = a <= dst->cnt[i];
69     else
70       carry = a < dst->cnt[i];
71
72     dst->cnt[i] = a;
73
74     if (! carry)
75       break;
76   }
77 }
78
79 /**
80  * add an integer to an counter
81  */
82 static INLINE void cnt_add_i(counter_t *dst, int src)
83 {
84   int i;
85   unsigned a = dst->cnt[0] + src;
86   unsigned carry = a < dst->cnt[0];
87
88   dst->cnt[0] = a;
89   if (! carry)
90     return;
91
92   for (i = 1; i < STAT_CNT_NUM; ++i) {
93     unsigned a = dst->cnt[i] + carry;
94
95     carry = a < dst->cnt[i];
96
97     dst->cnt[i] = a;
98
99     if (! carry)
100       break;
101   }
102 }
103
104 /**
105  * compare two counter
106  */
107 static INLINE int cnt_cmp(const counter_t *a, const counter_t *b)
108 {
109   int i;
110   unsigned va, vb;
111
112
113   for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) {
114     va = a->cnt[i];
115     vb = b->cnt[i];
116
117     if (va != vb)
118       break;
119   }
120
121   if (va != vb)
122     return va < vb ? -1 : 1;
123   return 0;
124 }
125
126 #endif /* _COUNTER_H_ */