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#include <float.h>
49#include <stdarg.h>
50
51
52#ifdef __cplusplus
53extern "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#elif defined(__WATCOMC__)
86#  define finite _finite
87#endif
88
89
90/**
91 * Disable assorted warnings
92 */
93#if !defined(OPENSTEP) && (defined(__WIN32__) && !defined(__CYGWIN__)) && !defined(BUILD_FOR_SNAP)
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#if defined(__WATCOMC__)
109#  pragma disable_message(201) /* Disable unreachable code warnings */
110#endif
111
112
113
114/**
115 * Function inlining
116 */
117#ifndef inline
118#  ifdef __cplusplus
119     /* C++ supports inline keyword */
120#  elif defined(__GNUC__)
121#    define inline __inline__
122#  elif defined(_MSC_VER)
123#    define inline __inline
124#  elif defined(__ICL)
125#    define inline __inline
126#  elif defined(__INTEL_COMPILER)
127     /* Intel compiler supports inline keyword */
128#  elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
129#    define inline __inline
130#  elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
131     /* C99 supports inline keyword */
132#  elif (__STDC_VERSION__ >= 199901L)
133     /* C99 supports inline keyword */
134#  else
135#    define inline
136#  endif
137#endif
138#ifndef INLINE
139#  define INLINE inline
140#endif
141
142
143/**
144 * PUBLIC/USED macros
145 *
146 * If we build the library with gcc's -fvisibility=hidden flag, we'll
147 * use the PUBLIC macro to mark functions that are to be exported.
148 *
149 * We also need to define a USED attribute, so the optimizer doesn't
150 * inline a static function that we later use in an alias. - ajax
151 */
152#ifndef PUBLIC
153#  if (defined(__GNUC__) && __GNUC__ >= 4) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
154#    define PUBLIC __attribute__((visibility("default")))
155#    define USED __attribute__((used))
156#  else
157#    define PUBLIC
158#    define USED
159#  endif
160#endif
161
162
163/**
164 * Some compilers don't like some of Mesa's const usage.  In those places use
165 * CONST instead of const.  Pass -DNO_CONST to compilers where this matters.
166 */
167#ifdef NO_CONST
168#  define CONST
169#else
170#  define CONST const
171#endif
172
173
174/**
175 * __builtin_expect macros
176 */
177#if !defined(__GNUC__)
178#  define __builtin_expect(x, y) (x)
179#endif
180
181#ifndef likely
182#  ifdef __GNUC__
183#    define likely(x)   __builtin_expect(!!(x), 1)
184#    define unlikely(x) __builtin_expect(!!(x), 0)
185#  else
186#    define likely(x)   (x)
187#    define unlikely(x) (x)
188#  endif
189#endif
190
191/**
192 * The __FUNCTION__ gcc variable is generally only used for debugging.
193 * If we're not using gcc, define __FUNCTION__ as a cpp symbol here.
194 * Don't define it if using a newer Windows compiler.
195 */
196#ifndef __FUNCTION__
197# if defined(__VMS)
198#  define __FUNCTION__ "VMS$NL:"
199# elif !defined(__GNUC__) && !defined(__xlC__) &&	\
200      (!defined(_MSC_VER) || _MSC_VER < 1300)
201#  if (__STDC_VERSION__ >= 199901L) /* C99 */ || \
202    (defined(__SUNPRO_C) && defined(__C99FEATURES__))
203#   define __FUNCTION__ __func__
204#  else
205#   define __FUNCTION__ "<unknown>"
206#  endif
207# endif
208#endif
209#ifndef __func__
210#  if (__STDC_VERSION__ >= 199901L) || \
211      (defined(__SUNPRO_C) && defined(__C99FEATURES__))
212       /* __func__ is part of C99 */
213#  elif defined(_MSC_VER)
214#    if _MSC_VER >= 1300
215#      define __func__ __FUNCTION__
216#    else
217#      define __func__ "<unknown>"
218#    endif
219#  endif
220#endif
221
222
223/**
224 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
225 * Do not use these unless absolutely necessary!
226 * Try to use a runtime test instead.
227 * For now, only used by some DRI hardware drivers for color/texel packing.
228 */
229#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
230#if defined(__linux__)
231#include <byteswap.h>
232#define CPU_TO_LE32( x )	bswap_32( x )
233#elif defined(__APPLE__)
234#include <CoreFoundation/CFByteOrder.h>
235#define CPU_TO_LE32( x )	CFSwapInt32HostToLittle( x )
236#elif (defined(_AIX) || defined(__blrts))
237static INLINE GLuint CPU_TO_LE32(GLuint x)
238{
239   return (((x & 0x000000ff) << 24) |
240           ((x & 0x0000ff00) <<  8) |
241           ((x & 0x00ff0000) >>  8) |
242           ((x & 0xff000000) >> 24));
243}
244#elif defined(__OpenBSD__)
245#include <sys/types.h>
246#define CPU_TO_LE32( x )	htole32( x )
247#else /*__linux__ */
248#include <sys/endian.h>
249#define CPU_TO_LE32( x )	bswap32( x )
250#endif /*__linux__*/
251#define MESA_BIG_ENDIAN 1
252#else
253#define CPU_TO_LE32( x )	( x )
254#define MESA_LITTLE_ENDIAN 1
255#endif
256#define LE32_TO_CPU( x )	CPU_TO_LE32( x )
257
258
259
260#if !defined(CAPI) && defined(WIN32) && !defined(BUILD_FOR_SNAP)
261#define CAPI _cdecl
262#endif
263
264
265/**
266 * Create a macro so that asm functions can be linked into compilers other
267 * than GNU C
268 */
269#ifndef _ASMAPI
270#if defined(WIN32) && !defined(BUILD_FOR_SNAP)/* was: !defined( __GNUC__ ) && !defined( VMS ) && !defined( __INTEL_COMPILER )*/
271#define _ASMAPI __cdecl
272#else
273#define _ASMAPI
274#endif
275#ifdef	PTR_DECL_IN_FRONT
276#define	_ASMAPIP * _ASMAPI
277#else
278#define	_ASMAPIP _ASMAPI *
279#endif
280#endif
281
282#ifdef USE_X86_ASM
283#define _NORMAPI _ASMAPI
284#define _NORMAPIP _ASMAPIP
285#else
286#define _NORMAPI
287#define _NORMAPIP *
288#endif
289
290
291/* Turn off macro checking systems used by other libraries */
292#ifdef CHECK
293#undef CHECK
294#endif
295
296
297/**
298 * ASSERT macro
299 */
300#if !defined(_WIN32_WCE)
301#if defined(BUILD_FOR_SNAP) && defined(CHECKED)
302#  define ASSERT(X)   _CHECK(X)
303#elif defined(DEBUG)
304#  define ASSERT(X)   assert(X)
305#else
306#  define ASSERT(X)
307#endif
308#endif
309
310
311/**
312 * Static (compile-time) assertion.
313 * Basically, use COND to dimension an array.  If COND is false/zero the
314 * array size will be -1 and we'll get a compilation error.
315 */
316#define STATIC_ASSERT(COND) \
317   do { \
318      typedef int static_assertion_failed[(!!(COND))*2-1]; \
319   } while (0)
320
321
322#if (__GNUC__ >= 3)
323#define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
324#else
325#define PRINTFLIKE(f, a)
326#endif
327
328#ifndef NULL
329#define NULL 0
330#endif
331
332
333/**
334 * LONGSTRING macro
335 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
336 */
337#if !defined(__GNUC__)
338# define LONGSTRING
339#else
340# define LONGSTRING __extension__
341#endif
342
343
344#ifndef M_PI
345#define M_PI (3.14159265358979323846)
346#endif
347
348#ifndef M_E
349#define M_E (2.7182818284590452354)
350#endif
351
352#ifndef M_LOG2E
353#define M_LOG2E     (1.4426950408889634074)
354#endif
355
356#ifndef ONE_DIV_SQRT_LN2
357#define ONE_DIV_SQRT_LN2 (1.201122408786449815)
358#endif
359
360#ifndef FLT_MAX_EXP
361#define FLT_MAX_EXP 128
362#endif
363
364
365/**
366 * USE_IEEE: Determine if we're using IEEE floating point
367 */
368#if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
369    defined(__s390x__) || defined(__powerpc__) || \
370    defined(__x86_64__) || \
371    defined(ia64) || defined(__ia64__) || \
372    defined(__hppa__) || defined(hpux) || \
373    defined(__mips) || defined(_MIPS_ARCH) || \
374    defined(__arm__) || \
375    defined(__sh__) || defined(__m32r__) || \
376    (defined(__sun) && defined(_IEEE_754)) || \
377    (defined(__alpha__) && (defined(__IEEE_FLOAT) || !defined(VMS)))
378#define USE_IEEE
379#define IEEE_ONE 0x3f800000
380#endif
381
382
383/**
384 * START/END_FAST_MATH macros:
385 *
386 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
387 *                  original mode to a temporary).
388 * END_FAST_MATH: Restore x86 FPU to original mode.
389 */
390#if defined(__GNUC__) && defined(__i386__)
391/*
392 * Set the x86 FPU control word to guarentee only 32 bits of precision
393 * are stored in registers.  Allowing the FPU to store more introduces
394 * differences between situations where numbers are pulled out of memory
395 * vs. situations where the compiler is able to optimize register usage.
396 *
397 * In the worst case, we force the compiler to use a memory access to
398 * truncate the float, by specifying the 'volatile' keyword.
399 */
400/* Hardware default: All exceptions masked, extended double precision,
401 * round to nearest (IEEE compliant):
402 */
403#define DEFAULT_X86_FPU		0x037f
404/* All exceptions masked, single precision, round to nearest:
405 */
406#define FAST_X86_FPU		0x003f
407/* The fldcw instruction will cause any pending FP exceptions to be
408 * raised prior to entering the block, and we clear any pending
409 * exceptions before exiting the block.  Hence, asm code has free
410 * reign over the FPU while in the fast math block.
411 */
412#if defined(NO_FAST_MATH)
413#define START_FAST_MATH(x)						\
414do {									\
415   static GLuint mask = DEFAULT_X86_FPU;				\
416   __asm__ ( "fnstcw %0" : "=m" (*&(x)) );				\
417   __asm__ ( "fldcw %0" : : "m" (mask) );				\
418} while (0)
419#else
420#define START_FAST_MATH(x)						\
421do {									\
422   static GLuint mask = FAST_X86_FPU;					\
423   __asm__ ( "fnstcw %0" : "=m" (*&(x)) );				\
424   __asm__ ( "fldcw %0" : : "m" (mask) );				\
425} while (0)
426#endif
427/* Restore original FPU mode, and clear any exceptions that may have
428 * occurred in the FAST_MATH block.
429 */
430#define END_FAST_MATH(x)						\
431do {									\
432   __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) );			\
433} while (0)
434
435#elif defined(__WATCOMC__) && defined(__386__)
436#define DEFAULT_X86_FPU		0x037f /* See GCC comments above */
437#define FAST_X86_FPU		0x003f /* See GCC comments above */
438void _watcom_start_fast_math(unsigned short *x,unsigned short *mask);
439#pragma aux _watcom_start_fast_math =                                   \
440   "fnstcw  word ptr [eax]"                                             \
441   "fldcw   word ptr [ecx]"                                             \
442   parm [eax] [ecx]                                                     \
443   modify exact [];
444void _watcom_end_fast_math(unsigned short *x);
445#pragma aux _watcom_end_fast_math =                                     \
446   "fnclex"                                                             \
447   "fldcw   word ptr [eax]"                                             \
448   parm [eax]                                                           \
449   modify exact [];
450#if defined(NO_FAST_MATH)
451#define START_FAST_MATH(x)                                              \
452do {                                                                    \
453   static GLushort mask = DEFAULT_X86_FPU;	                        \
454   _watcom_start_fast_math(&x,&mask);                                   \
455} while (0)
456#else
457#define START_FAST_MATH(x)                                              \
458do {                                                                    \
459   static GLushort mask = FAST_X86_FPU;                                 \
460   _watcom_start_fast_math(&x,&mask);                                   \
461} while (0)
462#endif
463#define END_FAST_MATH(x)  _watcom_end_fast_math(&x)
464
465#elif defined(_MSC_VER) && defined(_M_IX86)
466#define DEFAULT_X86_FPU		0x037f /* See GCC comments above */
467#define FAST_X86_FPU		0x003f /* See GCC comments above */
468#if defined(NO_FAST_MATH)
469#define START_FAST_MATH(x) do {\
470	static GLuint mask = DEFAULT_X86_FPU;\
471	__asm fnstcw word ptr [x]\
472	__asm fldcw word ptr [mask]\
473} while(0)
474#else
475#define START_FAST_MATH(x) do {\
476	static GLuint mask = FAST_X86_FPU;\
477	__asm fnstcw word ptr [x]\
478	__asm fldcw word ptr [mask]\
479} while(0)
480#endif
481#define END_FAST_MATH(x) do {\
482	__asm fnclex\
483	__asm fldcw word ptr [x]\
484} while(0)
485
486#else
487#define START_FAST_MATH(x)  x = 0
488#define END_FAST_MATH(x)  (void)(x)
489#endif
490
491
492#ifndef Elements
493#define Elements(x) (sizeof(x)/sizeof(*(x)))
494#endif
495
496
497
498#ifdef __cplusplus
499}
500#endif
501
502
503#endif /* COMPILER_H */
504