This commit is contained in:
Kevin Trogant 2026-01-04 14:17:15 +01:00
parent 207fd90233
commit 8c226f5f31
2 changed files with 251 additions and 243 deletions

View File

@ -7,11 +7,11 @@ BinPackParameters: 'false'
BreakBeforeBraces: Allman BreakBeforeBraces: Allman
ColumnLimit: '120' ColumnLimit: '120'
IndentPPDirectives: BeforeHash IndentPPDirectives: BeforeHash
IndentWidth: '2' IndentWidth: '4'
Language: Cpp Language: Cpp
PointerAlignment: Right PointerAlignment: Right
SortIncludes: 'false' SortIncludes: 'false'
TabWidth: '2' TabWidth: '4'
UseTab: ForContinuationAndIndentation UseTab: AlignWithSpaces
... ...

View File

@ -153,12 +153,14 @@ typedef struct arena_cp
byte *cp; byte *cp;
} arena_cp; } arena_cp;
static force_inline arena_cp SaveArena(arena a) static force_inline arena_cp
SaveArena(arena a)
{ {
return (arena_cp){a.begin}; return (arena_cp){a.begin};
} }
static force_inline void RestoreArena(arena *a, arena_cp cp) static force_inline void
RestoreArena(arena *a, arena_cp cp)
{ {
a->begin = cp.cp; a->begin = cp.cp;
} }
@ -194,7 +196,11 @@ typedef struct s8
#define S8(_s) \ #define S8(_s) \
(s8) { .data = (u8 *)_s, .length = lengthof(_s), } (s8) { .data = (u8 *)_s, .length = lengthof(_s), }
typedef struct { s8 first; s8 second; } split_result; typedef struct
{
s8 first;
s8 second;
} split_result;
/* constructs a string containing the bytes between begin and end (exclusive) */ /* constructs a string containing the bytes between begin and end (exclusive) */
RTC_API s8 S8Span(u8 *begin, u8 *end); RTC_API s8 S8Span(u8 *begin, u8 *end);
@ -230,8 +236,16 @@ RTC_API split_result S8Split2(s8 s, u8 c);
/* Creates a clone of string s on arena a */ /* Creates a clone of string s on arena a */
RTC_API s8 S8Clone(s8 s, arena *a); RTC_API s8 S8Clone(s8 s, arena *a);
typedef struct { i64 i; b32 ok; } s8_parse_i64_result; typedef struct
typedef struct { i32 i; b32 ok; } s8_parse_i32_result; {
i64 i;
b32 ok;
} s8_parse_i64_result;
typedef struct
{
i32 i;
b32 ok;
} s8_parse_i32_result;
/* Parses a integer from string s */ /* Parses a integer from string s */
RTC_API s8_parse_i64_result S8ParseI64(s8 s, int base); RTC_API s8_parse_i64_result S8ParseI64(s8 s, int base);
@ -273,48 +287,40 @@ RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length);
#define AtomicLoad(_ptr) _InterlockedOr(_ptr, 0) #define AtomicLoad(_ptr) _InterlockedOr(_ptr, 0)
#define AtomicLoadAcquire(_ptr) _InterlockedOr_HLEAcquire(_ptr, 0) #define AtomicLoadAcquire(_ptr) _InterlockedOr_HLEAcquire(_ptr, 0)
#elif defined(__TINYC__) #elif defined(__TINYC__)
#define AtomicInc32(_addend) do { \ #define AtomicInc32(_addend) \
__asm__ volatile( \ do \
"lock incl %0" \ { \
: "+m" (*_addend) \ __asm__ volatile("lock incl %0" : "+m"(*_addend)); \
); \
} while (0) } while (0)
#define AtomicInc64(_addend) do { \ #define AtomicInc64(_addend) \
__asm__ volatile( \ do \
"lock incq %0" \ { \
: "+m" (*_addend) \ __asm__ volatile("lock incq %0" : "+m"(*_addend)); \
); \
} while (0) } while (0)
#define AtomicAdd32(_addend, _val) do { \ #define AtomicAdd32(_addend, _val) \
__asm__ volatile( \ do \
"lock addl %1, %0" \ { \
: "+m" (*_addend) \ __asm__ volatile("lock addl %1, %0" : "+m"(*_addend) : "r"(_val)); \
: "r" (_val) \
); \
} while (0) } while (0)
#define AtomicAdd64(_addend, _val) do { \ #define AtomicAdd64(_addend, _val) \
__asm__ volatile( \ do \
"lock addq %1, %0" \ { \
: "+m" (*_addend) \ __asm__ volatile("lock addq %1, %0" : "+m"(*_addend) : "r"(_val)); \
: "r" (_val) \
); \
} while (0) } while (0)
/* This uses mov followed by mfence to ensure that /* This uses mov followed by mfence to ensure that
* the store becomes globally visible to any subsequent load or store. */ * the store becomes globally visible to any subsequent load or store. */
#define AtomicStore(_ptr, _val) do { \ #define AtomicStore(_ptr, _val) \
__asm__ volatile( \ do \
"movl %1, %0;" \ { \
__asm__ volatile("movl %1, %0;" \
"mfence;" \ "mfence;" \
: "=m"(*_ptr) \ : "=m"(*_ptr) \
: "r" (_val) \ : "r"(_val)); \
); \
} while (0) } while (0)
#define AtomicStoreRelease(_ptr, _val) do { \ #define AtomicStoreRelease(_ptr, _val) \
__asm__ volatile( \ do \
"movl %1, %0" \ { \
: "=m" (*_ptr) \ __asm__ volatile("movl %1, %0" : "=m"(*_ptr) : "r"(_val)); \
: "r" (_val) \
); \
} while (0) } while (0)
/* NOTE(Kevin): This should always compile to a mov, which is what we want. */ /* NOTE(Kevin): This should always compile to a mov, which is what we want. */
#define AtomicLoad(_ptr) (*(_ptr)) #define AtomicLoad(_ptr) (*(_ptr))
@ -334,25 +340,29 @@ RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length);
#define PopCount32(_x) __builtin_popcount(_x) #define PopCount32(_x) __builtin_popcount(_x)
#define PopCount64(_x) __builtin_popcountl(_x) #define PopCount64(_x) __builtin_popcountl(_x)
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
static force_inline unsigned int CTZ32(u32 x) static force_inline unsigned int
CTZ32(u32 x)
{ {
unsigned int index; unsigned int index;
_BitScanReverse(&index, x); _BitScanReverse(&index, x);
return index; return index;
} }
static force_inline unsigned int CTZ64(u64 x) static force_inline unsigned int
CTZ64(u64 x)
{ {
unsigned int index; unsigned int index;
_BitScanReverse64(&index, x); _BitScanReverse64(&index, x);
return index; return index;
} }
static force_inline unsigned int CLZ32(u32 x) static force_inline unsigned int
CLZ32(u32 x)
{ {
unsigned int index; unsigned int index;
_BitScanForward(&index, x); _BitScanForward(&index, x);
return index; return index;
} }
static force_inline unsigned int CLZ64(u64 x) static force_inline unsigned int
CLZ64(u64 x)
{ {
unsigned int index; unsigned int index;
_BitScanForward64(&index, x); _BitScanForward64(&index, x);
@ -679,8 +689,7 @@ JoinThread(thread *t)
RTC_API thread * RTC_API thread *
StartThread(thread_fn *fn, void *param) StartThread(thread_fn *fn, void *param)
{ {
HANDLE h = CreateThread( HANDLE h = CreateThread(NULL,
NULL,
0, /* Use default stack size */ 0, /* Use default stack size */
(LPTHREAD_START_ROUTINE)fn, (LPTHREAD_START_ROUTINE)fn,
param, param,
@ -705,4 +714,3 @@ JoinThread(thread *t)
#endif #endif
#endif #endif