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