From: Matthias Braun Date: Tue, 20 Mar 2007 12:58:13 +0000 (+0000) Subject: add COMPILETIME_ASSERT, LIKELY and UNLIKELY to util.h X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=cdffe3c6b77fd0277bfc5e39eabc7ff534eb808b;p=libfirm add COMPILETIME_ASSERT, LIKELY and UNLIKELY to util.h [r8719] --- diff --git a/ir/adt/util.h b/ir/adt/util.h index d38254029..c8acb5652 100644 --- a/ir/adt/util.h +++ b/ir/adt/util.h @@ -39,4 +39,32 @@ #define array_size(arr) \ (sizeof(arr) / sizeof((arr)[0])) +/** + * Asserts that the constant expression x is not zero at compiletime. name has + * to be a unique identifier. + * + * @note This uses the fact, that double case labels are not allowed. + */ +#define COMPILETIME_ASSERT(x, name) \ + static __attribute__((unused)) void compiletime_assert_##name (int h) { \ + switch(h) { case 0: case (x): ; } \ + } + +#ifdef __GNUC__ +/** + * Indicates to the compiler that the value of x is very likely 1 + * @note Only use this in speed critical code and when you are sure x is often 1 + */ +#define LIKELY(x) __builtin_expect((x), 1) + +/** + * Indicates to the compiler that it's very likely that x is 0 + * @note Only use this in speed critical code and when you are sure x is often 0 + */ +#define UNLIKELY(x) __builtin_expect((x), 0) +#else +#define LIKELY(x) x +#define UNLIKELY(x) x +#endif + #endif /* _UTIL_H */