86585f609da503565a94042a00d19f17086a6aa9
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"
31 /* Compute the size of an array */
33 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
36 /* For compatibility with Clang's __has_builtin() */
38 # define __has_builtin(x) 0
42 * __builtin_expect macros
44 #if !defined(HAVE___BUILTIN_EXPECT)
45 # define __builtin_expect(x, y) (x)
49 # ifdef HAVE___BUILTIN_EXPECT
50 # define likely(x) __builtin_expect(!!(x), 1)
51 # define unlikely(x) __builtin_expect(!!(x), 0)
53 # define likely(x) (x)
54 # define unlikely(x) (x)
60 * Static (compile-time) assertion.
61 * Basically, use COND to dimension an array. If COND is false/zero the
62 * array size will be -1 and we'll get a compilation error.
64 #define STATIC_ASSERT(COND) \
66 (void) sizeof(char [1 - 2*!(COND)]); \
71 * Unreachable macro. Useful for suppressing "control reaches end of non-void
74 #ifdef HAVE___BUILTIN_UNREACHABLE
75 #define unreachable(str) \
78 __builtin_unreachable(); \
80 #elif defined (_MSC_VER)
81 #define unreachable(str) \
87 #define unreachable(str) assert(!str)
91 * Assume macro. Useful for expressing our assumptions to the compiler,
92 * typically for purposes of silencing warnings.
94 #if __has_builtin(__builtin_assume)
95 #define assume(expr) \
98 __builtin_assume(expr); \
100 #elif defined HAVE___BUILTIN_UNREACHABLE
101 #define assume(expr) ((expr) ? ((void) 0) \
102 : (assert(!"assumption failed"), \
103 __builtin_unreachable()))
104 #elif defined (_MSC_VER)
105 #define assume(expr) __assume(expr)
107 #define assume(expr) assert(expr)
110 /* Attribute const is used for functions that have no effects other than their
111 * return value, and only rely on the argument values to compute the return
112 * value. As a result, calls to it can be CSEed. Note that using memory
113 * pointed to by the arguments is not allowed for const functions.
115 #ifdef HAVE_FUNC_ATTRIBUTE_CONST
116 #define ATTRIBUTE_CONST __attribute__((__const__))
118 #define ATTRIBUTE_CONST
121 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
122 #define FLATTEN __attribute__((__flatten__))
127 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
128 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
130 #define PRINTFLIKE(f, a)
133 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
134 #define MALLOCLIKE __attribute__((__malloc__))
139 /* Forced function inlining */
140 /* Note: Clang also sets __GNUC__ (see other cases below) */
141 #ifndef ALWAYS_INLINE
142 # if defined(__GNUC__)
143 # define ALWAYS_INLINE inline __attribute__((always_inline))
144 # elif defined(_MSC_VER)
145 # define ALWAYS_INLINE __forceinline
147 # define ALWAYS_INLINE inline
151 /* Used to optionally mark structures with misaligned elements or size as
152 * packed, to trade off performance for space.
154 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
155 #define PACKED __attribute__((__packed__))
160 /* Attribute pure is used for functions that have no effects other than their
161 * return value. As a result, calls to it can be dead code eliminated.
163 #ifdef HAVE_FUNC_ATTRIBUTE_PURE
164 #define ATTRIBUTE_PURE __attribute__((__pure__))
166 #define ATTRIBUTE_PURE
169 #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
170 #define ATTRIBUTE_RETURNS_NONNULL __attribute__((__returns_nonnull__))
172 #define ATTRIBUTE_RETURNS_NONNULL
177 # define NORETURN __declspec(noreturn)
178 # elif defined HAVE_FUNC_ATTRIBUTE_NORETURN
179 # define NORETURN __attribute__((__noreturn__))
187 * Macro function that evaluates to true if T is a trivially
188 * destructible type -- that is, if its (non-virtual) destructor
189 * performs no action and all member variables and base classes are
190 * trivially destructible themselves.
192 # if (defined(__clang__) && defined(__has_feature))
193 # if __has_feature(has_trivial_destructor)
194 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
196 # elif defined(__GNUC__)
197 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
198 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
200 # elif defined(_MSC_VER) && !defined(__INTEL_COMPILER)
201 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
203 # ifndef HAS_TRIVIAL_DESTRUCTOR
204 /* It's always safe (if inefficient) to assume that a
205 * destructor is non-trivial.
207 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
214 * If we build the library with gcc's -fvisibility=hidden flag, we'll
215 * use the PUBLIC macro to mark functions that are to be exported.
217 * We also need to define a USED attribute, so the optimizer doesn't
218 * inline a static function that we later use in an alias. - ajax
221 # if defined(__GNUC__)
222 # define PUBLIC __attribute__((visibility("default")))
223 # define USED __attribute__((used))
224 # elif defined(_MSC_VER)
225 # define PUBLIC __declspec(dllexport)
234 * UNUSED marks variables (or sometimes functions) that have to be defined,
235 * but are sometimes (or always) unused beyond that. A common case is for
236 * a function parameter to be used in some build configurations but not others.
237 * Another case is fallback vfuncs that don't do anything with their params.
239 * Note that this should not be used for identifiers used in `assert()`;
240 * see ASSERTED below.
242 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
243 #define UNUSED __attribute__((unused))
249 * Use ASSERTED to indicate that an identifier is unused outside of an `assert()`,
250 * so that assert-free builds don't get "unused variable" warnings.
253 #define ASSERTED UNUSED
258 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
259 #define MUST_CHECK __attribute__((warn_unused_result))
264 #if defined(__GNUC__)
265 #define ATTRIBUTE_NOINLINE __attribute__((noinline))
267 #define ATTRIBUTE_NOINLINE
272 * Check that STRUCT::FIELD can hold MAXVAL. We use a lot of bitfields
273 * in Mesa/gallium. We have to be sure they're of sufficient size to
274 * hold the largest expected value.
275 * Note that with MSVC, enums are signed and enum bitfields need one extra
276 * high bit (always zero) to ensure the max value is handled correctly.
277 * This macro will detect that with MSVC, but not GCC.
279 #define ASSERT_BITFIELD_SIZE(STRUCT, FIELD, MAXVAL) \
282 s.FIELD = (MAXVAL); \
283 assert((int) s.FIELD == (MAXVAL) && "Insufficient bitfield size!"); \
287 /** Compute ceiling of integer quotient of A divided by B. */
288 #define DIV_ROUND_UP( A, B ) ( ((A) + (B) - 1) / (B) )
290 /** Clamp X to [MIN,MAX]. Turn NaN into MIN, arbitrarily. */
291 #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
293 /** Minimum of two values: */
294 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
296 /** Maximum of two values: */
297 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
299 /** Minimum and maximum of three values: */
300 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
301 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
303 /** Align a value to a power of two */
304 #define ALIGN_POT(x, pot_align) (((x) + (pot_align) - 1) & ~((pot_align) - 1))
307 * Macro for declaring an explicit conversion operator. Defaults to an
308 * implicit conversion if C++11 is not supported.
310 #if __cplusplus >= 201103L
311 #define EXPLICIT_CONVERSION explicit
312 #elif defined(__cplusplus)
313 #define EXPLICIT_CONVERSION
316 /** Set a single bit */
317 #define BITFIELD_BIT(b) (1u << (b))
318 /** Set all bits up to excluding bit b */
319 #define BITFIELD_MASK(b) \
320 ((b) == 32 ? (~0u) : BITFIELD_BIT((b) % 32) - 1)
321 /** Set count bits starting from bit b */
322 #define BITFIELD_RANGE(b, count) \
323 (BITFIELD_MASK((b) + (count)) & ~BITFIELD_MASK(b))
325 /** Set a single bit */
326 #define BITFIELD64_BIT(b) (1ull << (b))
327 /** Set all bits up to excluding bit b */
328 #define BITFIELD64_MASK(b) \
329 ((b) == 64 ? (~0ull) : BITFIELD64_BIT(b) - 1)
330 /** Set count bits starting from bit b */
331 #define BITFIELD64_RANGE(b, count) \
332 (BITFIELD64_MASK((b) + (count)) & ~BITFIELD64_MASK(b))
334 #endif /* UTIL_MACROS_H */