mesa: Remove CHECK macro.
[mesa.git] / src / mesa / main / compiler.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file compiler.h
29 * Compiler-related stuff.
30 */
31
32
33 #ifndef COMPILER_H
34 #define COMPILER_H
35
36
37 #include <assert.h>
38 #include <ctype.h>
39 #include <math.h>
40 #include <limits.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <float.h>
45 #include <stdarg.h>
46
47 #include "util/macros.h"
48
49 #include "c99_compat.h" /* inline, __func__, etc. */
50
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56
57 /**
58 * Get standard integer types
59 */
60 #include <stdint.h>
61
62
63 /**
64 * Sun compilers define __i386 instead of the gcc-style __i386__
65 */
66 #ifdef __SUNPRO_C
67 # if !defined(__i386__) && defined(__i386)
68 # define __i386__
69 # elif !defined(__amd64__) && defined(__amd64)
70 # define __amd64__
71 # elif !defined(__sparc__) && defined(__sparc)
72 # define __sparc__
73 # endif
74 # if !defined(__volatile)
75 # define __volatile volatile
76 # endif
77 #endif
78
79
80 /**
81 * finite macro.
82 */
83 #if defined(_MSC_VER)
84 # define finite _finite
85 #endif
86
87
88 /**
89 * Disable assorted warnings
90 */
91 #if defined(_WIN32) && !defined(__CYGWIN__)
92 # if !defined(__GNUC__) /* mingw environment */
93 # pragma warning( disable : 4068 ) /* unknown pragma */
94 # pragma warning( disable : 4710 ) /* function 'foo' not inlined */
95 # pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
96 # pragma warning( disable : 4127 ) /* conditional expression is constant */
97 # if defined(MESA_MINWARN)
98 # pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
99 # pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
100 # pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
101 # pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
102 # pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
103 # endif
104 # endif
105 #endif
106
107
108
109 /* XXX: Use standard `inline` keyword instead */
110 #ifndef INLINE
111 # define INLINE inline
112 #endif
113
114
115 /**
116 * PUBLIC/USED macros
117 *
118 * If we build the library with gcc's -fvisibility=hidden flag, we'll
119 * use the PUBLIC macro to mark functions that are to be exported.
120 *
121 * We also need to define a USED attribute, so the optimizer doesn't
122 * inline a static function that we later use in an alias. - ajax
123 */
124 #ifndef PUBLIC
125 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
126 # define PUBLIC __attribute__((visibility("default")))
127 # define USED __attribute__((used))
128 # else
129 # define PUBLIC
130 # define USED
131 # endif
132 #endif
133
134
135 /* XXX: Use standard `__func__` instead */
136 #ifndef __FUNCTION__
137 # define __FUNCTION__ __func__
138 #endif
139
140 /**
141 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
142 * Do not use these unless absolutely necessary!
143 * Try to use a runtime test instead.
144 * For now, only used by some DRI hardware drivers for color/texel packing.
145 */
146 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
147 #if defined(__linux__)
148 #include <byteswap.h>
149 #define CPU_TO_LE32( x ) bswap_32( x )
150 #elif defined(__APPLE__)
151 #include <CoreFoundation/CFByteOrder.h>
152 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
153 #elif (defined(_AIX))
154 static inline GLuint CPU_TO_LE32(GLuint x)
155 {
156 return (((x & 0x000000ff) << 24) |
157 ((x & 0x0000ff00) << 8) |
158 ((x & 0x00ff0000) >> 8) |
159 ((x & 0xff000000) >> 24));
160 }
161 #elif defined(__OpenBSD__)
162 #include <sys/types.h>
163 #define CPU_TO_LE32( x ) htole32( x )
164 #else /*__linux__ */
165 #include <sys/endian.h>
166 #define CPU_TO_LE32( x ) bswap32( x )
167 #endif /*__linux__*/
168 #define MESA_BIG_ENDIAN 1
169 #else
170 #define CPU_TO_LE32( x ) ( x )
171 #define MESA_LITTLE_ENDIAN 1
172 #endif
173 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
174
175
176
177 /**
178 * Create a macro so that asm functions can be linked into compilers other
179 * than GNU C
180 */
181 #ifndef _ASMAPI
182 #if defined(_WIN32)
183 #define _ASMAPI __cdecl
184 #else
185 #define _ASMAPI
186 #endif
187 #ifdef PTR_DECL_IN_FRONT
188 #define _ASMAPIP * _ASMAPI
189 #else
190 #define _ASMAPIP _ASMAPI *
191 #endif
192 #endif
193
194 #ifdef USE_X86_ASM
195 #define _NORMAPI _ASMAPI
196 #define _NORMAPIP _ASMAPIP
197 #else
198 #define _NORMAPI
199 #define _NORMAPIP *
200 #endif
201
202
203 /**
204 * ASSERT macro
205 */
206 #if defined(DEBUG)
207 # define ASSERT(X) assert(X)
208 #else
209 # define ASSERT(X)
210 #endif
211
212
213 /*
214 * A trick to suppress uninitialized variable warning without generating any
215 * code
216 */
217 #define uninitialized_var(x) x = x
218
219 #ifndef NULL
220 #define NULL 0
221 #endif
222
223
224 /**
225 * LONGSTRING macro
226 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
227 */
228 #if !defined(__GNUC__)
229 # define LONGSTRING
230 #else
231 # define LONGSTRING __extension__
232 #endif
233
234
235 #ifndef M_PI
236 #define M_PI (3.14159265358979323846)
237 #endif
238
239 #ifndef M_E
240 #define M_E (2.7182818284590452354)
241 #endif
242
243 #ifndef M_LOG2E
244 #define M_LOG2E (1.4426950408889634074)
245 #endif
246
247 #ifndef ONE_DIV_SQRT_LN2
248 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
249 #endif
250
251 #ifndef FLT_MAX_EXP
252 #define FLT_MAX_EXP 128
253 #endif
254
255 #define IEEE_ONE 0x3f800000
256
257 /**
258 * START/END_FAST_MATH macros:
259 *
260 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
261 * original mode to a temporary).
262 * END_FAST_MATH: Restore x86 FPU to original mode.
263 */
264 #if defined(__GNUC__) && defined(__i386__)
265 /*
266 * Set the x86 FPU control word to guarentee only 32 bits of precision
267 * are stored in registers. Allowing the FPU to store more introduces
268 * differences between situations where numbers are pulled out of memory
269 * vs. situations where the compiler is able to optimize register usage.
270 *
271 * In the worst case, we force the compiler to use a memory access to
272 * truncate the float, by specifying the 'volatile' keyword.
273 */
274 /* Hardware default: All exceptions masked, extended double precision,
275 * round to nearest (IEEE compliant):
276 */
277 #define DEFAULT_X86_FPU 0x037f
278 /* All exceptions masked, single precision, round to nearest:
279 */
280 #define FAST_X86_FPU 0x003f
281 /* The fldcw instruction will cause any pending FP exceptions to be
282 * raised prior to entering the block, and we clear any pending
283 * exceptions before exiting the block. Hence, asm code has free
284 * reign over the FPU while in the fast math block.
285 */
286 #if defined(NO_FAST_MATH)
287 #define START_FAST_MATH(x) \
288 do { \
289 static GLuint mask = DEFAULT_X86_FPU; \
290 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
291 __asm__ ( "fldcw %0" : : "m" (mask) ); \
292 } while (0)
293 #else
294 #define START_FAST_MATH(x) \
295 do { \
296 static GLuint mask = FAST_X86_FPU; \
297 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
298 __asm__ ( "fldcw %0" : : "m" (mask) ); \
299 } while (0)
300 #endif
301 /* Restore original FPU mode, and clear any exceptions that may have
302 * occurred in the FAST_MATH block.
303 */
304 #define END_FAST_MATH(x) \
305 do { \
306 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
307 } while (0)
308
309 #elif defined(_MSC_VER) && defined(_M_IX86)
310 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
311 #define FAST_X86_FPU 0x003f /* See GCC comments above */
312 #if defined(NO_FAST_MATH)
313 #define START_FAST_MATH(x) do {\
314 static GLuint mask = DEFAULT_X86_FPU;\
315 __asm fnstcw word ptr [x]\
316 __asm fldcw word ptr [mask]\
317 } while(0)
318 #else
319 #define START_FAST_MATH(x) do {\
320 static GLuint mask = FAST_X86_FPU;\
321 __asm fnstcw word ptr [x]\
322 __asm fldcw word ptr [mask]\
323 } while(0)
324 #endif
325 #define END_FAST_MATH(x) do {\
326 __asm fnclex\
327 __asm fldcw word ptr [x]\
328 } while(0)
329
330 #else
331 #define START_FAST_MATH(x) x = 0
332 #define END_FAST_MATH(x) (void)(x)
333 #endif
334
335
336 #ifndef Elements
337 #define Elements(x) (sizeof(x)/sizeof(*(x)))
338 #endif
339
340 #ifdef __cplusplus
341 }
342 #endif
343
344
345 #endif /* COMPILER_H */