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