2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
29 * Compiler-related stuff.
39 #if defined(__alpha__) && defined(CCPML)
40 #include <cpml.h> /* use Compaq's Fast Math Library on Alpha */
48 #if defined(__linux__) && defined(__i386__)
49 #include <fpu_control.h>
61 * Get standard integer types
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;
74 typedef __int64
intptr_t;
75 typedef unsigned __int64
uintptr_t;
77 typedef __int32
intptr_t;
78 typedef unsigned __int32
uintptr_t;
81 # define INT64_C(__val) __val##i64
82 # define UINT64_C(__val) __val##ui64
89 * Sun compilers define __i386 instead of the gcc-style __i386__
92 # if !defined(__i386__) && defined(__i386)
94 # elif !defined(__amd64__) && defined(__amd64)
96 # elif !defined(__sparc__) && defined(__sparc)
99 # if !defined(__volatile)
100 # define __volatile volatile
108 #if defined(_MSC_VER)
109 # define finite _finite
110 #elif defined(__WATCOMC__)
111 # define finite _finite
116 * Disable assorted warnings
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 */
133 #if defined(__WATCOMC__)
134 # pragma disable_message(201) /* Disable unreachable code warnings */
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
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
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.
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
174 #if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
175 # define PUBLIC __attribute__((visibility("default")))
176 # define USED __attribute__((used))
184 * Some compilers don't like some of Mesa's const usage. In those places use
185 * CONST instead of const. Pass -DNO_CONST to compilers where this matters.
195 * __builtin_expect macros
197 #if !defined(__GNUC__)
198 # define __builtin_expect(x, y) x
202 #define likely(x) __builtin_expect(!!(x), 1)
203 #define unlikely(x) __builtin_expect(!!(x), 0)
205 #define likely(x) !!(x)
206 #define unlikely(x) !!(x)
210 * The __FUNCTION__ gcc variable is generally only used for debugging.
211 * If we're not using gcc, define __FUNCTION__ as a cpp symbol here.
212 * Don't define it if using a newer Windows compiler.
216 # define __FUNCTION__ "VMS$NL:"
217 # elif !defined(__GNUC__) && !defined(__xlC__) && \
218 (!defined(_MSC_VER) || _MSC_VER < 1300)
219 # if (__STDC_VERSION__ >= 199901L) /* C99 */ || \
220 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
221 # define __FUNCTION__ __func__
223 # define __FUNCTION__ "<unknown>"
228 # if (__STDC_VERSION__ >= 199901L) || \
229 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
230 /* __func__ is part of C99 */
231 # elif defined(_MSC_VER)
232 # if _MSC_VER >= 1300
233 # define __func__ __FUNCTION__
235 # define __func__ "<unknown>"
242 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
243 * Do not use these unless absolutely necessary!
244 * Try to use a runtime test instead.
245 * For now, only used by some DRI hardware drivers for color/texel packing.
247 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
248 #if defined(__linux__)
249 #include <byteswap.h>
250 #define CPU_TO_LE32( x ) bswap_32( x )
251 #elif defined(__APPLE__)
252 #include <CoreFoundation/CFByteOrder.h>
253 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
254 #elif (defined(_AIX) || defined(__blrts))
255 static INLINE GLuint
CPU_TO_LE32(GLuint x
)
257 return (((x
& 0x000000ff) << 24) |
258 ((x
& 0x0000ff00) << 8) |
259 ((x
& 0x00ff0000) >> 8) |
260 ((x
& 0xff000000) >> 24));
263 #include <sys/endian.h>
264 #define CPU_TO_LE32( x ) bswap32( x )
266 #define MESA_BIG_ENDIAN 1
268 #define CPU_TO_LE32( x ) ( x )
269 #define MESA_LITTLE_ENDIAN 1
271 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
275 #if !defined(CAPI) && defined(WIN32) && !defined(BUILD_FOR_SNAP)
281 * Create a macro so that asm functions can be linked into compilers other
285 #if defined(WIN32) && !defined(BUILD_FOR_SNAP)/* was: !defined( __GNUC__ ) && !defined( VMS ) && !defined( __INTEL_COMPILER )*/
286 #define _ASMAPI __cdecl
290 #ifdef PTR_DECL_IN_FRONT
291 #define _ASMAPIP * _ASMAPI
293 #define _ASMAPIP _ASMAPI *
298 #define _NORMAPI _ASMAPI
299 #define _NORMAPIP _ASMAPIP
306 /* This is a macro on IRIX */
312 /* Turn off macro checking systems used by other libraries */
321 #if !defined(_WIN32_WCE)
322 #if defined(BUILD_FOR_SNAP) && defined(CHECKED)
323 # define ASSERT(X) _CHECK(X)
325 # define ASSERT(X) assert(X)
332 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
334 #define PRINTFLIKE(f, a)
344 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
346 #if !defined(__GNUC__)
349 # define LONGSTRING __extension__
354 #define M_PI (3.1415926536)
358 #define M_E (2.7182818284590452354)
362 #define ONE_DIV_LN2 (1.442695040888963456)
365 #ifndef ONE_DIV_SQRT_LN2
366 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
370 #define FLT_MAX_EXP 128
375 * USE_IEEE: Determine if we're using IEEE floating point
377 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
378 defined(__s390x__) || defined(__powerpc__) || \
379 defined(__x86_64__) || \
380 defined(ia64) || defined(__ia64__) || \
381 defined(__hppa__) || defined(hpux) || \
382 defined(__mips) || defined(_MIPS_ARCH) || \
383 defined(__arm__) || \
384 defined(__sh__) || defined(__m32r__) || \
385 (defined(__sun) && defined(_IEEE_754)) || \
386 (defined(__alpha__) && (defined(__IEEE_FLOAT) || !defined(VMS)))
388 #define IEEE_ONE 0x3f800000
393 * START/END_FAST_MATH macros:
395 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
396 * original mode to a temporary).
397 * END_FAST_MATH: Restore x86 FPU to original mode.
399 #if defined(__GNUC__) && defined(__i386__)
401 * Set the x86 FPU control word to guarentee only 32 bits of precision
402 * are stored in registers. Allowing the FPU to store more introduces
403 * differences between situations where numbers are pulled out of memory
404 * vs. situations where the compiler is able to optimize register usage.
406 * In the worst case, we force the compiler to use a memory access to
407 * truncate the float, by specifying the 'volatile' keyword.
409 /* Hardware default: All exceptions masked, extended double precision,
410 * round to nearest (IEEE compliant):
412 #define DEFAULT_X86_FPU 0x037f
413 /* All exceptions masked, single precision, round to nearest:
415 #define FAST_X86_FPU 0x003f
416 /* The fldcw instruction will cause any pending FP exceptions to be
417 * raised prior to entering the block, and we clear any pending
418 * exceptions before exiting the block. Hence, asm code has free
419 * reign over the FPU while in the fast math block.
421 #if defined(NO_FAST_MATH)
422 #define START_FAST_MATH(x) \
424 static GLuint mask = DEFAULT_X86_FPU; \
425 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
426 __asm__ ( "fldcw %0" : : "m" (mask) ); \
429 #define START_FAST_MATH(x) \
431 static GLuint mask = FAST_X86_FPU; \
432 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
433 __asm__ ( "fldcw %0" : : "m" (mask) ); \
436 /* Restore original FPU mode, and clear any exceptions that may have
437 * occurred in the FAST_MATH block.
439 #define END_FAST_MATH(x) \
441 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
444 #elif defined(__WATCOMC__) && defined(__386__)
445 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
446 #define FAST_X86_FPU 0x003f /* See GCC comments above */
447 void _watcom_start_fast_math(unsigned short *x
,unsigned short *mask
);
448 #pragma aux _watcom_start_fast_math = \
449 "fnstcw word ptr [eax]" \
450 "fldcw word ptr [ecx]" \
453 void _watcom_end_fast_math(unsigned short *x
);
454 #pragma aux _watcom_end_fast_math = \
456 "fldcw word ptr [eax]" \
459 #if defined(NO_FAST_MATH)
460 #define START_FAST_MATH(x) \
462 static GLushort mask = DEFAULT_X86_FPU; \
463 _watcom_start_fast_math(&x,&mask); \
466 #define START_FAST_MATH(x) \
468 static GLushort mask = FAST_X86_FPU; \
469 _watcom_start_fast_math(&x,&mask); \
472 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
474 #elif defined(_MSC_VER) && defined(_M_IX86)
475 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
476 #define FAST_X86_FPU 0x003f /* See GCC comments above */
477 #if defined(NO_FAST_MATH)
478 #define START_FAST_MATH(x) do {\
479 static GLuint mask = DEFAULT_X86_FPU;\
480 __asm fnstcw word ptr [x]\
481 __asm fldcw word ptr [mask]\
484 #define START_FAST_MATH(x) do {\
485 static GLuint mask = FAST_X86_FPU;\
486 __asm fnstcw word ptr [x]\
487 __asm fldcw word ptr [mask]\
490 #define END_FAST_MATH(x) do {\
492 __asm fldcw word ptr [x]\
496 #define START_FAST_MATH(x) x = 0
497 #define END_FAST_MATH(x) (void)(x)
502 #define Elements(x) (sizeof(x)/sizeof(*(x)))
512 #endif /* COMPILER_H */