xref: /aosp_15_r20/external/deqp/framework/delibs/debase/deDefs.h (revision 35238bce31c2a825756842865a792f8cf7f89930)
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 #define DE_OS_FUCHSIA 9 /*!< Fuchsia                                    */
82 
83 /* OS detection (set to one of DE_OS_*). */
84 #if defined(DE_OS)
85 /* Allow definitions from outside. */
86 #elif defined(__ANDROID__)
87 #define DE_OS DE_OS_ANDROID
88 #elif defined(_WIN32_WCE) || defined(UNDER_CE)
89 #define DE_OS DE_OS_WINCE
90 #elif defined(_WIN32)
91 #define DE_OS DE_OS_WIN32
92 #elif defined(__unix__) || defined(__linux) || defined(__linux__)
93 #define DE_OS DE_OS_UNIX
94 #elif defined(__APPLE__)
95 #define DE_OS DE_OS_OSX
96 #elif defined(__EPOC32__)
97 #define DE_OS DE_OS_SYMBIAN
98 #elif defined(__QNX__)
99 #define DE_OS DE_OS_QNX
100 #else
101 #error Unknown operating system.
102 #endif
103 
104 #if ((DE_OS == DE_OS_WIN32) || (DE_OS == DE_OS_UNIX)) && !defined(DEQP_SURFACELESS) && !defined(NULLWS)
105 #define DE_PLATFORM_USE_LIBRARY_TYPE 1
106 #else
107 #undef DE_PLATFORM_USE_LIBRARY_TYPE
108 #endif
109 
110 /* CPUs */
111 #define DE_CPU_VANILLA 0
112 #define DE_CPU_X86 1
113 #define DE_CPU_ARM 2
114 #define DE_CPU_X86_64 3
115 #define DE_CPU_ARM_64 4
116 #define DE_CPU_MIPS 5
117 #define DE_CPU_MIPS_64 6
118 #define DE_CPU_RISCV_32 7
119 #define DE_CPU_RISCV_64 8
120 
121 /* CPU detection. */
122 #if defined(DE_CPU)
123 /* Allow definitions from outside. */
124 #elif defined(__aarch64__)
125 #define DE_CPU DE_CPU_ARM_64
126 #elif defined(__arm__) || defined(__ARM__) || defined(__ARM_NEON__) || defined(ARM_BUILD)
127 #define DE_CPU DE_CPU_ARM
128 #elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)
129 #define DE_CPU DE_CPU_X86_64
130 #elif defined(__i386__) || defined(_M_X86) || defined(_M_IX86) || defined(X86_BUILD)
131 #define DE_CPU DE_CPU_X86
132 #elif defined(__mips__) && ((__mips) == 32)
133 #define DE_CPU DE_CPU_MIPS
134 #elif defined(__mips__) && ((__mips) == 64)
135 #define DE_CPU DE_CPU_MIPS_64
136 #elif defined(__riscv) && ((__riscv_xlen) == 32)
137 #define DE_CPU DE_CPU_RISCV_32
138 #elif defined(__riscv) && ((__riscv_xlen) == 64)
139 #define DE_CPU DE_CPU_RISCV_64
140 #else
141 #error Unknown CPU.
142 #endif
143 
144 /* Endianness */
145 #define DE_BIG_ENDIAN 0
146 #define DE_LITTLE_ENDIAN 1
147 
148 #if defined(DE_ENDIANNESS)
149 /* Allow definitions from outside. */
150 #elif (DE_CPU == DE_CPU_X86) || (DE_CPU == DE_CPU_X86_64)
151 /* "detect" x86(_64) endianness */
152 #define DE_ENDIANNESS DE_LITTLE_ENDIAN
153 #elif ((DE_CPU == DE_CPU_MIPS) || (DE_CPU == DE_CPU_MIPS_64))
154 /* detect mips endianness using platform specific macros */
155 #if defined(__MIPSEB__) && !defined(__MIPSEL__)
156 #define DE_ENDIANNESS DE_BIG_ENDIAN
157 #elif !defined(__MIPSEB__) && defined(__MIPSEL__)
158 #define DE_ENDIANNESS DE_LITTLE_ENDIAN
159 #else
160 #error Invalid MIPS endianness.
161 #endif
162 #elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
163 #define DE_ENDIANNESS DE_LITTLE_ENDIAN
164 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
165 #define DE_ENDIANNESS DE_BIG_ENDIAN
166 #else
167 #error Unknown endianness.
168 #endif
169 
170 /* Sanity */
171 #if ((DE_CPU == DE_CPU_X86) || (DE_CPU == DE_CPU_X86_64)) && (DE_ENDIANNESS == DE_BIG_ENDIAN)
172 #error Invalid x86(_64) endianness.
173 #endif
174 
175 /* Sized data types. */
176 /* \note stddef.h is needed for size_t definition. */
177 #include <stddef.h>
178 #include <stdint.h>
179 #include <stdbool.h>
180 
181 #include "deDefs_kc_cts.h"
182 
183 /* Null pointer. */
184 #if defined(__cplusplus)
185 #define DE_NULL 0
186 #else
187 #define DE_NULL ((void *)0) /*!< Null pointer.                              */
188 #endif
189 
190 /* Function pointer type. */
191 typedef void (*deFunctionPtr)(void);
192 
193 /* Use DE_PTR_TYPE(T) instead of T* in macros to avoid clang-tidy warning. */
194 #define DE_PTR_TYPE(T) T * /* NOLINT(T) */
195 
196 /* Debug macro. */
197 #if defined(DE_DEBUG)
198 /* Already defined from outside. */
199 #else
200 #if (DE_COMPILER != DE_COMPILER_GCC)
201 #if defined(_DEBUG)
202 #define DE_DEBUG /*!< Debug build enabled? Usage: #if defined(DE_DEBUG).    */
203 #endif
204 #elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
205 #if !defined(NDEBUG)
206 #define DE_DEBUG
207 #endif
208 #endif
209 #endif
210 
211 /* Inline. */
212 #if (DE_COMPILER == DE_COMPILER_MSC)
213 #define DE_INLINE __forceinline
214 #elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
215 #define DE_INLINE static __inline__
216 #else
217 #define DE_INLINE inline /*!< Function inline.        */
218 #endif
219 
220 /* DE_DEV_BUILD -- only define when building on a development machine. */
221 #if !defined(DE_DEV_BUILD)
222 #if (DE_COMPILER == DE_COMPILER_MSC)
223 #define DE_DEV_BUILD
224 #endif
225 #endif
226 
227 /* DE_VALGRIND_BUILD -- define this in makefile if support for Valgrind is wanted. */
228 /*#define DE_VALGRIND_BUILD*/
229 
230 /** Length of array. C++ version does compile time check that passed value is an array reference. */
231 #if defined(__cplusplus)
232 // deArraySizeHelper is a function that receives a reference to an array of N elements of type T and returns a reference to an
233 // array of N chars. This forces the compiler to check the argument is an actual array and not some other type implementing
234 // operator[]. The actual function is never defined anywhere, but taking the sizeof() of the result is allowed and equal to N.
235 template <typename T, size_t N>
236 char (&deArraySizeHelper(T (&array)[N]))[N];
237 #define DE_LENGTH_OF_ARRAY(ARRAY) ((int)(sizeof(deArraySizeHelper(ARRAY))))
238 #else
239 #define DE_LENGTH_OF_ARRAY(ARRAY) ((int)(sizeof(ARRAY) / sizeof((ARRAY)[0])))
240 #endif
241 
242 #ifdef __cplusplus
243 extern "C"
244 {
245 #endif
246 
247     /* Assertion macro family. */
248     void deAssertFail(const char *reason, const char *file, int line);
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 /* Assertion macro. */
255 #if defined(DE_DEBUG) && !defined(DE_COVERAGE_BUILD)
256 #define DE_ASSERT(X)                              \
257     do                                            \
258     {                                             \
259         if (!(X))                                 \
260             deAssertFail(#X, __FILE__, __LINE__); \
261     } while (false)
262 #else
263 #define DE_ASSERT(X) /*@ -noeffect*/ ((void)0) /*!< Assertion macro. */
264 #endif
265 
266 /* Verify macro. Behaves like assert in debug build, but executes statement in release build. */
267 #if defined(DE_DEBUG)
268 #define DE_VERIFY(X)                              \
269     do                                            \
270     {                                             \
271         if (!(X))                                 \
272             deAssertFail(#X, __FILE__, __LINE__); \
273     } while (false)
274 #else
275 #define DE_VERIFY(X) X
276 #endif
277 
278 /* Fatal macro. */
279 #if defined(DE_DEBUG) && !defined(DE_COVERAGE_BUILD)
280 #define DE_FATAL(MSG)                                                           \
281     do                                                                          \
282     {                                                                           \
283         deAssertFail("" /* force to string literal */ MSG, __FILE__, __LINE__); \
284     } while (false)
285 #else
286 #define DE_FATAL(MSG) /*@ -noeffect*/ ((void)0) /*!< Fatal macro. */
287 #endif
288 
289 /** Test assert macro for use in testers (same as DE_ASSERT, but always enabled). */
290 #define DE_TEST_ASSERT(X)                         \
291     do                                            \
292     {                                             \
293         if (!(X))                                 \
294             deAssertFail(#X, __FILE__, __LINE__); \
295     } while (false)
296 
297 #if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
298     /* GCC 4.8 and newer warns about unused typedefs. */
299 #define DE_UNUSED_ATTR __attribute__((unused))
300 #else
301 #define DE_UNUSED_ATTR
302 #endif
303 
304 /** Compile-time assertion macro. */
305 #define DE_STATIC_ASSERT(X) typedef char DE_UNIQUE_NAME[(X) ? 1 : -1] DE_UNUSED_ATTR
306 
307 #define DE_UNIQUE_NAME DE_MAKE_NAME(__LINE__, hoax)
308 #define DE_MAKE_NAME(line, token) DE_MAKE_NAME2(line, token)
309 #define DE_MAKE_NAME2(line, token) _static_assert_##line##_##token
310 
311 /** Software breakpoint. */
312 #if (DE_CPU == DE_CPU_X86) && (DE_COMPILER == DE_COMPILER_MSC)
313 #define DE_BREAKPOINT()                                                                 \
314     do                                                                                  \
315     {                                                                                   \
316         printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); \
317         __asm { int 3 }                                                                  \
318     } while (false)
319 #elif (DE_CPU == DE_CPU_X86_64) && (DE_COMPILER == DE_COMPILER_MSC)
320 #define DE_BREAKPOINT()                                                                 \
321     do                                                                                  \
322     {                                                                                   \
323         printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); \
324         __debugbreak();                                                                 \
325     } while (false)
326 #elif (DE_CPU == DE_CPU_ARM) && (DE_COMPILER == DE_COMPILER_GCC)
327 #define DE_BREAKPOINT()                                                                 \
328     do                                                                                  \
329     {                                                                                   \
330         printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); \
331         __asm__ __volatile__("bkpt #3");                                                \
332     } while (false)
333 #elif (DE_CPU == DE_CPU_ARM_64) && (DE_COMPILER == DE_COMPILER_GCC)
334 #define DE_BREAKPOINT()                                                                 \
335     do                                                                                  \
336     {                                                                                   \
337         printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); \
338         __asm__ __volatile__("brk #3");                                                 \
339     } while (false)
340 #elif ((DE_CPU == DE_CPU_ARM) || (DE_CPU == DE_CPU_ARM_64)) && (DE_COMPILER == DE_COMPILER_MSC)
341 #define DE_BREAKPOINT()                                                                 \
342     do                                                                                  \
343     {                                                                                   \
344         printf("Software breakpoint encountered in %s, line %d\n", __FILE__, __LINE__); \
345         DebugBreak();                                                                   \
346     } while (false)
347 #else
348 #define DE_BREAKPOINT() DE_FATAL("Software breakpoint encountered!")
349 #endif
350 
351 /** Swap two values. */
352 #define DE_SWAP(TYPE, A, B) \
353     do                      \
354     {                       \
355         TYPE _tmp_ = (A);   \
356         (A)        = (B);   \
357         (B)        = _tmp_; \
358     } while (false)
359 
360 /** Used in enum to easify declarations for struct serialization. Declares 'NAME'_OFFSET, 'NAME'_SIZE, and offsets counter for next enum value by SIZE. */
361 #define DE_SERIALIZED_FIELD(NAME, SIZE) NAME##_OFFSET, NAME##_SIZE = (SIZE), _DE_TMP_##NAME = NAME##_OFFSET + (SIZE)-1
362 
363 /* Pointer size. */
364 #if defined(DE_PTR_SIZE)
365     /* nada */
366 #elif defined(_M_X64) || defined(__x86_64__) || defined(__amd64__) || defined(__aarch64__) || \
367     (defined(__mips) && ((__mips) == 64)) || defined(_LP64) || defined(__LP64__)
368 #define DE_PTR_SIZE 8
369 #else
370 #define DE_PTR_SIZE 4 /* default to 32-bit */
371 #endif
372 
373 /* Floating-point environment flag. */
374 #if defined(DE_FENV_ACCESS_ON)
375     /* Already defined */
376 #elif (DE_COMPILER == DE_COMPILER_CLANG) && (DE_CPU == DE_CPU_ARM)
377 // FENV_ACCESS is not supported, disable all optimizations to avoid incorrect fp operation ordering
378 // Google Bug: b/298204279
379 #define DE_FENV_ACCESS_ON _Pragma("clang optimize off")
380 #elif (DE_COMPILER == DE_COMPILER_CLANG) && (DE_CPU != DE_CPU_ARM)
381 #define DE_FENV_ACCESS_ON _Pragma("STDC FENV_ACCESS ON")
382 #elif (DE_COMPILER == DE_COMPILER_MSC)
383 #define DE_FENV_ACCESS_ON __pragma(fenv_access(on))
384 #else
385 #define DE_FENV_ACCESS_ON /* not supported */
386 #endif
387 
388 /** Unreferenced variable silencing. */
389 #define DE_UNREF(VAR) ((void)(VAR))
390 
391 /** DE_BEGIN_EXTERN_C and DE_END_EXTERN_C. */
392 #if defined(__cplusplus)
393 #define DE_BEGIN_EXTERN_C \
394     extern "C"            \
395     {
396 #define DE_END_EXTERN_C }
397 #else
398 #define DE_BEGIN_EXTERN_C
399 #define DE_END_EXTERN_C
400 #endif
401 
402 /** DE_NULL_STATEMENT */
403 #if defined(DE_DEBUG)
404 #define DE_NULL_STATEMENT \
405     do                    \
406     {                     \
407     } while (false)
408 #else
409 #define DE_NULL_STATEMENT (void)0
410 #endif
411 
412 /** GCC format string attributes */
413 #if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
414 #define DE_PRINTF_FUNC_ATTR(FORMAT_STRING, FIRST_ARG) __attribute__((format(printf, FORMAT_STRING, FIRST_ARG)))
415 #else
416 #define DE_PRINTF_FUNC_ATTR(FORMAT_STRING, FIRST_ARG)
417 #endif
418 
419 /** Potentially unused func attribute to silence warnings from C templates. */
420 #if (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
421 #define DE_UNUSED_FUNCTION __attribute__((unused))
422 #else
423 #define DE_UNUSED_FUNCTION
424 #endif
425 
426 #if (DE_COMPILER == DE_COMPILER_MSC)
427 #define DE_PACKED(...) __pragma(pack(push, 1)) typedef struct __VA_ARGS__ __pragma(pack(pop))
428 #elif (DE_COMPILER == DE_COMPILER_GCC) || (DE_COMPILER == DE_COMPILER_CLANG)
429 #define DE_PACKED(...) typedef struct __attribute__((__packed__)) __VA_ARGS__
430 #else
431 #define DE_PACKED
432 #endif
433 
deFatalStr(const char * reason)434     DE_INLINE const char *deFatalStr(const char *reason)
435     {
436         DE_ASSERT(0);
437         return reason;
438     }
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 #endif /* _DEDEFS_H */
445