- Implement all the state switching stuff needed for efficient fpu mode
[libfirm] / ir / adt / util.h
1 /**
2  * @file   util.h
3  * @date   31.05.2005
4  * @author Sebastian Hack
5  *
6  * Some utility macros.
7  *
8  * Copyright (C) 2005 Universitaet Karlsruhe
9  * Released under the GPL
10  */
11
12 #ifndef _UTIL_H
13 #define _UTIL_H
14
15 /**
16  * Get the offset of a member of a struct.
17  * @param type   The type of the struct member is in.
18  * @param member The name of the member.
19  * @return       The offset of member in type in bytes.
20  */
21 #define offset_of(type, member) \
22   ((char *) &(((type *) 0)->member) - (char *) 0)
23
24 /**
25  * Make pointer to the struct from a pointer to a member of that struct.
26  * @param ptr     The pointer to the member.
27  * @param type    The type of the struct.
28  * @param member  The name of the member.
29  * @return        A pointer to the struct member is in.
30  */
31 #define container_of(ptr, type, member) \
32         ((type *) ((char *) (ptr) - offset_of(type, member)))
33
34 /**
35  * Get the number of elements of a static array.
36  * @param arr The static array.
37  * @return The number of elements in that array.
38  */
39 #define array_size(arr) \
40   (sizeof(arr) / sizeof((arr)[0]))
41
42 /**
43  * Asserts that the constant expression x is not zero at compiletime. name has
44  * to be a unique identifier.
45  *
46  * @note This uses the fact, that double case labels are not allowed.
47  */
48 #define COMPILETIME_ASSERT(x, name) \
49     static __attribute__((unused)) void compiletime_assert_##name (int h) { \
50         switch(h) { case 0: case (x): ; } \
51     }
52
53 #ifdef __GNUC__
54 /**
55  * Indicates to the compiler that the value of x is very likely 1
56  * @note Only use this in speed critical code and when you are sure x is often 1
57  */
58 #define LIKELY(x)   __builtin_expect((x), 1)
59
60 /**
61  * Indicates to the compiler that it's very likely that x is 0
62  * @note Only use this in speed critical code and when you are sure x is often 0
63  */
64 #define UNLIKELY(x) __builtin_expect((x), 0)
65 #else
66 #define LIKELY(x)   x
67 #define UNLIKELY(x) x
68 #endif
69
70 #endif /* _UTIL_H */