2 * Copyright © 2014 Intel Corporation
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:
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
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
29 #include "c99_compat.h"
30 #include "c11_compat.h"
32 /* Compute the size of an array */
34 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
37 /* For compatibility with Clang's __has_builtin() */
39 # define __has_builtin(x) 0
43 * __builtin_expect macros
45 #if !defined(HAVE___BUILTIN_EXPECT)
46 # define __builtin_expect(x, y) (x)
50 # ifdef HAVE___BUILTIN_EXPECT
51 # define likely(x) __builtin_expect(!!(x), 1)
52 # define unlikely(x) __builtin_expect(!!(x), 0)
54 # define likely(x) (x)
55 # define unlikely(x) (x)
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.
65 #define STATIC_ASSERT(COND) \
67 (void) sizeof(char [1 - 2*!(COND)]); \
72 * Unreachable macro. Useful for suppressing "control reaches end of non-void
75 #if defined(HAVE___BUILTIN_UNREACHABLE) || __has_builtin(__builtin_unreachable)
76 #define unreachable(str) \
79 __builtin_unreachable(); \
81 #elif defined (_MSC_VER)
82 #define unreachable(str) \
88 #define unreachable(str) assert(!str)
92 * Assume macro. Useful for expressing our assumptions to the compiler,
93 * typically for purposes of silencing warnings.
95 #if __has_builtin(__builtin_assume)
96 #define assume(expr) \
99 __builtin_assume(expr); \
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)
108 #define assume(expr) assert(expr)
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.
116 #ifdef HAVE_FUNC_ATTRIBUTE_CONST
117 #define ATTRIBUTE_CONST __attribute__((__const__))
119 #define ATTRIBUTE_CONST
122 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
123 #define FLATTEN __attribute__((__flatten__))
128 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
129 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
131 #define PRINTFLIKE(f, a)
134 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
135 #define MALLOCLIKE __attribute__((__malloc__))
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
148 # define ALWAYS_INLINE inline
152 /* Used to optionally mark structures with misaligned elements or size as
153 * packed, to trade off performance for space.
155 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
156 #define PACKED __attribute__((__packed__))
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.
164 #ifdef HAVE_FUNC_ATTRIBUTE_PURE
165 #define ATTRIBUTE_PURE __attribute__((__pure__))
167 #define ATTRIBUTE_PURE
170 #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
171 #define ATTRIBUTE_RETURNS_NONNULL __attribute__((__returns_nonnull__))
173 #define ATTRIBUTE_RETURNS_NONNULL
178 # define NORETURN __declspec(noreturn)
179 # elif defined HAVE_FUNC_ATTRIBUTE_NORETURN
180 # define NORETURN __attribute__((__noreturn__))
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.
193 # if (defined(__clang__) && defined(__has_feature))
194 # if __has_feature(has_trivial_destructor)
195 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
197 # elif defined(__GNUC__)
198 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
199 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
201 # elif defined(_MSC_VER) && !defined(__INTEL_COMPILER)
202 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
204 # ifndef HAS_TRIVIAL_DESTRUCTOR
205 /* It's always safe (if inefficient) to assume that a
206 * destructor is non-trivial.
208 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
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.
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
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)
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.
240 * Note that this should not be used for identifiers used in `assert()`;
241 * see ASSERTED below.
243 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
244 #define UNUSED __attribute__((unused))
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.
254 #define ASSERTED UNUSED
259 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
260 #define MUST_CHECK __attribute__((warn_unused_result))
265 #if defined(__GNUC__)
266 #define ATTRIBUTE_NOINLINE __attribute__((noinline))
267 #elif defined(_MSC_VER)
268 #define ATTRIBUTE_NOINLINE __declspec(noinline)
270 #define ATTRIBUTE_NOINLINE
275 * Check that STRUCT::FIELD can hold MAXVAL. We use a lot of bitfields
276 * in Mesa/gallium. We have to be sure they're of sufficient size to
277 * hold the largest expected value.
278 * Note that with MSVC, enums are signed and enum bitfields need one extra
279 * high bit (always zero) to ensure the max value is handled correctly.
280 * This macro will detect that with MSVC, but not GCC.
282 #define ASSERT_BITFIELD_SIZE(STRUCT, FIELD, MAXVAL) \
285 s.FIELD = (MAXVAL); \
286 assert((int) s.FIELD == (MAXVAL) && "Insufficient bitfield size!"); \
290 /** Compute ceiling of integer quotient of A divided by B. */
291 #define DIV_ROUND_UP( A, B ) ( ((A) + (B) - 1) / (B) )
293 /** Clamp X to [MIN,MAX]. Turn NaN into MIN, arbitrarily. */
294 #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
296 /* Syntax sugar occuring frequently in graphics code */
297 #define SATURATE( X ) CLAMP(X, 0.0f, 1.0f)
299 /** Minimum of two values: */
300 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
302 /** Maximum of two values: */
303 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
305 /** Minimum and maximum of three values: */
306 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
307 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
309 /** Align a value to a power of two */
310 #define ALIGN_POT(x, pot_align) (((x) + (pot_align) - 1) & ~((pot_align) - 1))
313 * Macro for declaring an explicit conversion operator. Defaults to an
314 * implicit conversion if C++11 is not supported.
316 #if __cplusplus >= 201103L
317 #define EXPLICIT_CONVERSION explicit
318 #elif defined(__cplusplus)
319 #define EXPLICIT_CONVERSION
322 /** Set a single bit */
323 #define BITFIELD_BIT(b) (1u << (b))
324 /** Set all bits up to excluding bit b */
325 #define BITFIELD_MASK(b) \
326 ((b) == 32 ? (~0u) : BITFIELD_BIT((b) % 32) - 1)
327 /** Set count bits starting from bit b */
328 #define BITFIELD_RANGE(b, count) \
329 (BITFIELD_MASK((b) + (count)) & ~BITFIELD_MASK(b))
331 /** Set a single bit */
332 #define BITFIELD64_BIT(b) (1ull << (b))
333 /** Set all bits up to excluding bit b */
334 #define BITFIELD64_MASK(b) \
335 ((b) == 64 ? (~0ull) : BITFIELD64_BIT(b) - 1)
336 /** Set count bits starting from bit b */
337 #define BITFIELD64_RANGE(b, count) \
338 (BITFIELD64_MASK((b) + (count)) & ~BITFIELD64_MASK(b))
340 /* TODO: In future we should try to move this to u_debug.h once header
341 * dependencies are reorganised to allow this.
345 PIPE_DEBUG_TYPE_OUT_OF_MEMORY
= 1,
346 PIPE_DEBUG_TYPE_ERROR
,
347 PIPE_DEBUG_TYPE_SHADER_INFO
,
348 PIPE_DEBUG_TYPE_PERF_INFO
,
349 PIPE_DEBUG_TYPE_INFO
,
350 PIPE_DEBUG_TYPE_FALLBACK
,
351 PIPE_DEBUG_TYPE_CONFORMANCE
,
354 #if !defined(alignof) && !defined(__cplusplus)
355 #if __STDC_VERSION__ >= 201112L
356 #define alignof(t) _Alignof(t)
357 #elif defined(_MSC_VER)
358 #define alignof(t) __alignof(t)
360 #define alignof(t) __alignof__(t)
364 #endif /* UTIL_MACROS_H */