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