1af69d88dSmrg/*
2af69d88dSmrg * Copyright © 2014 Intel Corporation
3af69d88dSmrg *
4af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5af69d88dSmrg * copy of this software and associated documentation files (the "Software"),
6af69d88dSmrg * to deal in the Software without restriction, including without limitation
7af69d88dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8af69d88dSmrg * and/or sell copies of the Software, and to permit persons to whom the
9af69d88dSmrg * Software is furnished to do so, subject to the following conditions:
10af69d88dSmrg *
11af69d88dSmrg * The above copyright notice and this permission notice (including the next
12af69d88dSmrg * paragraph) shall be included in all copies or substantial portions of the
13af69d88dSmrg * Software.
14af69d88dSmrg *
15af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16af69d88dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17af69d88dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19af69d88dSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20af69d88dSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21af69d88dSmrg * IN THE SOFTWARE.
22af69d88dSmrg */
23af69d88dSmrg
24af69d88dSmrg#ifndef UTIL_MACROS_H
25af69d88dSmrg#define UTIL_MACROS_H
26af69d88dSmrg
277ec681f3Smrg#include <stdio.h>
2801e04c3fSmrg#include <assert.h>
2901e04c3fSmrg
3001e04c3fSmrg#include "c99_compat.h"
317ec681f3Smrg#include "c11_compat.h"
327ec681f3Smrg
337ec681f3Smrg#include <stdint.h>
3401e04c3fSmrg
35af69d88dSmrg/* Compute the size of an array */
36af69d88dSmrg#ifndef ARRAY_SIZE
3701e04c3fSmrg#  define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
38af69d88dSmrg#endif
39af69d88dSmrg
4001e04c3fSmrg/* For compatibility with Clang's __has_builtin() */
4101e04c3fSmrg#ifndef __has_builtin
4201e04c3fSmrg#  define __has_builtin(x) 0
4301e04c3fSmrg#endif
44af69d88dSmrg
457ec681f3Smrg#ifndef __has_attribute
467ec681f3Smrg#  define __has_attribute(x) 0
477ec681f3Smrg#endif
487ec681f3Smrg
49af69d88dSmrg/**
50af69d88dSmrg * __builtin_expect macros
51af69d88dSmrg */
5201e04c3fSmrg#if !defined(HAVE___BUILTIN_EXPECT)
53af69d88dSmrg#  define __builtin_expect(x, y) (x)
54af69d88dSmrg#endif
55af69d88dSmrg
56af69d88dSmrg#ifndef likely
5701e04c3fSmrg#  ifdef HAVE___BUILTIN_EXPECT
58af69d88dSmrg#    define likely(x)   __builtin_expect(!!(x), 1)
59af69d88dSmrg#    define unlikely(x) __builtin_expect(!!(x), 0)
60af69d88dSmrg#  else
61af69d88dSmrg#    define likely(x)   (x)
62af69d88dSmrg#    define unlikely(x) (x)
63af69d88dSmrg#  endif
64af69d88dSmrg#endif
65af69d88dSmrg
667ec681f3Smrg/**
677ec681f3Smrg * __builtin_types_compatible_p compat
687ec681f3Smrg */
697ec681f3Smrg#if defined(__cplusplus) || !defined(HAVE___BUILTIN_TYPES_COMPATIBLE_P)
707ec681f3Smrg#  define __builtin_types_compatible_p(type1, type2) (1)
717ec681f3Smrg#endif
72af69d88dSmrg
73af69d88dSmrg/**
74af69d88dSmrg * Static (compile-time) assertion.
75af69d88dSmrg */
767ec681f3Smrg#if defined(_MSC_VER)
777ec681f3Smrg   /* MSVC doesn't like VLA's, but it also dislikes zero length arrays
787ec681f3Smrg    * (which gcc is happy with), so we have to define STATIC_ASSERT()
797ec681f3Smrg    * slightly differently.
807ec681f3Smrg    */
817ec681f3Smrg#  define STATIC_ASSERT(COND) do {         \
827ec681f3Smrg      (void) sizeof(char [(COND) != 0]);   \
837ec681f3Smrg   } while (0)
847ec681f3Smrg#elif defined(__GNUC__)
857ec681f3Smrg   /* This version of STATIC_ASSERT() relies on VLAs.  If COND is
867ec681f3Smrg    * false/zero, the array size will be -1 and we'll get a compile
877ec681f3Smrg    * error
887ec681f3Smrg    */
897ec681f3Smrg#  define STATIC_ASSERT(COND) do {         \
90af69d88dSmrg      (void) sizeof(char [1 - 2*!(COND)]); \
91af69d88dSmrg   } while (0)
927ec681f3Smrg#else
937ec681f3Smrg#  define STATIC_ASSERT(COND) do { } while (0)
947ec681f3Smrg#endif
95af69d88dSmrg
967ec681f3Smrg/**
977ec681f3Smrg * container_of - cast a member of a structure out to the containing structure
987ec681f3Smrg * @ptr:        the pointer to the member.
997ec681f3Smrg * @type:       the type of the container struct this is embedded in.
1007ec681f3Smrg * @member:     the name of the member within the struct.
1017ec681f3Smrg */
1027ec681f3Smrg#ifndef __GNUC__
1037ec681f3Smrg   /* a grown-up compiler is required for the extra type checking: */
1047ec681f3Smrg#  define container_of(ptr, type, member)                               \
1057ec681f3Smrg      (type*)((uint8_t *)ptr - offsetof(type, member))
1067ec681f3Smrg#else
1077ec681f3Smrg#  define __same_type(a, b) \
1087ec681f3Smrg      __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
1097ec681f3Smrg#  define container_of(ptr, type, member) ({                            \
1107ec681f3Smrg         uint8_t *__mptr = (uint8_t *)(ptr);                            \
1117ec681f3Smrg         STATIC_ASSERT(__same_type(*(ptr), ((type *)0)->member) ||      \
1127ec681f3Smrg                       __same_type(*(ptr), void) ||                     \
1137ec681f3Smrg                       !"pointer type mismatch in container_of()");     \
1147ec681f3Smrg         ((type *)(__mptr - offsetof(type, member)));                   \
1157ec681f3Smrg      })
1167ec681f3Smrg#endif
117af69d88dSmrg
118af69d88dSmrg/**
119af69d88dSmrg * Unreachable macro. Useful for suppressing "control reaches end of non-void
120af69d88dSmrg * function" warnings.
121af69d88dSmrg */
1227ec681f3Smrg#if defined(HAVE___BUILTIN_UNREACHABLE) || __has_builtin(__builtin_unreachable)
123af69d88dSmrg#define unreachable(str)    \
124af69d88dSmrgdo {                        \
125af69d88dSmrg   assert(!str);            \
126af69d88dSmrg   __builtin_unreachable(); \
127af69d88dSmrg} while (0)
12801e04c3fSmrg#elif defined (_MSC_VER)
12901e04c3fSmrg#define unreachable(str)    \
130af69d88dSmrgdo {                        \
131af69d88dSmrg   assert(!str);            \
13201e04c3fSmrg   __assume(0);             \
13301e04c3fSmrg} while (0)
13401e04c3fSmrg#else
13501e04c3fSmrg#define unreachable(str) assert(!str)
13601e04c3fSmrg#endif
13701e04c3fSmrg
13801e04c3fSmrg/**
13901e04c3fSmrg * Assume macro. Useful for expressing our assumptions to the compiler,
14001e04c3fSmrg * typically for purposes of silencing warnings.
14101e04c3fSmrg */
14201e04c3fSmrg#if __has_builtin(__builtin_assume)
14301e04c3fSmrg#define assume(expr)       \
14401e04c3fSmrgdo {                       \
14501e04c3fSmrg   assert(expr);           \
14601e04c3fSmrg   __builtin_assume(expr); \
147af69d88dSmrg} while (0)
14801e04c3fSmrg#elif defined HAVE___BUILTIN_UNREACHABLE
14901e04c3fSmrg#define assume(expr) ((expr) ? ((void) 0) \
15001e04c3fSmrg                             : (assert(!"assumption failed"), \
15101e04c3fSmrg                                __builtin_unreachable()))
15201e04c3fSmrg#elif defined (_MSC_VER)
15301e04c3fSmrg#define assume(expr) __assume(expr)
15401e04c3fSmrg#else
15501e04c3fSmrg#define assume(expr) assert(expr)
156af69d88dSmrg#endif
157af69d88dSmrg
15801e04c3fSmrg/* Attribute const is used for functions that have no effects other than their
15901e04c3fSmrg * return value, and only rely on the argument values to compute the return
16001e04c3fSmrg * value.  As a result, calls to it can be CSEed.  Note that using memory
16101e04c3fSmrg * pointed to by the arguments is not allowed for const functions.
16201e04c3fSmrg */
16301e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_CONST
16401e04c3fSmrg#define ATTRIBUTE_CONST __attribute__((__const__))
16501e04c3fSmrg#else
16601e04c3fSmrg#define ATTRIBUTE_CONST
167af69d88dSmrg#endif
168af69d88dSmrg
16901e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
17001e04c3fSmrg#define FLATTEN __attribute__((__flatten__))
17101e04c3fSmrg#else
17201e04c3fSmrg#define FLATTEN
17301e04c3fSmrg#endif
174af69d88dSmrg
17501e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
1767ec681f3Smrg#if defined (__MINGW_PRINTF_FORMAT)
1777ec681f3Smrg# define PRINTFLIKE(f, a) __attribute__ ((format(__MINGW_PRINTF_FORMAT, f, a)))
1787ec681f3Smrg#else
1797ec681f3Smrg# define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
1807ec681f3Smrg#endif
181af69d88dSmrg#else
182af69d88dSmrg#define PRINTFLIKE(f, a)
183af69d88dSmrg#endif
184af69d88dSmrg
18501e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
18601e04c3fSmrg#define MALLOCLIKE __attribute__((__malloc__))
18701e04c3fSmrg#else
18801e04c3fSmrg#define MALLOCLIKE
18901e04c3fSmrg#endif
19001e04c3fSmrg
19101e04c3fSmrg/* Forced function inlining */
19201e04c3fSmrg/* Note: Clang also sets __GNUC__ (see other cases below) */
19301e04c3fSmrg#ifndef ALWAYS_INLINE
19401e04c3fSmrg#  if defined(__GNUC__)
19501e04c3fSmrg#    define ALWAYS_INLINE inline __attribute__((always_inline))
19601e04c3fSmrg#  elif defined(_MSC_VER)
19701e04c3fSmrg#    define ALWAYS_INLINE __forceinline
19801e04c3fSmrg#  else
19901e04c3fSmrg#    define ALWAYS_INLINE inline
20001e04c3fSmrg#  endif
20101e04c3fSmrg#endif
202af69d88dSmrg
203af69d88dSmrg/* Used to optionally mark structures with misaligned elements or size as
204af69d88dSmrg * packed, to trade off performance for space.
205af69d88dSmrg */
20601e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_PACKED
207af69d88dSmrg#define PACKED __attribute__((__packed__))
208af69d88dSmrg#else
209af69d88dSmrg#define PACKED
210af69d88dSmrg#endif
211af69d88dSmrg
21201e04c3fSmrg/* Attribute pure is used for functions that have no effects other than their
21301e04c3fSmrg * return value.  As a result, calls to it can be dead code eliminated.
21401e04c3fSmrg */
21501e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_PURE
21601e04c3fSmrg#define ATTRIBUTE_PURE __attribute__((__pure__))
21701e04c3fSmrg#else
21801e04c3fSmrg#define ATTRIBUTE_PURE
21901e04c3fSmrg#endif
22001e04c3fSmrg
22101e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
22201e04c3fSmrg#define ATTRIBUTE_RETURNS_NONNULL __attribute__((__returns_nonnull__))
22301e04c3fSmrg#else
22401e04c3fSmrg#define ATTRIBUTE_RETURNS_NONNULL
22501e04c3fSmrg#endif
22601e04c3fSmrg
22701e04c3fSmrg#ifndef NORETURN
22801e04c3fSmrg#  ifdef _MSC_VER
22901e04c3fSmrg#    define NORETURN __declspec(noreturn)
23001e04c3fSmrg#  elif defined HAVE_FUNC_ATTRIBUTE_NORETURN
23101e04c3fSmrg#    define NORETURN __attribute__((__noreturn__))
23201e04c3fSmrg#  else
23301e04c3fSmrg#    define NORETURN
23401e04c3fSmrg#  endif
23501e04c3fSmrg#endif
236af69d88dSmrg
2377ec681f3Smrg#ifdef _MSC_VER
2387ec681f3Smrg#define ALIGN16 __declspec(align(16))
2397ec681f3Smrg#else
2407ec681f3Smrg#define ALIGN16 __attribute__((aligned(16)))
2417ec681f3Smrg#endif
2427ec681f3Smrg
243af69d88dSmrg#ifdef __cplusplus
244af69d88dSmrg/**
245af69d88dSmrg * Macro function that evaluates to true if T is a trivially
246af69d88dSmrg * destructible type -- that is, if its (non-virtual) destructor
247af69d88dSmrg * performs no action and all member variables and base classes are
248af69d88dSmrg * trivially destructible themselves.
249af69d88dSmrg */
25001e04c3fSmrg#   if (defined(__clang__) && defined(__has_feature))
25101e04c3fSmrg#      if __has_feature(has_trivial_destructor)
252af69d88dSmrg#         define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
253af69d88dSmrg#      endif
25401e04c3fSmrg#   elif defined(__GNUC__)
25501e04c3fSmrg#      if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
256af69d88dSmrg#         define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
257af69d88dSmrg#      endif
25801e04c3fSmrg#   elif defined(_MSC_VER) && !defined(__INTEL_COMPILER)
25901e04c3fSmrg#      define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
260af69d88dSmrg#   endif
261af69d88dSmrg#   ifndef HAS_TRIVIAL_DESTRUCTOR
262af69d88dSmrg       /* It's always safe (if inefficient) to assume that a
263af69d88dSmrg        * destructor is non-trivial.
264af69d88dSmrg        */
265af69d88dSmrg#      define HAS_TRIVIAL_DESTRUCTOR(T) (false)
266af69d88dSmrg#   endif
267af69d88dSmrg#endif
268af69d88dSmrg
26901e04c3fSmrg/**
27001e04c3fSmrg * PUBLIC/USED macros
27101e04c3fSmrg *
27201e04c3fSmrg * If we build the library with gcc's -fvisibility=hidden flag, we'll
27301e04c3fSmrg * use the PUBLIC macro to mark functions that are to be exported.
27401e04c3fSmrg *
27501e04c3fSmrg * We also need to define a USED attribute, so the optimizer doesn't
27601e04c3fSmrg * inline a static function that we later use in an alias. - ajax
27701e04c3fSmrg */
27801e04c3fSmrg#ifndef PUBLIC
2797ec681f3Smrg#  if defined(_WIN32)
28001e04c3fSmrg#    define PUBLIC __declspec(dllexport)
28101e04c3fSmrg#    define USED
2827ec681f3Smrg#  elif defined(__GNUC__)
2837ec681f3Smrg#    define PUBLIC __attribute__((visibility("default")))
2847ec681f3Smrg#    define USED __attribute__((used))
28501e04c3fSmrg#  else
28601e04c3fSmrg#    define PUBLIC
28701e04c3fSmrg#    define USED
28801e04c3fSmrg#  endif
28901e04c3fSmrg#endif
29001e04c3fSmrg
2917ec681f3Smrg/**
2927ec681f3Smrg * UNUSED marks variables (or sometimes functions) that have to be defined,
2937ec681f3Smrg * but are sometimes (or always) unused beyond that. A common case is for
2947ec681f3Smrg * a function parameter to be used in some build configurations but not others.
2957ec681f3Smrg * Another case is fallback vfuncs that don't do anything with their params.
2967ec681f3Smrg *
2977ec681f3Smrg * Note that this should not be used for identifiers used in `assert()`;
2987ec681f3Smrg * see ASSERTED below.
2997ec681f3Smrg */
30001e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
30101e04c3fSmrg#define UNUSED __attribute__((unused))
30201e04c3fSmrg#else
30301e04c3fSmrg#define UNUSED
30401e04c3fSmrg#endif
30501e04c3fSmrg
3067ec681f3Smrg/**
3077ec681f3Smrg * Use ASSERTED to indicate that an identifier is unused outside of an `assert()`,
3087ec681f3Smrg * so that assert-free builds don't get "unused variable" warnings.
3097ec681f3Smrg */
3107ec681f3Smrg#ifdef NDEBUG
3117ec681f3Smrg#define ASSERTED UNUSED
3127ec681f3Smrg#else
3137ec681f3Smrg#define ASSERTED
3147ec681f3Smrg#endif
31501e04c3fSmrg
31601e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
31701e04c3fSmrg#define MUST_CHECK __attribute__((warn_unused_result))
31801e04c3fSmrg#else
31901e04c3fSmrg#define MUST_CHECK
32001e04c3fSmrg#endif
32101e04c3fSmrg
32201e04c3fSmrg#if defined(__GNUC__)
32301e04c3fSmrg#define ATTRIBUTE_NOINLINE __attribute__((noinline))
3247ec681f3Smrg#elif defined(_MSC_VER)
3257ec681f3Smrg#define ATTRIBUTE_NOINLINE __declspec(noinline)
32601e04c3fSmrg#else
32701e04c3fSmrg#define ATTRIBUTE_NOINLINE
32801e04c3fSmrg#endif
32901e04c3fSmrg
3307ec681f3Smrg/* Use as: enum name { X, Y } ENUM_PACKED; */
3317ec681f3Smrg#if defined(__GNUC__)
3327ec681f3Smrg#define ENUM_PACKED __attribute__((packed))
3337ec681f3Smrg#else
3347ec681f3Smrg#define ENUM_PACKED
3357ec681f3Smrg#endif
3367ec681f3Smrg
33701e04c3fSmrg
33801e04c3fSmrg/**
33901e04c3fSmrg * Check that STRUCT::FIELD can hold MAXVAL.  We use a lot of bitfields
34001e04c3fSmrg * in Mesa/gallium.  We have to be sure they're of sufficient size to
34101e04c3fSmrg * hold the largest expected value.
34201e04c3fSmrg * Note that with MSVC, enums are signed and enum bitfields need one extra
34301e04c3fSmrg * high bit (always zero) to ensure the max value is handled correctly.
34401e04c3fSmrg * This macro will detect that with MSVC, but not GCC.
34501e04c3fSmrg */
34601e04c3fSmrg#define ASSERT_BITFIELD_SIZE(STRUCT, FIELD, MAXVAL) \
34701e04c3fSmrg   do { \
3487ec681f3Smrg      ASSERTED STRUCT s; \
34901e04c3fSmrg      s.FIELD = (MAXVAL); \
35001e04c3fSmrg      assert((int) s.FIELD == (MAXVAL) && "Insufficient bitfield size!"); \
35101e04c3fSmrg   } while (0)
35201e04c3fSmrg
35301e04c3fSmrg
35401e04c3fSmrg/** Compute ceiling of integer quotient of A divided by B. */
3557ec681f3Smrg#define DIV_ROUND_UP( A, B )  ( ((A) + (B) - 1) / (B) )
35601e04c3fSmrg
35701e04c3fSmrg/** Clamp X to [MIN,MAX].  Turn NaN into MIN, arbitrarily. */
35801e04c3fSmrg#define CLAMP( X, MIN, MAX )  ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
35901e04c3fSmrg
3607ec681f3Smrg/* Syntax sugar occuring frequently in graphics code */
3617ec681f3Smrg#define SATURATE( X ) CLAMP(X, 0.0f, 1.0f)
3627ec681f3Smrg
36301e04c3fSmrg/** Minimum of two values: */
36401e04c3fSmrg#define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
36501e04c3fSmrg
36601e04c3fSmrg/** Maximum of two values: */
36701e04c3fSmrg#define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
36801e04c3fSmrg
36901e04c3fSmrg/** Minimum and maximum of three values: */
37001e04c3fSmrg#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
37101e04c3fSmrg#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
37201e04c3fSmrg
37301e04c3fSmrg/** Align a value to a power of two */
37401e04c3fSmrg#define ALIGN_POT(x, pot_align) (((x) + (pot_align) - 1) & ~((pot_align) - 1))
37501e04c3fSmrg
37601e04c3fSmrg/**
37701e04c3fSmrg * Macro for declaring an explicit conversion operator.  Defaults to an
37801e04c3fSmrg * implicit conversion if C++11 is not supported.
37901e04c3fSmrg */
38001e04c3fSmrg#if __cplusplus >= 201103L
38101e04c3fSmrg#define EXPLICIT_CONVERSION explicit
38201e04c3fSmrg#elif defined(__cplusplus)
38301e04c3fSmrg#define EXPLICIT_CONVERSION
38401e04c3fSmrg#endif
38501e04c3fSmrg
3868a1362adSmaya/** Set a single bit */
3878a1362adSmaya#define BITFIELD_BIT(b)      (1u << (b))
3888a1362adSmaya/** Set all bits up to excluding bit b */
3898a1362adSmaya#define BITFIELD_MASK(b)      \
3908a1362adSmaya   ((b) == 32 ? (~0u) : BITFIELD_BIT((b) % 32) - 1)
3918a1362adSmaya/** Set count bits starting from bit b  */
3928a1362adSmaya#define BITFIELD_RANGE(b, count) \
3938a1362adSmaya   (BITFIELD_MASK((b) + (count)) & ~BITFIELD_MASK(b))
3948a1362adSmaya
3958a1362adSmaya/** Set a single bit */
3968a1362adSmaya#define BITFIELD64_BIT(b)      (1ull << (b))
3978a1362adSmaya/** Set all bits up to excluding bit b */
3988a1362adSmaya#define BITFIELD64_MASK(b)      \
3998a1362adSmaya   ((b) == 64 ? (~0ull) : BITFIELD64_BIT(b) - 1)
4008a1362adSmaya/** Set count bits starting from bit b  */
4018a1362adSmaya#define BITFIELD64_RANGE(b, count) \
4028a1362adSmaya   (BITFIELD64_MASK((b) + (count)) & ~BITFIELD64_MASK(b))
4038a1362adSmaya
4047ec681f3Smrgstatic inline int64_t
4057ec681f3Smrgu_intN_max(unsigned bit_size)
4067ec681f3Smrg{
4077ec681f3Smrg   assert(bit_size <= 64 && bit_size > 0);
4087ec681f3Smrg   return INT64_MAX >> (64 - bit_size);
4097ec681f3Smrg}
4107ec681f3Smrg
4117ec681f3Smrgstatic inline int64_t
4127ec681f3Smrgu_intN_min(unsigned bit_size)
4137ec681f3Smrg{
4147ec681f3Smrg   /* On 2's compliment platforms, which is every platform Mesa is likely to
4157ec681f3Smrg    * every worry about, stdint.h generally calculated INT##_MIN in this
4167ec681f3Smrg    * manner.
4177ec681f3Smrg    */
4187ec681f3Smrg   return (-u_intN_max(bit_size)) - 1;
4197ec681f3Smrg}
4207ec681f3Smrg
4217ec681f3Smrgstatic inline uint64_t
4227ec681f3Smrgu_uintN_max(unsigned bit_size)
4237ec681f3Smrg{
4247ec681f3Smrg   assert(bit_size <= 64 && bit_size > 0);
4257ec681f3Smrg   return UINT64_MAX >> (64 - bit_size);
4267ec681f3Smrg}
4277ec681f3Smrg
4287ec681f3Smrg/* TODO: In future we should try to move this to u_debug.h once header
4297ec681f3Smrg * dependencies are reorganised to allow this.
4307ec681f3Smrg */
4317ec681f3Smrgenum pipe_debug_type
4327ec681f3Smrg{
4337ec681f3Smrg   PIPE_DEBUG_TYPE_OUT_OF_MEMORY = 1,
4347ec681f3Smrg   PIPE_DEBUG_TYPE_ERROR,
4357ec681f3Smrg   PIPE_DEBUG_TYPE_SHADER_INFO,
4367ec681f3Smrg   PIPE_DEBUG_TYPE_PERF_INFO,
4377ec681f3Smrg   PIPE_DEBUG_TYPE_INFO,
4387ec681f3Smrg   PIPE_DEBUG_TYPE_FALLBACK,
4397ec681f3Smrg   PIPE_DEBUG_TYPE_CONFORMANCE,
4407ec681f3Smrg};
4417ec681f3Smrg
4427ec681f3Smrg#if !defined(alignof) && !defined(__cplusplus)
4437ec681f3Smrg#if __STDC_VERSION__ >= 201112L
4447ec681f3Smrg#define alignof(t) _Alignof(t)
4457ec681f3Smrg#elif defined(_MSC_VER)
4467ec681f3Smrg#define alignof(t) __alignof(t)
4477ec681f3Smrg#else
4487ec681f3Smrg#define alignof(t) __alignof__(t)
4497ec681f3Smrg#endif
4507ec681f3Smrg#endif
4517ec681f3Smrg
4527ec681f3Smrg/* Macros for static type-safety checking.
4537ec681f3Smrg *
4547ec681f3Smrg * https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
4557ec681f3Smrg */
4567ec681f3Smrg
4577ec681f3Smrg#if __has_attribute(capability)
4587ec681f3Smrgtypedef int __attribute__((capability("mutex"))) lock_cap_t;
4597ec681f3Smrg
4607ec681f3Smrg#define guarded_by(l) __attribute__((guarded_by(l)))
4617ec681f3Smrg#define acquire_cap(l) __attribute((acquire_capability(l), no_thread_safety_analysis))
4627ec681f3Smrg#define release_cap(l) __attribute((release_capability(l), no_thread_safety_analysis))
4637ec681f3Smrg#define assert_cap(l) __attribute((assert_capability(l), no_thread_safety_analysis))
4647ec681f3Smrg#define requires_cap(l) __attribute((requires_capability(l)))
4657ec681f3Smrg#define disable_thread_safety_analysis __attribute((no_thread_safety_analysis))
4667ec681f3Smrg
4677ec681f3Smrg#else
4687ec681f3Smrg
4697ec681f3Smrgtypedef int lock_cap_t;
4707ec681f3Smrg
4717ec681f3Smrg#define guarded_by(l)
4727ec681f3Smrg#define acquire_cap(l)
4737ec681f3Smrg#define release_cap(l)
4747ec681f3Smrg#define assert_cap(l)
4757ec681f3Smrg#define requires_cap(l)
4767ec681f3Smrg#define disable_thread_safety_analysis
4777ec681f3Smrg
4787ec681f3Smrg#endif
4797ec681f3Smrg
4807ec681f3Smrg/* TODO: this could be different on non-x86 architectures. */
481fc67b037Smartin#ifndef CACHE_LINE_SIZE
4827ec681f3Smrg#define CACHE_LINE_SIZE 64
483fc67b037Smartin#endif
4847ec681f3Smrg
485af69d88dSmrg#endif /* UTIL_MACROS_H */
486