libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_compiler.h File Reference

Go to the source code of this file.

Macros

#define __STDC_WANT_LIB_EXT1__
 
#define GBL_NULL
 
#define GBL_EXPORT_SHARED
 
#define GBL_IMPORT_SHARED
 
#define GBL_INITIALIZER(f)
 
#define GBL_THREAD_LOCAL
 
#define GBL_CPP_RTTI
 
#define GBL_CPP_EXCEPTIONS
 
#define GBL_NOEXCEPT
 
#define GBL_CONSTEXPR
 
#define GBL_CONSTEVAL
 
#define GBL_CPP_CONCEPTS
 
#define GBL_CPP_SOURCE_LOCATION
 
#define GBL_DEPRECATED(msg)
 
#define GBL_FALLTHROUGH
 
#define GBL_LIKELY
 
#define GBL_MAYBE_UNUSED
 
#define GBL_NO_UNIQUE_ADDRESS
 
#define GBL_NODISCARD
 
#define GBL_NORETURN
 
#define GBL_UNLIKELY
 
#define GBL_STATIC_ASSERT(cond)
 
#define GBL_STATIC_ASSERT_MSG(cond, msg)
 
#define GBL_RESTRICT
 
#define GBL_STATIC_ARRAY(idx)
 
#define GBL_STRUCT_INIT(type, ...)
 
#define GBL_PREFETCH(...)
 
#define GBL_INLINE_
 
#define GBL_INLINE
 
#define GBL_FORCE_INLINE
 
#define GBL_ALLOCA
 
#define GBL_QUICK_EXIT(c)
 
#define GBL_PRAGMA_MACRO_PUSH(X)
 
#define GBL_PRAGMA_MACRO_POP(X)
 
#define GBL_MAX_ALIGN_T
 
#define GBL_ALLOC_MIN_SIZE
 
#define GBL_STMT_START
 
#define GBL_STMT_END
 
#define GBL_NULL_TERMINATED
 
#define GBL_BITMASK_POPCOUNT_SW(mask)
 
#define GBL_BITMASK_CLZ(mask)
 
#define GBL_BITMASK_CTZ(mask)
 
#define GBL_BITMASK_FFS(mask)
 
#define GBL_BITMASK_POPCOUNT(mask)
 

Detailed Description

Helper Definitions for language, compiler, and platform.

See also
gimbal_macro_utilities.h
Author
Falco Girgis

Definition in file gimbal_compiler.h.

Macro Definition Documentation

◆ __STDC_WANT_LIB_EXT1__

#define __STDC_WANT_LIB_EXT1__

Definition at line 12 of file gimbal_compiler.h.

◆ GBL_NULL

#define GBL_NULL

Definition at line 178 of file gimbal_compiler.h.

◆ GBL_EXPORT_SHARED

#define GBL_EXPORT_SHARED

Definition at line 199 of file gimbal_compiler.h.

◆ GBL_IMPORT_SHARED

#define GBL_IMPORT_SHARED

Definition at line 200 of file gimbal_compiler.h.

◆ GBL_INITIALIZER

#define GBL_INITIALIZER (   f)

Definition at line 221 of file gimbal_compiler.h.

