X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fstat%2Fcounter.h;h=6d251b2f72dea7a961b7a75798acf8a2bee0b29a;hb=204336e554f60a9209be6070df7bc70e2c359692;hp=f6074ca3ede879d07e537410e6b7d6c971b14966;hpb=5251238cbd55623a30a438de6972a0c3d48f514e;p=libfirm diff --git a/ir/stat/counter.h b/ir/stat/counter.h index f6074ca3e..6d251b2f7 100644 --- a/ir/stat/counter.h +++ b/ir/stat/counter.h @@ -12,40 +12,46 @@ #define _COUNTER_H_ #include +#include /* - * 32 bit should be enough for now + * 32 bit should be enough for most cases */ +#ifndef STAT_CNT_NUM #define STAT_CNT_NUM 1 +#endif typedef struct _counter_t { - unsigned cnt[STAT_CNT_NUM]; + unsigned cnt[STAT_CNT_NUM]; } counter_t; +/** initializes a counter with zero */ +#define ZERO_CNT { { 0 } } + /** * increase a counter */ static INLINE void cnt_inc(counter_t *cnt) { - int i; + int i; - for (i = 0; i < STAT_CNT_NUM; ++i) { - if (++cnt->cnt[i]) - break; - } + for (i = 0; i < STAT_CNT_NUM; ++i) { + if (++cnt->cnt[i]) + break; + } } /** - * decreace a counter + * decrease a counter */ static INLINE void cnt_dec(counter_t *cnt) { - int i; + int i; - for (i = 0; i < STAT_CNT_NUM; ++i) { - if (--cnt->cnt[i] != -1) - break; - } + for (i = 0; i < STAT_CNT_NUM; ++i) { + if (--cnt->cnt[i] != -1) + break; + } } /** @@ -53,7 +59,7 @@ static INLINE void cnt_dec(counter_t *cnt) */ static INLINE void cnt_clr(counter_t *cnt) { - memset(cnt->cnt, 0, sizeof(cnt->cnt)); + memset(cnt->cnt, 0, sizeof(cnt->cnt)); } /** @@ -61,46 +67,37 @@ static INLINE void cnt_clr(counter_t *cnt) */ static INLINE void cnt_add(counter_t *dst, const counter_t *src) { - int i, carry = 0; - - for (i = 0; i < STAT_CNT_NUM; ++i) { - unsigned a = dst->cnt[i] + src->cnt[i] + carry; + int i, carry = 0; - if (carry) - carry = a <= dst->cnt[i]; - else - carry = a < dst->cnt[i]; + for (i = 0; i < STAT_CNT_NUM; ++i) { + unsigned x = dst->cnt[i]; + unsigned y = src->cnt[i]; + unsigned a = x + y + carry; - dst->cnt[i] = a; + carry = (int)((x & y) | ((x | y) & ~a)) < 0 ? 1 : 0; - if (! carry) - break; - } + dst->cnt[i] = a; + } } /** - * add an integer to an counter + * add an (positive) integer to an counter */ static INLINE void cnt_add_i(counter_t *dst, int src) { - int i; - unsigned a = dst->cnt[0] + src; - unsigned carry = a < dst->cnt[0]; - - dst->cnt[0] = a; - if (! carry) - return; + int i; + unsigned carry = src; - for (i = 1; i < STAT_CNT_NUM; ++i) { - unsigned a = dst->cnt[i] + carry; + for (i = 0; i < STAT_CNT_NUM; ++i) { + unsigned a = dst->cnt[i] + carry; - carry = a < dst->cnt[i]; + carry = a < dst->cnt[i]; - dst->cnt[i] = a; + dst->cnt[i] = a; - if (! carry) - break; - } + if (! carry) + break; + } } /** @@ -108,20 +105,20 @@ static INLINE void cnt_add_i(counter_t *dst, int src) */ static INLINE int cnt_cmp(const counter_t *a, const counter_t *b) { - int i; - unsigned va, vb; + int i; + unsigned va, vb; - for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) { - va = a->cnt[i]; - vb = b->cnt[i]; + for (i = STAT_CNT_NUM - 1 ; i >= 0; --i) { + va = a->cnt[i]; + vb = b->cnt[i]; - if (va != vb) - break; - } + if (va != vb) + break; + } - if (va != vb) - return va < vb ? -1 : 1; - return 0; + if (va != vb) + return va < vb ? -1 : 1; + return 0; } /** @@ -129,18 +126,60 @@ static INLINE int cnt_cmp(const counter_t *a, const counter_t *b) */ static INLINE double cnt_to_dbl(const counter_t *a) { - int i; - double res = 0.0, scale = 1.0, tmp; + int i; + double res = 0.0, scale = 1.0, tmp; + + i = (1 << (sizeof(a->cnt[0]) * 4)); + tmp = ((double)i) * ((double)i); + + for (i = 0; i < STAT_CNT_NUM; ++i) { + res += scale * (double)a->cnt[i]; + + scale *= tmp; + } + return res; +} + +/** + * convert a counter into an unsigned + */ +static INLINE unsigned cnt_to_uint(const counter_t *a) +{ + int i; + + for (i = 1; i < STAT_CNT_NUM; ++i) + if (a->cnt[i]) + return UINT_MAX; + + return a->cnt[0]; +} + +/** + * check, if a counter is equal to an unsigned + */ +static INLINE int cnt_eq(const counter_t *a, unsigned value) +{ + int i; - i = (1 << (sizeof(a->cnt[i]) * 4)); - tmp = ((double)i) * ((double)i); + for (i = 1; i < STAT_CNT_NUM; ++i) + if (a->cnt[i]) + return 0; + + return a->cnt[0] == value; +} + +/** + * check, if a counter as greater than an unsigned + */ +static INLINE int cnt_gt(const counter_t *a, unsigned value) +{ + int i; - for (i = 0; i < STAT_CNT_NUM; ++i) { - res += scale * (double)a->cnt[i]; + for (i = 1; i < STAT_CNT_NUM; ++i) + if (a->cnt[i]) + return 1; - scale *= tmp; - } - return res; + return a->cnt[0] > value; } #endif /* _COUNTER_H_ */