First steps towards tcc compat: Make some stuff conditional

This commit is contained in:
Kevin Trogant 2025-12-27 16:31:10 +01:00
parent b3345df5bf
commit 3911b74a15
2 changed files with 78 additions and 50 deletions

View File

@ -53,7 +53,15 @@
* This assume x64, which is what I'm always using.
* You still need to build with the appropriate compilation
* flags (-march=avx2 etc. for gcc, /arch:AVX2 etc. for msvc) */
#if defined(__has_include) && !defined(RTC_NO_SIMD)
#if !__has_include(<immintrin.h>)
#define RTC_NO_SIMD
#else
#include <immintrin.h>
#endif
#elif !defined(RTC_NO_SIMD)
#include <immintrin.h>
#endif
/* MSVC additionally has intrin.h for microsoft specific intrinsics */
#ifdef _MSC_VER
#include <intrin.h>
@ -128,6 +136,9 @@ typedef int32_t b32;
}
#define force_inline __forceinline
#else
#define assert(x)
#define force_inline inline
#endif
/* Arena allocator */
@ -241,6 +252,7 @@ RTC_API s8 ReadEntireFileS8(s8 path, arena *a);
RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length);
/* Atomics */
#if !defined(RTC_NO_ATOMICS)
#if defined(__GNUC__) || defined(__clang__)
/* Atomic add */
#define AtomicAdd32(_addend, _val) __atomic_add_fetch((i32 *)_addend, _val, __ATOMIC_SEQ_CST)
@ -256,9 +268,13 @@ RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length);
#define AtomicStoreRelease(_ptr, _val) _InterlockedExchange_HLERelease(_ptr, _val)
#define AtomicLoad(_ptr) _InterlockedOr(_ptr, 0)
#define AtomicLoadAcquire(_ptr) _InterlockedOr_HLEAcquire(_ptr, 0)
#else
#define RTC_NO_ATOMICS
#endif
#endif
/* Aliases for intrinsics */
#if !defined(RTC_NO_INTRINSICS)
#if defined(__GNUC__) || defined(__clang__)
#define CTZ32(ui) __builtin_ctz(ui)
#define CTZ64(ul) __builtin_ctzl(ul)
@ -293,6 +309,9 @@ static force_inline unsigned int CLZ64(u64 x)
}
#define PopCount32(_x) __popcnt(_x)
#define PopCount64(_x) __popcnt64(_x)
#else
#define RTC_NO_INTRINSICS
#endif
#endif
/* Threading wrapper */

9
test.c
View File

@ -6,8 +6,13 @@
THREAD_FN(TestThread)
{
int *p = param;
#ifndef RTC_NO_ATOMICS
printf("Thread got param %d\n", AtomicLoad(p));
AtomicStore(p, 42);
#else
printf("Thread go param %d\n", *p);
*p = 42;
#endif
return 0;
}
@ -44,7 +49,11 @@ ThreadTest(void)
int p = 32;
thread *t = StartThread(TestThread, &p);
JoinThread(t);
#ifndef RTC_NO_ATOMICS
return AtomicLoad(&p) == 42;
#else
return p == 42;
#endif
}
internal int