mesa: remove support for GCC older than 4.1.0
[mesa.git] / src / mesa / main / compiler.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
24 */
25
26
27 /**
28 * \file compiler.h
29 * Compiler-related stuff.
30 */
31
32
33 #ifndef COMPILER_H
34 #define COMPILER_H
35
36
37 #include <assert.h>
38 #include <ctype.h>
39 #include <math.h>
40 #include <limits.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <float.h>
45 #include <stdarg.h>
46
47 #include "util/macros.h"
48
49 #include "c99_compat.h" /* inline, __func__, etc. */
50
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56
57 /**
58 * Get standard integer types
59 */
60 #include <stdint.h>
61
62
63 /**
64 * Sun compilers define __i386 instead of the gcc-style __i386__
65 */
66 #ifdef __SUNPRO_C
67 # if !defined(__i386__) && defined(__i386)
68 # define __i386__
69 # elif !defined(__amd64__) && defined(__amd64)
70 # define __amd64__
71 # elif !defined(__sparc__) && defined(__sparc)
72 # define __sparc__
73 # endif
74 # if !defined(__volatile)
75 # define __volatile volatile
76 # endif
77 #endif
78
79
80 /**
81 * finite macro.
82 */
83 #if defined(_MSC_VER)
84 # define finite _finite
85 #endif
86
87
88 /**
89 * Disable assorted warnings
90 */
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 */
103 # endif
104 # endif
105 #endif
106
107
108
109 /* XXX: Use standard `inline` keyword instead */
110 #ifndef INLINE
111 # define INLINE inline
112 #endif
113
114
115 /**
116 * PUBLIC/USED macros
117 *
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.
120 *
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
123 */
124 #ifndef PUBLIC
125 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
126 # define PUBLIC __attribute__((visibility("default")))
127 # define USED __attribute__((used))
128 # else
129 # define PUBLIC
130 # define USED
131 # endif
132 #endif
133
134
135 /* XXX: Use standard `__func__` instead */
136 #ifndef __FUNCTION__
137 # define __FUNCTION__ __func__
138 #endif
139
140 /**
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.
145 */
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)
155 {
156 return (((x & 0x000000ff) << 24) |
157 ((x & 0x0000ff00) << 8) |
158 ((x & 0x00ff0000) >> 8) |
159 ((x & 0xff000000) >> 24));
160 }
161 #elif defined(__OpenBSD__)
162 #include <sys/types.h>
163 #define CPU_TO_LE32( x ) htole32( x )
164 #else /*__linux__ */
165 #include <sys/endian.h>
166 #define CPU_TO_LE32( x ) bswap32( x )
167 #endif /*__linux__*/
168 #define MESA_BIG_ENDIAN 1
169 #else
170 #define CPU_TO_LE32( x ) ( x )
171 #define MESA_LITTLE_ENDIAN 1
172 #endif
173 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
174
175
176
177 #if !defined(CAPI) && defined(_WIN32)
178 #define CAPI _cdecl
179 #endif
180
181
182 /**
183 * Create a macro so that asm functions can be linked into compilers other
184 * than GNU C
185 */
186 #ifndef _ASMAPI
187 #if defined(_WIN32)
188 #define _ASMAPI __cdecl
189 #else
190 #define _ASMAPI
191 #endif
192 #ifdef PTR_DECL_IN_FRONT
193 #define _ASMAPIP * _ASMAPI
194 #else
195 #define _ASMAPIP _ASMAPI *
196 #endif
197 #endif
198
199 #ifdef USE_X86_ASM
200 #define _NORMAPI _ASMAPI
201 #define _NORMAPIP _ASMAPIP
202 #else
203 #define _NORMAPI
204 #define _NORMAPIP *
205 #endif
206
207
208 /* Turn off macro checking systems used by other libraries */
209 #ifdef CHECK
210 #undef CHECK
211 #endif
212
213
214 /**
215 * ASSERT macro
216 */
217 #if defined(DEBUG)
218 # define ASSERT(X) assert(X)
219 #else
220 # define ASSERT(X)
221 #endif
222
223
224 /*
225 * A trick to suppress uninitialized variable warning without generating any
226 * code
227 */
228 #define uninitialized_var(x) x = x
229
230 #ifndef NULL
231 #define NULL 0
232 #endif
233
234
235 /**
236 * LONGSTRING macro
237 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
238 */
239 #if !defined(__GNUC__)
240 # define LONGSTRING
241 #else
242 # define LONGSTRING __extension__
243 #endif
244
245
246 #ifndef M_PI
247 #define M_PI (3.14159265358979323846)
248 #endif
249
250 #ifndef M_E
251 #define M_E (2.7182818284590452354)
252 #endif
253
254 #ifndef M_LOG2E
255 #define M_LOG2E (1.4426950408889634074)
256 #endif
257
258 #ifndef ONE_DIV_SQRT_LN2
259 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
260 #endif
261
262 #ifndef FLT_MAX_EXP
263 #define FLT_MAX_EXP 128
264 #endif
265
266 #define IEEE_ONE 0x3f800000
267
268 /**
269 * START/END_FAST_MATH macros:
270 *
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.
274 */
275 #if defined(__GNUC__) && defined(__i386__)
276 /*
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.
281 *
282 * In the worst case, we force the compiler to use a memory access to
283 * truncate the float, by specifying the 'volatile' keyword.
284 */
285 /* Hardware default: All exceptions masked, extended double precision,
286 * round to nearest (IEEE compliant):
287 */
288 #define DEFAULT_X86_FPU 0x037f
289 /* All exceptions masked, single precision, round to nearest:
290 */
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.
296 */
297 #if defined(NO_FAST_MATH)
298 #define START_FAST_MATH(x) \
299 do { \
300 static GLuint mask = DEFAULT_X86_FPU; \
301 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
302 __asm__ ( "fldcw %0" : : "m" (mask) ); \
303 } while (0)
304 #else
305 #define START_FAST_MATH(x) \
306 do { \
307 static GLuint mask = FAST_X86_FPU; \
308 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
309 __asm__ ( "fldcw %0" : : "m" (mask) ); \
310 } while (0)
311 #endif
312 /* Restore original FPU mode, and clear any exceptions that may have
313 * occurred in the FAST_MATH block.
314 */
315 #define END_FAST_MATH(x) \
316 do { \
317 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
318 } while (0)
319
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]\
328 } while(0)
329 #else
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]\
334 } while(0)
335 #endif
336 #define END_FAST_MATH(x) do {\
337 __asm fnclex\
338 __asm fldcw word ptr [x]\
339 } while(0)
340
341 #else
342 #define START_FAST_MATH(x) x = 0
343 #define END_FAST_MATH(x) (void)(x)
344 #endif
345
346
347 #ifndef Elements
348 #define Elements(x) (sizeof(x)/sizeof(*(x)))
349 #endif
350
351 #ifdef __cplusplus
352 }
353 #endif
354
355
356 #endif /* COMPILER_H */