compiler.h revision 6d9a9e57dc312a2f9f09a6b826a2de93fab5ae26
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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#if defined(__linux__) && defined(__i386__)
49#include <fpu_control.h>
50#endif
51#include <float.h>
52#include <stdarg.h>
53
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59
60/**
61 * Get standard integer types
62 */
63#if defined(_MSC_VER)
64   typedef __int8             int8_t;
65   typedef unsigned __int8    uint8_t;
66   typedef __int16            int16_t;
67   typedef unsigned __int16   uint16_t;
68#  ifndef __eglplatform_h_
69     typedef __int32            int32_t;
70#  endif
71   typedef unsigned __int32   uint32_t;
72   typedef __int64            int64_t;
73   typedef unsigned __int64   uint64_t;
74
75#  if defined(_WIN64)
76     typedef __int64            intptr_t;
77     typedef unsigned __int64   uintptr_t;
78#  else
79     typedef __int32            intptr_t;
80     typedef unsigned __int32   uintptr_t;
81#  endif
82
83#  define INT64_C(__val) __val##i64
84#  define UINT64_C(__val) __val##ui64
85#else
86#  include <stdint.h>
87#endif
88
89
90/**
91  * Sun compilers define __i386 instead of the gcc-style __i386__
92 */
93#ifdef __SUNPRO_C
94# if !defined(__i386__) && defined(__i386)
95#  define __i386__
96# elif !defined(__amd64__) && defined(__amd64)
97#  define __amd64__
98# elif !defined(__sparc__) && defined(__sparc)
99#  define __sparc__
100# endif
101# if !defined(__volatile)
102#  define __volatile volatile
103# endif
104#endif
105
106
107/**
108 * finite macro.
109 */
110#if defined(_MSC_VER)
111#  define finite _finite
112#elif defined(__WATCOMC__)
113#  define finite _finite
114#endif
115
116
117/**
118 * Disable assorted warnings
119 */
120#if !defined(OPENSTEP) && (defined(__WIN32__) && !defined(__CYGWIN__)) && !defined(BUILD_FOR_SNAP)
121#  if !defined(__GNUC__) /* mingw environment */
122#    pragma warning( disable : 4068 ) /* unknown pragma */
123#    pragma warning( disable : 4710 ) /* function 'foo' not inlined */
124#    pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
125#    pragma warning( disable : 4127 ) /* conditional expression is constant */
126#    if defined(MESA_MINWARN)
127#      pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
128#      pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
129#      pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
130#      pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
131#      pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
132#    endif
133#  endif
134#endif
135#if defined(__WATCOMC__)
136#  pragma disable_message(201) /* Disable unreachable code warnings */
137#endif
138
139
140
141/**
142 * Function inlining
143 */
144#if defined(__GNUC__)
145#  define INLINE __inline__
146#elif defined(__MSC__)
147#  define INLINE __inline
148#elif defined(_MSC_VER)
149#  define INLINE __inline
150#elif defined(__ICL)
151#  define INLINE __inline
152#elif defined(__INTEL_COMPILER)
153#  define INLINE inline
154#elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
155#  define INLINE __inline
156#elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
157#  define INLINE inline
158#  define __inline inline
159#  define __inline__ inline
160#elif (__STDC_VERSION__ >= 199901L) /* C99 */
161#  define INLINE inline
162#else
163#  define INLINE
164#endif
165
166
167/**
168 * PUBLIC/USED macros
169 *
170 * If we build the library with gcc's -fvisibility=hidden flag, we'll
171 * use the PUBLIC macro to mark functions that are to be exported.
172 *
173 * We also need to define a USED attribute, so the optimizer doesn't
174 * inline a static function that we later use in an alias. - ajax
175 */
176#if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
177#  define PUBLIC __attribute__((visibility("default")))
178#  define USED __attribute__((used))
179#else
180#  define PUBLIC
181#  define USED
182#endif
183
184
185/**
186 * Some compilers don't like some of Mesa's const usage.  In those places use
187 * CONST instead of const.  Pass -DNO_CONST to compilers where this matters.
188 */
189#ifdef NO_CONST
190#  define CONST
191#else
192#  define CONST const
193#endif
194
195
196/**
197 * __builtin_expect macros
198 */
199#if !defined(__GNUC__)
200#  define __builtin_expect(x, y) x
201#endif
202
203
204/**
205 * The __FUNCTION__ gcc variable is generally only used for debugging.
206 * If we're not using gcc, define __FUNCTION__ as a cpp symbol here.
207 * Don't define it if using a newer Windows compiler.
208 */
209#ifndef __FUNCTION__
210# if defined(__VMS)
211#  define __FUNCTION__ "VMS$NL:"
212# elif !defined(__GNUC__) && !defined(__xlC__) &&	\
213      (!defined(_MSC_VER) || _MSC_VER < 1300)
214#  if (__STDC_VERSION__ >= 199901L) /* C99 */ || \
215    (defined(__SUNPRO_C) && defined(__C99FEATURES__))
216#   define __FUNCTION__ __func__
217#  else
218#   define __FUNCTION__ "<unknown>"
219#  endif
220# endif
221#endif
222
223
224/**
225 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
226 * Do not use these unless absolutely necessary!
227 * Try to use a runtime test instead.
228 * For now, only used by some DRI hardware drivers for color/texel packing.
229 */
230#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
231#if defined(__linux__)
232#include <byteswap.h>
233#define CPU_TO_LE32( x )	bswap_32( x )
234#elif defined(__APPLE__)
235#include <CoreFoundation/CFByteOrder.h>
236#define CPU_TO_LE32( x )	CFSwapInt32HostToLittle( x )
237#elif (defined(_AIX) || defined(__blrts))
238static INLINE GLuint CPU_TO_LE32(GLuint x)
239{
240   return (((x & 0x000000ff) << 24) |
241           ((x & 0x0000ff00) <<  8) |
242           ((x & 0x00ff0000) >>  8) |
243           ((x & 0xff000000) >> 24));
244}
245#else /*__linux__ */
246#include <sys/endian.h>
247#define CPU_TO_LE32( x )	bswap32( x )
248#endif /*__linux__*/
249#define MESA_BIG_ENDIAN 1
250#else
251#define CPU_TO_LE32( x )	( x )
252#define MESA_LITTLE_ENDIAN 1
253#endif
254#define LE32_TO_CPU( x )	CPU_TO_LE32( x )
255
256
257
258#if !defined(CAPI) && defined(WIN32) && !defined(BUILD_FOR_SNAP)
259#define CAPI _cdecl
260#endif
261
262
263/**
264 * Create a macro so that asm functions can be linked into compilers other
265 * than GNU C
266 */
267#ifndef _ASMAPI
268#if defined(WIN32) && !defined(BUILD_FOR_SNAP)/* was: !defined( __GNUC__ ) && !defined( VMS ) && !defined( __INTEL_COMPILER )*/
269#define _ASMAPI __cdecl
270#else
271#define _ASMAPI
272#endif
273#ifdef	PTR_DECL_IN_FRONT
274#define	_ASMAPIP * _ASMAPI
275#else
276#define	_ASMAPIP _ASMAPI *
277#endif
278#endif
279
280#ifdef USE_X86_ASM
281#define _NORMAPI _ASMAPI
282#define _NORMAPIP _ASMAPIP
283#else
284#define _NORMAPI
285#define _NORMAPIP *
286#endif
287
288
289/* This is a macro on IRIX */
290#ifdef _P
291#undef _P
292#endif
293
294
295/* Turn off macro checking systems used by other libraries */
296#ifdef CHECK
297#undef CHECK
298#endif
299
300
301/**
302 * ASSERT macro
303 */
304#if !defined(_WIN32_WCE)
305#if defined(BUILD_FOR_SNAP) && defined(CHECKED)
306#  define ASSERT(X)   _CHECK(X)
307#elif defined(DEBUG)
308#  define ASSERT(X)   assert(X)
309#else
310#  define ASSERT(X)
311#endif
312#endif
313
314
315#ifndef NULL
316#define NULL 0
317#endif
318
319
320/**
321 * LONGSTRING macro
322 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
323 */
324#if !defined(__GNUC__)
325# define LONGSTRING
326#else
327# define LONGSTRING __extension__
328#endif
329
330
331#ifndef M_PI
332#define M_PI (3.1415926536)
333#endif
334
335#ifndef M_E
336#define M_E (2.7182818284590452354)
337#endif
338
339#ifndef ONE_DIV_LN2
340#define ONE_DIV_LN2 (1.442695040888963456)
341#endif
342
343#ifndef ONE_DIV_SQRT_LN2
344#define ONE_DIV_SQRT_LN2 (1.201122408786449815)
345#endif
346
347#ifndef FLT_MAX_EXP
348#define FLT_MAX_EXP 128
349#endif
350
351
352/**
353 * USE_IEEE: Determine if we're using IEEE floating point
354 */
355#if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
356    defined(__s390x__) || defined(__powerpc__) || \
357    defined(__x86_64__) || \
358    defined(ia64) || defined(__ia64__) || \
359    defined(__hppa__) || defined(hpux) || \
360    defined(__mips) || defined(_MIPS_ARCH) || \
361    defined(__arm__) || \
362    defined(__sh__) || defined(__m32r__) || \
363    (defined(__sun) && defined(_IEEE_754)) || \
364    (defined(__alpha__) && (defined(__IEEE_FLOAT) || !defined(VMS)))
365#define USE_IEEE
366#define IEEE_ONE 0x3f800000
367#endif
368
369
370/**
371 * START/END_FAST_MATH macros:
372 *
373 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
374 *                  original mode to a temporary).
375 * END_FAST_MATH: Restore x86 FPU to original mode.
376 */
377#if defined(__GNUC__) && defined(__i386__)
378/*
379 * Set the x86 FPU control word to guarentee only 32 bits of precision
380 * are stored in registers.  Allowing the FPU to store more introduces
381 * differences between situations where numbers are pulled out of memory
382 * vs. situations where the compiler is able to optimize register usage.
383 *
384 * In the worst case, we force the compiler to use a memory access to
385 * truncate the float, by specifying the 'volatile' keyword.
386 */
387/* Hardware default: All exceptions masked, extended double precision,
388 * round to nearest (IEEE compliant):
389 */
390#define DEFAULT_X86_FPU		0x037f
391/* All exceptions masked, single precision, round to nearest:
392 */
393#define FAST_X86_FPU		0x003f
394/* The fldcw instruction will cause any pending FP exceptions to be
395 * raised prior to entering the block, and we clear any pending
396 * exceptions before exiting the block.  Hence, asm code has free
397 * reign over the FPU while in the fast math block.
398 */
399#if defined(NO_FAST_MATH)
400#define START_FAST_MATH(x)						\
401do {									\
402   static GLuint mask = DEFAULT_X86_FPU;				\
403   __asm__ ( "fnstcw %0" : "=m" (*&(x)) );				\
404   __asm__ ( "fldcw %0" : : "m" (mask) );				\
405} while (0)
406#else
407#define START_FAST_MATH(x)						\
408do {									\
409   static GLuint mask = FAST_X86_FPU;					\
410   __asm__ ( "fnstcw %0" : "=m" (*&(x)) );				\
411   __asm__ ( "fldcw %0" : : "m" (mask) );				\
412} while (0)
413#endif
414/* Restore original FPU mode, and clear any exceptions that may have
415 * occurred in the FAST_MATH block.
416 */
417#define END_FAST_MATH(x)						\
418do {									\
419   __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) );			\
420} while (0)
421
422#elif defined(__WATCOMC__) && defined(__386__)
423#define DEFAULT_X86_FPU		0x037f /* See GCC comments above */
424#define FAST_X86_FPU		0x003f /* See GCC comments above */
425void _watcom_start_fast_math(unsigned short *x,unsigned short *mask);
426#pragma aux _watcom_start_fast_math =                                   \
427   "fnstcw  word ptr [eax]"                                             \
428   "fldcw   word ptr [ecx]"                                             \
429   parm [eax] [ecx]                                                     \
430   modify exact [];
431void _watcom_end_fast_math(unsigned short *x);
432#pragma aux _watcom_end_fast_math =                                     \
433   "fnclex"                                                             \
434   "fldcw   word ptr [eax]"                                             \
435   parm [eax]                                                           \
436   modify exact [];
437#if defined(NO_FAST_MATH)
438#define START_FAST_MATH(x)                                              \
439do {                                                                    \
440   static GLushort mask = DEFAULT_X86_FPU;	                        \
441   _watcom_start_fast_math(&x,&mask);                                   \
442} while (0)
443#else
444#define START_FAST_MATH(x)                                              \
445do {                                                                    \
446   static GLushort mask = FAST_X86_FPU;                                 \
447   _watcom_start_fast_math(&x,&mask);                                   \
448} while (0)
449#endif
450#define END_FAST_MATH(x)  _watcom_end_fast_math(&x)
451
452#elif defined(_MSC_VER) && defined(_M_IX86)
453#define DEFAULT_X86_FPU		0x037f /* See GCC comments above */
454#define FAST_X86_FPU		0x003f /* See GCC comments above */
455#if defined(NO_FAST_MATH)
456#define START_FAST_MATH(x) do {\
457	static GLuint mask = DEFAULT_X86_FPU;\
458	__asm fnstcw word ptr [x]\
459	__asm fldcw word ptr [mask]\
460} while(0)
461#else
462#define START_FAST_MATH(x) do {\
463	static GLuint mask = FAST_X86_FPU;\
464	__asm fnstcw word ptr [x]\
465	__asm fldcw word ptr [mask]\
466} while(0)
467#endif
468#define END_FAST_MATH(x) do {\
469	__asm fnclex\
470	__asm fldcw word ptr [x]\
471} while(0)
472
473#else
474#define START_FAST_MATH(x)  x = 0
475#define END_FAST_MATH(x)  (void)(x)
476#endif
477
478
479#ifndef Elements
480#define Elements(x) (sizeof(x)/sizeof(*(x)))
481#endif
482
483
484
485#ifdef __cplusplus
486}
487#endif
488
489
490#endif /* COMPILER_H */
491