da5daff9bf8283e6796f85420dc1b170c95a3e24
[mesa.git] / src / util / macros.h
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef UTIL_MACROS_H
25 #define UTIL_MACROS_H
26
27 /* Compute the size of an array */
28 #ifndef ARRAY_SIZE
29 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
30 #endif
31
32
33 /**
34 * __builtin_expect macros
35 */
36 #if !defined(HAVE___BUILTIN_EXPECT)
37 # define __builtin_expect(x, y) (x)
38 #endif
39
40 #ifndef likely
41 # ifdef HAVE___BUILTIN_EXPECT
42 # define likely(x) __builtin_expect(!!(x), 1)
43 # define unlikely(x) __builtin_expect(!!(x), 0)
44 # else
45 # define likely(x) (x)
46 # define unlikely(x) (x)
47 # endif
48 #endif
49
50
51 /**
52 * Static (compile-time) assertion.
53 * Basically, use COND to dimension an array. If COND is false/zero the
54 * array size will be -1 and we'll get a compilation error.
55 */
56 #define STATIC_ASSERT(COND) \
57 do { \
58 (void) sizeof(char [1 - 2*!(COND)]); \
59 } while (0)
60
61
62 /**
63 * Unreachable macro. Useful for suppressing "control reaches end of non-void
64 * function" warnings.
65 */
66 #ifdef HAVE___BUILTIN_UNREACHABLE
67 #define unreachable(str) \
68 do { \
69 assert(!str); \
70 __builtin_unreachable(); \
71 } while (0)
72 #elif _MSC_VER >= 1200
73 #define unreachable(str) \
74 do { \
75 assert(!str); \
76 __assume(0); \
77 } while (0)
78 #endif
79
80 #ifndef unreachable
81 #define unreachable(str)
82 #endif
83
84 /**
85 * Assume macro. Useful for expressing our assumptions to the compiler,
86 * typically for purposes of silencing warnings.
87 */
88 #ifdef HAVE___BUILTIN_UNREACHABLE
89 #define assume(expr) ((expr) ? ((void) 0) \
90 : (assert(!"assumption failed"), \
91 __builtin_unreachable()))
92 #elif _MSC_VER >= 1200
93 #define assume(expr) __assume(expr)
94 #else
95 #define assume(expr) assert(expr)
96 #endif
97
98 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
99 #define FLATTEN __attribute__((__flatten__))
100 #else
101 #define FLATTEN
102 #endif
103
104 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
105 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
106 #else
107 #define PRINTFLIKE(f, a)
108 #endif
109
110 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
111 #define MALLOCLIKE __attribute__((__malloc__))
112 #else
113 #define MALLOCLIKE
114 #endif
115
116 /* Used to optionally mark structures with misaligned elements or size as
117 * packed, to trade off performance for space.
118 */
119 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
120 #define PACKED __attribute__((__packed__))
121 #else
122 #define PACKED
123 #endif
124
125 #ifdef __cplusplus
126 /**
127 * Macro function that evaluates to true if T is a trivially
128 * destructible type -- that is, if its (non-virtual) destructor
129 * performs no action and all member variables and base classes are
130 * trivially destructible themselves.
131 */
132 # if defined(__GNUC__)
133 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
134 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
135 # endif
136 # elif (defined(__clang__) && defined(__has_feature))
137 # if __has_feature(has_trivial_destructor)
138 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
139 # endif
140 # endif
141 # ifndef HAS_TRIVIAL_DESTRUCTOR
142 /* It's always safe (if inefficient) to assume that a
143 * destructor is non-trivial.
144 */
145 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
146 # endif
147 #endif
148
149 #endif /* UTIL_MACROS_H */