mesa: Add STRINGIFY macro.
[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 "c99_compat.h" /* inline, __func__, etc. */
48
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54
55 /**
56 * Get standard integer types
57 */
58 #include <stdint.h>
59
60
61 /**
62 * Sun compilers define __i386 instead of the gcc-style __i386__
63 */
64 #ifdef __SUNPRO_C
65 # if !defined(__i386__) && defined(__i386)
66 # define __i386__
67 # elif !defined(__amd64__) && defined(__amd64)
68 # define __amd64__
69 # elif !defined(__sparc__) && defined(__sparc)
70 # define __sparc__
71 # endif
72 # if !defined(__volatile)
73 # define __volatile volatile
74 # endif
75 #endif
76
77
78 /**
79 * finite macro.
80 */
81 #if defined(_MSC_VER)
82 # define finite _finite
83 #endif
84
85
86 /**
87 * Disable assorted warnings
88 */
89 #if defined(_WIN32) && !defined(__CYGWIN__)
90 # if !defined(__GNUC__) /* mingw environment */
91 # pragma warning( disable : 4068 ) /* unknown pragma */
92 # pragma warning( disable : 4710 ) /* function 'foo' not inlined */
93 # pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
94 # pragma warning( disable : 4127 ) /* conditional expression is constant */
95 # if defined(MESA_MINWARN)
96 # pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
97 # pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
98 # pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
99 # pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
100 # pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
101 # endif
102 # endif
103 #endif
104
105
106
107 /* XXX: Use standard `inline` keyword instead */
108 #ifndef INLINE
109 # define INLINE inline
110 #endif
111
112
113 /**
114 * PUBLIC/USED macros
115 *
116 * If we build the library with gcc's -fvisibility=hidden flag, we'll
117 * use the PUBLIC macro to mark functions that are to be exported.
118 *
119 * We also need to define a USED attribute, so the optimizer doesn't
120 * inline a static function that we later use in an alias. - ajax
121 */
122 #ifndef PUBLIC
123 # if (defined(__GNUC__) && __GNUC__ >= 4) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
124 # define PUBLIC __attribute__((visibility("default")))
125 # define USED __attribute__((used))
126 # else
127 # define PUBLIC
128 # define USED
129 # endif
130 #endif
131
132
133 /**
134 * __builtin_expect macros
135 */
136 #if !defined(__GNUC__)
137 # define __builtin_expect(x, y) (x)
138 #endif
139
140 #ifndef likely
141 # ifdef __GNUC__
142 # define likely(x) __builtin_expect(!!(x), 1)
143 # define unlikely(x) __builtin_expect(!!(x), 0)
144 # else
145 # define likely(x) (x)
146 # define unlikely(x) (x)
147 # endif
148 #endif
149
150 /* XXX: Use standard `__func__` instead */
151 #ifndef __FUNCTION__
152 # define __FUNCTION__ __func__
153 #endif
154
155 /**
156 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
157 * Do not use these unless absolutely necessary!
158 * Try to use a runtime test instead.
159 * For now, only used by some DRI hardware drivers for color/texel packing.
160 */
161 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
162 #if defined(__linux__)
163 #include <byteswap.h>
164 #define CPU_TO_LE32( x ) bswap_32( x )
165 #elif defined(__APPLE__)
166 #include <CoreFoundation/CFByteOrder.h>
167 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
168 #elif (defined(_AIX) || defined(__blrts))
169 static INLINE GLuint CPU_TO_LE32(GLuint x)
170 {
171 return (((x & 0x000000ff) << 24) |
172 ((x & 0x0000ff00) << 8) |
173 ((x & 0x00ff0000) >> 8) |
174 ((x & 0xff000000) >> 24));
175 }
176 #elif defined(__OpenBSD__)
177 #include <sys/types.h>
178 #define CPU_TO_LE32( x ) htole32( x )
179 #else /*__linux__ */
180 #include <sys/endian.h>
181 #define CPU_TO_LE32( x ) bswap32( x )
182 #endif /*__linux__*/
183 #define MESA_BIG_ENDIAN 1
184 #else
185 #define CPU_TO_LE32( x ) ( x )
186 #define MESA_LITTLE_ENDIAN 1
187 #endif
188 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
189
190
191
192 #if !defined(CAPI) && defined(_WIN32)
193 #define CAPI _cdecl
194 #endif
195
196
197 /**
198 * Create a macro so that asm functions can be linked into compilers other
199 * than GNU C
200 */
201 #ifndef _ASMAPI
202 #if defined(_WIN32)
203 #define _ASMAPI __cdecl
204 #else
205 #define _ASMAPI
206 #endif
207 #ifdef PTR_DECL_IN_FRONT
208 #define _ASMAPIP * _ASMAPI
209 #else
210 #define _ASMAPIP _ASMAPI *
211 #endif
212 #endif
213
214 #ifdef USE_X86_ASM
215 #define _NORMAPI _ASMAPI
216 #define _NORMAPIP _ASMAPIP
217 #else
218 #define _NORMAPI
219 #define _NORMAPIP *
220 #endif
221
222
223 /* Turn off macro checking systems used by other libraries */
224 #ifdef CHECK
225 #undef CHECK
226 #endif
227
228
229 /**
230 * ASSERT macro
231 */
232 #if !defined(_WIN32_WCE)
233 #if defined(DEBUG)
234 # define ASSERT(X) assert(X)
235 #else
236 # define ASSERT(X)
237 #endif
238 #endif
239
240
241 /**
242 * Static (compile-time) assertion.
243 * Basically, use COND to dimension an array. If COND is false/zero the
244 * array size will be -1 and we'll get a compilation error.
245 */
246 #define STATIC_ASSERT(COND) \
247 do { \
248 (void) sizeof(char [1 - 2*!(COND)]); \
249 } while (0)
250
251 /**
252 * Unreachable macro. Useful for suppressing "control reaches end of non-void
253 * function" warnings.
254 */
255 #if __GNUC__ >= 4 && __GNUC_MINOR__ >= 5
256 #define unreachable() __builtin_unreachable()
257 #elif (defined(__clang__) && defined(__has_builtin))
258 # if __has_builtin(__builtin_unreachable)
259 # define unreachable() __builtin_unreachable()
260 # endif
261 #endif
262
263 #ifndef unreachable
264 #define unreachable()
265 #endif
266
267 #if (__GNUC__ >= 3)
268 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
269 #else
270 #define PRINTFLIKE(f, a)
271 #endif
272
273 #ifndef NULL
274 #define NULL 0
275 #endif
276
277 /* Used to optionally mark structures with misaligned elements or size as
278 * packed, to trade off performance for space.
279 */
280 #if (__GNUC__ >= 3)
281 #define PACKED __attribute__((__packed__))
282 #else
283 #define PACKED
284 #endif
285
286
287 /**
288 * LONGSTRING macro
289 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
290 */
291 #if !defined(__GNUC__)
292 # define LONGSTRING
293 #else
294 # define LONGSTRING __extension__
295 #endif
296
297
298 #ifndef M_PI
299 #define M_PI (3.14159265358979323846)
300 #endif
301
302 #ifndef M_E
303 #define M_E (2.7182818284590452354)
304 #endif
305
306 #ifndef M_LOG2E
307 #define M_LOG2E (1.4426950408889634074)
308 #endif
309
310 #ifndef ONE_DIV_SQRT_LN2
311 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
312 #endif
313
314 #ifndef FLT_MAX_EXP
315 #define FLT_MAX_EXP 128
316 #endif
317
318
319 /**
320 * USE_IEEE: Determine if we're using IEEE floating point
321 */
322 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
323 defined(__s390__) || defined(__s390x__) || defined(__powerpc__) || \
324 defined(__x86_64__) || \
325 defined(__m68k__) || \
326 defined(ia64) || defined(__ia64__) || \
327 defined(__hppa__) || defined(hpux) || \
328 defined(__mips) || defined(_MIPS_ARCH) || \
329 defined(__arm__) || defined(__aarch64__) || \
330 defined(__sh__) || defined(__m32r__) || \
331 (defined(__sun) && defined(_IEEE_754)) || \
332 defined(__alpha__)
333 #define USE_IEEE
334 #define IEEE_ONE 0x3f800000
335 #endif
336
337
338 /**
339 * START/END_FAST_MATH macros:
340 *
341 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
342 * original mode to a temporary).
343 * END_FAST_MATH: Restore x86 FPU to original mode.
344 */
345 #if defined(__GNUC__) && defined(__i386__)
346 /*
347 * Set the x86 FPU control word to guarentee only 32 bits of precision
348 * are stored in registers. Allowing the FPU to store more introduces
349 * differences between situations where numbers are pulled out of memory
350 * vs. situations where the compiler is able to optimize register usage.
351 *
352 * In the worst case, we force the compiler to use a memory access to
353 * truncate the float, by specifying the 'volatile' keyword.
354 */
355 /* Hardware default: All exceptions masked, extended double precision,
356 * round to nearest (IEEE compliant):
357 */
358 #define DEFAULT_X86_FPU 0x037f
359 /* All exceptions masked, single precision, round to nearest:
360 */
361 #define FAST_X86_FPU 0x003f
362 /* The fldcw instruction will cause any pending FP exceptions to be
363 * raised prior to entering the block, and we clear any pending
364 * exceptions before exiting the block. Hence, asm code has free
365 * reign over the FPU while in the fast math block.
366 */
367 #if defined(NO_FAST_MATH)
368 #define START_FAST_MATH(x) \
369 do { \
370 static GLuint mask = DEFAULT_X86_FPU; \
371 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
372 __asm__ ( "fldcw %0" : : "m" (mask) ); \
373 } while (0)
374 #else
375 #define START_FAST_MATH(x) \
376 do { \
377 static GLuint mask = FAST_X86_FPU; \
378 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
379 __asm__ ( "fldcw %0" : : "m" (mask) ); \
380 } while (0)
381 #endif
382 /* Restore original FPU mode, and clear any exceptions that may have
383 * occurred in the FAST_MATH block.
384 */
385 #define END_FAST_MATH(x) \
386 do { \
387 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
388 } while (0)
389
390 #elif defined(_MSC_VER) && defined(_M_IX86)
391 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
392 #define FAST_X86_FPU 0x003f /* See GCC comments above */
393 #if defined(NO_FAST_MATH)
394 #define START_FAST_MATH(x) do {\
395 static GLuint mask = DEFAULT_X86_FPU;\
396 __asm fnstcw word ptr [x]\
397 __asm fldcw word ptr [mask]\
398 } while(0)
399 #else
400 #define START_FAST_MATH(x) do {\
401 static GLuint mask = FAST_X86_FPU;\
402 __asm fnstcw word ptr [x]\
403 __asm fldcw word ptr [mask]\
404 } while(0)
405 #endif
406 #define END_FAST_MATH(x) do {\
407 __asm fnclex\
408 __asm fldcw word ptr [x]\
409 } while(0)
410
411 #else
412 #define START_FAST_MATH(x) x = 0
413 #define END_FAST_MATH(x) (void)(x)
414 #endif
415
416
417 #ifndef Elements
418 #define Elements(x) (sizeof(x)/sizeof(*(x)))
419 #endif
420
421 #ifdef __cplusplus
422 /**
423 * Macro function that evaluates to true if T is a trivially
424 * destructible type -- that is, if its (non-virtual) destructor
425 * performs no action and all member variables and base classes are
426 * trivially destructible themselves.
427 */
428 # if defined(__GNUC__)
429 # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
430 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
431 # endif
432 # elif (defined(__clang__) && defined(__has_feature))
433 # if __has_feature(has_trivial_destructor)
434 # define HAS_TRIVIAL_DESTRUCTOR(T) __has_trivial_destructor(T)
435 # endif
436 # endif
437 # ifndef HAS_TRIVIAL_DESTRUCTOR
438 /* It's always safe (if inefficient) to assume that a
439 * destructor is non-trivial.
440 */
441 # define HAS_TRIVIAL_DESTRUCTOR(T) (false)
442 # endif
443 #endif
444
445 #ifdef __cplusplus
446 }
447 #endif
448
449
450 #endif /* COMPILER_H */