180f2f6436bafaee9544c52577348377a1b62386
[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 <math.h>
28
29 /* Compute the size of an array */
30 #ifndef ARRAY_SIZE
31 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
32 #endif
33
34 /* For compatibility with Clang's __has_builtin() */
35 #ifndef __has_builtin
36 # define __has_builtin(x) 0
37 #endif
38
39 /**
40 * __builtin_expect macros
41 */
42 #if !defined(HAVE___BUILTIN_EXPECT)
43 # define __builtin_expect(x, y) (x)
44 #endif
45
46 #ifndef likely
47 # ifdef HAVE___BUILTIN_EXPECT
48 # define likely(x) __builtin_expect(!!(x), 1)
49 # define unlikely(x) __builtin_expect(!!(x), 0)
50 # else
51 # define likely(x) (x)
52 # define unlikely(x) (x)
53 # endif
54 #endif
55
56
57 /**
58 * Static (compile-time) assertion.
59 * Basically, use COND to dimension an array. If COND is false/zero the
60 * array size will be -1 and we'll get a compilation error.
61 */
62 #define STATIC_ASSERT(COND) \
63 do { \
64 (void) sizeof(char [1 - 2*!(COND)]); \
65 } while (0)
66
67
68 /**
69 * Unreachable macro. Useful for suppressing "control reaches end of non-void
70 * function" warnings.
71 */
72 #ifdef HAVE___BUILTIN_UNREACHABLE
73 #define unreachable(str) \
74 do { \
75 assert(!str); \
76 __builtin_unreachable(); \
77 } while (0)
78 #elif _MSC_VER >= 1200
79 #define unreachable(str) \
80 do { \
81 assert(!str); \
82 __assume(0); \
83 } while (0)
84 #endif
85
86 #ifndef unreachable
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 _MSC_VER >= 1200
105 #define assume(expr) __assume(expr)
106 #else
107 #define assume(expr) assert(expr)
108 #endif
109
110 #ifdef HAVE_FUNC_ATTRIBUTE_FLATTEN
111 #define FLATTEN __attribute__((__flatten__))
112 #else
113 #define FLATTEN
114 #endif
115
116 #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
117 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
118 #else
119 #define PRINTFLIKE(f, a)
120 #endif
121
122 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
123 #define MALLOCLIKE __attribute__((__malloc__))
124 #else
125 #define MALLOCLIKE
126 #endif
127
128 /* Used to optionally mark structures with misaligned elements or size as
129 * packed, to trade off performance for space.
130 */
131 #ifdef HAVE_FUNC_ATTRIBUTE_PACKED
132 #define PACKED __attribute__((__packed__))
133 #else
134 #define PACKED
135 #endif
136
137 #ifdef __cplusplus
138 /**
139 * Macro function that evaluates to true if T is a trivially
140 * destructible type -- that is, if its (non-virtual) destructor
141 * performs no action and all member variables and base classes are
142 * trivially destructible themselves.
143 */
144 # if defined(__GNUC__)
145 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
146 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
147 # endif
148 # elif (defined(__clang__) && defined(__has_feature))
149 # if __has_feature(has_trivial_destructor)
150 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
151 # endif
152 # endif
153 # ifndef HAS_TRIVIAL_DESTRUCTOR
154 /* It's always safe (if inefficient) to assume that a
155 * destructor is non-trivial.
156 */
157 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
158 # endif
159 #endif
160
161 #if defined(fpclassify)
162 /* ISO C99 says that fpclassify is a macro. Assume that any implementation
163 * of fpclassify, whether it's in a C99 compiler or not, will be a macro.
164 */
165 #elif defined(_MSC_VER)
166 /* Not required on VS2013 and above. */
167 /* Oddly, the fpclassify() function doesn't exist in such a form
168 * on MSVC. This is an implementation using slightly different
169 * lower-level Windows functions.
170 */
171 #include <float.h>
172
173 static inline enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL}
174 fpclassify(double x)
175 {
176 switch(_fpclass(x)) {
177 case _FPCLASS_SNAN: /* signaling NaN */
178 case _FPCLASS_QNAN: /* quiet NaN */
179 return FP_NAN;
180 case _FPCLASS_NINF: /* negative infinity */
181 case _FPCLASS_PINF: /* positive infinity */
182 return FP_INFINITE;
183 case _FPCLASS_NN: /* negative normal */
184 case _FPCLASS_PN: /* positive normal */
185 return FP_NORMAL;
186 case _FPCLASS_ND: /* negative denormalized */
187 case _FPCLASS_PD: /* positive denormalized */
188 return FP_SUBNORMAL;
189 case _FPCLASS_NZ: /* negative zero */
190 case _FPCLASS_PZ: /* positive zero */
191 return FP_ZERO;
192 default:
193 /* Should never get here; but if we do, this will guarantee
194 * that the pattern is not treated like a number.
195 */
196 return FP_NAN;
197 }
198 }
199
200 #else
201
202 enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL}
203 fpclassify(double x)
204 {
205 /* XXX do something better someday */
206 return FP_NORMAL;
207 }
208
209 #endif
210
211 #endif /* UTIL_MACROS_H */