417 { __VA_ARGS__ })
418#else
419# define GBL_STRUCT_INIT(type, ...) ((type){ __VA_ARGS__ })
420#endif
421
422#ifdef __cplusplus
423# define GBL_ALIGNAS(e) alignas(e)
424# define GBL_ALIGNOF(e) alignof(e)
425#elif defined(GBL_C11)
426# define GBL_ALIGNAS(e) _Alignas(e)
427# define GBL_ALIGNOF(e) _Alignof(e)
428#endif
429
430#if defined(_MSC_VER) || defined(__MINGW64__)
431# define GBL_ALIGNED_ALLOC(a, s) _aligned_malloc(s, a)
432# define GBL_ALIGNED_REALLOC(p, a, s) _aligned_realloc(p, s, a)
433# define GBL_ALIGNED_FREE(p) _aligned_free(p)
434#elif defined(__MINGW32__)
435# define GBL_ALIGNED_ALLOC(a, s) __mingw_aligned_malloc(s, a)
436# define GBL_ALIGNED_REALLOC(p, a, s) __mingw_aligned_realloc(p, s, a)
437# define GBL_ALIGNED_FREE(p) __mingw_aligned_free(p)
438#elif defined(GBL_C11) || defined(GBL_CPP17)
439# include <stdlib.h>
440# define GBL_ALIGNED_ALLOC(a, s) aligned_alloc(a, s)
441# define GBL_ALIGNED_REALLOC(p, a, s) realloc(p, s)
442# define GBL_ALIGNED_FREE(p) free(p)
443#endif
444
445#if GBL_CONFIG_PREFETCH_ENABLED
446# ifndef GBL_PREFETCH
447# if defined(_MSC_VER) || defined(__MINGW64__)
448# if defined(_M_ARM64) || defined(_M_ARM64EC)
449# include <intrin.h>
450# else
451# include <immintrin.h>
452# endif
453# define GBL_PREFETCH(addr) _mm_prefetch(addr, _MM_HINT_T0)
454# elif defined(__GNUC__)
455# define GBL_PREFETCH __builtin_prefetch
456# endif
457# else
458# define GBL_PREFETCH(...)
459# endif
460#else
461# define GBL_PREFETCH(...)
462#endif
463
464#ifdef __cplusplus
465# define GBL_INLINE_ inline
466#else
467# define GBL_INLINE_ static inline
468#endif
469
470#ifdef __cplusplus
471# define GBL_INLINE GBL_MAYBE_UNUSED GBL_INLINE_
472#else
473# define GBL_INLINE GBL_MAYBE_UNUSED GBL_INLINE_
474#endif
475
476#ifdef __GNUC__
477# define GBL_FORCE_INLINE __attribute__((always_inline)) GBL_INLINE_
478#elif defined(_MSC_VER)
479# define GBL_FORCE_INLINE __forceinline
480#else
481# define GBL_FORCE_INLINE GBL_INLINE
482#endif
483
484// ====== NONSTANDARD COMPILER-SPECIFIC C FUNCTIONS ==========
485
486// "Safer" C11 functions with boundaries
487#if 0
488#ifdef __STDC_WANT_LIB_EXT1__
489#define GBL_C11_EXT1 1
490#define GBL_VPRINTF vprintf_s
491#define GBL_VFPRINTF vfprintf_s
492#define GBL_VSPRINTF vsprintf_s
493#define GBL_VSNPRINTF vsnprintf_s
494#define GBL_MEMSET memset_s
495#define GBL_MEMCPY memcpy_s
496#else
497#define GBL_C11_EXT1 0
498#define GBL_VPRINTF vprintf
499#define GBL_VFPRINTF vfprintf
500#define GBL_VSPRINTF vsprintf
501#define GBL_VSNPRINTF vsnprintf
502#define GBL_MEMSET memset
503#define GBL_MEMCPY memcpy
504#endif
505#endif
506
507// alloca()
508#ifndef GBL_ALLOCA
509# if defined(__APPLE__) || defined(__GLIBC__) || \
510 defined(__sun) || defined(__CYGWIN__) || \
511 defined(__EMSCRIPTEN__) || defined(VITA) || \
512 defined(__DREAMCAST__) || defined(PSP)
513# include <alloca.h> // Sane platforms
514# elif defined(_WIN32)
515# include <malloc.h> // Windoez
516# ifndef alloca
517# define alloca _alloca // Clang with MS Codegen
518# endif
519# else
520# include <stdlib.h> // BSD extension
521# endif
522# define GBL_ALLOCA alloca
523#endif
524
525#if __APPLE__ // at very least fucking MacOS headers are missing it!
526# define GBL_QUICK_EXIT(c) exit(c)
527#elif defined(GBL_CPP11)
528# define GBL_QUICK_EXIT(c) quick_exit(c)
529#elif defined(GBL_C11)
530# define GBL_QUICK_EXIT(c) quick_exit(c)
531#else
532# define GBL_QUICK_EXIT(c) exit(c)
533#endif
534
535#ifndef GBL_PRAGMA_MACRO_PUSH
536# define GBL_PRAGMA_MACRO_PUSH(X) push_macro(X)
537#endif
538
539#ifndef GBL_PRAGMA_MACRO_POP
540# define GBL_PRAGMA_MACRO_POP(X) pop_macro(X)
541#endif
542
543#ifdef _MSC_VER
544# define GBL_MAX_ALIGN_T double
545#else
546# define GBL_MAX_ALIGN_T max_align_t
547#endif
548
549#define GBL_ALLOC_MIN_SIZE GBL_ALIGNOF(GBL_MAX_ALIGN_T)
550
551/* ==========================================================
552 * Provide simple macro statement wrappers (adapted from Perl):
553 * G_STMT_START { statements; } G_STMT_END;
554 * can be used as a single statement, as in
555 * if (x) G_STMT_START { ... } G_STMT_END; else ...
556 *
557 * For gcc we will wrap the statements within `({' and `})' braces.
558 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
559 * and otherwise within `do' and `while (0)'.
560 * ========================================================
561 * Graciously stolen from GLib. Imitation is the most sincere form of flattery.
562 * --Falco Girgis
563 */
564/* REMOVED WHILE TRYING TO AVOID CLANG WARNINGS AND TO BE STANDARD COMPLIANT
565 * # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
566# define GBL_STMT_START (void) __extension__ (
567# define GBL_STMT_END )
568# */
569#if !(defined (GBL_STMT_START) && defined (GBL_STMT_END))
570# if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
571# define GBL_STMT_START do
572# define GBL_STMT_END while(0)
573# else
574# if (defined (sun) || defined (__sun__))
575# define GBL_STMT_START if(1)
576# define GBL_STMT_END else(void)0
577# else
578# define GBL_STMT_START do
579# define GBL_STMT_END while(0)
580# endif
581# endif
582#endif
583
584#define GBL_NULL_TERMINATED
585
586// Low-Level BitMask Operations
587#ifdef GBL_CPP20
588# include <bit>
589# define GBL_BITMASK_CLZ(mask) std::countl_zero(mask)
590# define GBL_BITMASK_CTZ(mask) std::countr_zero(mask)
591 inline constexpr auto GBL_BITMASK_FFS(auto mask) noexcept {
592 const auto idx = GBL_BITMASK_CLZ(mask);
593 return idx? idx + 1 : 0;
594 }
595# define GBL_BITMASK_POPCOUNT(mask) std::popcount(mask)
596#else
597# define GBL_BITMASK_POPCOUNT_SW(mask) \
598 (((mask) >= sizeof(unsigned long) * CHAR_BIT) ? \
599 (unsigned long) -1 : (1u << (mask)) - 1)
600
601# if defined(__clang__) || defined(__GNUC__)
602# define GBL_BITMASK_CLZ(mask) __builtin_clz(mask) // count leading zeroes
603# define GBL_BITMASK_CTZ(mask) __builtin_ctz(mask) // count trailing zeroes
604# define GBL_BITMASK_FFS(mask) __builtin_ffs(mask) // find first set
605# define GBL_BITMASK_POPCOUNT(mask) __builtin_popcount(mask) // count number of 1s
606# elif defined(_MSC_VER)
607# include <intrin.h>
608 GBL_CONSTEXPR GBL_INLINE unsigned GBL_BITMASK_CLZ(unsigned mask) GBL_NOEXCEPT {
609 unsigned long idx = 0;
610 return _BitScanReverse(&idx, mask)? ((sizeof(unsigned) * 8) - idx) : 0;
611 }
612 GBL_CONSTEXPR GBL_INLINE unsigned GBL_BITMASK_CTZ(unsigned mask) GBL_NOEXCEPT {
613 unsigned long idx = 0;
614 return _BitScanForward(&idx, mask)? idx : 0;
615 }
616 GBL_CONSTEXPR GBL_INLINE unsigned GBL_BITMASK_FFS(unsigned mask) GBL_NOEXCEPT {
617 const unsigned idx = GBL_BITMASK_CTZ(mask);
618 return idx? idx + 1 : 0;
619 }
620# define GBL_BITMASK_POPCOUNT(mask) GBL_BITMASK_POPCOUNT_SW(mask)
621# else
622# define GBL_BITMASK_CLZ(mask) // implement me in software later TODO
623# define GBL_BITMASK_CTZ(mask)
624# define GBL_BITMASK_FFS(mask)
625# define GBL_BITMASK_POPCOUNT(mask) GBL_BITMASK_POPCOUNT_SW(mask)
626# endif
627#endif
628
629#endif // GIMBAL_COMPILER_H

