2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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 */
51 #include "c99_compat.h" /* inline, __func__, etc. */
60 * Get standard integer types
66 * Sun compilers define __i386 instead of the gcc-style __i386__
69 # if !defined(__i386__) && defined(__i386)
71 # elif !defined(__amd64__) && defined(__amd64)
73 # elif !defined(__sparc__) && defined(__sparc)
76 # if !defined(__volatile)
77 # define __volatile volatile
86 # define finite _finite
87 #elif defined(__WATCOMC__)
88 # define finite _finite
93 * Disable assorted warnings
95 #if !defined(OPENSTEP) && (defined(_WIN32) && !defined(__CYGWIN__))
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 */
110 #if defined(__WATCOMC__)
111 # pragma disable_message(201) /* Disable unreachable code warnings */
116 /* XXX: Use standard `inline` keyword instead */
118 # define INLINE inline
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.
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
132 # if (defined(__GNUC__) && __GNUC__ >= 4) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
133 # define PUBLIC __attribute__((visibility("default")))
134 # define USED __attribute__((used))
143 * __builtin_expect macros
145 #if !defined(__GNUC__)
146 # define __builtin_expect(x, y) (x)
151 # define likely(x) __builtin_expect(!!(x), 1)
152 # define unlikely(x) __builtin_expect(!!(x), 0)
154 # define likely(x) (x)
155 # define unlikely(x) (x)
159 /* XXX: Use standard `__func__` instead */
161 # define __FUNCTION__ __func__
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.
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
)
180 return (((x
& 0x000000ff) << 24) |
181 ((x
& 0x0000ff00) << 8) |
182 ((x
& 0x00ff0000) >> 8) |
183 ((x
& 0xff000000) >> 24));
185 #elif defined(__OpenBSD__)
186 #include <sys/types.h>
187 #define CPU_TO_LE32( x ) htole32( x )
189 #include <sys/endian.h>
190 #define CPU_TO_LE32( x ) bswap32( x )
192 #define MESA_BIG_ENDIAN 1
194 #define CPU_TO_LE32( x ) ( x )
195 #define MESA_LITTLE_ENDIAN 1
197 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
201 #if !defined(CAPI) && defined(_WIN32)
207 * Create a macro so that asm functions can be linked into compilers other
212 #define _ASMAPI __cdecl
216 #ifdef PTR_DECL_IN_FRONT
217 #define _ASMAPIP * _ASMAPI
219 #define _ASMAPIP _ASMAPI *
224 #define _NORMAPI _ASMAPI
225 #define _NORMAPIP _ASMAPIP
232 /* Turn off macro checking systems used by other libraries */
241 #if !defined(_WIN32_WCE)
243 # define ASSERT(X) assert(X)
251 * Static (compile-time) assertion.
252 * Basically, use COND to dimension an array. If COND is false/zero the
253 * array size will be -1 and we'll get a compilation error.
255 #define STATIC_ASSERT(COND) \
257 (void) sizeof(char [1 - 2*!(COND)]); \
262 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
264 #define PRINTFLIKE(f, a)
271 /* Used to optionally mark structures with misaligned elements or size as
272 * packed, to trade off performance for space.
275 #define PACKED __attribute__((__packed__))
283 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
285 #if !defined(__GNUC__)
288 # define LONGSTRING __extension__
293 #define M_PI (3.14159265358979323846)
297 #define M_E (2.7182818284590452354)
301 #define M_LOG2E (1.4426950408889634074)
304 #ifndef ONE_DIV_SQRT_LN2
305 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
309 #define FLT_MAX_EXP 128
314 * USE_IEEE: Determine if we're using IEEE floating point
316 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
317 defined(__s390__) || defined(__s390x__) || defined(__powerpc__) || \
318 defined(__x86_64__) || \
319 defined(__m68k__) || \
320 defined(ia64) || defined(__ia64__) || \
321 defined(__hppa__) || defined(hpux) || \
322 defined(__mips) || defined(_MIPS_ARCH) || \
323 defined(__arm__) || \
324 defined(__sh__) || defined(__m32r__) || \
325 (defined(__sun) && defined(_IEEE_754)) || \
328 #define IEEE_ONE 0x3f800000
333 * START/END_FAST_MATH macros:
335 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
336 * original mode to a temporary).
337 * END_FAST_MATH: Restore x86 FPU to original mode.
339 #if defined(__GNUC__) && defined(__i386__)
341 * Set the x86 FPU control word to guarentee only 32 bits of precision
342 * are stored in registers. Allowing the FPU to store more introduces
343 * differences between situations where numbers are pulled out of memory
344 * vs. situations where the compiler is able to optimize register usage.
346 * In the worst case, we force the compiler to use a memory access to
347 * truncate the float, by specifying the 'volatile' keyword.
349 /* Hardware default: All exceptions masked, extended double precision,
350 * round to nearest (IEEE compliant):
352 #define DEFAULT_X86_FPU 0x037f
353 /* All exceptions masked, single precision, round to nearest:
355 #define FAST_X86_FPU 0x003f
356 /* The fldcw instruction will cause any pending FP exceptions to be
357 * raised prior to entering the block, and we clear any pending
358 * exceptions before exiting the block. Hence, asm code has free
359 * reign over the FPU while in the fast math block.
361 #if defined(NO_FAST_MATH)
362 #define START_FAST_MATH(x) \
364 static GLuint mask = DEFAULT_X86_FPU; \
365 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
366 __asm__ ( "fldcw %0" : : "m" (mask) ); \
369 #define START_FAST_MATH(x) \
371 static GLuint mask = FAST_X86_FPU; \
372 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
373 __asm__ ( "fldcw %0" : : "m" (mask) ); \
376 /* Restore original FPU mode, and clear any exceptions that may have
377 * occurred in the FAST_MATH block.
379 #define END_FAST_MATH(x) \
381 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
384 #elif defined(__WATCOMC__) && defined(__386__)
385 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
386 #define FAST_X86_FPU 0x003f /* See GCC comments above */
387 void _watcom_start_fast_math(unsigned short *x
,unsigned short *mask
);
388 #pragma aux _watcom_start_fast_math = \
389 "fnstcw word ptr [eax]" \
390 "fldcw word ptr [ecx]" \
393 void _watcom_end_fast_math(unsigned short *x
);
394 #pragma aux _watcom_end_fast_math = \
396 "fldcw word ptr [eax]" \
399 #if defined(NO_FAST_MATH)
400 #define START_FAST_MATH(x) \
402 static GLushort mask = DEFAULT_X86_FPU; \
403 _watcom_start_fast_math(&x,&mask); \
406 #define START_FAST_MATH(x) \
408 static GLushort mask = FAST_X86_FPU; \
409 _watcom_start_fast_math(&x,&mask); \
412 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
414 #elif defined(_MSC_VER) && defined(_M_IX86)
415 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
416 #define FAST_X86_FPU 0x003f /* See GCC comments above */
417 #if defined(NO_FAST_MATH)
418 #define START_FAST_MATH(x) do {\
419 static GLuint mask = DEFAULT_X86_FPU;\
420 __asm fnstcw word ptr [x]\
421 __asm fldcw word ptr [mask]\
424 #define START_FAST_MATH(x) do {\
425 static GLuint mask = FAST_X86_FPU;\
426 __asm fnstcw word ptr [x]\
427 __asm fldcw word ptr [mask]\
430 #define END_FAST_MATH(x) do {\
432 __asm fldcw word ptr [x]\
436 #define START_FAST_MATH(x) x = 0
437 #define END_FAST_MATH(x) (void)(x)
442 #define Elements(x) (sizeof(x)/sizeof(*(x)))
447 * Macro function that evaluates to true if T is a trivially
448 * destructible type -- that is, if its (non-virtual) destructor
449 * performs no action and all member variables and base classes are
450 * trivially destructible themselves.
452 # if defined(__GNUC__)
453 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
454 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
456 # elif (defined(__clang__) && defined(__has_feature))
457 # if __has_feature(has_trivial_destructor)
458 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
461 # ifndef HAS_TRIVIAL_DESTRUCTOR
462 /* It's always safe (if inefficient) to assume that a
463 * destructor is non-trivial.
465 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
474 #endif /* COMPILER_H */