Zycore  1.2.0.0
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 #else
140 # error "Unsupported architecture detected"
141 #endif
142 
143 /* ============================================================================================== */
144 /* Debug/Release detection */
145 /* ============================================================================================== */
146 
147 #if defined(ZYAN_MSVC) || defined(ZYAN_BORLAND)
148 # ifdef _DEBUG
149 # define ZYAN_DEBUG
150 # else
151 # define ZYAN_RELEASE
152 # endif
153 #elif defined(ZYAN_GNUC) || defined(ZYAN_ICC)
154 # ifdef NDEBUG
155 # define ZYAN_RELEASE
156 # else
157 # define ZYAN_DEBUG
158 # endif
159 #else
160 # define ZYAN_RELEASE
161 #endif
162 
163 /* ============================================================================================== */
164 /* Deprecation hint */
165 /* ============================================================================================== */
166 
167 #if defined(ZYAN_GCC) || defined(ZYAN_CLANG)
168 # define ZYAN_DEPRECATED __attribute__((__deprecated__))
169 #elif defined(ZYAN_MSVC)
170 # define ZYAN_DEPRECATED __declspec(deprecated)
171 #else
172 # define ZYAN_DEPRECATED
173 #endif
174 
175 /* ============================================================================================== */
176 /* Generic DLL import/export helpers */
177 /* ============================================================================================== */
178 
179 #if defined(ZYAN_MSVC)
180 # define ZYAN_DLLEXPORT __declspec(dllexport)
181 # define ZYAN_DLLIMPORT __declspec(dllimport)
182 #else
183 # define ZYAN_DLLEXPORT
184 # define ZYAN_DLLIMPORT
185 #endif
186 
187 /* ============================================================================================== */
188 /* Zycore dll{export,import} */
189 /* ============================================================================================== */
190 
191 // This is a cut-down version of what CMake's `GenerateExportHeader` would usually generate. To
192 // simplify builds without CMake, we define these things manually instead of relying on CMake
193 // to generate the header.
194 //
195 // For static builds, our CMakeList will define `ZYCORE_STATIC_BUILD`. For shared library builds,
196 // our CMake will define `ZYCORE_SHOULD_EXPORT` depending on whether the target is being imported or
197 // exported. If CMake isn't used, users can manually define these to fit their use-case.
198 
199 // Backward compatibility: CMake would previously generate these variables names. However, because
200 // they have pretty cryptic names, we renamed them when we got rid of `GenerateExportHeader`. For
201 // backward compatibility for users that don't use CMake and previously manually defined these, we
202 // translate the old defines here and print a warning.
203 #if defined(ZYCORE_STATIC_DEFINE)
204 # pragma message("ZYCORE_STATIC_DEFINE was renamed to ZYCORE_STATIC_BUILD.")
205 # define ZYCORE_STATIC_BUILD
206 #endif
207 #if defined(Zycore_EXPORTS)
208 # pragma message("Zycore_EXPORTS was renamed to ZYCORE_SHOULD_EXPORT.")
209 # define ZYCORE_SHOULD_EXPORT
210 #endif
211 
215 #if defined(ZYCORE_STATIC_BUILD)
216 # define ZYCORE_EXPORT
217 #else
218 # if defined(ZYCORE_SHOULD_EXPORT)
219 # define ZYCORE_EXPORT ZYAN_DLLEXPORT
220 # else
221 # define ZYCORE_EXPORT ZYAN_DLLIMPORT
222 # endif
223 #endif
224 
228 #define ZYCORE_NO_EXPORT
229 
230 /* ============================================================================================== */
231 /* Misc compatibility macros */
232 /* ============================================================================================== */
233 
234 #if defined(ZYAN_CLANG)
235 # define ZYAN_NO_SANITIZE(what) __attribute__((no_sanitize(what)))
236 #else
237 # define ZYAN_NO_SANITIZE(what)
238 #endif
239 
240 #if defined(ZYAN_MSVC) || defined(ZYAN_BORLAND)
241 # define ZYAN_INLINE __inline
242 #else
243 # define ZYAN_INLINE static inline
244 #endif
245 
246 #if defined(ZYAN_MSVC)
247 # define ZYAN_NOINLINE __declspec(noinline)
248 #elif defined(ZYAN_GCC) || defined(ZYAN_CLANG)
249 # define ZYAN_NOINLINE __attribute__((noinline))
250 #else
251 # define ZYAN_NOINLINE
252 #endif
253 
254 /* ============================================================================================== */
255 /* Debugging and optimization macros */
256 /* ============================================================================================== */
257 
261 #if defined(ZYAN_NO_LIBC)
262 # define ZYAN_ASSERT(condition) (void)(condition)
263 #elif defined(ZYAN_WINDOWS) && defined(ZYAN_KERNEL)
264 # include <wdm.h>
265 # define ZYAN_ASSERT(condition) NT_ASSERT(condition)
266 #else
267 # include <assert.h>
268 # define ZYAN_ASSERT(condition) assert(condition)
269 #endif
270 
274 #if __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
275 # define ZYAN_STATIC_ASSERT(x) _Static_assert(x, #x)
276 #elif (defined(__cplusplus) && __cplusplus >= 201103L) || \
277  (defined(__cplusplus) && defined (_MSC_VER) && (_MSC_VER >= 1600)) || \
278  (defined (_MSC_VER) && (_MSC_VER >= 1800))
279 # define ZYAN_STATIC_ASSERT(x) static_assert(x, #x)
280 #else
281 # define ZYAN_STATIC_ASSERT(x) \
282  typedef int ZYAN_MACRO_CONCAT_EXPAND(ZYAN_SASSERT_, __COUNTER__) [(x) ? 1 : -1]
283 #endif
284 
288 #if defined(ZYAN_RELEASE)
289 # if defined(ZYAN_CLANG) // GCC eagerly evals && RHS, we have to use nested ifs.
290 # if __has_builtin(__builtin_unreachable)
291 # define ZYAN_UNREACHABLE __builtin_unreachable()
292 # else
293 # define ZYAN_UNREACHABLE for(;;)
294 # endif
295 # elif defined(ZYAN_GCC) && ((__GNUC__ == 4 && __GNUC_MINOR__ > 4) || __GNUC__ > 4)
296 # define ZYAN_UNREACHABLE __builtin_unreachable()
297 # elif defined(ZYAN_ICC)
298 # ifdef ZYAN_WINDOWS
299 # include <stdlib.h> // "missing return statement" workaround
300 # define ZYAN_UNREACHABLE __assume(0); (void)abort()
301 # else
302 # define ZYAN_UNREACHABLE __builtin_unreachable()
303 # endif
304 # elif defined(ZYAN_MSVC)
305 # define ZYAN_UNREACHABLE __assume(0)
306 # else
307 # define ZYAN_UNREACHABLE for(;;)
308 # endif
309 #elif defined(ZYAN_NO_LIBC)
310 # define ZYAN_UNREACHABLE for(;;)
311 #elif defined(ZYAN_WINDOWS) && defined(ZYAN_KERNEL)
312 # define ZYAN_UNREACHABLE { __fastfail(0); for(;;){} }
313 #else
314 # include <stdlib.h>
315 # define ZYAN_UNREACHABLE { assert(0); abort(); }
316 #endif
317 
318 /* ============================================================================================== */
319 /* Utils */
320 /* ============================================================================================== */
321 
322 /* ---------------------------------------------------------------------------------------------- */
323 /* General purpose */
324 /* ---------------------------------------------------------------------------------------------- */
325 
331 #define ZYAN_UNUSED(x) (void)(x)
332 
336 #if defined(ZYAN_GCC) && __GNUC__ >= 7
337 # define ZYAN_FALLTHROUGH __attribute__((__fallthrough__))
338 #else
339 # define ZYAN_FALLTHROUGH
340 #endif
341 
347 #define ZYAN_BITFIELD(x) : x
348 
352 #define ZYAN_REQUIRES_LIBC
353 
360 #if defined(__RESHARPER__)
361 # define ZYAN_PRINTF_ATTR(format_index, first_to_check) \
362  [[gnu::format(printf, format_index, first_to_check)]]
363 #elif defined(ZYAN_GCC)
364 # define ZYAN_PRINTF_ATTR(format_index, first_to_check) \
365  __attribute__((format(printf, format_index, first_to_check)))
366 #else
367 # define ZYAN_PRINTF_ATTR(format_index, first_to_check)
368 #endif
369 
376 #if defined(__RESHARPER__)
377 # define ZYAN_WPRINTF_ATTR(format_index, first_to_check) \
378  [[rscpp::format(wprintf, format_index, first_to_check)]]
379 #else
380 # define ZYAN_WPRINTF_ATTR(format_index, first_to_check)
381 #endif
382 
383 /* ---------------------------------------------------------------------------------------------- */
384 /* Arrays */
385 /* ---------------------------------------------------------------------------------------------- */
386 
394 #define ZYAN_ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0]))
395 
396 /* ---------------------------------------------------------------------------------------------- */
397 /* Arithmetic */
398 /* ---------------------------------------------------------------------------------------------- */
399 
408 #define ZYAN_MIN(a, b) (((a) < (b)) ? (a) : (b))
409 
418 #define ZYAN_MAX(a, b) (((a) > (b)) ? (a) : (b))
419 
427 #define ZYAN_ABS(a) (((a) < 0) ? -(a) : (a))
428 
438 #define ZYAN_IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
439 
445 #define ZYAN_IS_ALIGNED_TO(x, align) (((x) & ((align) - 1)) == 0)
446 
457 #define ZYAN_ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1))
458 
469 #define ZYAN_ALIGN_DOWN(x, align) (((x) - 1) & ~((align) - 1))
470 
471 /* ---------------------------------------------------------------------------------------------- */
472 /* Bit operations */
473 /* ---------------------------------------------------------------------------------------------- */
474 
475 /*
476  * Checks, if the bit at index `b` is required to present the ordinal value `n`.
477  *
478  * @param n The ordinal value.
479  * @param b The bit index.
480  *
481  * @return `ZYAN_TRUE`, if the bit at index `b` is required to present the ordinal value `n` or
482  * `ZYAN_FALSE`, if not.
483  *
484  * Note that this macro always returns `ZYAN_FALSE` for `n == 0`.
485  */
486 #define ZYAN_NEEDS_BIT(n, b) (((unsigned long)(n) >> (b)) > 0)
487 
488 /*
489  * Returns the number of bits required to represent the ordinal value `n`.
490  *
491  * @param n The ordinal value.
492  *
493  * @return The number of bits required to represent the ordinal value `n`.
494  *
495  * Note that this macro returns `0` for `n == 0`.
496  */
497 #define ZYAN_BITS_TO_REPRESENT(n) \
498  ( \
499  ZYAN_NEEDS_BIT(n, 0) + ZYAN_NEEDS_BIT(n, 1) + \
500  ZYAN_NEEDS_BIT(n, 2) + ZYAN_NEEDS_BIT(n, 3) + \
501  ZYAN_NEEDS_BIT(n, 4) + ZYAN_NEEDS_BIT(n, 5) + \
502  ZYAN_NEEDS_BIT(n, 6) + ZYAN_NEEDS_BIT(n, 7) + \
503  ZYAN_NEEDS_BIT(n, 8) + ZYAN_NEEDS_BIT(n, 9) + \
504  ZYAN_NEEDS_BIT(n, 10) + ZYAN_NEEDS_BIT(n, 11) + \
505  ZYAN_NEEDS_BIT(n, 12) + ZYAN_NEEDS_BIT(n, 13) + \
506  ZYAN_NEEDS_BIT(n, 14) + ZYAN_NEEDS_BIT(n, 15) + \
507  ZYAN_NEEDS_BIT(n, 16) + ZYAN_NEEDS_BIT(n, 17) + \
508  ZYAN_NEEDS_BIT(n, 18) + ZYAN_NEEDS_BIT(n, 19) + \
509  ZYAN_NEEDS_BIT(n, 20) + ZYAN_NEEDS_BIT(n, 21) + \
510  ZYAN_NEEDS_BIT(n, 22) + ZYAN_NEEDS_BIT(n, 23) + \
511  ZYAN_NEEDS_BIT(n, 24) + ZYAN_NEEDS_BIT(n, 25) + \
512  ZYAN_NEEDS_BIT(n, 26) + ZYAN_NEEDS_BIT(n, 27) + \
513  ZYAN_NEEDS_BIT(n, 28) + ZYAN_NEEDS_BIT(n, 29) + \
514  ZYAN_NEEDS_BIT(n, 30) + ZYAN_NEEDS_BIT(n, 31) \
515  )
516 
517 /* ---------------------------------------------------------------------------------------------- */
518 
519 /* ============================================================================================== */
520 
521 #endif /* ZYCORE_DEFINES_H */