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.
47 #include "util/macros.h"
49 #include "c99_compat.h" /* inline, __func__, etc. */
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
89 * Disable assorted warnings
91 #if defined(_WIN32) && !defined(__CYGWIN__)
92 # if !defined(__GNUC__) /* mingw environment */
93 # pragma warning( disable : 4068 ) /* unknown pragma */
94 # pragma warning( disable : 4710 ) /* function 'foo' not inlined */
95 # pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
96 # pragma warning( disable : 4127 ) /* conditional expression is constant */
97 # if defined(MESA_MINWARN)
98 # pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
99 # pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
100 # pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
101 # pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
102 # pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
109 /* XXX: Use standard `inline` keyword instead */
111 # define INLINE inline
118 * If we build the library with gcc's -fvisibility=hidden flag, we'll
119 * use the PUBLIC macro to mark functions that are to be exported.
121 * We also need to define a USED attribute, so the optimizer doesn't
122 * inline a static function that we later use in an alias. - ajax
125 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
126 # define PUBLIC __attribute__((visibility("default")))
127 # define USED __attribute__((used))
135 /* XXX: Use standard `__func__` instead */
137 # define __FUNCTION__ __func__
141 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
142 * Do not use these unless absolutely necessary!
143 * Try to use a runtime test instead.
144 * For now, only used by some DRI hardware drivers for color/texel packing.
146 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
147 #if defined(__linux__)
148 #include <byteswap.h>
149 #define CPU_TO_LE32( x ) bswap_32( x )
150 #elif defined(__APPLE__)
151 #include <CoreFoundation/CFByteOrder.h>
152 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
153 #elif (defined(_AIX))
154 static inline GLuint
CPU_TO_LE32(GLuint x
)
156 return (((x
& 0x000000ff) << 24) |
157 ((x
& 0x0000ff00) << 8) |
158 ((x
& 0x00ff0000) >> 8) |
159 ((x
& 0xff000000) >> 24));
161 #elif defined(__OpenBSD__)
162 #include <sys/types.h>
163 #define CPU_TO_LE32( x ) htole32( x )
165 #include <sys/endian.h>
166 #define CPU_TO_LE32( x ) bswap32( x )
168 #define MESA_BIG_ENDIAN 1
170 #define CPU_TO_LE32( x ) ( x )
171 #define MESA_LITTLE_ENDIAN 1
173 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
177 #if !defined(CAPI) && defined(_WIN32)
183 * Create a macro so that asm functions can be linked into compilers other
188 #define _ASMAPI __cdecl
192 #ifdef PTR_DECL_IN_FRONT
193 #define _ASMAPIP * _ASMAPI
195 #define _ASMAPIP _ASMAPI *
200 #define _NORMAPI _ASMAPI
201 #define _NORMAPIP _ASMAPIP
208 /* Turn off macro checking systems used by other libraries */
218 # define ASSERT(X) assert(X)
225 * A trick to suppress uninitialized variable warning without generating any
228 #define uninitialized_var(x) x = x
237 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
239 #if !defined(__GNUC__)
242 # define LONGSTRING __extension__
247 #define M_PI (3.14159265358979323846)
251 #define M_E (2.7182818284590452354)
255 #define M_LOG2E (1.4426950408889634074)
258 #ifndef ONE_DIV_SQRT_LN2
259 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
263 #define FLT_MAX_EXP 128
266 #define IEEE_ONE 0x3f800000
269 * START/END_FAST_MATH macros:
271 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
272 * original mode to a temporary).
273 * END_FAST_MATH: Restore x86 FPU to original mode.
275 #if defined(__GNUC__) && defined(__i386__)
277 * Set the x86 FPU control word to guarentee only 32 bits of precision
278 * are stored in registers. Allowing the FPU to store more introduces
279 * differences between situations where numbers are pulled out of memory
280 * vs. situations where the compiler is able to optimize register usage.
282 * In the worst case, we force the compiler to use a memory access to
283 * truncate the float, by specifying the 'volatile' keyword.
285 /* Hardware default: All exceptions masked, extended double precision,
286 * round to nearest (IEEE compliant):
288 #define DEFAULT_X86_FPU 0x037f
289 /* All exceptions masked, single precision, round to nearest:
291 #define FAST_X86_FPU 0x003f
292 /* The fldcw instruction will cause any pending FP exceptions to be
293 * raised prior to entering the block, and we clear any pending
294 * exceptions before exiting the block. Hence, asm code has free
295 * reign over the FPU while in the fast math block.
297 #if defined(NO_FAST_MATH)
298 #define START_FAST_MATH(x) \
300 static GLuint mask = DEFAULT_X86_FPU; \
301 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
302 __asm__ ( "fldcw %0" : : "m" (mask) ); \
305 #define START_FAST_MATH(x) \
307 static GLuint mask = FAST_X86_FPU; \
308 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
309 __asm__ ( "fldcw %0" : : "m" (mask) ); \
312 /* Restore original FPU mode, and clear any exceptions that may have
313 * occurred in the FAST_MATH block.
315 #define END_FAST_MATH(x) \
317 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
320 #elif defined(_MSC_VER) && defined(_M_IX86)
321 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
322 #define FAST_X86_FPU 0x003f /* See GCC comments above */
323 #if defined(NO_FAST_MATH)
324 #define START_FAST_MATH(x) do {\
325 static GLuint mask = DEFAULT_X86_FPU;\
326 __asm fnstcw word ptr [x]\
327 __asm fldcw word ptr [mask]\
330 #define START_FAST_MATH(x) do {\
331 static GLuint mask = FAST_X86_FPU;\
332 __asm fnstcw word ptr [x]\
333 __asm fldcw word ptr [mask]\
336 #define END_FAST_MATH(x) do {\
338 __asm fldcw word ptr [x]\
342 #define START_FAST_MATH(x) x = 0
343 #define END_FAST_MATH(x) (void)(x)
348 #define Elements(x) (sizeof(x)/sizeof(*(x)))
356 #endif /* COMPILER_H */