util: rename xmlpool.h to driconf.h
[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 #include <assert.h>
28
29 #include "c99_compat.h"
30 #include "c11_compat.h"
31
32 /* Compute the size of an array */
33 #ifndef ARRAY_SIZE
34 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
35 #endif
36
37 /* For compatibility with Clang's __has_builtin() */
38 #ifndef __has_builtin
39 # define __has_builtin(x) 0
40 #endif
41
42 /**
43 * __builtin_expect macros
44 */
45 #if !defined(HAVE___BUILTIN_EXPECT)
46 # define __builtin_expect(x, y) (x)
47 #endif
48
49 #ifndef likely
50 # ifdef HAVE___BUILTIN_EXPECT
51 # define likely(x) __builtin_expect(!!(x), 1)
52 # define unlikely(x) __builtin_expect(!!(x), 0)
53 # else
54 # define likely(x) (x)
55 # define unlikely(x) (x)
56 # endif
57 #endif
58
59
60 /**
61 * Static (compile-time) assertion.
62 * Basically, use COND to dimension an array. If COND is false/zero the
63 * array size will be -1 and we'll get a compilation error.
64 */
65 #define STATIC_ASSERT(COND) \
66 do { \
67 (void) sizeof(char [1 - 2*!(COND)]); \
68 } while (0)
69
70
71 /**
72 * Unreachable macro. Useful for suppressing "control reaches end of non-void
73 * function" warnings.
74 */
75 #if defined(HAVE___BUILTIN_UNREACHABLE) || __has_builtin(__builtin_unreachable)
76 #define unreachable(str) \
77 do { \
78 assert(!str); \
79 __builtin_unreachable(); \
80 } while (0)
81 #elif defined (_MSC_VER)
82 #define unreachable(str) \
83 do { \
84 assert(!str); \
85 __assume(0); \
86 } while (0)
87 #else
88 #define unreachable(str) assert(!str)
89 #endif
90
91 /**
92 * Assume macro. Useful for expressing our assumptions to the compiler,
93 * typically for purposes of silencing warnings.
94 */
95 #if __has_builtin(__builtin_assume)
96 #define assume(expr) \
97 do { \
98 assert(expr); \
99 __builtin_assume(expr); \
100 } while (0)
101 #elif defined HAVE___BUILTIN_UNREACHABLE
102 #define assume(expr) ((expr) ? ((void) 0) \
103 : (assert(!"assumption failed"), \
104 __builtin_unreachable()))
105 #elif defined (_MSC_VER)
106 #define assume(expr) __assume(expr)
107 #else
108 #define assume(expr) assert(expr)
109 #endif
110
111 /* Attribute const is used for functions that have no effects other than their
112 * return value, and only rely on the argument values to compute the return
113 * value. As a result, calls to it can be CSEed. Note that using memory
114 * pointed to by the arguments is not allowed for const functions.
115 */
116 #ifdef HAVE_FUNC_ATTRIBUTE_CONST
117 #define ATTRIBUTE_CONST __attribute__((__const__))
118 #else
119 #define ATTRIBUTE_CONST
120 #endif
121
122 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
123 #define FLATTEN __attribute__((__flatten__))
124 #else
125 #define FLATTEN
126 #endif
127
128 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
129 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
130 #else
131 #define PRINTFLIKE(f, a)
132 #endif
133
134 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
135 #define MALLOCLIKE __attribute__((__malloc__))
136 #else
137 #define MALLOCLIKE
138 #endif
139
140 /* Forced function inlining */
141 /* Note: Clang also sets __GNUC__ (see other cases below) */
142 #ifndef ALWAYS_INLINE
143 # if defined(__GNUC__)
144 # define ALWAYS_INLINE inline __attribute__((always_inline))
145 # elif defined(_MSC_VER)
146 # define ALWAYS_INLINE __forceinline
147 # else
148 # define ALWAYS_INLINE inline
149 # endif
150 #endif
151
152 /* Used to optionally mark structures with misaligned elements or size as
153 * packed, to trade off performance for space.
154 */
155 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
156 #define PACKED __attribute__((__packed__))
157 #else
158 #define PACKED
159 #endif
160
161 /* Attribute pure is used for functions that have no effects other than their
162 * return value. As a result, calls to it can be dead code eliminated.
163 */
164 #ifdef HAVE_FUNC_ATTRIBUTE_PURE
165 #define ATTRIBUTE_PURE __attribute__((__pure__))
166 #else
167 #define ATTRIBUTE_PURE
168 #endif
169
170 #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
171 #define ATTRIBUTE_RETURNS_NONNULL __attribute__((__returns_nonnull__))
172 #else
173 #define ATTRIBUTE_RETURNS_NONNULL
174 #endif
175
176 #ifndef NORETURN
177 # ifdef _MSC_VER
178 # define NORETURN __declspec(noreturn)
179 # elif defined HAVE_FUNC_ATTRIBUTE_NORETURN
180 # define NORETURN __attribute__((__noreturn__))
181 # else
182 # define NORETURN
183 # endif
184 #endif
185
186 #ifdef __cplusplus
187 /**
188 * Macro function that evaluates to true if T is a trivially
189 * destructible type -- that is, if its (non-virtual) destructor
190 * performs no action and all member variables and base classes are
191 * trivially destructible themselves.
192 */
193 # if (defined(__clang__) && defined(__has_feature))
194 # if __has_feature(has_trivial_destructor)
195 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
196 # endif
197 # elif defined(__GNUC__)
198 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
199 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
200 # endif
201 # elif defined(_MSC_VER) && !defined(__INTEL_COMPILER)
202 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
203 # endif
204 # ifndef HAS_TRIVIAL_DESTRUCTOR
205 /* It's always safe (if inefficient) to assume that a
206 * destructor is non-trivial.
207 */
208 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
209 # endif
210 #endif
211
212 /**
213 * PUBLIC/USED macros
214 *
215 * If we build the library with gcc's -fvisibility=hidden flag, we'll
216 * use the PUBLIC macro to mark functions that are to be exported.
217 *
218 * We also need to define a USED attribute, so the optimizer doesn't
219 * inline a static function that we later use in an alias. - ajax
220 */
221 #ifndef PUBLIC
222 # if defined(__GNUC__)
223 # define PUBLIC __attribute__((visibility("default")))
224 # define USED __attribute__((used))
225 # elif defined(_MSC_VER)
226 # define PUBLIC __declspec(dllexport)
227 # define USED
228 # else
229 # define PUBLIC
230 # define USED
231 # endif
232 #endif
233
234 /**
235 * UNUSED marks variables (or sometimes functions) that have to be defined,
236 * but are sometimes (or always) unused beyond that. A common case is for
237 * a function parameter to be used in some build configurations but not others.
238 * Another case is fallback vfuncs that don't do anything with their params.
239 *
240 * Note that this should not be used for identifiers used in `assert()`;
241 * see ASSERTED below.
242 */
243 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
244 #define UNUSED __attribute__((unused))
245 #else
246 #define UNUSED
247 #endif
248
249 /**
250 * Use ASSERTED to indicate that an identifier is unused outside of an `assert()`,
251 * so that assert-free builds don't get "unused variable" warnings.
252 */
253 #ifdef NDEBUG
254 #define ASSERTED UNUSED
255 #else
256 #define ASSERTED
257 #endif
258
259 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
260 #define MUST_CHECK __attribute__((warn_unused_result))
261 #else
262 #define MUST_CHECK
263 #endif
264
265 #if defined(__GNUC__)
266 #define ATTRIBUTE_NOINLINE __attribute__((noinline))
267 #else
268 #define ATTRIBUTE_NOINLINE
269 #endif
270
271
272 /**
273 * Check that STRUCT::FIELD can hold MAXVAL. We use a lot of bitfields
274 * in Mesa/gallium. We have to be sure they're of sufficient size to
275 * hold the largest expected value.
276 * Note that with MSVC, enums are signed and enum bitfields need one extra
277 * high bit (always zero) to ensure the max value is handled correctly.
278 * This macro will detect that with MSVC, but not GCC.
279 */
280 #define ASSERT_BITFIELD_SIZE(STRUCT, FIELD, MAXVAL) \
281 do { \
282 ASSERTED STRUCT s; \
283 s.FIELD = (MAXVAL); \
284 assert((int) s.FIELD == (MAXVAL) && "Insufficient bitfield size!"); \
285 } while (0)
286
287
288 /** Compute ceiling of integer quotient of A divided by B. */
289 #define DIV_ROUND_UP( A, B ) ( ((A) + (B) - 1) / (B) )
290
291 /** Clamp X to [MIN,MAX]. Turn NaN into MIN, arbitrarily. */
292 #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
293
294 /* Syntax sugar occuring frequently in graphics code */
295 #define SATURATE( X ) CLAMP(X, 0.0f, 1.0f)
296
297 /** Minimum of two values: */
298 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
299
300 /** Maximum of two values: */
301 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
302
303 /** Minimum and maximum of three values: */
304 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
305 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
306
307 /** Align a value to a power of two */
308 #define ALIGN_POT(x, pot_align) (((x) + (pot_align) - 1) & ~((pot_align) - 1))
309
310 /**
311 * Macro for declaring an explicit conversion operator. Defaults to an
312 * implicit conversion if C++11 is not supported.
313 */
314 #if __cplusplus >= 201103L
315 #define EXPLICIT_CONVERSION explicit
316 #elif defined(__cplusplus)
317 #define EXPLICIT_CONVERSION
318 #endif
319
320 /** Set a single bit */
321 #define BITFIELD_BIT(b) (1u << (b))
322 /** Set all bits up to excluding bit b */
323 #define BITFIELD_MASK(b) \
324 ((b) == 32 ? (~0u) : BITFIELD_BIT((b) % 32) - 1)
325 /** Set count bits starting from bit b */
326 #define BITFIELD_RANGE(b, count) \
327 (BITFIELD_MASK((b) + (count)) & ~BITFIELD_MASK(b))
328
329 /** Set a single bit */
330 #define BITFIELD64_BIT(b) (1ull << (b))
331 /** Set all bits up to excluding bit b */
332 #define BITFIELD64_MASK(b) \
333 ((b) == 64 ? (~0ull) : BITFIELD64_BIT(b) - 1)
334 /** Set count bits starting from bit b */
335 #define BITFIELD64_RANGE(b, count) \
336 (BITFIELD64_MASK((b) + (count)) & ~BITFIELD64_MASK(b))
337
338 /* TODO: In future we should try to move this to u_debug.h once header
339 * dependencies are reorganised to allow this.
340 */
341 enum pipe_debug_type
342 {
343 PIPE_DEBUG_TYPE_OUT_OF_MEMORY = 1,
344 PIPE_DEBUG_TYPE_ERROR,
345 PIPE_DEBUG_TYPE_SHADER_INFO,
346 PIPE_DEBUG_TYPE_PERF_INFO,
347 PIPE_DEBUG_TYPE_INFO,
348 PIPE_DEBUG_TYPE_FALLBACK,
349 PIPE_DEBUG_TYPE_CONFORMANCE,
350 };
351
352 #endif /* UTIL_MACROS_H */