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(_WIN32) 223 # define PUBLIC __declspec(dllexport) 224 # define USED 225 # elif defined(__GNUC__) 226 # define PUBLIC __attribute__((visibility("default"))) 227 # define USED __attribute__((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 #elif defined(_MSC_VER) 268 #define ATTRIBUTE_NOINLINE __declspec(noinline) 269 #else 270 #define ATTRIBUTE_NOINLINE 271 #endif 272 273 274 /** 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. 281 */ 282 #define ASSERT_BITFIELD_SIZE(STRUCT, FIELD, MAXVAL) \ 283 do { \ 284 ASSERTED STRUCT s; \ 285 s.FIELD = (MAXVAL); \ 286 assert((int) s.FIELD == (MAXVAL) && "Insufficient bitfield size!"); \ 287 } while (0) 288 289 290 /** Compute ceiling of integer quotient of A divided by B. */ 291 #define DIV_ROUND_UP( A, B ) ( ((A) + (B) - 1) / (B) ) 292 293 /** Clamp X to [MIN,MAX]. Turn NaN into MIN, arbitrarily. */ 294 #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) ) 295 296 /* Syntax sugar occuring frequently in graphics code */ 297 #define SATURATE( X ) CLAMP(X, 0.0f, 1.0f) 298 299 /** Minimum of two values: */ 300 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) 301 302 /** Maximum of two values: */ 303 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) 304 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)) 308 309 /** Align a value to a power of two */ 310 #define ALIGN_POT(x, pot_align) (((x) + (pot_align) - 1) & ~((pot_align) - 1)) 311 312 /** 313 * Macro for declaring an explicit conversion operator. Defaults to an 314 * implicit conversion if C++11 is not supported. 315 */ 316 #if __cplusplus >= 201103L 317 #define EXPLICIT_CONVERSION explicit 318 #elif defined(__cplusplus) 319 #define EXPLICIT_CONVERSION 320 #endif 321 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)) 330 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)) 339 340 /* TODO: In future we should try to move this to u_debug.h once header 341 * dependencies are reorganised to allow this. 342 */ 343 enum pipe_debug_type 344 { 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, 352 }; 353 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) 359 #else 360 #define alignof(t) __alignof__(t) 361 #endif 362 #endif 363 364 #endif /* UTIL_MACROS_H */ 365