util: Rename PURE to ATTRIBUTE_PURE.
[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 /* Compute the size of an array */
28 #ifndef ARRAY_SIZE
29 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
30 #endif
31
32 /* For compatibility with Clang's __has_builtin() */
33 #ifndef __has_builtin
34 # define __has_builtin(x) 0
35 #endif
36
37 /**
38 * __builtin_expect macros
39 */
40 #if !defined(HAVE___BUILTIN_EXPECT)
41 # define __builtin_expect(x, y) (x)
42 #endif
43
44 #ifndef likely
45 # ifdef HAVE___BUILTIN_EXPECT
46 # define likely(x) __builtin_expect(!!(x), 1)
47 # define unlikely(x) __builtin_expect(!!(x), 0)
48 # else
49 # define likely(x) (x)
50 # define unlikely(x) (x)
51 # endif
52 #endif
53
54
55 /**
56 * Static (compile-time) assertion.
57 * Basically, use COND to dimension an array. If COND is false/zero the
58 * array size will be -1 and we'll get a compilation error.
59 */
60 #define STATIC_ASSERT(COND) \
61 do { \
62 (void) sizeof(char [1 - 2*!(COND)]); \
63 } while (0)
64
65
66 /**
67 * Unreachable macro. Useful for suppressing "control reaches end of non-void
68 * function" warnings.
69 */
70 #ifdef HAVE___BUILTIN_UNREACHABLE
71 #define unreachable(str) \
72 do { \
73 assert(!str); \
74 __builtin_unreachable(); \
75 } while (0)
76 #elif defined (_MSC_VER)
77 #define unreachable(str) \
78 do { \
79 assert(!str); \
80 __assume(0); \
81 } while (0)
82 #else
83 #define unreachable(str) assert(!str)
84 #endif
85
86 /**
87 * Assume macro. Useful for expressing our assumptions to the compiler,
88 * typically for purposes of silencing warnings.
89 */
90 #if __has_builtin(__builtin_assume)
91 #define assume(expr) \
92 do { \
93 assert(expr); \
94 __builtin_assume(expr); \
95 } while (0)
96 #elif defined HAVE___BUILTIN_UNREACHABLE
97 #define assume(expr) ((expr) ? ((void) 0) \
98 : (assert(!"assumption failed"), \
99 __builtin_unreachable()))
100 #elif defined (_MSC_VER)
101 #define assume(expr) __assume(expr)
102 #else
103 #define assume(expr) assert(expr)
104 #endif
105
106 /* Attribute const is used for functions that have no effects other than their
107 * return value, and only rely on the argument values to compute the return
108 * value. As a result, calls to it can be CSEed. Note that using memory
109 * pointed to by the arguments is not allowed for const functions.
110 */
111 #ifdef HAVE_FUNC_ATTRIBUTE_CONST
112 #define ATTRIBUTE_CONST __attribute__((__const__))
113 #else
114 #define ATTRIBUTE_CONST
115 #endif
116
117 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
118 #define FLATTEN __attribute__((__flatten__))
119 #else
120 #define FLATTEN
121 #endif
122
123 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
124 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
125 #else
126 #define PRINTFLIKE(f, a)
127 #endif
128
129 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
130 #define MALLOCLIKE __attribute__((__malloc__))
131 #else
132 #define MALLOCLIKE
133 #endif
134
135 /* Used to optionally mark structures with misaligned elements or size as
136 * packed, to trade off performance for space.
137 */
138 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
139 #define PACKED __attribute__((__packed__))
140 #else
141 #define PACKED
142 #endif
143
144 /* Attribute pure is used for functions that have no effects other than their
145 * return value. As a result, calls to it can be dead code eliminated.
146 */
147 #ifdef HAVE_FUNC_ATTRIBUTE_PURE
148 #define ATTRIBUTE_PURE __attribute__((__pure__))
149 #else
150 #define ATTRIBUTE_PURE
151 #endif
152
153 #ifdef __cplusplus
154 /**
155 * Macro function that evaluates to true if T is a trivially
156 * destructible type -- that is, if its (non-virtual) destructor
157 * performs no action and all member variables and base classes are
158 * trivially destructible themselves.
159 */
160 # if defined(__GNUC__)
161 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
162 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
163 # endif
164 # elif (defined(__clang__) && defined(__has_feature))
165 # if __has_feature(has_trivial_destructor)
166 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
167 # endif
168 # endif
169 # ifndef HAS_TRIVIAL_DESTRUCTOR
170 /* It's always safe (if inefficient) to assume that a
171 * destructor is non-trivial.
172 */
173 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
174 # endif
175 #endif
176
177 /**
178 * PUBLIC/USED macros
179 *
180 * If we build the library with gcc's -fvisibility=hidden flag, we'll
181 * use the PUBLIC macro to mark functions that are to be exported.
182 *
183 * We also need to define a USED attribute, so the optimizer doesn't
184 * inline a static function that we later use in an alias. - ajax
185 */
186 #ifndef PUBLIC
187 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
188 # define PUBLIC __attribute__((visibility("default")))
189 # define USED __attribute__((used))
190 # elif defined(_MSC_VER)
191 # define PUBLIC __declspec(dllexport)
192 # define USED
193 # else
194 # define PUBLIC
195 # define USED
196 # endif
197 #endif
198
199 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
200 #define UNUSED __attribute__((unused))
201 #else
202 #define UNUSED
203 #endif
204
205 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
206 #define MUST_CHECK __attribute__((warn_unused_result))
207 #else
208 #define MUST_CHECK
209 #endif
210
211 /** Compute ceiling of integer quotient of A divided by B. */
212 #define DIV_ROUND_UP( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
213
214 #endif /* UTIL_MACROS_H */