rstdlib

Generic Base Layer for writing C Programs
git clone anongit@rnpnr.xyz:rstdlib.git
Log | Files | Refs | Feed | README | LICENSE

rstd_intrinsics.h (1815B)


      1 ///////////////////
      2 // NOTE: Intrisics
      3 #ifndef RSTD_INTRINSICS_H
      4 #define RSTD_INTRINSICS_H
      5 
      6 #if COMPILER_CLANG || COMPILER_GCC
      7   #define force_inline inline __attribute__((always_inline))
      8 #elif COMPILER_MSVC
      9   #define force_inline __forceinline
     10 #endif
     11 
     12 #if COMPILER_MSVC || (COMPILER_CLANG && OS_WINDOWS)
     13   #pragma section(".rdata$", read)
     14   #define read_only __declspec(allocate(".rdata$"))
     15 #elif COMPILER_CLANG
     16   #define read_only __attribute__((section(".rodata")))
     17 #elif COMPILER_GCC
     18   /* TODO(rnp): not supported on GCC, putting it in rodata causes warnings and writing to
     19    * it doesn't cause a fault */
     20   #define read_only
     21 #endif
     22 
     23 #if COMPILER_MSVC
     24 
     25   #define alignas(n)     __declspec(align(n))
     26   #define no_return      __declspec(noreturn)
     27 
     28   #define likely(x)      (x)
     29   #define unlikely(x)    (x)
     30 
     31   #define assume(x)      __assume(x)
     32   #define debugbreak     __debugbreak
     33   #define unreachable()  __assume(0)
     34 
     35   #if ARCH_ARM64
     36     #define cpu_yield()  __yield()
     37   #endif
     38 
     39 #else /* !COMPILER_MSVC */
     40 
     41   #define alignas(n)     __attribute__((aligned(n)))
     42   #define no_return      __attribute__((noreturn))
     43 
     44   #define likely(x)      (__builtin_expect(!!(x), 1))
     45   #define unlikely(x)    (__builtin_expect(!!(x), 0))
     46 
     47   #if COMPILER_CLANG
     48     #define assume(x)    __builtin_assume(x)
     49   #else
     50     #define assume(x)    __attribute__((assume(x)))
     51   #endif
     52   #define unreachable()  __builtin_unreachable()
     53 
     54   #if ARCH_ARM64
     55     /* TODO(rnp)? debuggers just loop here forever and need a manual PC increment (step over) */
     56     #define debugbreak() asm volatile ("brk 0xf000")
     57     #define cpu_yield()  asm volatile ("yield")
     58   #else
     59     #define debugbreak() asm volatile ("int3; nop")
     60   #endif
     61 
     62 #endif /* !COMPILER_MSVC */
     63 
     64 #if ARCH_X64
     65   #define cpu_yield()    _mm_pause()
     66 #endif
     67 
     68 #endif /* RSTD_INTRINSICS_H */