05de9ff7cd022ec333f14125407f42d1f51927e6
[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 #if !defined(PIPE_OS_ANDROID)
71 typedef unsigned int uint;
72 #endif
73 typedef unsigned short ushort;
74 #endif
75 typedef unsigned char ubyte;
76
77 typedef unsigned char boolean;
78 #ifndef TRUE
79 #define TRUE true
80 #endif
81 #ifndef FALSE
82 #define FALSE false
83 #endif
84
85 #ifndef va_copy
86 #ifdef __va_copy
87 #define va_copy(dest, src) __va_copy((dest), (src))
88 #else
89 #define va_copy(dest, src) (dest) = (src)
90 #endif
91 #endif
92
93 /* Function inlining */
94 #ifndef INLINE
95 # ifdef __cplusplus
96 # define INLINE inline
97 # elif defined(__GNUC__)
98 # define INLINE __inline__
99 # elif defined(_MSC_VER)
100 # define INLINE __inline
101 # elif defined(__ICL)
102 # define INLINE __inline
103 # elif defined(__INTEL_COMPILER)
104 # define INLINE inline
105 # elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
106 # define INLINE __inline
107 # elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
108 # define INLINE inline
109 # elif (__STDC_VERSION__ >= 199901L) /* C99 */
110 # define INLINE inline
111 # else
112 # define INLINE
113 # endif
114 #endif
115
116 /* Forced function inlining */
117 #ifndef ALWAYS_INLINE
118 # ifdef __GNUC__
119 # define ALWAYS_INLINE inline __attribute__((always_inline))
120 # elif defined(_MSC_VER)
121 # define ALWAYS_INLINE __forceinline
122 # else
123 # define ALWAYS_INLINE INLINE
124 # endif
125 #endif
126
127 /*
128 * Define the C99 restrict keyword.
129 *
130 * See also:
131 * - http://cellperformance.beyond3d.com/articles/2006/05/demystifying-the-restrict-keyword.html
132 */
133 #ifndef restrict
134 # if (__STDC_VERSION__ >= 199901L)
135 /* C99 */
136 # elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
137 /* C99 */
138 # elif defined(__GNUC__)
139 # define restrict __restrict__
140 # elif defined(_MSC_VER)
141 # define restrict __restrict
142 # else
143 # define restrict /* */
144 # endif
145 #endif
146
147
148 /* Function visibility */
149 #ifndef PUBLIC
150 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
151 # define PUBLIC __attribute__((visibility("default")))
152 # elif defined(_MSC_VER)
153 # define PUBLIC __declspec(dllexport)
154 # else
155 # define PUBLIC
156 # endif
157 #endif
158
159
160 /* The __FUNCTION__ gcc variable is generally only used for debugging.
161 * If we're not using gcc, define __FUNCTION__ as a cpp symbol here.
162 */
163 #ifndef __FUNCTION__
164 # if !defined(__GNUC__)
165 # if (__STDC_VERSION__ >= 199901L) /* C99 */ || \
166 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
167 # define __FUNCTION__ __func__
168 # else
169 # define __FUNCTION__ "<unknown>"
170 # endif
171 # endif
172 # if defined(_MSC_VER) && _MSC_VER < 1300
173 # define __FUNCTION__ "<unknown>"
174 # endif
175 #endif
176 #ifndef __func__
177 # if (__STDC_VERSION__ >= 199901L) || \
178 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
179 /* __func__ is part of C99 */
180 # elif defined(_MSC_VER)
181 # if _MSC_VER >= 1300
182 # define __func__ __FUNCTION__
183 # else
184 # define __func__ "<unknown>"
185 # endif
186 # endif
187 #endif
188
189
190
191 /* This should match linux gcc cdecl semantics everywhere, so that we
192 * just codegen one calling convention on all platforms.
193 */
194 #ifdef _MSC_VER
195 #define PIPE_CDECL __cdecl
196 #else
197 #define PIPE_CDECL
198 #endif
199
200
201
202 #if defined(__GNUC__)
203 #define PIPE_DEPRECATED __attribute__((__deprecated__))
204 #else
205 #define PIPE_DEPRECATED
206 #endif
207
208
209
210 /* Macros for data alignment. */
211 #if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
212
213 /* See http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Type-Attributes.html */
214 #define PIPE_ALIGN_TYPE(_alignment, _type) _type __attribute__((aligned(_alignment)))
215
216 /* See http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Variable-Attributes.html */
217 #define PIPE_ALIGN_VAR(_alignment) __attribute__((aligned(_alignment)))
218
219 #if (__GNUC__ > 4 || (__GNUC__ == 4 &&__GNUC_MINOR__>1)) && !defined(PIPE_ARCH_X86_64)
220 #define PIPE_ALIGN_STACK __attribute__((force_align_arg_pointer))
221 #else
222 #define PIPE_ALIGN_STACK
223 #endif
224
225 #elif defined(_MSC_VER)
226
227 /* See http://msdn.microsoft.com/en-us/library/83ythb65.aspx */
228 #define PIPE_ALIGN_TYPE(_alignment, _type) __declspec(align(_alignment)) _type
229 #define PIPE_ALIGN_VAR(_alignment) __declspec(align(_alignment))
230
231 #define PIPE_ALIGN_STACK
232
233 #elif defined(SWIG)
234
235 #define PIPE_ALIGN_TYPE(_alignment, _type) _type
236 #define PIPE_ALIGN_VAR(_alignment)
237
238 #define PIPE_ALIGN_STACK
239
240 #else
241
242 #error "Unsupported compiler"
243
244 #endif
245
246
247 #if defined(__GNUC__)
248
249 #define PIPE_READ_WRITE_BARRIER() __asm__("":::"memory")
250
251 #elif defined(_MSC_VER)
252
253 void _ReadWriteBarrier(void);
254 #pragma intrinsic(_ReadWriteBarrier)
255 #define PIPE_READ_WRITE_BARRIER() _ReadWriteBarrier()
256
257 #else
258
259 #warning "Unsupported compiler"
260 #define PIPE_READ_WRITE_BARRIER() /* */
261
262 #endif
263
264
265 /* You should use these macros to mark if blocks where the if condition
266 * is either likely to be true, or unlikely to be true.
267 *
268 * This will inform human readers of this fact, and will also inform
269 * the compiler, who will in turn inform the CPU.
270 *
271 * CPUs often start executing code inside the if or the else blocks
272 * without knowing whether the condition is true or not, and will have
273 * to throw the work away if they find out later they executed the
274 * wrong part of the if.
275 *
276 * If these macros are used, the CPU is more likely to correctly predict
277 * the right path, and will avoid speculatively executing the wrong branch,
278 * thus not throwing away work, resulting in better performance.
279 *
280 * In light of this, it is also a good idea to mark as "likely" a path
281 * which is not necessarily always more likely, but that will benefit much
282 * more from performance improvements since it is already much faster than
283 * the other path, or viceversa with "unlikely".
284 *
285 * Example usage:
286 * if(unlikely(do_we_need_a_software_fallback()))
287 * do_software_fallback();
288 * else
289 * render_with_gpu();
290 *
291 * The macros follow the Linux kernel convention, and more examples can
292 * be found there.
293 *
294 * Note that profile guided optimization can offer better results, but
295 * needs an appropriate coverage suite and does not inform human readers.
296 */
297 #ifndef likely
298 # if defined(__GNUC__)
299 # define likely(x) __builtin_expect(!!(x), 1)
300 # define unlikely(x) __builtin_expect(!!(x), 0)
301 # else
302 # define likely(x) (x)
303 # define unlikely(x) (x)
304 # endif
305 #endif
306
307
308 #if defined(__cplusplus)
309 }
310 #endif
311
312
313 #endif /* P_COMPILER_H */