mesa: change MAX_PROGRAM_ADDRESS_REGS to 1, clamp to it in state tracker
[mesa.git] / src / mesa / main / compiler.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
25 */
26
27
28 /**
29 * \file compiler.h
30 * Compiler-related stuff.
31 */
32
33
34 #ifndef COMPILER_H
35 #define COMPILER_H
36
37
38 #include <assert.h>
39 #include <ctype.h>
40 #if defined(__alpha__) && defined(CCPML)
41 #include <cpml.h> /* use Compaq's Fast Math Library on Alpha */
42 #else
43 #include <math.h>
44 #endif
45 #include <limits.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <float.h>
50 #include <stdarg.h>
51
52 #include "c99_compat.h" /* inline, __func__, etc. */
53
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59
60 /**
61 * Get standard integer types
62 */
63 #include <stdint.h>
64
65
66 /**
67 * Sun compilers define __i386 instead of the gcc-style __i386__
68 */
69 #ifdef __SUNPRO_C
70 # if !defined(__i386__) && defined(__i386)
71 # define __i386__
72 # elif !defined(__amd64__) && defined(__amd64)
73 # define __amd64__
74 # elif !defined(__sparc__) && defined(__sparc)
75 # define __sparc__
76 # endif
77 # if !defined(__volatile)
78 # define __volatile volatile
79 # endif
80 #endif
81
82
83 /**
84 * finite macro.
85 */
86 #if defined(_MSC_VER)
87 # define finite _finite
88 #elif defined(__WATCOMC__)
89 # define finite _finite
90 #endif
91
92
93 /**
94 * Disable assorted warnings
95 */
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 */
108 # endif
109 # endif
110 #endif
111 #if defined(__WATCOMC__)
112 # pragma disable_message(201) /* Disable unreachable code warnings */
113 #endif
114
115
116
117 /* XXX: Use standard `inline` keyword instead */
118 #ifndef INLINE
119 # define INLINE inline
120 #endif
121
122
123 /**
124 * PUBLIC/USED macros
125 *
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.
128 *
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
131 */
132 #ifndef PUBLIC
133 # if (defined(__GNUC__) && __GNUC__ >= 4) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
134 # define PUBLIC __attribute__((visibility("default")))
135 # define USED __attribute__((used))
136 # else
137 # define PUBLIC
138 # define USED
139 # endif
140 #endif
141
142
143 /**
144 * __builtin_expect macros
145 */
146 #if !defined(__GNUC__)
147 # define __builtin_expect(x, y) (x)
148 #endif
149
150 #ifndef likely
151 # ifdef __GNUC__
152 # define likely(x) __builtin_expect(!!(x), 1)
153 # define unlikely(x) __builtin_expect(!!(x), 0)
154 # else
155 # define likely(x) (x)
156 # define unlikely(x) (x)
157 # endif
158 #endif
159
160 /* XXX: Use standard `__func__` instead */
161 #ifndef __FUNCTION__
162 # define __FUNCTION__ __func__
163 #endif
164
165 /**
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.
170 */
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)
180 {
181 return (((x & 0x000000ff) << 24) |
182 ((x & 0x0000ff00) << 8) |
183 ((x & 0x00ff0000) >> 8) |
184 ((x & 0xff000000) >> 24));
185 }
186 #elif defined(__OpenBSD__)
187 #include <sys/types.h>
188 #define CPU_TO_LE32( x ) htole32( x )
189 #else /*__linux__ */
190 #include <sys/endian.h>
191 #define CPU_TO_LE32( x ) bswap32( x )
192 #endif /*__linux__*/
193 #define MESA_BIG_ENDIAN 1
194 #else
195 #define CPU_TO_LE32( x ) ( x )
196 #define MESA_LITTLE_ENDIAN 1
197 #endif
198 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
199
200
201
202 #if !defined(CAPI) && defined(_WIN32) && !defined(BUILD_FOR_SNAP)
203 #define CAPI _cdecl
204 #endif
205
206
207 /**
208 * Create a macro so that asm functions can be linked into compilers other
209 * than GNU C
210 */
211 #ifndef _ASMAPI
212 #if defined(_WIN32) && !defined(BUILD_FOR_SNAP)/* was: !defined( __GNUC__ ) && !defined( VMS ) && !defined( __INTEL_COMPILER )*/
213 #define _ASMAPI __cdecl
214 #else
215 #define _ASMAPI
216 #endif
217 #ifdef PTR_DECL_IN_FRONT
218 #define _ASMAPIP * _ASMAPI
219 #else
220 #define _ASMAPIP _ASMAPI *
221 #endif
222 #endif
223
224 #ifdef USE_X86_ASM
225 #define _NORMAPI _ASMAPI
226 #define _NORMAPIP _ASMAPIP
227 #else
228 #define _NORMAPI
229 #define _NORMAPIP *
230 #endif
231
232
233 /* Turn off macro checking systems used by other libraries */
234 #ifdef CHECK
235 #undef CHECK
236 #endif
237
238
239 /**
240 * ASSERT macro
241 */
242 #if !defined(_WIN32_WCE)
243 #if defined(BUILD_FOR_SNAP) && defined(CHECKED)
244 # define ASSERT(X) _CHECK(X)
245 #elif defined(DEBUG)
246 # define ASSERT(X) assert(X)
247 #else
248 # define ASSERT(X)
249 #endif
250 #endif
251
252
253 /**
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.
257 */
258 #define STATIC_ASSERT(COND) \
259 do { \
260 (void) sizeof(char [1 - 2*!(COND)]); \
261 } while (0)
262
263
264 #if (__GNUC__ >= 3)
265 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
266 #else
267 #define PRINTFLIKE(f, a)
268 #endif
269
270 #ifndef NULL
271 #define NULL 0
272 #endif
273
274
275 /**
276 * LONGSTRING macro
277 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
278 */
279 #if !defined(__GNUC__)
280 # define LONGSTRING
281 #else
282 # define LONGSTRING __extension__
283 #endif
284
285
286 #ifndef M_PI
287 #define M_PI (3.14159265358979323846)
288 #endif
289
290 #ifndef M_E
291 #define M_E (2.7182818284590452354)
292 #endif
293
294 #ifndef M_LOG2E
295 #define M_LOG2E (1.4426950408889634074)
296 #endif
297
298 #ifndef ONE_DIV_SQRT_LN2
299 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
300 #endif
301
302 #ifndef FLT_MAX_EXP
303 #define FLT_MAX_EXP 128
304 #endif
305
306
307 /**
308 * USE_IEEE: Determine if we're using IEEE floating point
309 */
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))
321 #define USE_IEEE
322 #define IEEE_ONE 0x3f800000
323 #endif
324
325
326 /**
327 * START/END_FAST_MATH macros:
328 *
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.
332 */
333 #if defined(__GNUC__) && defined(__i386__)
334 /*
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.
339 *
340 * In the worst case, we force the compiler to use a memory access to
341 * truncate the float, by specifying the 'volatile' keyword.
342 */
343 /* Hardware default: All exceptions masked, extended double precision,
344 * round to nearest (IEEE compliant):
345 */
346 #define DEFAULT_X86_FPU 0x037f
347 /* All exceptions masked, single precision, round to nearest:
348 */
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.
354 */
355 #if defined(NO_FAST_MATH)
356 #define START_FAST_MATH(x) \
357 do { \
358 static GLuint mask = DEFAULT_X86_FPU; \
359 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
360 __asm__ ( "fldcw %0" : : "m" (mask) ); \
361 } while (0)
362 #else
363 #define START_FAST_MATH(x) \
364 do { \
365 static GLuint mask = FAST_X86_FPU; \
366 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
367 __asm__ ( "fldcw %0" : : "m" (mask) ); \
368 } while (0)
369 #endif
370 /* Restore original FPU mode, and clear any exceptions that may have
371 * occurred in the FAST_MATH block.
372 */
373 #define END_FAST_MATH(x) \
374 do { \
375 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
376 } while (0)
377
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]" \
385 parm [eax] [ecx] \
386 modify exact [];
387 void _watcom_end_fast_math(unsigned short *x);
388 #pragma aux _watcom_end_fast_math = \
389 "fnclex" \
390 "fldcw word ptr [eax]" \
391 parm [eax] \
392 modify exact [];
393 #if defined(NO_FAST_MATH)
394 #define START_FAST_MATH(x) \
395 do { \
396 static GLushort mask = DEFAULT_X86_FPU; \
397 _watcom_start_fast_math(&x,&mask); \
398 } while (0)
399 #else
400 #define START_FAST_MATH(x) \
401 do { \
402 static GLushort mask = FAST_X86_FPU; \
403 _watcom_start_fast_math(&x,&mask); \
404 } while (0)
405 #endif
406 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
407
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]\
416 } while(0)
417 #else
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]\
422 } while(0)
423 #endif
424 #define END_FAST_MATH(x) do {\
425 __asm fnclex\
426 __asm fldcw word ptr [x]\
427 } while(0)
428
429 #else
430 #define START_FAST_MATH(x) x = 0
431 #define END_FAST_MATH(x) (void)(x)
432 #endif
433
434
435 #ifndef Elements
436 #define Elements(x) (sizeof(x)/sizeof(*(x)))
437 #endif
438
439
440
441 #ifdef __cplusplus
442 }
443 #endif
444
445
446 #endif /* COMPILER_H */