403a14f65d1b2094577031069e061f47ff703bf5
[cparser] / adt / util.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5
6 /**
7  * @file
8  * @date    16.03.2007
9  * @brief   Various utility functions that wrap compiler specific extensions
10  * @author  Matthias Braun
11  */
12 #ifndef _FIRM_UTIL_H_
13 #define _FIRM_UTIL_H_
14
15 /**
16  * Asserts that the constant expression x is not zero at compiletime. name has
17  * to be a unique identifier.
18  *
19  * @note This uses the fact, that double case labels are not allowed.
20  */
21 #define COMPILETIME_ASSERT(x, name) \
22         static __attribute__((unused)) void compiletime_assert_##name (int h) { \
23                 switch(h) { case 0: case (x): {} } \
24         }
25
26 /**
27  * Indicates to the compiler that the value of x is very likely 1
28  * @note Only use this in speed critical code and when you are sure x is often 1
29  */
30 #define LIKELY(x)   __builtin_expect((x), 1)
31 /**
32  * Indicates to the compiler that it's very likely that x is 0
33  * @note Only use this in speed critical code and when you are sure x is often 0
34  */
35 #define UNLIKELY(x) __builtin_expect((x), 0)
36
37 #define lengthof(x) (sizeof(x) / sizeof(*(x)))
38
39 #define endof(x) ((x) + lengthof(x))
40
41 #undef MAX
42 #define MAX(a, b) ((a) > (b) ? (a) : (b))
43
44 #endif