• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(_MSC_VER)
34 #	define DE_DETAIL_DETECTED_COMPILER DE_COMPILER_MSC
35 #elif defined(__clang__)
36 #	define DE_DETAIL_DETECTED_COMPILER DE_COMPILER_CLANG
37 #elif defined(__GNUC__)
38 #   define DE_DETAIL_DETECTED_COMPILER DE_COMPILER_GCC
39 #else
40 	/* DE_DETAIL_DETECTED_COMPILER not set */
41 #endif
42 
43 /* Compiler setting. */
44 #if defined(DE_COMPILER)
45 	/* Allow definitions from outside, but fail early if it conflicts with our detection */
46 #	if defined(DE_DETAIL_DETECTED_COMPILER) && (DE_COMPILER != DE_DETAIL_DETECTED_COMPILER)
47 		/* conflict, print a nice error messages for the most common misconfigs,
48 		 * GCC and Clang, and a generic for other conflicts.
49 		 */
50 #		if (DE_DETAIL_DETECTED_COMPILER == DE_COMPILER_CLANG) && (DE_COMPILER == DE_COMPILER_GCC)
51 #			error Detected compiler is Clang, but got DE_COMPILER == DE_COMPILER_GCC
52 #		elif (DE_DETAIL_DETECTED_COMPILER == DE_COMPILER_GCC) && (DE_COMPILER == DE_COMPILER_CLANG)
53 #			error Detected compiler is GCC, but got DE_COMPILER == DE_COMPILER_CLANG
54 #		else
55 #			error Detected compiler does not match the supplied compiler.
56 #		endif
57 #	endif
58 	/* Clear autodetect vars. */
59 #	if defined(DE_DETAIL_DETECTED_COMPILER)
60 #		undef DE_DETAIL_DETECTED_COMPILER
61 #	endif
62 #else
63 	/* No definition given from outside, try to autodetect */
64 #	if defined(DE_DETAIL_DETECTED_COMPILER)
65 #		define DE_COMPILER DE_DETAIL_DETECTED_COMPILER /*!< Compiler identification (set to one of DE_COMPILER_*). */
66 #	else
67 #		error Unknown compiler.
68 #	endif
69 #endif
70 
71 /* Operating systems. */
72 #define DE_OS_VANILLA	0			/*!< Vanilla OS.								*/
73 #define DE_OS_WIN32		1			/*!< Microsoft Windows desktop					*/
74 #define DE_OS_UNIX      2			/*!< Unix (or compatible)						*/
75 #define DE_OS_WINCE		3			/*!< Windows CE, Windows Mobile or Pocket PC	*/
76 #define DE_OS_OSX		4			/*!< Mac OS X									*/
77 #define DE_OS_ANDROID	5			/*!< Android									*/
78 #define DE_OS_SYMBIAN	6			/*!< Symbian OS									*/
79 #define DE_OS_IOS		7			/*!< iOS										*/
80 #define DE_OS_QNX       8           /*!< QNX                                        */
81 
82 /* OS detection (set to one of DE_OS_*). */
83 #if defined(DE_OS)
84 	/* Allow definitions from outside. */
85 #elif defined(__ANDROID__)
86 #	define DE_OS DE_OS_ANDROID
87 #elif defined(_WIN32_WCE) || defined(UNDER_CE)
88 #	define DE_OS DE_OS_WINCE
89 #elif defined(_WIN32)
90 #	define DE_OS DE_OS_WIN32
91 #elif defined(__unix__) || defined(__linux) || defined(__linux__)
92 #   define DE_OS DE_OS_UNIX
93 #elif defined(__APPLE__)
94 #	define DE_OS DE_OS_OSX
95 #elif defined(__EPOC32__)
96 #	define DE_OS DE_OS_SYMBIAN
97 #elif defined(__QNX__)
98 #   define DE_OS DE_OS_QNX
99 #else
100 #	error Unknown operating system.
101 #endif
102 
103 /* CPUs */
104 #define DE_CPU_VANILLA	0
105 #define DE_CPU_X86		1
106 #define DE_CPU_ARM		2
107 #define DE_CPU_X86_64	3
108 #define DE_CPU_ARM_64	4
109 #define DE_CPU_MIPS		5
110 #define DE_CPU_MIPS_64	6
111 
112 /* CPU detection. */
113 #if defined(DE_CPU)
114 	/* Allow definitions from outside. */
115 #elif defined(__aarch64__)
116 #	define DE_CPU DE_CPU_ARM_64
117 #elif defined(__arm__) || defined(__ARM__) || defined(__ARM_NEON__) || defined(ARM_BUILD)
118 #	define DE_CPU DE_CPU_ARM
119 #elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
120 #	define DE_CPU DE_CPU_X86_64
121 #elif defined(__i386__) || defined(_M_X86) || defined(_M_IX86) || defined(X86_BUILD)
122 #	define DE_CPU DE_CPU_X86
123 #elif defined(__mips__) && ((__mips) == 32)
124 #	define DE_CPU DE_CPU_MIPS
125 #elif defined(__mips__) && ((__mips) == 64)
126 #	define DE_CPU DE_CPU_MIPS_64
127 #else
128 #	error Unknown CPU.
129 #endif
130 
131 /* Endianness */
132 #define DE_BIG_ENDIAN		0
133 #define DE_LITTLE_ENDIAN	1
134 
135 #if defined(DE_ENDIANNESS)
136 	/* Allow definitions from outside. */
137 #elif (DE_CPU == DE_CPU_X86) || (DE_CPU == DE_CPU_X86_64)
138 	/* "detect" x86(_64) endianness */
139 #	define DE_ENDIANNESS DE_LITTLE_ENDIAN
140 #elif ((DE_CPU == DE_CPU_MIPS) || (DE_CPU == DE_CPU_MIPS_64))
141 	/* detect mips endianness using platform specific macros */
142 #	if defined(__MIPSEB__) && !defined(__MIPSEL__)
143 #		define DE_ENDIANNESS DE_BIG_ENDIAN
144 #	elif !defined(__MIPSEB__) && defined(__MIPSEL__)
145 #		define DE_ENDIANNESS DE_LITTLE_ENDIAN
146 #	else
147 #		error Invalid MIPS endianness.
148 #	endif
149 #elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
150 #	define DE_ENDIANNESS DE_LITTLE_ENDIAN
151 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
152 #	define DE_ENDIANNESS DE_BIG_ENDIAN
153 #else
154 #	error Unknown endianness.
155 #endif
156 
157 /* Sanity */
158 #if ((DE_CPU == DE_CPU_X86) || (DE_CPU == DE_CPU_X86_64)) && (DE_ENDIANNESS == DE_BIG_ENDIAN)
159 #	error Invalid x86(_64) endianness.
160 #endif
161 
162 /* Sized data types. */
163 typedef signed char			deInt8;
164 typedef signed short		deInt16;
165 typedef signed int			deInt32;
166 typedef unsigned char		deUint8;
167 typedef unsigned short		deUint16;
168 typedef unsigned int		deUint32;
169 
170 #if (DE_COMPILER == DE_COMPILER_MSC)
171 	typedef signed __int64		deInt64;
172 	typedef unsigned __int64	deUint64;
173 
174 #	if (DE_OS == DE_OS_WINCE)
175 #		include <basetsd.h>
176 		typedef INT_PTR			deIntptr;
177 		typedef UINT_PTR		deUintptr;
178 #	elif (DE_OS == DE_OS_WIN32)
179 #		include <crtdefs.h>
180 		typedef intptr_t		deIntptr;
181 		typedef uintptr_t		deUintptr;
182 #	else
183 #		error Define intptr types.
184 #	endif
185 
186 #elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
187 	/* \note stddef.h is needed for size_t definition. */
188 #	include <stddef.h>
189 #	include <stdint.h>
190 	typedef int64_t				deInt64;
191 	typedef uint64_t			deUint64;
192 	typedef intptr_t			deIntptr;
193 	typedef uintptr_t			deUintptr;
194 #else
195 #	error Define 64-bit and intptr types.
196 #endif
197 
198 /** Boolean type. */
199 typedef int deBool;
200 #define DE_TRUE		1		/*!< True value for deBool.		*/
201 #define DE_FALSE	0		/*!< False value for deBool.	*/
202 
203 /* Null pointer. */
204 #if defined(__cplusplus)
205 #	define DE_NULL 0
206 #else
207 #	define DE_NULL ((void*)0)		/*!< Null pointer.				*/
208 #endif
209 
210 /* Function pointer type. */
211 typedef void (*deFunctionPtr) (void);
212 
213 /* Use DE_PTR_TYPE(T) instead of T* in macros to avoid clang-tidy warning. */
214 #define DE_PTR_TYPE(T) T*  /* NOLINT(T) */
215 
216 /* Debug macro. */
217 #if defined(DE_DEBUG)
218 	/* Already defined from outside. */
219 #else
220 #	if (DE_COMPILER != DE_COMPILER_GCC)
221 #		if defined(_DEBUG)
222 #			define DE_DEBUG					/*!< Debug build enabled? Usage: #if defined(DE_DEBUG).	*/
223 #		endif
224 #	elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
225 #		if !defined(NDEBUG)
226 #			define DE_DEBUG
227 #		endif
228 #	endif
229 #endif
230 
231 /* Debug code macro. */
232 #if defined(DE_DEBUG)
233 #	define DE_DEBUG_CODE(X) X
234 #else
235 #	define DE_DEBUG_CODE(X)
236 #endif
237 
238 /* Inline. */
239 #if (DE_COMPILER == DE_COMPILER_MSC)
240 #	define DE_INLINE __forceinline
241 #elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
242 #   define DE_INLINE static __inline__
243 #else
244 #	define DE_INLINE inline			/*!< Function inline.		*/
245 #endif
246 
247 /* DE_DEV_BUILD -- only define when building on a development machine. */
248 #if !defined(DE_DEV_BUILD)
249 #	if (DE_COMPILER == DE_COMPILER_MSC)
250 #		define DE_DEV_BUILD
251 #	endif
252 #endif
253 
254 /* DE_VALGRIND_BUILD -- define this in makefile if support for Valgrind is wanted. */
255 /*#define DE_VALGRIND_BUILD*/
256 
257 /** Length of array. C++ version does compile time check that passed value is an array reference. */
258 #if defined(__cplusplus)
259 	// deArraySizeHelper is a function that receives a reference to an array of N elements of type T and returns a reference to an
260 	// array of N chars. This forces the compiler to check the argument is an actual array and not some other type implementing
261 	// operator[]. The actual function is never defined anywhere, but taking the sizeof() of the result is allowed and equal to N.
262 	template <typename T, size_t N> char (&deArraySizeHelper(T (&array)[N]))[N];
263 #	define DE_LENGTH_OF_ARRAY(ARRAY) ((int)(sizeof(deArraySizeHelper(ARRAY))))
264 #else
265 #	define DE_LENGTH_OF_ARRAY(ARRAY) ((int)(sizeof(ARRAY) / sizeof((ARRAY)[0])))
266 #endif
267 
268 #ifdef __cplusplus
269 extern "C" {
270 #endif
271 
272 /* Assertion macro family. */
273 void deAssertFail(const char* reason, const char* file, int line);
274 
275 /* Assertion failure callback. Requires DE_ASSERT_FAILURE_CALLBACK to be defined or otherwise has no effect. */
276 typedef void (*deAssertFailureCallbackFunc) (const char* reason, const char* file, int line);
277 void deSetAssertFailureCallback (deAssertFailureCallbackFunc callback);
278 
deGetFalse(void)279 DE_INLINE deBool deGetFalse (void) { return DE_FALSE; }
deGetTrue(void)280 DE_INLINE deBool deGetTrue (void) { return DE_TRUE; }
281 
282 /* Assertion macro. */
283 #if defined(DE_DEBUG) && !defined(DE_COVERAGE_BUILD)
284 #	define DE_ASSERT(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())
285 #else
286 #	define DE_ASSERT(X) /*@ -noeffect*/ ((void)0)	/*!< Assertion macro. */
287 #endif
288 
289 /* Verify macro. Behaves like assert in debug build, but executes statement in release build. */
290 #if defined(DE_DEBUG)
291 #	define DE_VERIFY(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())
292 #else
293 #	define DE_VERIFY(X) X
294 #endif
295 
296 /* Fatal macro. */
297 #if defined(DE_DEBUG) && !defined(DE_COVERAGE_BUILD)
298 #	define DE_FATAL(MSG) do { deAssertFail("" /* force to string literal */ MSG, __FILE__, __LINE__); } while(deGetFalse())
299 #else
300 #	define DE_FATAL(MSG) /*@ -noeffect*/ ((void)0)	/*!< Fatal macro. */
301 #endif
302 
303 /** Test assert macro for use in testers (same as DE_ASSERT, but always enabled). */
304 #define DE_TEST_ASSERT(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) deAssertFail(#X, __FILE__, __LINE__); } while(deGetFalse())
305 
306 #if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
307 	/* GCC 4.8 and newer warns about unused typedefs. */
308 #	define DE_UNUSED_TYPEDEF_ATTR __attribute__((unused))
309 #else
310 #	define DE_UNUSED_TYPEDEF_ATTR
311 #endif
312 
313 /** Compile-time assertion macro. */
314 #define DE_STATIC_ASSERT(X)						typedef char DE_UNIQUE_NAME[(X) ? 1 : -1] DE_UNUSED_TYPEDEF_ATTR
315 #define DE_HEADER_STATIC_ASSERT(HEADERTOKEN, X)	typedef char DE_HEADER_UNIQUE_NAME(HEADERTOKEN)[(X) ? 1 : -1] DE_UNUSED_TYPEDEF_ATTR
316 
317 #define DE_UNIQUE_NAME						DE_MAKE_NAME(__LINE__, hoax)
318 #define DE_HEADER_UNIQUE_NAME(HEADERTOKEN)	DE_MAKE_NAME(__LINE__, HEADERTOKEN)
319 #define DE_MAKE_NAME(line, token) DE_MAKE_NAME2(line, token)
320 #define DE_MAKE_NAME2(line, token) _static_assert_##line##_##token
321 
322 /** Software breakpoint. */
323 #if (DE_CPU == DE_CPU_X86) && (DE_COMPILER == DE_COMPILER_MSC)
324 #	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __asm { int 3 } } while (deGetFalse())
325 #elif (DE_CPU == DE_CPU_X86_64) && (DE_COMPILER == DE_COMPILER_MSC)
326 #	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __debugbreak(); } while (deGetFalse())
327 #elif (DE_CPU == DE_CPU_ARM) && (DE_COMPILER == DE_COMPILER_GCC)
328 #	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __asm__ __volatile__ ( "bkpt #3" ); } while (deGetFalse())
329 #elif (DE_CPU == DE_CPU_ARM_64) && (DE_COMPILER == DE_COMPILER_GCC)
330 #	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); __asm__ __volatile__ ( "brk #3" ); } while (deGetFalse())
331 #elif ((DE_CPU == DE_CPU_ARM) || (DE_CPU == DE_CPU_ARM_64)) && (DE_COMPILER == DE_COMPILER_MSC)
332 #	define DE_BREAKPOINT() do { printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); DebugBreak(); } while (deGetFalse())
333 #else
334 #	define DE_BREAKPOINT() DE_FATAL("Software breakpoint encountered!")
335 #endif
336 
337 /** Swap two values. */
338 #define DE_SWAP(TYPE, A, B) do { TYPE _tmp_ = (A); (A) = (B); (B) = _tmp_; } while(deGetFalse())
339 
340 /** Offset of a struct member. */
341 #define DE_OFFSET_OF(STRUCT, MEMBER) ((deUint32)(deUintptr)(deUint8*)&(((STRUCT*)0)->MEMBER))
342 
343 /* Pointer size. */
344 #if defined(DE_PTR_SIZE)
345 	/* nada */
346 #elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__) || defined(__aarch64__) || (defined(__mips) && ((__mips) == 64)) || defined(_LP64) || defined(__LP64__)
347 #	define DE_PTR_SIZE 8
348 #else
349 #	define DE_PTR_SIZE 4	/* default to 32-bit */
350 #endif
351 
352 /** Unreferenced variable silencing. */
353 #define DE_UNREF(VAR) ((void)(VAR))
354 
355 /** DE_BEGIN_EXTERN_C and DE_END_EXTERN_C. */
356 #if defined(__cplusplus)
357 #	define DE_BEGIN_EXTERN_C extern "C" {
358 #	define DE_END_EXTERN_C }
359 #else
360 #	define DE_BEGIN_EXTERN_C
361 #	define DE_END_EXTERN_C
362 #endif
363 
364 /** DE_NULL_STATEMENT */
365 #if defined(DE_DEBUG)
366 #	define DE_NULL_STATEMENT do {} while (deGetFalse())
367 #else
368 #	define DE_NULL_STATEMENT (void)0
369 #endif
370 
371 /** GCC format string attributes */
372 #if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
373 #	define DE_PRINTF_FUNC_ATTR(FORMAT_STRING, FIRST_ARG) __attribute__ ((format(printf, FORMAT_STRING, FIRST_ARG)))
374 #else
375 #	define DE_PRINTF_FUNC_ATTR(FORMAT_STRING, FIRST_ARG)
376 #endif
377 
378 /** Potentially unused func attribute to silence warnings from C templates. */
379 #if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
380 #	define DE_UNUSED_FUNCTION __attribute__((unused))
381 #else
382 #	define DE_UNUSED_FUNCTION
383 #endif
384 
385 #ifdef __cplusplus
386 }
387 #endif
388 
389 #endif /* _DEDEFS_H */
390