2
3
4
5
6
7
8
9
10
11
12
13
14
15
17#ifndef GIMBAL_COMPILER_H
18#define GIMBAL_COMPILER_H
20#define __STDC_WANT_LIB_EXT1__ 1
26#ifdef __STDC_VERSION__
28# if (__STDC_VERSION__ >= 199409L
)
31# if (__STDC_VERSION__ >= 199901L
)
34# if (__STDC_VERSION__ >= 201112L
)
37# if (__STDC_VERSION__ >= 201710L
)
40# if (__STDC_VERSION__ >= 202311L
)
47# if (__cplusplus
>= 199711L
)
50# if (__cplusplus
>= 201103L
)
53# if (__cplusplus
>= 201402L
)
56# if (__cplusplus
>= 201703L
)
59# if (__cplusplus
>= 202002L
)
62# if (__cplusplus
>= 202302L
)
75#elif defined(__clang__
)
77#elif defined(__MINGW32__)
79#elif defined(__MINGW64__)
81#elif defined(__EMSCRIPTEN__)
82# define GBL_EMSCRIPTEN 1
83#elif defined(__GNUC__)
86# define GBL_GREENHILL 1
87#elif defined(__CC_ARM)
89#elif defined(__IAR_SYSTEMS__ICC__)
94# define GBL_NULL nullptr
101# define GBL_EXPORT_SHARED __declspec(dllexport)
102# define GBL_IMPORT_SHARED __declspec(dllimport)
103#elif defined(__clang__
) || defined(__GNUC__
)
104# define GBL_EXPORT_SHARED __attribute__((visibility("default")))
105# define GBL_IMPORT_SHARED
107# define GBL_EXPORT_SHARED
108# define GBL_IMPORT_SHARED
114 _Pragma("float_control(except, off)")
115 _Pragma("fenv_access(off)")
116 _Pragma("float_control(precise, off)")
117 _Pragma("fp_contract(on)")
118# define GBL_FP_PRECISE
119 _Pragma("float_control(precise, on)")
120 _Pragma("fenv_access(on)")
121 _Pragma("float_control(except, on)")
123# define GBL_FP_FAST __attribute__((optimize("-ffast-math")))
124# define GBL_FP_PRECISE __attribute__((optimize("-fno-fast-math")))
131# define GBL_CPP_RTTI 1
132#elif defined(__clang__)
133# if __has_feature(cxx_rtti)
134# define GBL_CPP_RTTI 1
136# define GBL_CPP_RTTI 0
138#elif defined(__GNUG__) && defined(__GXX_RTTI)
139# define GBL_CPP_RTTI 1
140#elif defined(_MSC_VER) && defined(_CPPRTTI)
141# define GBL_CPP_RTTI 1
143# define GBL_CPP_RTTI 0
147#ifdef __cpp_exceptions
148# define GBL_CPP_EXCEPTIONS 1
149# define GBL_NOEXCEPT noexcept
151# define GBL_CPP_EXCEPTIONS 0
156# define GBL_CONSTEXPR constexpr
158# define GBL_CONSTEVAL consteval
160# define GBL_CONSTEVAL constexpr
163# define GBL_CONSTEXPR
164# define GBL_CONSTEVAL
169# define GBL_CPP_CONCEPTS 1
171# define GBL_CPP_CONCEPTS 0
176#ifdef __cpp_lib_source_location
177# define GBL_CPP_SOURCE_LOCATION 1
179# define GBL_CPP_SOURCE_LOCATION 0
183#if defined(__has_cpp_attribute
)
184# if __has_cpp_attribute
(deprecated)
185# define GBL_DEPRECATED(msg) [[deprecated(msg)]]
187# define GBL_DEPRECATED(msg)
190# define GBL_DEPRECATED(msg)
194#if defined(__has_cpp_attribute
)
195# if __has_cpp_attribute
(fallthrough)
196# define GBL_FALLTHROUGH [[fallthrough]]
198# define GBL_FALLTHROUGH
201# define GBL_FALLTHROUGH
205#if defined(__has_cpp_attribute
)
206# if __has_cpp_attribute
(likely)
207# define GBL_LIKELY(exp) (exp) [[likely]]
212# define GBL_LIKELY(exp) (__builtin_expect(!!(exp), 1
))
214# define GBL_LIKELY(exp) (exp)
219#if defined(__has_cpp_attribute
)
220# if __has_cpp_attribute
(unlikely)
221# define GBL_UNLIKELY(exp) (exp) [[unlikely]]
226# define GBL_UNLIKELY(exp) (__builtin_expect(!!(exp), 0
))
228# define GBL_UNLIKELY(exp) (exp)
233#if defined(__has_cpp_attribute
)
234# if __has_cpp_attribute
(maybe_unused)
235# define GBL_MAYBE_UNUSED [[maybe_unused]]
237# define GBL_MAYBE_UNUSED
239#elif defined(__clang__) || defined(__GNUC__)
240# define GBL_MAYBE_UNUSED __attribute__((unused))
242# define GBL_MAYBE_UNUSED
246#if defined(__has_cpp_attribute
)
247# if __has_cpp_attribute
(no_unique_address)
248# define GBL_NO_UNIQUE_ADDRESS [[no_unique_address]]
250# define GBL_NO_UNIQUE_ADDRESS
253# define GBL_NO_UNIQUE_ADDRESS
257#if defined(__has_cpp_attribute
)
258# if __has_cpp_attribute
(nodiscard)
259# define GBL_NODISCARD [[nodiscard]]
261# define GBL_NODISCARD
264# define GBL_NODISCARD
268#if defined(__has_cpp_attribute
)
269# if __has_cpp_attribute
(noreturn)
270# define GBL_NORETURN [[noreturn]]
276# define GBL_NORETURN _Noreturn
282#if __cpp_static_assert
283# if __cpp_static_assert
> 201400
284# define GBL_STATIC_ASSERT(cond) static_assert(cond)
286# define GBL_STATIC_ASSERT(cond) static_assert(cond, #cond)
288# define GBL_STATIC_ASSERT_MSG(cond, msg) static_assert(cond, msg)
289#elif defined(GBL_C11)
290# define GBL_STATIC_ASSERT(cond) _Static_assert(cond, #cond);
291# define GBL_STATIC_ASSERT_MSG(cond, msg) _Static_assert(cond, msg);
293# define GBL_STATIC_ASSERT(cond)
294# define GBL_STATIC_ASSERT_MSG(cond, msg)
298# define GBL_RESTRICT restrict
304# define GBL_STATIC_ARRAY(idx) static idx
306# define GBL_STATIC_ARRAY(idx) idx
310# define GBL_STRUCT_INIT(type, ...) (type{ __VA_ARGS__ })
312# define GBL_STRUCT_INIT(type, ...) ((type){ __VA_ARGS__ })
316# define GBL_ALIGNAS(e) alignas(e)
317# define GBL_ALIGNOF(e) alignof(e)
318#elif defined(GBL_C11)
319# define GBL_ALIGNAS(e) _Alignas(e)
320# define GBL_ALIGNOF(e) _Alignof(e)
324# define GBL_INLINE_ inline
326# define GBL_INLINE_ static inline
332# define GBL_INLINE GBL_MAYBE_UNUSED GBL_INLINE_
336# define GBL_FORCE_INLINE __attribute__((always_inline)) GBL_INLINE_
337#elif defined(_MSC_VER)
338# define GBL_FORCE_INLINE __forceinline
340# define GBL_FORCE_INLINE GBL_INLINE
344# define GBL_NO_INLINE __attribute__((noinline))
345#elif defined(_MSC_VER)
346# define GBL_NO_INLINE __declspec(noinline)
348# define GBL_NO_INLINE
355#ifdef __STDC_WANT_LIB_EXT1__
356#define GBL_C11_EXT1 1
357#define GBL_VPRINTF vprintf_s
358#define GBL_VFPRINTF vfprintf_s
359#define GBL_VSPRINTF vsprintf_s
360#define GBL_VSNPRINTF vsnprintf_s
361#define GBL_MEMSET memset_s
362#define GBL_MEMCPY memcpy_s
364#define GBL_C11_EXT1 0
365#define GBL_VPRINTF vprintf
366#define GBL_VFPRINTF vfprintf
367#define GBL_VSPRINTF vsprintf
368#define GBL_VSNPRINTF vsnprintf
369#define GBL_MEMSET memset
370#define GBL_MEMCPY memcpy
374#ifndef GBL_PRAGMA_MACRO_PUSH
375# define GBL_PRAGMA_MACRO_PUSH(X) push_macro(X)
378#ifndef GBL_PRAGMA_MACRO_POP
379# define GBL_PRAGMA_MACRO_POP(X) pop_macro(X)
383# define GBL_MAX_ALIGN_T double
385# define GBL_MAX_ALIGN_T max_align_t
390#define GBL_NULL_TERMINATED
394# define GBL_BSWAP_U16(v) _byteswap_ushort(v)
395# define GBL_BSWAP_U32(v) _byteswap_ulong(v)
396# define GBL_BSWAP_U64(v) _byteswap_uint64(v)
398# define GBL_BSWAP_U16(v) __builtin_bswap16(v)
399# define GBL_BSWAP_U32(v) __builtin_bswap32(v)
400# define GBL_BSWAP_U64(v) __builtin_bswap64(v)
406# define GBL_BITMASK_CLZ(mask) std::countl_zero(mask)
407# define GBL_BITMASK_CTZ(mask) std::countr_zero(mask)
408 inline constexpr auto GBL_BITMASK_FFS(
auto mask)
noexcept {
409 const auto idx = GBL_BITMASK_CLZ(mask);
410 return idx? idx + 1 : 0;
412# define GBL_BITMASK_POPCOUNT(mask) std::popcount(mask)
414# define GBL_BITMASK_POPCOUNT_SW(mask)
415 (((mask) >= sizeof(unsigned long) * CHAR_BIT) ?
416 (unsigned long) -1
: (1u
<< (mask)) - 1
)
418# if defined(__clang__
) || defined(__GNUC__
)
419# define GBL_BITMASK_CLZ(mask) __builtin_clz(mask)
420# define GBL_BITMASK_CTZ(mask) __builtin_ctz(mask)
421# define GBL_BITMASK_FFS(mask) __builtin_ffs(mask)
422# define GBL_BITMASK_POPCOUNT(mask) __builtin_popcount(mask)
423# elif defined(_MSC_VER)
425 GBL_CONSTEXPR GBL_INLINE
unsigned GBL_BITMASK_CLZ(
unsigned mask) GBL_NOEXCEPT {
426 unsigned long idx = 0;
427 return _BitScanReverse(&idx, mask)? ((
sizeof(
unsigned) * 8) - idx) : 0;
429 GBL_CONSTEXPR GBL_INLINE
unsigned GBL_BITMASK_CTZ(
unsigned mask) GBL_NOEXCEPT {
430 unsigned long idx = 0;
431 return _BitScanForward(&idx, mask)? idx : 0;
433 GBL_CONSTEXPR GBL_INLINE
unsigned GBL_BITMASK_FFS(
unsigned mask) GBL_NOEXCEPT {
434 const unsigned idx = GBL_BITMASK_CTZ(mask);
435 return idx? idx + 1 : 0;
437# define GBL_BITMASK_POPCOUNT(mask) GBL_BITMASK_POPCOUNT_SW(mask)
439# define GBL_BITMASK_CLZ(mask)
440# define GBL_BITMASK_CTZ(mask)
441# define GBL_BITMASK_FFS(mask)
442# define GBL_BITMASK_POPCOUNT(mask) GBL_BITMASK_POPCOUNT_SW(mask)
#define GBL_UNLIKELY(exp)