1 #ifndef _DEDEFS_H
2 #define _DEDEFS_H
3 /*-------------------------------------------------------------------------
4 * drawElements Base Portability Library
5 * -------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Basic portability.
24 *//*--------------------------------------------------------------------*/
25
26 /* Compilers. */
27 #define DE_COMPILER_VANILLA 0 /*!< Vanilla compiler. Used for disabling all platform-specific optimizations. */
28 #define DE_COMPILER_MSC 1 /*!< Microsoft Visual Studio. */
29 #define DE_COMPILER_GCC 2 /*!< Gnu C Compiler. */
30 #define DE_COMPILER_CLANG 3 /*!< LLVM Clang Compiler. */
31
32 /* Compiler detection. */
33 #if defined(DE_COMPILER)
34 /* Allow definitions from outside. */
35 #elif defined(_MSC_VER)
36 # define DE_COMPILER DE_COMPILER_MSC /*!< Compiler identification (set to one of DE_COMPILER_*). */
37 #elif defined(__clang__)
38 # define DE_COMPILER DE_COMPILER_CLANG
39 #elif defined(__GNUC__)
40 # define DE_COMPILER DE_COMPILER_GCC
41 #else
42 # error Unknown compiler.
43 #endif
44
45 /* Operating systems. */
46 #define DE_OS_VANILLA 0 /*!< Vanilla OS. */
47 #define DE_OS_WIN32 1 /*!< Microsoft Windows desktop */
48 #define DE_OS_UNIX 2 /*!< Unix (or compatible) */
49 #define DE_OS_WINCE 3 /*!< Windows CE, Windows Mobile or Pocket PC */
50 #define DE_OS_OSX 4 /*!< Mac OS X */
51 #define DE_OS_ANDROID 5 /*!< Android */
52 #define DE_OS_SYMBIAN 6 /*!< Symbian OS */
53 #define DE_OS_IOS 7 /*!< iOS */
54
55 /* OS detection (set to one of DE_OS_*). */
56 #if defined(DE_OS)
57 /* Allow definitions from outside. */
58 #elif defined(__ANDROID__)
59 # define DE_OS DE_OS_ANDROID
60 #elif defined(_WIN32_WCE) || defined(UNDER_CE)
61 # define DE_OS DE_OS_WINCE
62 #elif defined(_WIN32)
63 # define DE_OS DE_OS_WIN32
64 #elif defined(__unix__) || defined(__linux) || defined(__linux__)
65 # define DE_OS DE_OS_UNIX
66 #elif defined(__APPLE__)
67 # define DE_OS DE_OS_OSX
68 #elif defined(__EPOC32__)
69 # define DE_OS DE_OS_SYMBIAN
70 #else
71 # error Unknown operating system.
72 #endif
73
74 /* CPUs */
75 #define DE_CPU_VANILLA 0
76 #define DE_CPU_X86 1
77 #define DE_CPU_ARM 2
78 #define DE_CPU_X86_64 3
79 #define DE_CPU_ARM_64 4
80 #define DE_CPU_MIPS 5
81 #define DE_CPU_MIPS_64 6
82
83 /* CPU detection. */
84 #if defined(DE_CPU)
85 /* Allow definitions from outside. */
86 #elif defined(__aarch64__)
87 # define DE_CPU DE_CPU_ARM_64
88 #elif defined(__arm__) || defined(__ARM__) || defined(__ARM_NEON__) || defined(ARM_BUILD)
89 # define DE_CPU DE_CPU_ARM
90 #elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
91 # define DE_CPU DE_CPU_X86_64
92 #elif defined(__i386__) || defined(_M_X86) || defined(_M_IX86) || defined(X86_BUILD)
93 # define DE_CPU DE_CPU_X86
94 #elif defined(__mips__) && ((__mips) == 32)
95 # define DE_CPU DE_CPU_MIPS
96 #elif defined(__mips__) && ((__mips) == 64)
97 # define DE_CPU DE_CPU_MIPS_64
98 #else
99 # error Unknown CPU.
100 #endif
101
102 /* Endianness */
103 #define DE_BIG_ENDIAN 0
104 #define DE_LITTLE_ENDIAN 1
105
106 #if defined(DE_ENDIANNESS)
107 /* Allow definitions from outside. */
108 #elif (DE_CPU == DE_CPU_X86) || (DE_CPU == DE_CPU_X86_64)
109 /* "detect" x86(_64) endianness */
110 # define DE_ENDIANNESS DE_LITTLE_ENDIAN
111 #elif ((DE_CPU == DE_CPU_MIPS) || (DE_CPU == DE_CPU_MIPS_64))
112 /* detect mips endianness using platform specific macros */
113 # if defined(__MIPSEB__) && !defined(__MIPSEL__)
114 # define DE_ENDIANNESS DE_BIG_ENDIAN
115 # elif !defined(__MIPSEB__) && defined(__MIPSEL__)
116 # define DE_ENDIANNESS DE_LITTLE_ENDIAN
117 # else
118 # error Invalid MIPS endianness.
119 # endif
120 #elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
121 # define DE_ENDIANNESS DE_LITTLE_ENDIAN
122 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
123 # define DE_ENDIANNESS DE_BIG_ENDIAN
124 #else
125 # error Unknown endianness.
126 #endif
127
128 /* Sanity */
129 #if ((DE_CPU == DE_CPU_X86) || (DE_CPU == DE_CPU_X86_64)) && (DE_ENDIANNESS == DE_BIG_ENDIAN)
130 # error Invalid x86(_64) endianness.
131 #endif
132
133 /* Sized data types. */
134 typedef signed char deInt8;
135 typedef signed short deInt16;
136 typedef signed int deInt32;
137 typedef unsigned char deUint8;
138 typedef unsigned short deUint16;
139 typedef unsigned int deUint32;
140
141 #if (DE_COMPILER == DE_COMPILER_MSC)
142 typedef signed __int64 deInt64;
143 typedef unsigned __int64 deUint64;
144
145 # if (DE_OS == DE_OS_WINCE)
146 # include <basetsd.h>
147 typedef INT_PTR deIntptr;
148 typedef UINT_PTR deUintptr;
149 # elif (DE_OS == DE_OS_WIN32)
150 # include <crtdefs.h>
151 typedef intptr_t deIntptr;
152 typedef uintptr_t deUintptr;
153 # else
154 # error Define intptr types.
155 # endif
156
157 #elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
158 /* \note stddef.h is needed for size_t definition. */
159 # include <stddef.h>
160 # include <stdint.h>
161 typedef int64_t deInt64;
162 typedef uint64_t deUint64;
163 typedef intptr_t deIntptr;
164 typedef uintptr_t deUintptr;
165 #else
166 # error Define 64-bit and intptr types.
167 #endif
168
169 /** Boolean type. */
170 typedef int deBool;
171 #define DE_TRUE 1 /*!< True value for deBool. */
172 #define DE_FALSE 0 /*!< False value for deBool. */
173
174 /* Null pointer. */
175 #if defined(__cplusplus)
176 # define DE_NULL 0
177 #else
178 # define DE_NULL ((void*)0) /*!< Null pointer. */
179 #endif
180
181 /* Function pointer type. */
182 typedef void (*deFunctionPtr) (void);
183
184 /* Debug macro. */
185 #if defined(DE_DEBUG)
186 /* Already defined from outside. */
187 #else
188 # if (DE_COMPILER != DE_COMPILER_GCC)
189 # if defined(_DEBUG)
190 # define DE_DEBUG /*!< Debug build enabled? Usage: #if defined(DE_DEBUG). */
191 # endif
192 # elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
193 # if !defined(NDEBUG)
194 # define DE_DEBUG
195 # endif
196 # endif
197 #endif
198
199 /* Debug code macro. */
200 #if defined(DE_DEBUG)
201 # define DE_DEBUG_CODE(X) X
202 #else
203 # define DE_DEBUG_CODE(X)
204 #endif
205
206 /* Inline. */
207 #if (DE_COMPILER == DE_COMPILER_MSC)
208 # define DE_INLINE __forceinline
209 #elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
210 # define DE_INLINE static __inline__
211 #else
212 # define DE_INLINE inline /*!< Function inline. */
213 #endif
214
215 /* DE_DEV_BUILD -- only define when building on a development machine. */
216 #if !defined(DE_DEV_BUILD)
217 # if (DE_COMPILER == DE_COMPILER_MSC)
218 # define DE_DEV_BUILD
219 # endif
220 #endif
221
222 /* DE_VALGRIND_BUILD -- define this in makefile if support for Valgrind is wanted. */
223 /*#define DE_VALGRIND_BUILD*/
224
225 /** Length of array. C++ version does compile time check that passed value is an array reference. */
226 #if defined(__cplusplus) && (DE_COMPILER == DE_COMPILER_MSC)
227 template <typename T, size_t N> char (&deArraySizeHelper(T (&array)[N]))[N];
228 # define DE_LENGTH_OF_ARRAY(ARRAY) ((int)(sizeof(deArraySizeHelper(ARRAY))))
229 #else
230 # define DE_LENGTH_OF_ARRAY(ARRAY) ((int)(sizeof(ARRAY) / sizeof((ARRAY)[0])))
231 #endif
232
233 #ifdef __cplusplus
234 extern "C" {
235 #endif
236
237 /* Assertion macro family. */
238 void deAssertFail(const char* reason, const char* file, int line);
239
deGetFalse(void)240 DE_INLINE deBool deGetFalse (void) { return DE_FALSE; }
deGetTrue(void)241 DE_INLINE deBool deGetTrue (void) { return DE_TRUE; }
242
243 /* Assertion macro. */
244 #if defined(DE_DEBUG) && !defined(DE_COVERAGE_BUILD)
245 # define DE_ASSERT(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())
246 #else
247 # define DE_ASSERT(X) /*@ -noeffect*/ ((void)0) /*!< Assertion macro. */
248 #endif
249
250 /* Assertion failure callback. Requires DE_ASSERT_FAILURE_CALLBACK to be defined or otherwise has no effect. */
251 typedef void (*deAssertFailureCallbackFunc) (const char* reason, const char* file, int line);
252 void deSetAssertFailureCallback (deAssertFailureCallbackFunc callback);
253
254 /* Verify macro. Behaves like assert in debug build, but executes statement in release build. */
255 #if defined(DE_DEBUG)
256 # define DE_VERIFY(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())
257 #else
258 # define DE_VERIFY(X) X
259 #endif
260
261 /** Test assert macro for use in testers (same as DE_ASSERT, but always enabled). */
262 #define DE_TEST_ASSERT(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())
263
264 /** Compile-time assertion macro. */
265 #if (DE_COMPILER == DE_COMPILER_GCC)
266 /* GCC 4.8 and newer warns about unused typedefs. */
267 # define DE_UNUSED_TYPEDEF_ATTR __attribute__((unused))
268 #else
269 # define DE_UNUSED_TYPEDEF_ATTR
270 #endif
271 #define DE_STATIC_ASSERT(X) typedef char DE_UNIQUE_NAME[(X) ? 1 : -1] DE_UNUSED_TYPEDEF_ATTR
272 #define DE_HEADER_STATIC_ASSERT(HEADERTOKEN, X) typedef char DE_HEADER_UNIQUE_NAME(HEADERTOKEN)[(X) ? 1 : -1] DE_UNUSED_TYPEDEF_ATTR
273
274 #define DE_UNIQUE_NAME DE_MAKE_NAME(__LINE__, hoax)
275 #define DE_HEADER_UNIQUE_NAME(HEADERTOKEN) DE_MAKE_NAME(__LINE__, HEADERTOKEN)
276 #define DE_MAKE_NAME(line, token) DE_MAKE_NAME2(line, token)
277 #define DE_MAKE_NAME2(line, token) _static_assert_##line##_##token
278
279 /** Software breakpoint. */
280 #if (DE_CPU == DE_CPU_X86) && (DE_COMPILER == DE_COMPILER_MSC)
281 # define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __asm { int 3 } } while (deGetFalse())
282 #elif (DE_CPU == DE_CPU_X86_64) && (DE_COMPILER == DE_COMPILER_MSC)
283 # define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __debugbreak(); } while (deGetFalse())
284 #elif (DE_CPU == DE_CPU_ARM) && (DE_COMPILER == DE_COMPILER_GCC)
285 # define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __asm__ __volatile__ ( "bkpt #3" ); } while (deGetFalse())
286 #elif (DE_CPU == DE_CPU_ARM) && (DE_COMPILER == DE_COMPILER_MSC)
287 # define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); DebugBreak(); } while (deGetFalse())
288 #else
289 # define DE_BREAKPOINT() DE_ASSERT(!"Software breakpoint encountered!")
290 #endif
291
292 /** Swap two values. */
293 #define DE_SWAP(TYPE, A, B) do { TYPE _tmp_ = A; A = B; B = _tmp_; } while(deGetFalse())
294
295 /** Offset of a struct member. */
296 #define DE_OFFSET_OF(STRUCT, MEMBER) ((int)(deUintptr)(deUint8*)&(((STRUCT*)0)->MEMBER))
297
298 /* Pointer size. */
299 #if defined(DE_PTR_SIZE)
300 /* nada */
301 #elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__) || defined(__aarch64__) || (defined(__mips) && ((__mips) == 64)) || defined(_LP64) || defined(__LP64__)
302 # define DE_PTR_SIZE 8
303 #else
304 # define DE_PTR_SIZE 4 /* default to 32-bit */
305 #endif
306
307 /** Unreferenced variable silencing. */
308 #define DE_UNREF(VAR) ((void)(VAR))
309
310 /** DE_BEGIN_EXTERN_C and DE_END_EXTERN_C. */
311 #if defined(__cplusplus)
312 # define DE_BEGIN_EXTERN_C extern "C" {
313 # define DE_END_EXTERN_C }
314 #else
315 # define DE_BEGIN_EXTERN_C
316 # define DE_END_EXTERN_C
317 #endif
318
319 /** DE_NULL_STATEMENT */
320 #if defined(DE_DEBUG)
321 # define DE_NULL_STATEMENT do {} while (deGetFalse())
322 #else
323 # define DE_NULL_STATEMENT (void)0
324 #endif
325
326 /** GCC format string attributes */
327 #if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
328 # define DE_PRINTF_FUNC_ATTR(FORMAT_STRING, FIRST_ARG) __attribute__ ((format(printf, FORMAT_STRING, FIRST_ARG)))
329 #else
330 # define DE_PRINTF_FUNC_ATTR(FORMAT_STRING, FIRST_ARG)
331 #endif
332
333 /** Potentially unused func attribute to silence warnings from C templates. */
334 #if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
335 # define DE_UNUSED_FUNCTION __attribute__((unused))
336 #else
337 # define DE_UNUSED_FUNCTION
338 #endif
339
340 #ifdef __cplusplus
341 }
342 #endif
343
344 #endif /* _DEDEFS_H */
345