mesa: Change "BRIAN PAUL" to "THE AUTHORS" in license text.
[mesa.git] / src / mesa / main / compiler.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions 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 MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 #if defined(__alpha__) && defined(CCPML)
40 #include <cpml.h> /* use Compaq's Fast Math Library on Alpha */
41 #else
42 #include <math.h>
43 #endif
44 #include <limits.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <float.h>
49 #include <stdarg.h>
50
51 #include "c99_compat.h" /* inline, __func__, etc. */
52
53
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58
59 /**
60 * Get standard integer types
61 */
62 #include <stdint.h>
63
64
65 /**
66 * Sun compilers define __i386 instead of the gcc-style __i386__
67 */
68 #ifdef __SUNPRO_C
69 # if !defined(__i386__) && defined(__i386)
70 # define __i386__
71 # elif !defined(__amd64__) && defined(__amd64)
72 # define __amd64__
73 # elif !defined(__sparc__) && defined(__sparc)
74 # define __sparc__
75 # endif
76 # if !defined(__volatile)
77 # define __volatile volatile
78 # endif
79 #endif
80
81
82 /**
83 * finite macro.
84 */
85 #if defined(_MSC_VER)
86 # define finite _finite
87 #elif defined(__WATCOMC__)
88 # define finite _finite
89 #endif
90
91
92 /**
93 * Disable assorted warnings
94 */
95 #if !defined(OPENSTEP) && (defined(_WIN32) && !defined(__CYGWIN__)) && !defined(BUILD_FOR_SNAP)
96 # if !defined(__GNUC__) /* mingw environment */
97 # pragma warning( disable : 4068 ) /* unknown pragma */
98 # pragma warning( disable : 4710 ) /* function 'foo' not inlined */
99 # pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
100 # pragma warning( disable : 4127 ) /* conditional expression is constant */
101 # if defined(MESA_MINWARN)
102 # pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
103 # pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
104 # pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
105 # pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
106 # pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
107 # endif
108 # endif
109 #endif
110 #if defined(__WATCOMC__)
111 # pragma disable_message(201) /* Disable unreachable code warnings */
112 #endif
113
114
115
116 /* XXX: Use standard `inline` keyword instead */
117 #ifndef INLINE
118 # define INLINE inline
119 #endif
120
121
122 /**
123 * PUBLIC/USED macros
124 *
125 * If we build the library with gcc's -fvisibility=hidden flag, we'll
126 * use the PUBLIC macro to mark functions that are to be exported.
127 *
128 * We also need to define a USED attribute, so the optimizer doesn't
129 * inline a static function that we later use in an alias. - ajax
130 */
131 #ifndef PUBLIC
132 # if (defined(__GNUC__) && __GNUC__ >= 4) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
133 # define PUBLIC __attribute__((visibility("default")))
134 # define USED __attribute__((used))
135 # else
136 # define PUBLIC
137 # define USED
138 # endif
139 #endif
140
141
142 /**
143 * __builtin_expect macros
144 */
145 #if !defined(__GNUC__)
146 # define __builtin_expect(x, y) (x)
147 #endif
148
149 #ifndef likely
150 # ifdef __GNUC__
151 # define likely(x) __builtin_expect(!!(x), 1)
152 # define unlikely(x) __builtin_expect(!!(x), 0)
153 # else
154 # define likely(x) (x)
155 # define unlikely(x) (x)
156 # endif
157 #endif
158
159 /* XXX: Use standard `__func__` instead */
160 #ifndef __FUNCTION__
161 # define __FUNCTION__ __func__
162 #endif
163
164 /**
165 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
166 * Do not use these unless absolutely necessary!
167 * Try to use a runtime test instead.
168 * For now, only used by some DRI hardware drivers for color/texel packing.
169 */
170 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
171 #if defined(__linux__)
172 #include <byteswap.h>
173 #define CPU_TO_LE32( x ) bswap_32( x )
174 #elif defined(__APPLE__)
175 #include <CoreFoundation/CFByteOrder.h>
176 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
177 #elif (defined(_AIX) || defined(__blrts))
178 static INLINE GLuint CPU_TO_LE32(GLuint x)
179 {
180 return (((x & 0x000000ff) << 24) |
181 ((x & 0x0000ff00) << 8) |
182 ((x & 0x00ff0000) >> 8) |
183 ((x & 0xff000000) >> 24));
184 }
185 #elif defined(__OpenBSD__)
186 #include <sys/types.h>
187 #define CPU_TO_LE32( x ) htole32( x )
188 #else /*__linux__ */
189 #include <sys/endian.h>
190 #define CPU_TO_LE32( x ) bswap32( x )
191 #endif /*__linux__*/
192 #define MESA_BIG_ENDIAN 1
193 #else
194 #define CPU_TO_LE32( x ) ( x )
195 #define MESA_LITTLE_ENDIAN 1
196 #endif
197 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
198
199
200
201 #if !defined(CAPI) && defined(_WIN32) && !defined(BUILD_FOR_SNAP)
202 #define CAPI _cdecl
203 #endif
204
205
206 /**
207 * Create a macro so that asm functions can be linked into compilers other
208 * than GNU C
209 */
210 #ifndef _ASMAPI
211 #if defined(_WIN32) && !defined(BUILD_FOR_SNAP)/* was: !defined( __GNUC__ ) && !defined( VMS ) && !defined( __INTEL_COMPILER )*/
212 #define _ASMAPI __cdecl
213 #else
214 #define _ASMAPI
215 #endif
216 #ifdef PTR_DECL_IN_FRONT
217 #define _ASMAPIP * _ASMAPI
218 #else
219 #define _ASMAPIP _ASMAPI *
220 #endif
221 #endif
222
223 #ifdef USE_X86_ASM
224 #define _NORMAPI _ASMAPI
225 #define _NORMAPIP _ASMAPIP
226 #else
227 #define _NORMAPI
228 #define _NORMAPIP *
229 #endif
230
231
232 /* Turn off macro checking systems used by other libraries */
233 #ifdef CHECK
234 #undef CHECK
235 #endif
236
237
238 /**
239 * ASSERT macro
240 */
241 #if !defined(_WIN32_WCE)
242 #if defined(BUILD_FOR_SNAP) && defined(CHECKED)
243 # define ASSERT(X) _CHECK(X)
244 #elif defined(DEBUG)
245 # define ASSERT(X) assert(X)
246 #else
247 # define ASSERT(X)
248 #endif
249 #endif
250
251
252 /**
253 * Static (compile-time) assertion.
254 * Basically, use COND to dimension an array. If COND is false/zero the
255 * array size will be -1 and we'll get a compilation error.
256 */
257 #define STATIC_ASSERT(COND) \
258 do { \
259 (void) sizeof(char [1 - 2*!(COND)]); \
260 } while (0)
261
262
263 #if (__GNUC__ >= 3)
264 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
265 #else
266 #define PRINTFLIKE(f, a)
267 #endif
268
269 #ifndef NULL
270 #define NULL 0
271 #endif
272
273
274 /**
275 * LONGSTRING macro
276 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
277 */
278 #if !defined(__GNUC__)
279 # define LONGSTRING
280 #else
281 # define LONGSTRING __extension__
282 #endif
283
284
285 #ifndef M_PI
286 #define M_PI (3.14159265358979323846)
287 #endif
288
289 #ifndef M_E
290 #define M_E (2.7182818284590452354)
291 #endif
292
293 #ifndef M_LOG2E
294 #define M_LOG2E (1.4426950408889634074)
295 #endif
296
297 #ifndef ONE_DIV_SQRT_LN2
298 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
299 #endif
300
301 #ifndef FLT_MAX_EXP
302 #define FLT_MAX_EXP 128
303 #endif
304
305
306 /**
307 * USE_IEEE: Determine if we're using IEEE floating point
308 */
309 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
310 defined(__s390__) || defined(__s390x__) || defined(__powerpc__) || \
311 defined(__x86_64__) || \
312 defined(__m68k__) || \
313 defined(ia64) || defined(__ia64__) || \
314 defined(__hppa__) || defined(hpux) || \
315 defined(__mips) || defined(_MIPS_ARCH) || \
316 defined(__arm__) || \
317 defined(__sh__) || defined(__m32r__) || \
318 (defined(__sun) && defined(_IEEE_754)) || \
319 (defined(__alpha__) && defined(__IEEE_FLOAT))
320 #define USE_IEEE
321 #define IEEE_ONE 0x3f800000
322 #endif
323
324
325 /**
326 * START/END_FAST_MATH macros:
327 *
328 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
329 * original mode to a temporary).
330 * END_FAST_MATH: Restore x86 FPU to original mode.
331 */
332 #if defined(__GNUC__) && defined(__i386__)
333 /*
334 * Set the x86 FPU control word to guarentee only 32 bits of precision
335 * are stored in registers. Allowing the FPU to store more introduces
336 * differences between situations where numbers are pulled out of memory
337 * vs. situations where the compiler is able to optimize register usage.
338 *
339 * In the worst case, we force the compiler to use a memory access to
340 * truncate the float, by specifying the 'volatile' keyword.
341 */
342 /* Hardware default: All exceptions masked, extended double precision,
343 * round to nearest (IEEE compliant):
344 */
345 #define DEFAULT_X86_FPU 0x037f
346 /* All exceptions masked, single precision, round to nearest:
347 */
348 #define FAST_X86_FPU 0x003f
349 /* The fldcw instruction will cause any pending FP exceptions to be
350 * raised prior to entering the block, and we clear any pending
351 * exceptions before exiting the block. Hence, asm code has free
352 * reign over the FPU while in the fast math block.
353 */
354 #if defined(NO_FAST_MATH)
355 #define START_FAST_MATH(x) \
356 do { \
357 static GLuint mask = DEFAULT_X86_FPU; \
358 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
359 __asm__ ( "fldcw %0" : : "m" (mask) ); \
360 } while (0)
361 #else
362 #define START_FAST_MATH(x) \
363 do { \
364 static GLuint mask = FAST_X86_FPU; \
365 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
366 __asm__ ( "fldcw %0" : : "m" (mask) ); \
367 } while (0)
368 #endif
369 /* Restore original FPU mode, and clear any exceptions that may have
370 * occurred in the FAST_MATH block.
371 */
372 #define END_FAST_MATH(x) \
373 do { \
374 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
375 } while (0)
376
377 #elif defined(__WATCOMC__) && defined(__386__)
378 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
379 #define FAST_X86_FPU 0x003f /* See GCC comments above */
380 void _watcom_start_fast_math(unsigned short *x,unsigned short *mask);
381 #pragma aux _watcom_start_fast_math = \
382 "fnstcw word ptr [eax]" \
383 "fldcw word ptr [ecx]" \
384 parm [eax] [ecx] \
385 modify exact [];
386 void _watcom_end_fast_math(unsigned short *x);
387 #pragma aux _watcom_end_fast_math = \
388 "fnclex" \
389 "fldcw word ptr [eax]" \
390 parm [eax] \
391 modify exact [];
392 #if defined(NO_FAST_MATH)
393 #define START_FAST_MATH(x) \
394 do { \
395 static GLushort mask = DEFAULT_X86_FPU; \
396 _watcom_start_fast_math(&x,&mask); \
397 } while (0)
398 #else
399 #define START_FAST_MATH(x) \
400 do { \
401 static GLushort mask = FAST_X86_FPU; \
402 _watcom_start_fast_math(&x,&mask); \
403 } while (0)
404 #endif
405 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
406
407 #elif defined(_MSC_VER) && defined(_M_IX86)
408 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
409 #define FAST_X86_FPU 0x003f /* See GCC comments above */
410 #if defined(NO_FAST_MATH)
411 #define START_FAST_MATH(x) do {\
412 static GLuint mask = DEFAULT_X86_FPU;\
413 __asm fnstcw word ptr [x]\
414 __asm fldcw word ptr [mask]\
415 } while(0)
416 #else
417 #define START_FAST_MATH(x) do {\
418 static GLuint mask = FAST_X86_FPU;\
419 __asm fnstcw word ptr [x]\
420 __asm fldcw word ptr [mask]\
421 } while(0)
422 #endif
423 #define END_FAST_MATH(x) do {\
424 __asm fnclex\
425 __asm fldcw word ptr [x]\
426 } while(0)
427
428 #else
429 #define START_FAST_MATH(x) x = 0
430 #define END_FAST_MATH(x) (void)(x)
431 #endif
432
433
434 #ifndef Elements
435 #define Elements(x) (sizeof(x)/sizeof(*(x)))
436 #endif
437
438
439
440 #ifdef __cplusplus
441 }
442 #endif
443
444
445 #endif /* COMPILER_H */