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