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
... ...

134
rtcore.h
View File

@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. * SOFTWARE.
*/ */
/* My base layer library. /* My base layer library.
* - fixed width types * - fixed width types
@ -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;
} }
@ -179,9 +181,9 @@ enum
*/ */
#define alloc(...) allocx(__VA_ARGS__, alloc4, alloc3, alloc2)(__VA_ARGS__) #define alloc(...) allocx(__VA_ARGS__, alloc4, alloc3, alloc2)(__VA_ARGS__)
#define allocx(a, b, c, d, e, ...) e #define allocx(a, b, c, d, e, ...) e
#define alloc2(a, t) (t*)ArenaAlloc(a, isizeof(t), _Alignof(t), 1, 0) #define alloc2(a, t) (t *)ArenaAlloc(a, isizeof(t), _Alignof(t), 1, 0)
#define alloc3(a, t, n) (t*)ArenaAlloc(a, isizeof(t), _Alignof(t), n, 0) #define alloc3(a, t, n) (t *)ArenaAlloc(a, isizeof(t), _Alignof(t), n, 0)
#define alloc4(a, t, n, f) (t*)ArenaAlloc(a, isizeof(t), _Alignof(t), n, f) #define alloc4(a, t, n, f) (t *)ArenaAlloc(a, isizeof(t), _Alignof(t), n, f)
RTC_API void *ArenaAlloc(arena *a, isize size, isize align, isize n, int flags); RTC_API void *ArenaAlloc(arena *a, isize size, isize align, isize n, int flags);
@ -194,10 +196,14 @@ 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);
/* check if two strings are equal */ /* check if two strings are equal */
RTC_API b32 S8Equals(s8 a, s8 b); RTC_API b32 S8Equals(s8 a, s8 b);
/* compare two strings. analogue to strcmp */ /* compare two strings. analogue to strcmp */
@ -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) \
#define AtomicStoreRelease(_ptr, _val) do { \ do \
__asm__ volatile( \ { \
"movl %1, %0" \ __asm__ volatile("movl %1, %0" : "=m"(*_ptr) : "r"(_val)); \
: "=m" (*_ptr) \
: "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,30 +340,34 @@ 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);
return index; return index;
} }
#define PopCount32(_x) __popcnt(_x) #define PopCount32(_x) __popcnt(_x)
#define PopCount64(_x) __popcnt64(_x) #define PopCount64(_x) __popcnt64(_x)
#else #else
@ -371,9 +381,9 @@ typedef struct thread thread;
/* Win32 uses DWORD as the return type, Linux uses void *. /* Win32 uses DWORD as the return type, Linux uses void *.
* I don't think that I've ever used a threads return value... */ * I don't think that I've ever used a threads return value... */
#ifdef _WIN32 #ifdef _WIN32
#define THREAD_RETURN_TYPE u32 #define THREAD_RETURN_TYPE u32
#elif defined(__linux__) #elif defined(__linux__)
#define THREAD_RETURN_TYPE void * #define THREAD_RETURN_TYPE void *
#endif #endif
/* Generates a thread entry point */ /* Generates a thread entry point */
@ -418,7 +428,7 @@ ArenaAlloc(arena *a, isize size, isize align, isize n, int flags)
/* S8 funcs */ /* S8 funcs */
RTC_API s8 RTC_API s8
S8Span(u8* begin, u8* end) S8Span(u8 *begin, u8 *end)
{ {
s8 s = {0}; s8 s = {0};
s.data = begin; s.data = begin;
@ -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