mesa: Avoid redefining PUBLIC/USE/likely/unlikely macros.
[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 * BRIAN PAUL 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 #if defined(__linux__) && defined(__i386__)
49 #include <fpu_control.h>
50 #endif
51 #include <float.h>
52 #include <stdarg.h>
53
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59
60 /**
61 * Get standard integer types
62 */
63 #if defined(_MSC_VER)
64 typedef __int8 int8_t;
65 typedef unsigned __int8 uint8_t;
66 typedef __int16 int16_t;
67 typedef unsigned __int16 uint16_t;
68 typedef __int32 int32_t;
69 typedef unsigned __int32 uint32_t;
70 typedef __int64 int64_t;
71 typedef unsigned __int64 uint64_t;
72
73 # if defined(_WIN64)
74 typedef __int64 intptr_t;
75 typedef unsigned __int64 uintptr_t;
76 # else
77 typedef __int32 intptr_t;
78 typedef unsigned __int32 uintptr_t;
79 # endif
80
81 # define INT64_C(__val) __val##i64
82 # define UINT64_C(__val) __val##ui64
83 #else
84 # include <stdint.h>
85 #endif
86
87
88 /**
89 * Sun compilers define __i386 instead of the gcc-style __i386__
90 */
91 #ifdef __SUNPRO_C
92 # if !defined(__i386__) && defined(__i386)
93 # define __i386__
94 # elif !defined(__amd64__) && defined(__amd64)
95 # define __amd64__
96 # elif !defined(__sparc__) && defined(__sparc)
97 # define __sparc__
98 # endif
99 # if !defined(__volatile)
100 # define __volatile volatile
101 # endif
102 #endif
103
104
105 /**
106 * finite macro.
107 */
108 #if defined(_MSC_VER)
109 # define finite _finite
110 #elif defined(__WATCOMC__)
111 # define finite _finite
112 #endif
113
114
115 /**
116 * Disable assorted warnings
117 */
118 #if !defined(OPENSTEP) && (defined(__WIN32__) && !defined(__CYGWIN__)) && !defined(BUILD_FOR_SNAP)
119 # if !defined(__GNUC__) /* mingw environment */
120 # pragma warning( disable : 4068 ) /* unknown pragma */
121 # pragma warning( disable : 4710 ) /* function 'foo' not inlined */
122 # pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
123 # pragma warning( disable : 4127 ) /* conditional expression is constant */
124 # if defined(MESA_MINWARN)
125 # pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
126 # pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
127 # pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
128 # pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
129 # pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
130 # endif
131 # endif
132 #endif
133 #if defined(__WATCOMC__)
134 # pragma disable_message(201) /* Disable unreachable code warnings */
135 #endif
136
137
138
139 /**
140 * Function inlining
141 */
142 #if defined(__GNUC__)
143 # define INLINE __inline__
144 #elif defined(__MSC__)
145 # define INLINE __inline
146 #elif defined(_MSC_VER)
147 # define INLINE __inline
148 #elif defined(__ICL)
149 # define INLINE __inline
150 #elif defined(__INTEL_COMPILER)
151 # define INLINE inline
152 #elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
153 # define INLINE __inline
154 #elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
155 # define INLINE inline
156 # define __inline inline
157 # define __inline__ inline
158 #elif (__STDC_VERSION__ >= 199901L) /* C99 */
159 # define INLINE inline
160 #else
161 # define INLINE
162 #endif
163
164
165 /**
166 * PUBLIC/USED macros
167 *
168 * If we build the library with gcc's -fvisibility=hidden flag, we'll
169 * use the PUBLIC macro to mark functions that are to be exported.
170 *
171 * We also need to define a USED attribute, so the optimizer doesn't
172 * inline a static function that we later use in an alias. - ajax
173 */
174 #ifndef PUBLIC
175 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
176 # define PUBLIC __attribute__((visibility("default")))
177 # define USED __attribute__((used))
178 # else
179 # define PUBLIC
180 # define USED
181 # endif
182 #endif
183
184
185 /**
186 * Some compilers don't like some of Mesa's const usage. In those places use
187 * CONST instead of const. Pass -DNO_CONST to compilers where this matters.
188 */
189 #ifdef NO_CONST
190 # define CONST
191 #else
192 # define CONST const
193 #endif
194
195
196 /**
197 * __builtin_expect macros
198 */
199 #if !defined(__GNUC__)
200 # define __builtin_expect(x, y) (x)
201 #endif
202
203 #ifndef likely
204 # ifdef __GNUC__
205 # define likely(x) __builtin_expect(!!(x), 1)
206 # define unlikely(x) __builtin_expect(!!(x), 0)
207 # else
208 # define likely(x) (x)
209 # define unlikely(x) (x)
210 # endif
211 #endif
212
213 /**
214 * The __FUNCTION__ gcc variable is generally only used for debugging.
215 * If we're not using gcc, define __FUNCTION__ as a cpp symbol here.
216 * Don't define it if using a newer Windows compiler.
217 */
218 #ifndef __FUNCTION__
219 # if defined(__VMS)
220 # define __FUNCTION__ "VMS$NL:"
221 # elif !defined(__GNUC__) && !defined(__xlC__) && \
222 (!defined(_MSC_VER) || _MSC_VER < 1300)
223 # if (__STDC_VERSION__ >= 199901L) /* C99 */ || \
224 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
225 # define __FUNCTION__ __func__
226 # else
227 # define __FUNCTION__ "<unknown>"
228 # endif
229 # endif
230 #endif
231 #ifndef __func__
232 # if (__STDC_VERSION__ >= 199901L) || \
233 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
234 /* __func__ is part of C99 */
235 # elif defined(_MSC_VER)
236 # if _MSC_VER >= 1300
237 # define __func__ __FUNCTION__
238 # else
239 # define __func__ "<unknown>"
240 # endif
241 # endif
242 #endif
243
244
245 /**
246 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
247 * Do not use these unless absolutely necessary!
248 * Try to use a runtime test instead.
249 * For now, only used by some DRI hardware drivers for color/texel packing.
250 */
251 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
252 #if defined(__linux__)
253 #include <byteswap.h>
254 #define CPU_TO_LE32( x ) bswap_32( x )
255 #elif defined(__APPLE__)
256 #include <CoreFoundation/CFByteOrder.h>
257 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
258 #elif (defined(_AIX) || defined(__blrts))
259 static INLINE GLuint CPU_TO_LE32(GLuint x)
260 {
261 return (((x & 0x000000ff) << 24) |
262 ((x & 0x0000ff00) << 8) |
263 ((x & 0x00ff0000) >> 8) |
264 ((x & 0xff000000) >> 24));
265 }
266 #else /*__linux__ */
267 #include <sys/endian.h>
268 #define CPU_TO_LE32( x ) bswap32( x )
269 #endif /*__linux__*/
270 #define MESA_BIG_ENDIAN 1
271 #else
272 #define CPU_TO_LE32( x ) ( x )
273 #define MESA_LITTLE_ENDIAN 1
274 #endif
275 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
276
277
278
279 #if !defined(CAPI) && defined(WIN32) && !defined(BUILD_FOR_SNAP)
280 #define CAPI _cdecl
281 #endif
282
283
284 /**
285 * Create a macro so that asm functions can be linked into compilers other
286 * than GNU C
287 */
288 #ifndef _ASMAPI
289 #if defined(WIN32) && !defined(BUILD_FOR_SNAP)/* was: !defined( __GNUC__ ) && !defined( VMS ) && !defined( __INTEL_COMPILER )*/
290 #define _ASMAPI __cdecl
291 #else
292 #define _ASMAPI
293 #endif
294 #ifdef PTR_DECL_IN_FRONT
295 #define _ASMAPIP * _ASMAPI
296 #else
297 #define _ASMAPIP _ASMAPI *
298 #endif
299 #endif
300
301 #ifdef USE_X86_ASM
302 #define _NORMAPI _ASMAPI
303 #define _NORMAPIP _ASMAPIP
304 #else
305 #define _NORMAPI
306 #define _NORMAPIP *
307 #endif
308
309
310 /* This is a macro on IRIX */
311 #ifdef _P
312 #undef _P
313 #endif
314
315
316 /* Turn off macro checking systems used by other libraries */
317 #ifdef CHECK
318 #undef CHECK
319 #endif
320
321
322 /**
323 * ASSERT macro
324 */
325 #if !defined(_WIN32_WCE)
326 #if defined(BUILD_FOR_SNAP) && defined(CHECKED)
327 # define ASSERT(X) _CHECK(X)
328 #elif defined(DEBUG)
329 # define ASSERT(X) assert(X)
330 #else
331 # define ASSERT(X)
332 #endif
333 #endif
334
335 #if (__GNUC__ >= 3)
336 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
337 #else
338 #define PRINTFLIKE(f, a)
339 #endif
340
341 #ifndef NULL
342 #define NULL 0
343 #endif
344
345
346 /**
347 * LONGSTRING macro
348 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
349 */
350 #if !defined(__GNUC__)
351 # define LONGSTRING
352 #else
353 # define LONGSTRING __extension__
354 #endif
355
356
357 #ifndef M_PI
358 #define M_PI (3.1415926536)
359 #endif
360
361 #ifndef M_E
362 #define M_E (2.7182818284590452354)
363 #endif
364
365 #ifndef M_LOG2E
366 #define M_LOG2E (1.4426950408889634074)
367 #endif
368
369 #ifndef ONE_DIV_LN2
370 #define ONE_DIV_LN2 (1.442695040888963456)
371 #endif
372
373 #ifndef ONE_DIV_SQRT_LN2
374 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
375 #endif
376
377 #ifndef FLT_MAX_EXP
378 #define FLT_MAX_EXP 128
379 #endif
380
381
382 /**
383 * USE_IEEE: Determine if we're using IEEE floating point
384 */
385 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
386 defined(__s390x__) || defined(__powerpc__) || \
387 defined(__x86_64__) || \
388 defined(ia64) || defined(__ia64__) || \
389 defined(__hppa__) || defined(hpux) || \
390 defined(__mips) || defined(_MIPS_ARCH) || \
391 defined(__arm__) || \
392 defined(__sh__) || defined(__m32r__) || \
393 (defined(__sun) && defined(_IEEE_754)) || \
394 (defined(__alpha__) && (defined(__IEEE_FLOAT) || !defined(VMS)))
395 #define USE_IEEE
396 #define IEEE_ONE 0x3f800000
397 #endif
398
399
400 /**
401 * START/END_FAST_MATH macros:
402 *
403 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
404 * original mode to a temporary).
405 * END_FAST_MATH: Restore x86 FPU to original mode.
406 */
407 #if defined(__GNUC__) && defined(__i386__)
408 /*
409 * Set the x86 FPU control word to guarentee only 32 bits of precision
410 * are stored in registers. Allowing the FPU to store more introduces
411 * differences between situations where numbers are pulled out of memory
412 * vs. situations where the compiler is able to optimize register usage.
413 *
414 * In the worst case, we force the compiler to use a memory access to
415 * truncate the float, by specifying the 'volatile' keyword.
416 */
417 /* Hardware default: All exceptions masked, extended double precision,
418 * round to nearest (IEEE compliant):
419 */
420 #define DEFAULT_X86_FPU 0x037f
421 /* All exceptions masked, single precision, round to nearest:
422 */
423 #define FAST_X86_FPU 0x003f
424 /* The fldcw instruction will cause any pending FP exceptions to be
425 * raised prior to entering the block, and we clear any pending
426 * exceptions before exiting the block. Hence, asm code has free
427 * reign over the FPU while in the fast math block.
428 */
429 #if defined(NO_FAST_MATH)
430 #define START_FAST_MATH(x) \
431 do { \
432 static GLuint mask = DEFAULT_X86_FPU; \
433 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
434 __asm__ ( "fldcw %0" : : "m" (mask) ); \
435 } while (0)
436 #else
437 #define START_FAST_MATH(x) \
438 do { \
439 static GLuint mask = FAST_X86_FPU; \
440 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
441 __asm__ ( "fldcw %0" : : "m" (mask) ); \
442 } while (0)
443 #endif
444 /* Restore original FPU mode, and clear any exceptions that may have
445 * occurred in the FAST_MATH block.
446 */
447 #define END_FAST_MATH(x) \
448 do { \
449 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
450 } while (0)
451
452 #elif defined(__WATCOMC__) && defined(__386__)
453 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
454 #define FAST_X86_FPU 0x003f /* See GCC comments above */
455 void _watcom_start_fast_math(unsigned short *x,unsigned short *mask);
456 #pragma aux _watcom_start_fast_math = \
457 "fnstcw word ptr [eax]" \
458 "fldcw word ptr [ecx]" \
459 parm [eax] [ecx] \
460 modify exact [];
461 void _watcom_end_fast_math(unsigned short *x);
462 #pragma aux _watcom_end_fast_math = \
463 "fnclex" \
464 "fldcw word ptr [eax]" \
465 parm [eax] \
466 modify exact [];
467 #if defined(NO_FAST_MATH)
468 #define START_FAST_MATH(x) \
469 do { \
470 static GLushort mask = DEFAULT_X86_FPU; \
471 _watcom_start_fast_math(&x,&mask); \
472 } while (0)
473 #else
474 #define START_FAST_MATH(x) \
475 do { \
476 static GLushort mask = FAST_X86_FPU; \
477 _watcom_start_fast_math(&x,&mask); \
478 } while (0)
479 #endif
480 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
481
482 #elif defined(_MSC_VER) && defined(_M_IX86)
483 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
484 #define FAST_X86_FPU 0x003f /* See GCC comments above */
485 #if defined(NO_FAST_MATH)
486 #define START_FAST_MATH(x) do {\
487 static GLuint mask = DEFAULT_X86_FPU;\
488 __asm fnstcw word ptr [x]\
489 __asm fldcw word ptr [mask]\
490 } while(0)
491 #else
492 #define START_FAST_MATH(x) do {\
493 static GLuint mask = FAST_X86_FPU;\
494 __asm fnstcw word ptr [x]\
495 __asm fldcw word ptr [mask]\
496 } while(0)
497 #endif
498 #define END_FAST_MATH(x) do {\
499 __asm fnclex\
500 __asm fldcw word ptr [x]\
501 } while(0)
502
503 #else
504 #define START_FAST_MATH(x) x = 0
505 #define END_FAST_MATH(x) (void)(x)
506 #endif
507
508
509 #ifndef Elements
510 #define Elements(x) (sizeof(x)/sizeof(*(x)))
511 #endif
512
513
514
515 #ifdef __cplusplus
516 }
517 #endif
518
519
520 #endif /* COMPILER_H */