compiler.h revision 848b8605
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 "util/macros.h"
48
49#include "c99_compat.h" /* inline, __func__, etc. */
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#endif
86
87
88/**
89 * Disable assorted warnings
90 */
91#if defined(_WIN32) && !defined(__CYGWIN__)
92#  if !defined(__GNUC__) /* mingw environment */
93#    pragma warning( disable : 4068 ) /* unknown pragma */
94#    pragma warning( disable : 4710 ) /* function 'foo' not inlined */
95#    pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
96#    pragma warning( disable : 4127 ) /* conditional expression is constant */
97#    if defined(MESA_MINWARN)
98#      pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
99#      pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
100#      pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
101#      pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
102#      pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
103#    endif
104#  endif
105#endif
106
107
108
109/* XXX: Use standard `inline` keyword instead */
110#ifndef INLINE
111#  define INLINE inline
112#endif
113
114
115/**
116 * PUBLIC/USED macros
117 *
118 * If we build the library with gcc's -fvisibility=hidden flag, we'll
119 * use the PUBLIC macro to mark functions that are to be exported.
120 *
121 * We also need to define a USED attribute, so the optimizer doesn't
122 * inline a static function that we later use in an alias. - ajax
123 */
124#ifndef PUBLIC
125#  if (defined(__GNUC__) && __GNUC__ >= 4) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
126#    define PUBLIC __attribute__((visibility("default")))
127#    define USED __attribute__((used))
128#  else
129#    define PUBLIC
130#    define USED
131#  endif
132#endif
133
134
135/* XXX: Use standard `__func__` instead */
136#ifndef __FUNCTION__
137#  define __FUNCTION__ __func__
138#endif
139
140/**
141 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
142 * Do not use these unless absolutely necessary!
143 * Try to use a runtime test instead.
144 * For now, only used by some DRI hardware drivers for color/texel packing.
145 */
146#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
147#if defined(__linux__)
148#include <byteswap.h>
149#define CPU_TO_LE32( x )	bswap_32( x )
150#elif defined(__APPLE__)
151#include <CoreFoundation/CFByteOrder.h>
152#define CPU_TO_LE32( x )	CFSwapInt32HostToLittle( x )
153#elif (defined(_AIX) || defined(__blrts))
154static INLINE GLuint CPU_TO_LE32(GLuint x)
155{
156   return (((x & 0x000000ff) << 24) |
157           ((x & 0x0000ff00) <<  8) |
158           ((x & 0x00ff0000) >>  8) |
159           ((x & 0xff000000) >> 24));
160}
161#elif defined(__OpenBSD__)
162#include <sys/types.h>
163#define CPU_TO_LE32( x )	htole32( x )
164#else /*__linux__ */
165#include <sys/endian.h>
166#define CPU_TO_LE32( x )	bswap32( x )
167#endif /*__linux__*/
168#define MESA_BIG_ENDIAN 1
169#else
170#define CPU_TO_LE32( x )	( x )
171#define MESA_LITTLE_ENDIAN 1
172#endif
173#define LE32_TO_CPU( x )	CPU_TO_LE32( x )
174
175
176
177#if !defined(CAPI) && defined(_WIN32)
178#define CAPI _cdecl
179#endif
180
181
182/**
183 * Create a macro so that asm functions can be linked into compilers other
184 * than GNU C
185 */
186#ifndef _ASMAPI
187#if defined(_WIN32)
188#define _ASMAPI __cdecl
189#else
190#define _ASMAPI
191#endif
192#ifdef	PTR_DECL_IN_FRONT
193#define	_ASMAPIP * _ASMAPI
194#else
195#define	_ASMAPIP _ASMAPI *
196#endif
197#endif
198
199#ifdef USE_X86_ASM
200#define _NORMAPI _ASMAPI
201#define _NORMAPIP _ASMAPIP
202#else
203#define _NORMAPI
204#define _NORMAPIP *
205#endif
206
207
208/* Turn off macro checking systems used by other libraries */
209#ifdef CHECK
210#undef CHECK
211#endif
212
213
214/**
215 * ASSERT macro
216 */
217#if !defined(_WIN32_WCE)
218#if defined(DEBUG)
219#  define ASSERT(X)   assert(X)
220#else
221#  define ASSERT(X)
222#endif
223#endif
224
225
226/*
227 * A trick to suppress uninitialized variable warning without generating any
228 * code
229 */
230#define uninitialized_var(x) x = x
231
232#ifndef NULL
233#define NULL 0
234#endif
235
236
237/**
238 * LONGSTRING macro
239 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
240 */
241#if !defined(__GNUC__)
242# define LONGSTRING
243#else
244# define LONGSTRING __extension__
245#endif
246
247
248#ifndef M_PI
249#define M_PI (3.14159265358979323846)
250#endif
251
252#ifndef M_E
253#define M_E (2.7182818284590452354)
254#endif
255
256#ifndef M_LOG2E
257#define M_LOG2E     (1.4426950408889634074)
258#endif
259
260#ifndef ONE_DIV_SQRT_LN2
261#define ONE_DIV_SQRT_LN2 (1.201122408786449815)
262#endif
263
264#ifndef FLT_MAX_EXP
265#define FLT_MAX_EXP 128
266#endif
267
268#define IEEE_ONE 0x3f800000
269
270/**
271 * START/END_FAST_MATH macros:
272 *
273 * START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
274 *                  original mode to a temporary).
275 * END_FAST_MATH: Restore x86 FPU to original mode.
276 */
277#if defined(__GNUC__) && defined(__i386__)
278/*
279 * Set the x86 FPU control word to guarentee only 32 bits of precision
280 * are stored in registers.  Allowing the FPU to store more introduces
281 * differences between situations where numbers are pulled out of memory
282 * vs. situations where the compiler is able to optimize register usage.
283 *
284 * In the worst case, we force the compiler to use a memory access to
285 * truncate the float, by specifying the 'volatile' keyword.
286 */
287/* Hardware default: All exceptions masked, extended double precision,
288 * round to nearest (IEEE compliant):
289 */
290#define DEFAULT_X86_FPU		0x037f
291/* All exceptions masked, single precision, round to nearest:
292 */
293#define FAST_X86_FPU		0x003f
294/* The fldcw instruction will cause any pending FP exceptions to be
295 * raised prior to entering the block, and we clear any pending
296 * exceptions before exiting the block.  Hence, asm code has free
297 * reign over the FPU while in the fast math block.
298 */
299#if defined(NO_FAST_MATH)
300#define START_FAST_MATH(x)						\
301do {									\
302   static GLuint mask = DEFAULT_X86_FPU;				\
303   __asm__ ( "fnstcw %0" : "=m" (*&(x)) );				\
304   __asm__ ( "fldcw %0" : : "m" (mask) );				\
305} while (0)
306#else
307#define START_FAST_MATH(x)						\
308do {									\
309   static GLuint mask = FAST_X86_FPU;					\
310   __asm__ ( "fnstcw %0" : "=m" (*&(x)) );				\
311   __asm__ ( "fldcw %0" : : "m" (mask) );				\
312} while (0)
313#endif
314/* Restore original FPU mode, and clear any exceptions that may have
315 * occurred in the FAST_MATH block.
316 */
317#define END_FAST_MATH(x)						\
318do {									\
319   __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) );			\
320} while (0)
321
322#elif defined(_MSC_VER) && defined(_M_IX86)
323#define DEFAULT_X86_FPU		0x037f /* See GCC comments above */
324#define FAST_X86_FPU		0x003f /* See GCC comments above */
325#if defined(NO_FAST_MATH)
326#define START_FAST_MATH(x) do {\
327	static GLuint mask = DEFAULT_X86_FPU;\
328	__asm fnstcw word ptr [x]\
329	__asm fldcw word ptr [mask]\
330} while(0)
331#else
332#define START_FAST_MATH(x) do {\
333	static GLuint mask = FAST_X86_FPU;\
334	__asm fnstcw word ptr [x]\
335	__asm fldcw word ptr [mask]\
336} while(0)
337#endif
338#define END_FAST_MATH(x) do {\
339	__asm fnclex\
340	__asm fldcw word ptr [x]\
341} while(0)
342
343#else
344#define START_FAST_MATH(x)  x = 0
345#define END_FAST_MATH(x)  (void)(x)
346#endif
347
348
349#ifndef Elements
350#define Elements(x) (sizeof(x)/sizeof(*(x)))
351#endif
352
353#ifdef __cplusplus
354}
355#endif
356
357
358#endif /* COMPILER_H */
359