◆ GBL_THREAD_LOCAL

#define GBL_THREAD_LOCAL

Definition at line 235 of file gimbal_compiler.h.

◆ GBL_CPP_RTTI

#define GBL_CPP_RTTI

Definition at line 255 of file gimbal_compiler.h.

◆ GBL_CPP_EXCEPTIONS

#define GBL_CPP_EXCEPTIONS

Definition at line 263 of file gimbal_compiler.h.

◆ GBL_NOEXCEPT

#define GBL_NOEXCEPT

Definition at line 264 of file gimbal_compiler.h.

◆ GBL_CONSTEXPR

#define GBL_CONSTEXPR

Definition at line 275 of file gimbal_compiler.h.

◆ GBL_CONSTEVAL

#define GBL_CONSTEVAL

Definition at line 276 of file gimbal_compiler.h.

◆ GBL_CPP_CONCEPTS

#define GBL_CPP_CONCEPTS

Definition at line 283 of file gimbal_compiler.h.

◆ GBL_CPP_SOURCE_LOCATION

#define GBL_CPP_SOURCE_LOCATION

Definition at line 291 of file gimbal_compiler.h.

◆ GBL_DEPRECATED

#define GBL_DEPRECATED (   msg)

Definition at line 302 of file gimbal_compiler.h.

