gallium: Avoid redefinition of likely/unlikely macros on non gcc compilers.
[mesa.git] / src / gallium / include / pipe / p_compiler.h
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef P_COMPILER_H
29 #define P_COMPILER_H
30
31
32 #include "p_config.h"
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stddef.h>
37 #include <stdarg.h>
38 #include <limits.h>
39
40
41 #if defined(_WIN32) && !defined(__WIN32__)
42 #define __WIN32__
43 #endif
44
45 #if defined(_MSC_VER)
46
47 /* Avoid 'expression is always true' warning */
48 #pragma warning(disable: 4296)
49
50 #endif /* _MSC_VER */
51
52
53 /*
54 * Alternative stdint.h and stdbool.h headers are supplied in include/c99 for
55 * systems that lack it.
56 */
57 #ifndef __STDC_LIMIT_MACROS
58 #define __STDC_LIMIT_MACROS 1
59 #endif
60 #include <stdint.h>
61 #include <stdbool.h>
62
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68
69 #if !defined(__HAIKU__) && !defined(__USE_MISC)
70 typedef unsigned int uint;
71 typedef unsigned short ushort;
72 #endif
73 typedef unsigned char ubyte;
74
75 typedef unsigned char boolean;
76 #ifndef TRUE
77 #define TRUE true
78 #endif
79 #ifndef FALSE
80 #define FALSE false
81 #endif
82
83 #ifndef va_copy
84 #ifdef __va_copy
85 #define va_copy(dest, src) __va_copy((dest), (src))
86 #else
87 #define va_copy(dest, src) (dest) = (src)
88 #endif
89 #endif
90
91 /* Function inlining */
92 #ifndef INLINE
93 # ifdef __cplusplus
94 # define INLINE inline
95 # elif defined(__GNUC__)
96 # define INLINE __inline__
97 # elif defined(_MSC_VER)
98 # define INLINE __inline
99 # elif defined(__ICL)
100 # define INLINE __inline
101 # elif defined(__INTEL_COMPILER)
102 # define INLINE inline
103 # elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
104 # define INLINE __inline
105 # elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
106 # define INLINE inline
107 # elif (__STDC_VERSION__ >= 199901L) /* C99 */
108 # define INLINE inline
109 # else
110 # define INLINE
111 # endif
112 #endif
113
114 /* Forced function inlining */
115 #ifndef ALWAYS_INLINE
116 # ifdef __GNUC__
117 # define ALWAYS_INLINE inline __attribute__((always_inline))
118 # elif defined(_MSC_VER)
119 # define ALWAYS_INLINE __forceinline
120 # else
121 # define ALWAYS_INLINE INLINE
122 # endif
123 #endif
124
125 /*
126 * Define the C99 restrict keyword.
127 *
128 * See also:
129 * - http://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html
130 */
131 #ifndef restrict
132 # if (__STDC_VERSION__ >= 199901L)
133 /* C99 */
134 # elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
135 /* C99 */
136 # elif defined(__GNUC__)
137 # define restrict __restrict__
138 # elif defined(_MSC_VER)
139 # define restrict __restrict
140 # else
141 # define restrict /* */
142 # endif
143 #endif
144
145
146 /* Function visibility */
147 #ifndef PUBLIC
148 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
149 # define PUBLIC __attribute__((visibility("default")))
150 # elif defined(_MSC_VER)
151 # define PUBLIC __declspec(dllexport)
152 # else
153 # define PUBLIC
154 # endif
155 #endif
156
157
158 /* The __FUNCTION__ gcc variable is generally only used for debugging.
159 * If we're not using gcc, define __FUNCTION__ as a cpp symbol here.
160 */
161 #ifndef __FUNCTION__
162 # if !defined(__GNUC__)
163 # if (__STDC_VERSION__ >= 199901L) /* C99 */ || \
164 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
165 # define __FUNCTION__ __func__
166 # else
167 # define __FUNCTION__ "<unknown>"
168 # endif
169 # endif
170 # if defined(_MSC_VER) && _MSC_VER < 1300
171 # define __FUNCTION__ "<unknown>"
172 # endif
173 #endif
174 #ifndef __func__
175 # if (__STDC_VERSION__ >= 199901L) || \
176 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
177 /* __func__ is part of C99 */
178 # elif defined(_MSC_VER)
179 # if _MSC_VER >= 1300
180 # define __func__ __FUNCTION__
181 # else
182 # define __func__ "<unknown>"
183 # endif
184 # endif
185 #endif
186
187
188
189 /* This should match linux gcc cdecl semantics everywhere, so that we
190 * just codegen one calling convention on all platforms.
191 */
192 #ifdef _MSC_VER
193 #define PIPE_CDECL __cdecl
194 #else
195 #define PIPE_CDECL
196 #endif
197
198
199
200 #if defined(__GNUC__)
201 #define PIPE_DEPRECATED __attribute__((__deprecated__))
202 #else
203 #define PIPE_DEPRECATED
204 #endif
205
206
207
208 /* Macros for data alignment. */
209 #if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
210
211 /* See http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Type-Attributes.html */
212 #define PIPE_ALIGN_TYPE(_alignment, _type) _type __attribute__((aligned(_alignment)))
213
214 /* See http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Variable-Attributes.html */
215 #define PIPE_ALIGN_VAR(_alignment) __attribute__((aligned(_alignment)))
216
217 #if (__GNUC__ > 4 || (__GNUC__ == 4 &&__GNUC_MINOR__>1)) && !defined(PIPE_ARCH_X86_64)
218 #define PIPE_ALIGN_STACK __attribute__((force_align_arg_pointer))
219 #else
220 #define PIPE_ALIGN_STACK
221 #endif
222
223 #elif defined(_MSC_VER)
224
225 /* See http://msdn.microsoft.com/en-us/library/83ythb65.aspx */
226 #define PIPE_ALIGN_TYPE(_alignment, _type) __declspec(align(_alignment)) _type
227 #define PIPE_ALIGN_VAR(_alignment) __declspec(align(_alignment))
228
229 #define PIPE_ALIGN_STACK
230
231 #elif defined(SWIG)
232
233 #define PIPE_ALIGN_TYPE(_alignment, _type) _type
234 #define PIPE_ALIGN_VAR(_alignment)
235
236 #define PIPE_ALIGN_STACK
237
238 #else
239
240 #error "Unsupported compiler"
241
242 #endif
243
244
245 #if defined(__GNUC__)
246
247 #define PIPE_READ_WRITE_BARRIER() __asm__("":::"memory")
248
249 #elif defined(_MSC_VER)
250
251 void _ReadWriteBarrier(void);
252 #pragma intrinsic(_ReadWriteBarrier)
253 #define PIPE_READ_WRITE_BARRIER() _ReadWriteBarrier()
254
255 #else
256
257 #warning "Unsupported compiler"
258 #define PIPE_READ_WRITE_BARRIER() /* */
259
260 #endif
261
262
263 /* You should use these macros to mark if blocks where the if condition
264 * is either likely to be true, or unlikely to be true.
265 *
266 * This will inform human readers of this fact, and will also inform
267 * the compiler, who will in turn inform the CPU.
268 *
269 * CPUs often start executing code inside the if or the else blocks
270 * without knowing whether the condition is true or not, and will have
271 * to throw the work away if they find out later they executed the
272 * wrong part of the if.
273 *
274 * If these macros are used, the CPU is more likely to correctly predict
275 * the right path, and will avoid speculatively executing the wrong branch,
276 * thus not throwing away work, resulting in better performance.
277 *
278 * In light of this, it is also a good idea to mark as "likely" a path
279 * which is not necessarily always more likely, but that will benefit much
280 * more from performance improvements since it is already much faster than
281 * the other path, or viceversa with "unlikely".
282 *
283 * Example usage:
284 * if(unlikely(do_we_need_a_software_fallback()))
285 * do_software_fallback();
286 * else
287 * render_with_gpu();
288 *
289 * The macros follow the Linux kernel convention, and more examples can
290 * be found there.
291 *
292 * Note that profile guided optimization can offer better results, but
293 * needs an appropriate coverage suite and does not inform human readers.
294 */
295 #ifndef likely
296 # if defined(__GNUC__)
297 # define likely(x) __builtin_expect(!!(x), 1)
298 # define unlikely(x) __builtin_expect(!!(x), 0)
299 # else
300 # define likely(x) (x)
301 # define unlikely(x) (x)
302 # endif
303 #endif
304
305
306 #if defined(__cplusplus)
307 }
308 #endif
309
310
311 #endif /* P_COMPILER_H */