e644e2359c3fe0b18d98574331550a3298cfe81d
[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
39 #include "util/macros.h"
40
41 #include "c99_compat.h" /* inline, __func__, etc. */
42
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48
49 /**
50 * Get standard integer types
51 */
52 #include <stdint.h>
53
54
55 /**
56 * Sun compilers define __i386 instead of the gcc-style __i386__
57 */
58 #ifdef __SUNPRO_C
59 # if !defined(__i386__) && defined(__i386)
60 # define __i386__
61 # elif !defined(__amd64__) && defined(__amd64)
62 # define __amd64__
63 # elif !defined(__sparc__) && defined(__sparc)
64 # define __sparc__
65 # endif
66 # if !defined(__volatile)
67 # define __volatile volatile
68 # endif
69 #endif
70
71
72 /**
73 * finite macro.
74 */
75 #if defined(_MSC_VER)
76 # define finite _finite
77 #endif
78
79
80 /**
81 * Disable assorted warnings
82 */
83 #if defined(_WIN32) && !defined(__CYGWIN__)
84 # if !defined(__GNUC__) /* mingw environment */
85 # pragma warning( disable : 4068 ) /* unknown pragma */
86 # pragma warning( disable : 4710 ) /* function 'foo' not inlined */
87 # pragma warning( disable : 4711 ) /* function 'foo' selected for automatic inline expansion */
88 # pragma warning( disable : 4127 ) /* conditional expression is constant */
89 # if defined(MESA_MINWARN)
90 # pragma warning( disable : 4244 ) /* '=' : conversion from 'const double ' to 'float ', possible loss of data */
91 # pragma warning( disable : 4018 ) /* '<' : signed/unsigned mismatch */
92 # pragma warning( disable : 4305 ) /* '=' : truncation from 'const double ' to 'float ' */
93 # pragma warning( disable : 4550 ) /* 'function' undefined; assuming extern returning int */
94 # pragma warning( disable : 4761 ) /* integral size mismatch in argument; conversion supplied */
95 # endif
96 # endif
97 #endif
98
99
100
101 /* XXX: Use standard `inline` keyword instead */
102 #ifndef INLINE
103 # define INLINE inline
104 #endif
105
106
107 /**
108 * PUBLIC/USED macros
109 *
110 * If we build the library with gcc's -fvisibility=hidden flag, we'll
111 * use the PUBLIC macro to mark functions that are to be exported.
112 *
113 * We also need to define a USED attribute, so the optimizer doesn't
114 * inline a static function that we later use in an alias. - ajax
115 */
116 #ifndef PUBLIC
117 # if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
118 # define PUBLIC __attribute__((visibility("default")))
119 # define USED __attribute__((used))
120 # else
121 # define PUBLIC
122 # define USED
123 # endif
124 #endif
125
126
127 /* XXX: Use standard `__func__` instead */
128 #ifndef __FUNCTION__
129 # define __FUNCTION__ __func__
130 #endif
131
132 /**
133 * Either define MESA_BIG_ENDIAN or MESA_LITTLE_ENDIAN, and CPU_TO_LE32.
134 * Do not use these unless absolutely necessary!
135 * Try to use a runtime test instead.
136 * For now, only used by some DRI hardware drivers for color/texel packing.
137 */
138 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
139 #if defined(__linux__)
140 #include <byteswap.h>
141 #define CPU_TO_LE32( x ) bswap_32( x )
142 #elif defined(__APPLE__)
143 #include <CoreFoundation/CFByteOrder.h>
144 #define CPU_TO_LE32( x ) CFSwapInt32HostToLittle( x )
145 #elif (defined(_AIX))
146 static inline GLuint CPU_TO_LE32(GLuint x)
147 {
148 return (((x & 0x000000ff) << 24) |
149 ((x & 0x0000ff00) << 8) |
150 ((x & 0x00ff0000) >> 8) |
151 ((x & 0xff000000) >> 24));
152 }
153 #elif defined(__OpenBSD__)
154 #include <sys/types.h>
155 #define CPU_TO_LE32( x ) htole32( x )
156 #else /*__linux__ */
157 #include <sys/endian.h>
158 #define CPU_TO_LE32( x ) bswap32( x )
159 #endif /*__linux__*/
160 #define MESA_BIG_ENDIAN 1
161 #else
162 #define CPU_TO_LE32( x ) ( x )
163 #define MESA_LITTLE_ENDIAN 1
164 #endif
165 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
166
167
168
169 /**
170 * Create a macro so that asm functions can be linked into compilers other
171 * than GNU C
172 */
173 #ifndef _ASMAPI
174 #if defined(_WIN32)
175 #define _ASMAPI __cdecl
176 #else
177 #define _ASMAPI
178 #endif
179 #ifdef PTR_DECL_IN_FRONT
180 #define _ASMAPIP * _ASMAPI
181 #else
182 #define _ASMAPIP _ASMAPI *
183 #endif
184 #endif
185
186 #ifdef USE_X86_ASM
187 #define _NORMAPI _ASMAPI
188 #define _NORMAPIP _ASMAPIP
189 #else
190 #define _NORMAPI
191 #define _NORMAPIP *
192 #endif
193
194
195 /*
196 * A trick to suppress uninitialized variable warning without generating any
197 * code
198 */
199 #define uninitialized_var(x) x = x
200
201
202 /**
203 * LONGSTRING macro
204 * gcc -pedantic warns about long string literals, LONGSTRING silences that.
205 */
206 #if !defined(__GNUC__)
207 # define LONGSTRING
208 #else
209 # define LONGSTRING __extension__
210 #endif
211
212 #ifndef ONE_DIV_SQRT_LN2
213 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
214 #endif
215
216 #ifndef FLT_MAX_EXP
217 #define FLT_MAX_EXP 128
218 #endif
219
220 #define IEEE_ONE 0x3f800000
221
222 #ifndef Elements
223 #define Elements(x) (sizeof(x)/sizeof(*(x)))
224 #endif
225
226 #ifdef __cplusplus
227 }
228 #endif
229
230
231 #endif /* COMPILER_H */