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 */
58 * Get standard integer types
64 * Sun compilers define __i386 instead of the gcc-style __i386__
67 # if !defined(__i386__) && defined(__i386)
69 # elif !defined(__amd64__) && defined(__amd64)
71 # elif !defined(__sparc__) && defined(__sparc)
74 # if !defined(__volatile)
75 # define __volatile volatile
84 # define finite _finite
85 #elif defined(__WATCOMC__)
86 # define finite _finite
91 * Disable assorted warnings
93 #if !defined(OPENSTEP) && (defined(_WIN32) && !defined(__CYGWIN__)) && !defined(BUILD_FOR_SNAP)
94 # if !defined(__GNUC__) /* mingw environment */
95 # pragma warning( disable : 4068 ) /* unknown pragma */
96 # pragma warning( disable : 4710 ) /* function 'foo' not inlined */
97 # pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
98 # pragma warning( disable : 4127 ) /* conditional expression is constant */
99 # if defined(MESA_MINWARN)
100 # pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
101 # pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
102 # pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
103 # pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
104 # pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
108 #if defined(__WATCOMC__)
109 # pragma disable_message(201) /* Disable unreachable code warnings */
119 /* C++ supports inline keyword */
120 # elif defined(__GNUC__)
121 # define inline __inline__
122 # elif defined(_MSC_VER)
123 # define inline __inline
124 # elif defined(__ICL)
125 # define inline __inline
126 # elif defined(__INTEL_COMPILER)
127 /* Intel compiler supports inline keyword */
128 # elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
129 # define inline __inline
130 # elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
131 /* C99 supports inline keyword */
132 # elif (__STDC_VERSION__ >= 199901L)
133 /* C99 supports inline keyword */
139 # define INLINE inline
146 * If we build the library with gcc's -fvisibility=hidden flag, we'll
147 * use the PUBLIC macro to mark functions that are to be exported.
149 * We also need to define a USED attribute, so the optimizer doesn't
150 * inline a static function that we later use in an alias. - ajax
153 # if (defined(__GNUC__) && __GNUC__ >= 4) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
154 # define PUBLIC __attribute__((visibility("default")))
155 # define USED __attribute__((used))
164 * __builtin_expect macros
166 #if !defined(__GNUC__)
167 # define __builtin_expect(x, y) (x)
172 # define likely(x) __builtin_expect(!!(x), 1)
173 # define unlikely(x) __builtin_expect(!!(x), 0)
175 # define likely(x) (x)
176 # define unlikely(x) (x)
181 * The __FUNCTION__ gcc variable is generally only used for debugging.
182 * If we're not using gcc, define __FUNCTION__ as a cpp symbol here.
183 * Don't define it if using a newer Windows compiler.
187 # define __FUNCTION__ "VMS$NL:"
188 # elif !defined(__GNUC__) && !defined(__xlC__) && \
189 (!defined(_MSC_VER) || _MSC_VER < 1300)
190 # if (__STDC_VERSION__ >= 199901L) /* C99 */ || \
191 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
192 # define __FUNCTION__ __func__
194 # define __FUNCTION__ "<unknown>"
199 # if (__STDC_VERSION__ >= 199901L) || \
200 (defined(__SUNPRO_C) && defined(__C99FEATURES__))
201 /* __func__ is part of C99 */
202 # elif defined(_MSC_VER)
203 # if _MSC_VER >= 1300
204 # define __func__ __FUNCTION__
206 # define __func__ "<unknown>"
213 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
214 * Do not use these unless absolutely necessary!
215 * Try to use a runtime test instead.
216 * For now, only used by some DRI hardware drivers for color/texel packing.
218 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
219 #if defined(__linux__)
220 #include <byteswap.h>
221 #define CPU_TO_LE32( x ) bswap_32( x )
222 #elif defined(__APPLE__)
223 #include <CoreFoundation/CFByteOrder.h>
224 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
225 #elif (defined(_AIX) || defined(__blrts))
226 static INLINE GLuint
CPU_TO_LE32(GLuint x
)
228 return (((x
& 0x000000ff) << 24) |
229 ((x
& 0x0000ff00) << 8) |
230 ((x
& 0x00ff0000) >> 8) |
231 ((x
& 0xff000000) >> 24));
233 #elif defined(__OpenBSD__)
234 #include <sys/types.h>
235 #define CPU_TO_LE32( x ) htole32( x )
237 #include <sys/endian.h>
238 #define CPU_TO_LE32( x ) bswap32( x )
240 #define MESA_BIG_ENDIAN 1
242 #define CPU_TO_LE32( x ) ( x )
243 #define MESA_LITTLE_ENDIAN 1
245 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
249 #if !defined(CAPI) && defined(_WIN32) && !defined(BUILD_FOR_SNAP)
255 * Create a macro so that asm functions can be linked into compilers other
259 #if defined(_WIN32) && !defined(BUILD_FOR_SNAP)/* was: !defined( __GNUC__ ) && !defined( VMS ) && !defined( __INTEL_COMPILER )*/
260 #define _ASMAPI __cdecl
264 #ifdef PTR_DECL_IN_FRONT
265 #define _ASMAPIP * _ASMAPI
267 #define _ASMAPIP _ASMAPI *
272 #define _NORMAPI _ASMAPI
273 #define _NORMAPIP _ASMAPIP
280 /* Turn off macro checking systems used by other libraries */
289 #if !defined(_WIN32_WCE)
290 #if defined(BUILD_FOR_SNAP) && defined(CHECKED)
291 # define ASSERT(X) _CHECK(X)
293 # define ASSERT(X) assert(X)
301 * Static (compile-time) assertion.
302 * Basically, use COND to dimension an array. If COND is false/zero the
303 * array size will be -1 and we'll get a compilation error.
305 #define STATIC_ASSERT(COND) \
307 typedef int static_assertion_failed[(!!(COND))*2-1]; \
312 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
314 #define PRINTFLIKE(f, a)
324 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
326 #if !defined(__GNUC__)
329 # define LONGSTRING __extension__
334 #define M_PI (3.14159265358979323846)
338 #define M_E (2.7182818284590452354)
342 #define M_LOG2E (1.4426950408889634074)
345 #ifndef ONE_DIV_SQRT_LN2
346 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
350 #define FLT_MAX_EXP 128
355 * USE_IEEE: Determine if we're using IEEE floating point
357 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
358 defined(__s390x__) || defined(__powerpc__) || \
359 defined(__x86_64__) || \
360 defined(ia64) || defined(__ia64__) || \
361 defined(__hppa__) || defined(hpux) || \
362 defined(__mips) || defined(_MIPS_ARCH) || \
363 defined(__arm__) || \
364 defined(__sh__) || defined(__m32r__) || \
365 (defined(__sun) && defined(_IEEE_754)) || \
366 (defined(__alpha__) && (defined(__IEEE_FLOAT) || !defined(VMS)))
368 #define IEEE_ONE 0x3f800000
373 * START/END_FAST_MATH macros:
375 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
376 * original mode to a temporary).
377 * END_FAST_MATH: Restore x86 FPU to original mode.
379 #if defined(__GNUC__) && defined(__i386__)
381 * Set the x86 FPU control word to guarentee only 32 bits of precision
382 * are stored in registers. Allowing the FPU to store more introduces
383 * differences between situations where numbers are pulled out of memory
384 * vs. situations where the compiler is able to optimize register usage.
386 * In the worst case, we force the compiler to use a memory access to
387 * truncate the float, by specifying the 'volatile' keyword.
389 /* Hardware default: All exceptions masked, extended double precision,
390 * round to nearest (IEEE compliant):
392 #define DEFAULT_X86_FPU 0x037f
393 /* All exceptions masked, single precision, round to nearest:
395 #define FAST_X86_FPU 0x003f
396 /* The fldcw instruction will cause any pending FP exceptions to be
397 * raised prior to entering the block, and we clear any pending
398 * exceptions before exiting the block. Hence, asm code has free
399 * reign over the FPU while in the fast math block.
401 #if defined(NO_FAST_MATH)
402 #define START_FAST_MATH(x) \
404 static GLuint mask = DEFAULT_X86_FPU; \
405 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
406 __asm__ ( "fldcw %0" : : "m" (mask) ); \
409 #define START_FAST_MATH(x) \
411 static GLuint mask = FAST_X86_FPU; \
412 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
413 __asm__ ( "fldcw %0" : : "m" (mask) ); \
416 /* Restore original FPU mode, and clear any exceptions that may have
417 * occurred in the FAST_MATH block.
419 #define END_FAST_MATH(x) \
421 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
424 #elif defined(__WATCOMC__) && defined(__386__)
425 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
426 #define FAST_X86_FPU 0x003f /* See GCC comments above */
427 void _watcom_start_fast_math(unsigned short *x
,unsigned short *mask
);
428 #pragma aux _watcom_start_fast_math = \
429 "fnstcw word ptr [eax]" \
430 "fldcw word ptr [ecx]" \
433 void _watcom_end_fast_math(unsigned short *x
);
434 #pragma aux _watcom_end_fast_math = \
436 "fldcw word ptr [eax]" \
439 #if defined(NO_FAST_MATH)
440 #define START_FAST_MATH(x) \
442 static GLushort mask = DEFAULT_X86_FPU; \
443 _watcom_start_fast_math(&x,&mask); \
446 #define START_FAST_MATH(x) \
448 static GLushort mask = FAST_X86_FPU; \
449 _watcom_start_fast_math(&x,&mask); \
452 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
454 #elif defined(_MSC_VER) && defined(_M_IX86)
455 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
456 #define FAST_X86_FPU 0x003f /* See GCC comments above */
457 #if defined(NO_FAST_MATH)
458 #define START_FAST_MATH(x) do {\
459 static GLuint mask = DEFAULT_X86_FPU;\
460 __asm fnstcw word ptr [x]\
461 __asm fldcw word ptr [mask]\
464 #define START_FAST_MATH(x) do {\
465 static GLuint mask = FAST_X86_FPU;\
466 __asm fnstcw word ptr [x]\
467 __asm fldcw word ptr [mask]\
470 #define END_FAST_MATH(x) do {\
472 __asm fldcw word ptr [x]\
476 #define START_FAST_MATH(x) x = 0
477 #define END_FAST_MATH(x) (void)(x)
482 #define Elements(x) (sizeof(x)/sizeof(*(x)))
492 #endif /* COMPILER_H */