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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
30 * Compiler-related stuff.
40 #if defined(__alpha__) && defined(CCPML)
41 #include <cpml.h> /* use Compaq's Fast Math Library on Alpha */
52 #include "c99_compat.h" /* inline, __func__, etc. */
61 * Get standard integer types
67 * Sun compilers define __i386 instead of the gcc-style __i386__
70 # if !defined(__i386__) && defined(__i386)
72 # elif !defined(__amd64__) && defined(__amd64)
74 # elif !defined(__sparc__) && defined(__sparc)
77 # if !defined(__volatile)
78 # define __volatile volatile
87 # define finite _finite
88 #elif defined(__WATCOMC__)
89 # define finite _finite
94 * Disable assorted warnings
96 #if !defined(OPENSTEP) && (defined(_WIN32) && !defined(__CYGWIN__)) && !defined(BUILD_FOR_SNAP)
97 # if !defined(__GNUC__) /* mingw environment */
98 # pragma warning( disable : 4068 ) /* unknown pragma */
99 # pragma warning( disable : 4710 ) /* function 'foo' not inlined */
100 # pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
101 # pragma warning( disable : 4127 ) /* conditional expression is constant */
102 # if defined(MESA_MINWARN)
103 # pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
104 # pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
105 # pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
106 # pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
107 # pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
111 #if defined(__WATCOMC__)
112 # pragma disable_message(201) /* Disable unreachable code warnings */
117 /* XXX: Use standard `inline` keyword instead */
119 # define INLINE inline
126 * If we build the library with gcc's -fvisibility=hidden flag, we'll
127 * use the PUBLIC macro to mark functions that are to be exported.
129 * We also need to define a USED attribute, so the optimizer doesn't
130 * inline a static function that we later use in an alias. - ajax
133 # if (defined(__GNUC__) && __GNUC__ >= 4) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
134 # define PUBLIC __attribute__((visibility("default")))
135 # define USED __attribute__((used))
144 * __builtin_expect macros
146 #if !defined(__GNUC__)
147 # define __builtin_expect(x, y) (x)
152 # define likely(x) __builtin_expect(!!(x), 1)
153 # define unlikely(x) __builtin_expect(!!(x), 0)
155 # define likely(x) (x)
156 # define unlikely(x) (x)
160 /* XXX: Use standard `__func__` instead */
162 # define __FUNCTION__ __func__
166 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
167 * Do not use these unless absolutely necessary!
168 * Try to use a runtime test instead.
169 * For now, only used by some DRI hardware drivers for color/texel packing.
171 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
172 #if defined(__linux__)
173 #include <byteswap.h>
174 #define CPU_TO_LE32( x ) bswap_32( x )
175 #elif defined(__APPLE__)
176 #include <CoreFoundation/CFByteOrder.h>
177 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
178 #elif (defined(_AIX) || defined(__blrts))
179 static INLINE GLuint
CPU_TO_LE32(GLuint x
)
181 return (((x
& 0x000000ff) << 24) |
182 ((x
& 0x0000ff00) << 8) |
183 ((x
& 0x00ff0000) >> 8) |
184 ((x
& 0xff000000) >> 24));
186 #elif defined(__OpenBSD__)
187 #include <sys/types.h>
188 #define CPU_TO_LE32( x ) htole32( x )
190 #include <sys/endian.h>
191 #define CPU_TO_LE32( x ) bswap32( x )
193 #define MESA_BIG_ENDIAN 1
195 #define CPU_TO_LE32( x ) ( x )
196 #define MESA_LITTLE_ENDIAN 1
198 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
202 #if !defined(CAPI) && defined(_WIN32) && !defined(BUILD_FOR_SNAP)
208 * Create a macro so that asm functions can be linked into compilers other
212 #if defined(_WIN32) && !defined(BUILD_FOR_SNAP)/* was: !defined( __GNUC__ ) && !defined( VMS ) && !defined( __INTEL_COMPILER )*/
213 #define _ASMAPI __cdecl
217 #ifdef PTR_DECL_IN_FRONT
218 #define _ASMAPIP * _ASMAPI
220 #define _ASMAPIP _ASMAPI *
225 #define _NORMAPI _ASMAPI
226 #define _NORMAPIP _ASMAPIP
233 /* Turn off macro checking systems used by other libraries */
242 #if !defined(_WIN32_WCE)
243 #if defined(BUILD_FOR_SNAP) && defined(CHECKED)
244 # define ASSERT(X) _CHECK(X)
246 # define ASSERT(X) assert(X)
254 * Static (compile-time) assertion.
255 * Basically, use COND to dimension an array. If COND is false/zero the
256 * array size will be -1 and we'll get a compilation error.
258 #define STATIC_ASSERT(COND) \
260 (void) sizeof(char [1 - 2*!(COND)]); \
265 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
267 #define PRINTFLIKE(f, a)
277 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
279 #if !defined(__GNUC__)
282 # define LONGSTRING __extension__
287 #define M_PI (3.14159265358979323846)
291 #define M_E (2.7182818284590452354)
295 #define M_LOG2E (1.4426950408889634074)
298 #ifndef ONE_DIV_SQRT_LN2
299 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
303 #define FLT_MAX_EXP 128
308 * USE_IEEE: Determine if we're using IEEE floating point
310 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
311 defined(__s390__) || defined(__s390x__) || defined(__powerpc__) || \
312 defined(__x86_64__) || \
313 defined(__m68k__) || \
314 defined(ia64) || defined(__ia64__) || \
315 defined(__hppa__) || defined(hpux) || \
316 defined(__mips) || defined(_MIPS_ARCH) || \
317 defined(__arm__) || \
318 defined(__sh__) || defined(__m32r__) || \
319 (defined(__sun) && defined(_IEEE_754)) || \
320 (defined(__alpha__) && defined(__IEEE_FLOAT))
322 #define IEEE_ONE 0x3f800000
327 * START/END_FAST_MATH macros:
329 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
330 * original mode to a temporary).
331 * END_FAST_MATH: Restore x86 FPU to original mode.
333 #if defined(__GNUC__) && defined(__i386__)
335 * Set the x86 FPU control word to guarentee only 32 bits of precision
336 * are stored in registers. Allowing the FPU to store more introduces
337 * differences between situations where numbers are pulled out of memory
338 * vs. situations where the compiler is able to optimize register usage.
340 * In the worst case, we force the compiler to use a memory access to
341 * truncate the float, by specifying the 'volatile' keyword.
343 /* Hardware default: All exceptions masked, extended double precision,
344 * round to nearest (IEEE compliant):
346 #define DEFAULT_X86_FPU 0x037f
347 /* All exceptions masked, single precision, round to nearest:
349 #define FAST_X86_FPU 0x003f
350 /* The fldcw instruction will cause any pending FP exceptions to be
351 * raised prior to entering the block, and we clear any pending
352 * exceptions before exiting the block. Hence, asm code has free
353 * reign over the FPU while in the fast math block.
355 #if defined(NO_FAST_MATH)
356 #define START_FAST_MATH(x) \
358 static GLuint mask = DEFAULT_X86_FPU; \
359 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
360 __asm__ ( "fldcw %0" : : "m" (mask) ); \
363 #define START_FAST_MATH(x) \
365 static GLuint mask = FAST_X86_FPU; \
366 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
367 __asm__ ( "fldcw %0" : : "m" (mask) ); \
370 /* Restore original FPU mode, and clear any exceptions that may have
371 * occurred in the FAST_MATH block.
373 #define END_FAST_MATH(x) \
375 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
378 #elif defined(__WATCOMC__) && defined(__386__)
379 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
380 #define FAST_X86_FPU 0x003f /* See GCC comments above */
381 void _watcom_start_fast_math(unsigned short *x
,unsigned short *mask
);
382 #pragma aux _watcom_start_fast_math = \
383 "fnstcw word ptr [eax]" \
384 "fldcw word ptr [ecx]" \
387 void _watcom_end_fast_math(unsigned short *x
);
388 #pragma aux _watcom_end_fast_math = \
390 "fldcw word ptr [eax]" \
393 #if defined(NO_FAST_MATH)
394 #define START_FAST_MATH(x) \
396 static GLushort mask = DEFAULT_X86_FPU; \
397 _watcom_start_fast_math(&x,&mask); \
400 #define START_FAST_MATH(x) \
402 static GLushort mask = FAST_X86_FPU; \
403 _watcom_start_fast_math(&x,&mask); \
406 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
408 #elif defined(_MSC_VER) && defined(_M_IX86)
409 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
410 #define FAST_X86_FPU 0x003f /* See GCC comments above */
411 #if defined(NO_FAST_MATH)
412 #define START_FAST_MATH(x) do {\
413 static GLuint mask = DEFAULT_X86_FPU;\
414 __asm fnstcw word ptr [x]\
415 __asm fldcw word ptr [mask]\
418 #define START_FAST_MATH(x) do {\
419 static GLuint mask = FAST_X86_FPU;\
420 __asm fnstcw word ptr [x]\
421 __asm fldcw word ptr [mask]\
424 #define END_FAST_MATH(x) do {\
426 __asm fldcw word ptr [x]\
430 #define START_FAST_MATH(x) x = 0
431 #define END_FAST_MATH(x) (void)(x)
436 #define Elements(x) (sizeof(x)/sizeof(*(x)))
446 #endif /* COMPILER_H */