Zycore 1.4.0.0
Loading...
Searching...
No Matches
Defines.h
Go to the documentation of this file.
1/***************************************************************************************************
2
3 Zyan Core Library (Zycore-C)
4
5 Original Author : Florian Bernd, Joel Hoener
6
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24
25***************************************************************************************************/
26
32#ifndef ZYCORE_DEFINES_H
33#define ZYCORE_DEFINES_H
34
35/* ============================================================================================== */
36/* Meta macros */
37/* ============================================================================================== */
38
47#define ZYAN_MACRO_CONCAT(x, y) x ## y
48
58#define ZYAN_MACRO_CONCAT_EXPAND(x, y) ZYAN_MACRO_CONCAT(x, y)
59
60/* ============================================================================================== */
61/* Compiler detection */
62/* ============================================================================================== */
63
64#if defined(__clang__)
65# define ZYAN_CLANG
66# define ZYAN_GNUC
67#elif defined(__ICC) || defined(__INTEL_COMPILER)
68# define ZYAN_ICC
69#elif defined(__GNUC__) || defined(__GNUG__)
70# define ZYAN_GCC
71# define ZYAN_GNUC
72#elif defined(_MSC_VER)
73# define ZYAN_MSVC
74#elif defined(__BORLANDC__)
75# define ZYAN_BORLAND
76#else
77# define ZYAN_UNKNOWN_COMPILER
78#endif
79
80/* ============================================================================================== */
81/* Platform detection */
82/* ============================================================================================== */
83
84#if defined(_WIN32)
85# define ZYAN_WINDOWS
86#elif defined(__EMSCRIPTEN__)
87# define ZYAN_EMSCRIPTEN
88#elif defined(__wasi__) || defined(__WASI__)
89// via: https://reviews.llvm.org/D57155
90# define ZYAN_WASI
91#elif defined(__APPLE__)
92# define ZYAN_APPLE
93# define ZYAN_POSIX
94#elif defined(__linux)
95# define ZYAN_LINUX
96# define ZYAN_POSIX
97#elif defined(__FreeBSD__)
98# define ZYAN_FREEBSD
99# define ZYAN_POSIX
100#elif defined(sun) || defined(__sun)
101# define ZYAN_SOLARIS
102# define ZYAN_POSIX
103#elif defined(__unix)
104# define ZYAN_UNIX
105# define ZYAN_POSIX
106#elif defined(__posix)
107# define ZYAN_POSIX
108#else
109# define ZYAN_UNKNOWN_PLATFORM
110#endif
111
112/* ============================================================================================== */
113/* Kernel mode detection */
114/* ============================================================================================== */
115
116#if (defined(ZYAN_WINDOWS) && defined(_KERNEL_MODE)) || \
117 (defined(ZYAN_APPLE) && defined(KERNEL)) || \
118 (defined(ZYAN_LINUX) && defined(__KERNEL__)) || \
119 (defined(__FreeBSD_kernel__))
120# define ZYAN_KERNEL
121#else
122# define ZYAN_USER
123#endif
124
125/* ============================================================================================== */
126/* Architecture detection */
127/* ============================================================================================== */
128
129#if defined(_M_AMD64) || defined(__x86_64__)
130# define ZYAN_X64
131#elif defined(_M_IX86) || defined(__i386__)
132# define ZYAN_X86
133#elif defined(_M_ARM64) || defined(__aarch64__)
134# define ZYAN_AARCH64
135#elif defined(_M_ARM) || defined(_M_ARMT) || defined(__arm__) || defined(__thumb__)
136# define ZYAN_ARM
137#elif defined(__EMSCRIPTEN__) || defined(__wasm__) || defined(__WASM__)
138# define ZYAN_WASM
139#elif defined(__loongarch__)
140# define ZYAN_LOONGARCH
141#elif defined(__powerpc64__)
142# define ZYAN_PPC64
143#elif defined(__powerpc__)
144# define ZYAN_PPC
145#elif defined(__riscv) && __riscv_xlen == 64
146# define ZYAN_RISCV64
147#else
148# error "Unsupported architecture detected"
149#endif
150
151/* ============================================================================================== */
152/* Debug/Release detection */
153/* ============================================================================================== */
154
155#if defined(ZYAN_MSVC) || defined(ZYAN_BORLAND)
156# ifdef _DEBUG
157# define ZYAN_DEBUG
158# else
159# define ZYAN_RELEASE
160# endif
161#elif defined(ZYAN_GNUC) || defined(ZYAN_ICC)
162# ifdef NDEBUG
163# define ZYAN_RELEASE
164# else
165# define ZYAN_DEBUG
166# endif
167#else
168# define ZYAN_RELEASE
169#endif
170
171/* ============================================================================================== */
172/* Deprecation hint */
173/* ============================================================================================== */
174
175#if defined(ZYAN_GCC) || defined(ZYAN_CLANG)
176# define ZYAN_DEPRECATED __attribute__((__deprecated__))
177#elif defined(ZYAN_MSVC)
178# define ZYAN_DEPRECATED __declspec(deprecated)
179#else
180# define ZYAN_DEPRECATED
181#endif
182
183/* ============================================================================================== */
184/* Generic DLL import/export helpers */
185/* ============================================================================================== */
186
187#if defined(ZYAN_MSVC)
188# define ZYAN_DLLEXPORT __declspec(dllexport)
189# define ZYAN_DLLIMPORT __declspec(dllimport)
190#else
191# define ZYAN_DLLEXPORT
192# define ZYAN_DLLIMPORT
193#endif
194
195/* ============================================================================================== */
196/* Zycore dll{export,import} */
197/* ============================================================================================== */
198
199// This is a cut-down version of what CMake's `GenerateExportHeader` would usually generate. To
200// simplify builds without CMake, we define these things manually instead of relying on CMake
201// to generate the header.
202//
203// For static builds, our CMakeList will define `ZYCORE_STATIC_BUILD`. For shared library builds,
204// our CMake will define `ZYCORE_SHOULD_EXPORT` depending on whether the target is being imported or
205// exported. If CMake isn't used, users can manually define these to fit their use-case.
206
207// Backward compatibility: CMake would previously generate these variables names. However, because
208// they have pretty cryptic names, we renamed them when we got rid of `GenerateExportHeader`. For
209// backward compatibility for users that don't use CMake and previously manually defined these, we
210// translate the old defines here and print a warning.
211#if defined(ZYCORE_STATIC_DEFINE)
212# pragma message("ZYCORE_STATIC_DEFINE was renamed to ZYCORE_STATIC_BUILD.")
213# define ZYCORE_STATIC_BUILD
214#endif
215#if defined(Zycore_EXPORTS)
216# pragma message("Zycore_EXPORTS was renamed to ZYCORE_SHOULD_EXPORT.")
217# define ZYCORE_SHOULD_EXPORT
218#endif
219
223#if defined(ZYCORE_STATIC_BUILD)
224# define ZYCORE_EXPORT
225#else
226# if defined(ZYCORE_SHOULD_EXPORT)
227# define ZYCORE_EXPORT ZYAN_DLLEXPORT
228# else
229# define ZYCORE_EXPORT ZYAN_DLLIMPORT
230# endif
231#endif
232
236#define ZYCORE_NO_EXPORT
237
238/* ============================================================================================== */
239/* Misc compatibility macros */
240/* ============================================================================================== */
241
242#if defined(ZYAN_CLANG)
243# define ZYAN_NO_SANITIZE(what) __attribute__((no_sanitize(what)))
244#else
245# define ZYAN_NO_SANITIZE(what)
246#endif
247
248#if defined(ZYAN_MSVC) || defined(ZYAN_BORLAND)
249# define ZYAN_INLINE __inline
250#else
251# define ZYAN_INLINE static inline
252#endif
253
254#if defined(ZYAN_MSVC)
255# define ZYAN_NOINLINE __declspec(noinline)
256#elif defined(ZYAN_GCC) || defined(ZYAN_CLANG)
257# define ZYAN_NOINLINE __attribute__((noinline))
258#else
259# define ZYAN_NOINLINE
260#endif
261
262/* ============================================================================================== */
263/* Debugging and optimization macros */
264/* ============================================================================================== */
265
269#if defined(ZYAN_NO_LIBC)
270# define ZYAN_ASSERT(condition) (void)(condition)
271#elif defined(ZYAN_WINDOWS) && defined(ZYAN_KERNEL)
272# include <wdm.h>
273# define ZYAN_ASSERT(condition) NT_ASSERT(condition)
274#else
275# include <assert.h>
276# define ZYAN_ASSERT(condition) assert(condition)
277#endif
278
282#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
283# define ZYAN_STATIC_ASSERT(x) _Static_assert(x, #x)
284#elif (defined(__cplusplus) && __cplusplus >= 201103L) || \
285 (defined(__cplusplus) && defined (_MSC_VER) && (_MSC_VER >= 1600)) || \
286 (defined (_MSC_VER) && (_MSC_VER >= 1800))
287# define ZYAN_STATIC_ASSERT(x) static_assert(x, #x)
288#else
289# define ZYAN_STATIC_ASSERT(x) \
290 typedef int ZYAN_MACRO_CONCAT_EXPAND(ZYAN_SASSERT_, __COUNTER__) [(x) ? 1 : -1]
291#endif
292
296#if defined(ZYAN_RELEASE)
297# if defined(ZYAN_CLANG) // GCC eagerly evals && RHS, we have to use nested ifs.
298# if __has_builtin(__builtin_unreachable)
299# define ZYAN_UNREACHABLE __builtin_unreachable()
300# else
301# define ZYAN_UNREACHABLE for(;;)
302# endif
303# elif defined(ZYAN_GCC) && ((__GNUC__ == 4 && __GNUC_MINOR__ > 4) || __GNUC__ > 4)
304# define ZYAN_UNREACHABLE __builtin_unreachable()
305# elif defined(ZYAN_ICC)
306# ifdef ZYAN_WINDOWS
307# include <stdlib.h> // "missing return statement" workaround
308# define ZYAN_UNREACHABLE __assume(0); (void)abort()
309# else
310# define ZYAN_UNREACHABLE __builtin_unreachable()
311# endif
312# elif defined(ZYAN_MSVC)
313# define ZYAN_UNREACHABLE __assume(0)
314# else
315# define ZYAN_UNREACHABLE for(;;)
316# endif
317#elif defined(ZYAN_NO_LIBC)
318# define ZYAN_UNREACHABLE for(;;)
319#elif defined(ZYAN_WINDOWS) && defined(ZYAN_KERNEL)
320# define ZYAN_UNREACHABLE { __fastfail(0); for(;;){} }
321#else
322# include <stdlib.h>
323# define ZYAN_UNREACHABLE { assert(0); abort(); }
324#endif
325
326/* ============================================================================================== */
327/* Utils */
328/* ============================================================================================== */
329
330/* ---------------------------------------------------------------------------------------------- */
331/* General purpose */
332/* ---------------------------------------------------------------------------------------------- */
333
339#define ZYAN_UNUSED(x) (void)(x)
340
344#if defined(ZYAN_GCC) && __GNUC__ >= 7
345# define ZYAN_FALLTHROUGH ; __attribute__((__fallthrough__))
346#else
347# define ZYAN_FALLTHROUGH
348#endif
349
355#define ZYAN_BITFIELD(x) : x
356
360#define ZYAN_REQUIRES_LIBC
361
368#if defined(__RESHARPER__)
369# define ZYAN_PRINTF_ATTR(format_index, first_to_check) \
370 [[gnu::format(printf, format_index, first_to_check)]]
371#elif defined(ZYAN_GCC)
372# define ZYAN_PRINTF_ATTR(format_index, first_to_check) \
373 __attribute__((format(printf, format_index, first_to_check)))
374#else
375# define ZYAN_PRINTF_ATTR(format_index, first_to_check)
376#endif
377
384#if defined(__RESHARPER__)
385# define ZYAN_WPRINTF_ATTR(format_index, first_to_check) \
386 [[rscpp::format(wprintf, format_index, first_to_check)]]
387#else
388# define ZYAN_WPRINTF_ATTR(format_index, first_to_check)
389#endif
390
391/* ---------------------------------------------------------------------------------------------- */
392/* Arrays */
393/* ---------------------------------------------------------------------------------------------- */
394
402#define ZYAN_ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0]))
403
404/* ---------------------------------------------------------------------------------------------- */
405/* Arithmetic */
406/* ---------------------------------------------------------------------------------------------- */
407
416#define ZYAN_MIN(a, b) (((a) < (b)) ? (a) : (b))
417
426#define ZYAN_MAX(a, b) (((a) > (b)) ? (a) : (b))
427
435#define ZYAN_ABS(a) (((a) < 0) ? -(a) : (a))
436
446#define ZYAN_IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
447
453#define ZYAN_IS_ALIGNED_TO(x, align) (((x) & ((align) - 1)) == 0)
454
465#define ZYAN_ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1))
466
477#define ZYAN_ALIGN_DOWN(x, align) (((x) - 1) & ~((align) - 1))
478
479/* ---------------------------------------------------------------------------------------------- */
480/* Bit operations */
481/* ---------------------------------------------------------------------------------------------- */
482
483/*
484 * Checks, if the bit at index `b` is required to present the ordinal value `n`.
485 *
486 * @param n The ordinal value.
487 * @param b The bit index.
488 *
489 * @return `ZYAN_TRUE`, if the bit at index `b` is required to present the ordinal value `n` or
490 * `ZYAN_FALSE`, if not.
491 *
492 * Note that this macro always returns `ZYAN_FALSE` for `n == 0`.
493 */
494#define ZYAN_NEEDS_BIT(n, b) (((unsigned long)(n) >> (b)) > 0)
495
496/*
497 * Returns the number of bits required to represent the ordinal value `n`.
498 *
499 * @param n The ordinal value.
500 *
501 * @return The number of bits required to represent the ordinal value `n`.
502 *
503 * Note that this macro returns `0` for `n == 0`.
504 */
505#define ZYAN_BITS_TO_REPRESENT(n) \
506 ( \
507 ZYAN_NEEDS_BIT(n, 0) + ZYAN_NEEDS_BIT(n, 1) + \
508 ZYAN_NEEDS_BIT(n, 2) + ZYAN_NEEDS_BIT(n, 3) + \
509 ZYAN_NEEDS_BIT(n, 4) + ZYAN_NEEDS_BIT(n, 5) + \
510 ZYAN_NEEDS_BIT(n, 6) + ZYAN_NEEDS_BIT(n, 7) + \
511 ZYAN_NEEDS_BIT(n, 8) + ZYAN_NEEDS_BIT(n, 9) + \
512 ZYAN_NEEDS_BIT(n, 10) + ZYAN_NEEDS_BIT(n, 11) + \
513 ZYAN_NEEDS_BIT(n, 12) + ZYAN_NEEDS_BIT(n, 13) + \
514 ZYAN_NEEDS_BIT(n, 14) + ZYAN_NEEDS_BIT(n, 15) + \
515 ZYAN_NEEDS_BIT(n, 16) + ZYAN_NEEDS_BIT(n, 17) + \
516 ZYAN_NEEDS_BIT(n, 18) + ZYAN_NEEDS_BIT(n, 19) + \
517 ZYAN_NEEDS_BIT(n, 20) + ZYAN_NEEDS_BIT(n, 21) + \
518 ZYAN_NEEDS_BIT(n, 22) + ZYAN_NEEDS_BIT(n, 23) + \
519 ZYAN_NEEDS_BIT(n, 24) + ZYAN_NEEDS_BIT(n, 25) + \
520 ZYAN_NEEDS_BIT(n, 26) + ZYAN_NEEDS_BIT(n, 27) + \
521 ZYAN_NEEDS_BIT(n, 28) + ZYAN_NEEDS_BIT(n, 29) + \
522 ZYAN_NEEDS_BIT(n, 30) + ZYAN_NEEDS_BIT(n, 31) \
523 )
524
525/* ---------------------------------------------------------------------------------------------- */
526
527/* ============================================================================================== */
528
529#endif /* ZYCORE_DEFINES_H */