util: Make CLAMP turn NaN into MIN.
[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 #include <assert.h>
28
29 #include "c99_compat.h"
30
31 /* Compute the size of an array */
32 #ifndef ARRAY_SIZE
33 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
34 #endif
35
36 /* For compatibility with Clang's __has_builtin() */
37 #ifndef __has_builtin
38 # define __has_builtin(x) 0
39 #endif
40
41 /**
42 * __builtin_expect macros
43 */
44 #if !defined(HAVE___BUILTIN_EXPECT)
45 # define __builtin_expect(x, y) (x)
46 #endif
47
48 #ifndef likely
49 # ifdef HAVE___BUILTIN_EXPECT
50 # define likely(x) __builtin_expect(!!(x), 1)
51 # define unlikely(x) __builtin_expect(!!(x), 0)
52 # else
53 # define likely(x) (x)
54 # define unlikely(x) (x)
55 # endif
56 #endif
57
58
59 /**
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.
63 */
64 #define STATIC_ASSERT(COND) \
65 do { \
66 (void) sizeof(char [1 - 2*!(COND)]); \
67 } while (0)
68
69
70 /**
71 * Unreachable macro. Useful for suppressing "control reaches end of non-void
72 * function" warnings.
73 */
74 #ifdef HAVE___BUILTIN_UNREACHABLE
75 #define unreachable(str) \
76 do { \
77 assert(!str); \
78 __builtin_unreachable(); \
79 } while (0)
80 #elif defined (_MSC_VER)
81 #define unreachable(str) \
82 do { \
83 assert(!str); \
84 __assume(0); \
85 } while (0)
86 #else
87 #define unreachable(str) assert(!str)
88 #endif
89
90 /**
91 * Assume macro. Useful for expressing our assumptions to the compiler,
92 * typically for purposes of silencing warnings.
93 */
94 #if __has_builtin(__builtin_assume)
95 #define assume(expr) \
96 do { \
97 assert(expr); \
98 __builtin_assume(expr); \
99 } while (0)
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)
106 #else
107 #define assume(expr) assert(expr)
108 #endif
109
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.
114 */
115 #ifdef HAVE_FUNC_ATTRIBUTE_CONST
116 #define ATTRIBUTE_CONST __attribute__((__const__))
117 #else
118 #define ATTRIBUTE_CONST
119 #endif
120
121 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
122 #define FLATTEN __attribute__((__flatten__))
123 #else
124 #define FLATTEN
125 #endif
126
127 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
128 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
129 #else
130 #define PRINTFLIKE(f, a)
131 #endif
132
133 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
134 #define MALLOCLIKE __attribute__((__malloc__))
135 #else
136 #define MALLOCLIKE
137 #endif
138
139 /* Forced function inlining */
140 #ifndef ALWAYS_INLINE
141 # if defined(__GNUC__) || defined(__clang__)
142 # define ALWAYS_INLINE inline __attribute__((always_inline))
143 # elif defined(_MSC_VER)
144 # define ALWAYS_INLINE __forceinline
145 # else
146 # define ALWAYS_INLINE inline
147 # endif
148 #endif
149
150 /* Used to optionally mark structures with misaligned elements or size as
151 * packed, to trade off performance for space.
152 */
153 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
154 #define PACKED __attribute__((__packed__))
155 #else
156 #define PACKED
157 #endif
158
159 /* Attribute pure is used for functions that have no effects other than their
160 * return value. As a result, calls to it can be dead code eliminated.
161 */
162 #ifdef HAVE_FUNC_ATTRIBUTE_PURE
163 #define ATTRIBUTE_PURE __attribute__((__pure__))
164 #else
165 #define ATTRIBUTE_PURE
166 #endif
167
168 #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
169 #define ATTRIBUTE_RETURNS_NONNULL __attribute__((__returns_nonnull__))
170 #else
171 #define ATTRIBUTE_RETURNS_NONNULL
172 #endif
173
174 #ifdef __cplusplus
175 /**
176 * Macro function that evaluates to true if T is a trivially
177 * destructible type -- that is, if its (non-virtual) destructor
178 * performs no action and all member variables and base classes are
179 * trivially destructible themselves.
180 */
181 # if (defined(__clang__) && defined(__has_feature))
182 # if __has_feature(has_trivial_destructor)
183 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
184 # endif
185 # elif defined(__GNUC__)
186 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
187 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
188 # endif
189 # elif defined(_MSC_VER) && !defined(__INTEL_COMPILER)
190 # if _MSC_VER >= 1800
191 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
192 # endif
193 # endif
194 # ifndef HAS_TRIVIAL_DESTRUCTOR
195 /* It's always safe (if inefficient) to assume that a
196 * destructor is non-trivial.
197 */
198 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
199 # endif
200 #endif
201
202 /**
203 * PUBLIC/USED macros
204 *
205 * If we build the library with gcc's -fvisibility=hidden flag, we'll
206 * use the PUBLIC macro to mark functions that are to be exported.
207 *
208 * We also need to define a USED attribute, so the optimizer doesn't
209 * inline a static function that we later use in an alias. - ajax
210 */
211 #ifndef PUBLIC
212 # if defined(__GNUC__)
213 # define PUBLIC __attribute__((visibility("default")))
214 # define USED __attribute__((used))
215 # elif defined(_MSC_VER)
216 # define PUBLIC __declspec(dllexport)
217 # define USED
218 # else
219 # define PUBLIC
220 # define USED
221 # endif
222 #endif
223
224 #ifdef HAVE_FUNC_ATTRIBUTE_UNUSED
225 #define UNUSED __attribute__((unused))
226 #else
227 #define UNUSED
228 #endif
229
230 #define MAYBE_UNUSED UNUSED
231
232 #ifdef HAVE_FUNC_ATTRIBUTE_WARN_UNUSED_RESULT
233 #define MUST_CHECK __attribute__((warn_unused_result))
234 #else
235 #define MUST_CHECK
236 #endif
237
238 #if defined(__GNUC__)
239 #define ATTRIBUTE_NOINLINE __attribute__((noinline))
240 #else
241 #define ATTRIBUTE_NOINLINE
242 #endif
243
244 /** Compute ceiling of integer quotient of A divided by B. */
245 #define DIV_ROUND_UP( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
246
247 /** Clamp X to [MIN,MAX]. Turn NaN into MIN, arbitrarily. */
248 #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
249
250 /** Minimum of two values: */
251 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
252
253 /** Maximum of two values: */
254 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
255
256 /** Minimum and maximum of three values: */
257 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
258 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
259
260 #endif /* UTIL_MACROS_H */