◆ GBL_FALLTHROUGH

#define GBL_FALLTHROUGH

Definition at line 313 of file gimbal_compiler.h.

◆ GBL_LIKELY

#define GBL_LIKELY

Definition at line 324 of file gimbal_compiler.h.

◆ GBL_MAYBE_UNUSED

#define GBL_MAYBE_UNUSED

Definition at line 338 of file gimbal_compiler.h.

◆ GBL_NO_UNIQUE_ADDRESS

#define GBL_NO_UNIQUE_ADDRESS

Definition at line 349 of file gimbal_compiler.h.

◆ GBL_NODISCARD

#define GBL_NODISCARD

Definition at line 360 of file gimbal_compiler.h.

◆ GBL_NORETURN

#define GBL_NORETURN

Definition at line 374 of file gimbal_compiler.h.

◆ GBL_UNLIKELY

#define GBL_UNLIKELY

Definition at line 386 of file gimbal_compiler.h.

◆ GBL_STATIC_ASSERT

#define GBL_STATIC_ASSERT (   cond)

Definition at line 400 of file gimbal_compiler.h.

◆ GBL_STATIC_ASSERT_MSG

#define GBL_STATIC_ASSERT_MSG (   cond,
  msg 
)

Definition at line 401 of file gimbal_compiler.h.

◆ GBL_RESTRICT

#define GBL_RESTRICT

Definition at line 407 of file gimbal_compiler.h.

◆ GBL_STATIC_ARRAY

#define GBL_STATIC_ARRAY (   idx)

Definition at line 413 of file gimbal_compiler.h.

◆ GBL_STRUCT_INIT

#define GBL_STRUCT_INIT (   type,
  ... 
)

Definition at line 419 of file gimbal_compiler.h.

◆ GBL_PREFETCH

#define GBL_PREFETCH (   ...)

Definition at line 461 of file gimbal_compiler.h.

◆ GBL_INLINE_

#define GBL_INLINE_

Definition at line 467 of file gimbal_compiler.h.

◆ GBL_INLINE

#define GBL_INLINE

Definition at line 473 of file gimbal_compiler.h.

◆ GBL_FORCE_INLINE

#define GBL_FORCE_INLINE

Definition at line 481 of file gimbal_compiler.h.

◆ GBL_ALLOCA

#define GBL_ALLOCA

Definition at line 522 of file gimbal_compiler.h.

◆ GBL_QUICK_EXIT

#define GBL_QUICK_EXIT (   c)

Definition at line 532 of file gimbal_compiler.h.

◆ GBL_PRAGMA_MACRO_PUSH

#define GBL_PRAGMA_MACRO_PUSH (   X)

Definition at line 536 of file gimbal_compiler.h.

◆ GBL_PRAGMA_MACRO_POP

#define GBL_PRAGMA_MACRO_POP (   X)

Definition at line 540 of file gimbal_compiler.h.

◆ GBL_MAX_ALIGN_T

#define GBL_MAX_ALIGN_T

Definition at line 546 of file gimbal_compiler.h.

◆ GBL_ALLOC_MIN_SIZE

#define GBL_ALLOC_MIN_SIZE

Definition at line 549 of file gimbal_compiler.h.

◆ GBL_STMT_START

#define GBL_STMT_START

Definition at line 578 of file gimbal_compiler.h.

◆ GBL_STMT_END

#define GBL_STMT_END

Definition at line 579 of file gimbal_compiler.h.

◆ GBL_NULL_TERMINATED

#define GBL_NULL_TERMINATED

Definition at line 584 of file gimbal_compiler.h.

◆ GBL_BITMASK_POPCOUNT_SW

#define GBL_BITMASK_POPCOUNT_SW (   mask)

Definition at line 597 of file gimbal_compiler.h.

599 : (1u << (mask)) - 1)

◆ GBL_BITMASK_CLZ

#define GBL_BITMASK_CLZ (   mask)

Definition at line 622 of file gimbal_compiler.h.

◆ GBL_BITMASK_CTZ

#define GBL_BITMASK_CTZ (   mask)

Definition at line 623 of file gimbal_compiler.h.

◆ GBL_BITMASK_FFS

#define GBL_BITMASK_FFS (   mask)

Definition at line 624 of file gimbal_compiler.h.

◆ GBL_BITMASK_POPCOUNT

#define GBL_BITMASK_POPCOUNT (   mask)

Definition at line 625 of file gimbal_compiler.h.