From 8c226f5f3193651e5fa1a548c3e2d480b7ff44e9 Mon Sep 17 00:00:00 2001 From: Kevin Trogant Date: Sun, 4 Jan 2026 14:17:15 +0100 Subject: [PATCH] reformat --- .clang-format | 6 +- rtcore.h | 488 +++++++++++++++++++++++++------------------------- 2 files changed, 251 insertions(+), 243 deletions(-) diff --git a/.clang-format b/.clang-format index 1d6e399..e3a303a 100644 --- a/.clang-format +++ b/.clang-format @@ -7,11 +7,11 @@ BinPackParameters: 'false' BreakBeforeBraces: Allman ColumnLimit: '120' IndentPPDirectives: BeforeHash -IndentWidth: '2' +IndentWidth: '4' Language: Cpp PointerAlignment: Right SortIncludes: 'false' -TabWidth: '2' -UseTab: ForContinuationAndIndentation +TabWidth: '4' +UseTab: AlignWithSpaces ... diff --git a/rtcore.h b/rtcore.h index 56dbccd..09440a4 100644 --- a/rtcore.h +++ b/rtcore.h @@ -4,25 +4,25 @@ /* * rtcore.h - Simple base layer library for C * Copyright (C) 2025 Kevin Trogant - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * 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. -*/ + */ /* My base layer library. * - fixed width types @@ -34,7 +34,7 @@ * - atomics * - simple file io */ - + #include #include #include @@ -42,11 +42,11 @@ #include #ifndef RTC_API - #ifdef __cplusplus - #define RTC_API extern "C" - #else - #define RTC_API extern - #endif + #ifdef __cplusplus + #define RTC_API extern "C" + #else + #define RTC_API extern + #endif #endif /* Nowadays, immintrin.h includes everything I tend to care about (simd) @@ -86,16 +86,16 @@ typedef int32_t b32; /* Utility macros */ #if defined(__GNUC__) || defined(__clang__) - #ifdef __has_builtin - #if __has_builtin(__builtin_expect) + #ifdef __has_builtin + #if __has_builtin(__builtin_expect) #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) #endif #endif #endif #ifndef likely - #define likely(x) (x) - #define unlikely(x) (x) + #define likely(x) (x) + #define unlikely(x) (x) #endif #define internal static @@ -120,55 +120,57 @@ typedef int32_t b32; /* number of characters in string constant s (exluding the terminating 0) */ #define lengthof(s) (countof(s) - 1) #if defined(__GNUC__) || defined(__clang__) - #define assert(x) \ - while (!(x)) \ - { \ - __builtin_trap(); \ - __builtin_unreachable(); \ + #define assert(x) \ + while (!(x)) \ + { \ + __builtin_trap(); \ + __builtin_unreachable(); \ } #define force_inline inline __attribute__((always_inline)) #elif defined(_MSC_VER) - #define assert(x) \ - if (!(x)) \ - { \ - __debugbreak(); \ + #define assert(x) \ + if (!(x)) \ + { \ + __debugbreak(); \ } - #define force_inline __forceinline + #define force_inline __forceinline #else - #define assert(x) + #define assert(x) #define force_inline inline #endif /* Arena allocator */ typedef struct arena { - byte *begin; - byte *end; + byte *begin; + byte *end; } arena; typedef struct arena_cp { - byte *cp; + byte *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; } enum { - /* Disables memory zeroing */ - ALLOC_NOZERO = 0x1, - /* Allocation failure returns NULL instead of aborting */ - ALLOC_SOFTFAIL = 0x2, + /* Disables memory zeroing */ + ALLOC_NOZERO = 0x1, + /* Allocation failure returns NULL instead of aborting */ + ALLOC_SOFTFAIL = 0x2, }; /* allocate objects from an arena. @@ -179,9 +181,9 @@ enum */ #define alloc(...) allocx(__VA_ARGS__, alloc4, alloc3, alloc2)(__VA_ARGS__) #define allocx(a, b, c, d, e, ...) e -#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 alloc4(a, t, n, f) (t*)ArenaAlloc(a, isizeof(t), _Alignof(t), n, f) +#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 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); @@ -194,10 +196,14 @@ typedef struct s8 #define S8(_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) */ -RTC_API s8 S8Span(u8* begin, u8* end); +RTC_API s8 S8Span(u8 *begin, u8 *end); /* check if two strings are equal */ RTC_API b32 S8Equals(s8 a, s8 b); /* 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 */ RTC_API s8 S8Clone(s8 s, arena *a); -typedef struct { i64 i; b32 ok; } s8_parse_i64_result; -typedef struct { i32 i; b32 ok; } s8_parse_i32_result; +typedef struct +{ + i64 i; + b32 ok; +} s8_parse_i64_result; +typedef struct +{ + i32 i; + b32 ok; +} s8_parse_i32_result; /* Parses a integer from string s */ RTC_API s8_parse_i64_result S8ParseI64(s8 s, int base); @@ -273,49 +287,41 @@ RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length); #define AtomicLoad(_ptr) _InterlockedOr(_ptr, 0) #define AtomicLoadAcquire(_ptr) _InterlockedOr_HLEAcquire(_ptr, 0) #elif defined(__TINYC__) - #define AtomicInc32(_addend) do { \ - __asm__ volatile( \ - "lock incl %0" \ - : "+m" (*_addend) \ - ); \ + #define AtomicInc32(_addend) \ + do \ + { \ + __asm__ volatile("lock incl %0" : "+m"(*_addend)); \ } while (0) - #define AtomicInc64(_addend) do { \ - __asm__ volatile( \ - "lock incq %0" \ - : "+m" (*_addend) \ - ); \ + #define AtomicInc64(_addend) \ + do \ + { \ + __asm__ volatile("lock incq %0" : "+m"(*_addend)); \ } while (0) - #define AtomicAdd32(_addend, _val) do { \ - __asm__ volatile( \ - "lock addl %1, %0" \ - : "+m" (*_addend) \ - : "r" (_val) \ - ); \ + #define AtomicAdd32(_addend, _val) \ + do \ + { \ + __asm__ volatile("lock addl %1, %0" : "+m"(*_addend) : "r"(_val)); \ } while (0) - #define AtomicAdd64(_addend, _val) do { \ - __asm__ volatile( \ - "lock addq %1, %0" \ - : "+m" (*_addend) \ - : "r" (_val) \ - ); \ + #define AtomicAdd64(_addend, _val) \ + do \ + { \ + __asm__ volatile("lock addq %1, %0" : "+m"(*_addend) : "r"(_val)); \ } while (0) /* This uses mov followed by mfence to ensure that * the store becomes globally visible to any subsequent load or store. */ - #define AtomicStore(_ptr, _val) do { \ - __asm__ volatile( \ - "movl %1, %0;" \ - "mfence;" \ - : "=m" (*_ptr) \ - : "r" (_val) \ - ); \ - } while(0) - #define AtomicStoreRelease(_ptr, _val) do { \ - __asm__ volatile( \ - "movl %1, %0" \ - : "=m" (*_ptr) \ - : "r" (_val) \ - ); \ - } while (0) + #define AtomicStore(_ptr, _val) \ + do \ + { \ + __asm__ volatile("movl %1, %0;" \ + "mfence;" \ + : "=m"(*_ptr) \ + : "r"(_val)); \ + } while (0) + #define AtomicStoreRelease(_ptr, _val) \ + do \ + { \ + __asm__ volatile("movl %1, %0" : "=m"(*_ptr) : "r"(_val)); \ + } while (0) /* NOTE(Kevin): This should always compile to a mov, which is what we want. */ #define AtomicLoad(_ptr) (*(_ptr)) #define AtomicLoadAcquire(_ptr) (*(_ptr)) @@ -334,30 +340,34 @@ RTC_API b32 WriteEntireFile(s8 path, byte *data, isize length); #define PopCount32(_x) __builtin_popcount(_x) #define PopCount64(_x) __builtin_popcountl(_x) #elif defined(_MSC_VER) - static force_inline unsigned int CTZ32(u32 x) - { - unsigned int index; - _BitScanReverse(&index, x); - return index; - } - static force_inline unsigned int CTZ64(u64 x) - { - unsigned int index; - _BitScanReverse64(&index, x); - return index; - } - static force_inline unsigned int CLZ32(u32 x) - { - unsigned int index; - _BitScanForward(&index, x); - return index; - } - static force_inline unsigned int CLZ64(u64 x) - { - unsigned int index; - _BitScanForward64(&index, x); - return index; - } +static force_inline unsigned int +CTZ32(u32 x) +{ + unsigned int index; + _BitScanReverse(&index, x); + return index; +} +static force_inline unsigned int +CTZ64(u64 x) +{ + unsigned int index; + _BitScanReverse64(&index, x); + return index; +} +static force_inline unsigned int +CLZ32(u32 x) +{ + unsigned int index; + _BitScanForward(&index, x); + return index; +} +static force_inline unsigned int +CLZ64(u64 x) +{ + unsigned int index; + _BitScanForward64(&index, x); + return index; +} #define PopCount32(_x) __popcnt(_x) #define PopCount64(_x) __popcnt64(_x) #else @@ -371,9 +381,9 @@ typedef struct thread thread; /* Win32 uses DWORD as the return type, Linux uses void *. * I don't think that I've ever used a threads return value... */ #ifdef _WIN32 -#define THREAD_RETURN_TYPE u32 + #define THREAD_RETURN_TYPE u32 #elif defined(__linux__) -#define THREAD_RETURN_TYPE void * + #define THREAD_RETURN_TYPE void * #endif /* Generates a thread entry point */ @@ -393,10 +403,10 @@ RTC_API THREAD_RETURN_TYPE JoinThread(thread *t); #include #ifdef __linux__ - #include + #include #elif defined(_WIN32) - #define WIN32_LEAN_AND_MEAN - #include + #define WIN32_LEAN_AND_MEAN + #include #endif /* Alloc */ @@ -418,104 +428,104 @@ ArenaAlloc(arena *a, isize size, isize align, isize n, int flags) /* S8 funcs */ RTC_API s8 -S8Span(u8* begin, u8* end) +S8Span(u8 *begin, u8 *end) { - s8 s = {0}; - s.data = begin; - s.length = begin ? end - begin : 0; - return s; + s8 s = {0}; + s.data = begin; + s.length = begin ? end - begin : 0; + return s; } RTC_API b32 S8Equals(s8 a, s8 b) { - return a.length == b.length && (!a.length || (memcmp(a.data, b.data, a.length) == 0)); + return a.length == b.length && (!a.length || (memcmp(a.data, b.data, a.length) == 0)); } RTC_API isize S8Compare(s8 a, s8 b) { - isize n = min(a.length, b.length); - for (isize i = 0; i < n; ++i) - { - if (a.data[i] != b.data[i]) - return (isize)a.data[i] - (isize)b.data[i]; - } - return a.length - b.length; + isize n = min(a.length, b.length); + for (isize i = 0; i < n; ++i) + { + if (a.data[i] != b.data[i]) + return (isize)a.data[i] - (isize)b.data[i]; + } + return a.length - b.length; } RTC_API u64 S8Hash(s8 s) { - u64 hash = 0xcbf29ce484222325; - for (isize i = 0; i < s.length; ++i) - hash = (hash ^ s.data[i]) * 0x00000100000001b3; - return hash; + u64 hash = 0xcbf29ce484222325; + for (isize i = 0; i < s.length; ++i) + hash = (hash ^ s.data[i]) * 0x00000100000001b3; + return hash; } RTC_API s8 S8Trim(s8 s) { - for (; s.length && *s.data <= ' '; s.data++, s.length--) - { - } - for (; s.length && s.data[s.length - 1] <= ' '; s.length--) - { - } - return s; + for (; s.length && *s.data <= ' '; s.data++, s.length--) + { + } + for (; s.length && s.data[s.length - 1] <= ' '; s.length--) + { + } + return s; } RTC_API s8 S8TrimLeft(s8 s) { - for (; s.length && *s.data <= ' '; s.data++, s.length--) - { - } - return s; + for (; s.length && *s.data <= ' '; s.data++, s.length--) + { + } + return s; } RTC_API s8 S8TrimRight(s8 s) { - for (; s.length && s.data[s.length - 1] <= ' '; s.length--) - { - } - return s; + for (; s.length && s.data[s.length - 1] <= ' '; s.length--) + { + } + return s; } RTC_API u8 * S8Chr(s8 s, u8 c) { - for (isize i = 0; i < s.length; ++i) - { - if (s.data[i] == c) - return &s.data[i]; - } - return NULL; + for (isize i = 0; i < s.length; ++i) + { + if (s.data[i] == c) + return &s.data[i]; + } + return NULL; } RTC_API u8 * S8End(s8 s) { - return &s.data[s.length - 1]; + return &s.data[s.length - 1]; } RTC_API split_result S8Split(s8 s, u8 c) { - u8 *chr = S8Chr(s, c); - if (!chr) - return (split_result){s}; - return (split_result){ - S8Span(s.data, chr), - S8Span(chr + 1, S8End(s) + 1), - }; + u8 *chr = S8Chr(s, c); + if (!chr) + return (split_result){s}; + return (split_result){ + S8Span(s.data, chr), + S8Span(chr + 1, S8End(s) + 1), + }; } RTC_API split_result S8Split2(s8 s, u8 c) { - u8 *chr = S8Chr(s, c); - if (!chr) - return (split_result){s}; - split_result r = { - .first = S8Span(s.data, chr), - }; - u8 *end = S8End(s) + 1; - while (chr != end && *chr == c) - ++chr; - r.second = S8Span(chr, end); - return r; + u8 *chr = S8Chr(s, c); + if (!chr) + return (split_result){s}; + split_result r = { + .first = S8Span(s.data, chr), + }; + u8 *end = S8End(s) + 1; + while (chr != end && *chr == c) + ++chr; + r.second = S8Span(chr, end); + return r; } RTC_API s8 S8Clone(s8 s, arena *a) @@ -529,38 +539,38 @@ S8Clone(s8 s, arena *a) RTC_API s8_parse_i64_result S8ParseI64(s8 s, int base) { - isize at = s.length - 1; - i64 exp = 1; - i64 val = 0; - while (at >= 0) - { - u8 c = s.data[at]; - int digit = 0; - if (c >= '0' && c <= '9') - digit = c - '0'; - else if (c >= 'A' && c <= 'Z') - digit = c - 'A'; - else if (c >= 'a' && c <= 'z') - digit = c - 'a'; - else if (c == '-') - { - val *= -1; - break; - } - else if (c == '+') - { - break; - } - else - return (s8_parse_i64_result){0}; - if (digit >= base) - return (s8_parse_i64_result){0}; - - val += digit * exp; - exp *= base; - --at; - } - return (s8_parse_i64_result){ + isize at = s.length - 1; + i64 exp = 1; + i64 val = 0; + while (at >= 0) + { + u8 c = s.data[at]; + int digit = 0; + if (c >= '0' && c <= '9') + digit = c - '0'; + else if (c >= 'A' && c <= 'Z') + digit = c - 'A'; + else if (c >= 'a' && c <= 'z') + digit = c - 'a'; + else if (c == '-') + { + val *= -1; + break; + } + else if (c == '+') + { + break; + } + else + return (s8_parse_i64_result){0}; + if (digit >= base) + return (s8_parse_i64_result){0}; + + val += digit * exp; + exp *= base; + --at; + } + return (s8_parse_i64_result){ .i = val, .ok = 1, }; @@ -568,38 +578,38 @@ S8ParseI64(s8 s, int base) RTC_API s8_parse_i32_result S8ParseI32(s8 s, int base) { - isize at = s.length - 1; - i32 exp = 1; - i32 val = 0; - while (at >= 0) - { - u8 c = s.data[at]; - int digit = 0; - if (c >= '0' && c <= '9') - digit = c - '0'; - else if (c >= 'A' && c <= 'Z') - digit = c - 'A'; - else if (c >= 'a' && c <= 'z') - digit = c - 'a'; - else if (c == '-') - { - val *= -1; - break; - } - else if (c == '+') - { - break; - } - else - return (s8_parse_i32_result){0}; - if (digit >= base) - return (s8_parse_i32_result){0}; - - val += digit * exp; - exp *= base; - --at; - } - return (s8_parse_i32_result){ + isize at = s.length - 1; + i32 exp = 1; + i32 val = 0; + while (at >= 0) + { + u8 c = s.data[at]; + int digit = 0; + if (c >= '0' && c <= '9') + digit = c - '0'; + else if (c >= 'A' && c <= 'Z') + digit = c - 'A'; + else if (c >= 'a' && c <= 'z') + digit = c - 'a'; + else if (c == '-') + { + val *= -1; + break; + } + else if (c == '+') + { + break; + } + else + return (s8_parse_i32_result){0}; + if (digit >= base) + return (s8_parse_i32_result){0}; + + val += digit * exp; + exp *= base; + --at; + } + return (s8_parse_i32_result){ .i = val, .ok = 1, }; @@ -672,20 +682,19 @@ JoinThread(thread *t) { void *ret = NULL; pthread_join((pthread_t)(usize)t, &ret); - return ret; + return ret; } #elif defined(_WIN32) RTC_API thread * StartThread(thread_fn *fn, void *param) { - HANDLE h = CreateThread( - NULL, - 0, /* Use default stack size */ - (LPTHREAD_START_ROUTINE)fn, - param, - 0, - NULL); + HANDLE h = CreateThread(NULL, + 0, /* Use default stack size */ + (LPTHREAD_START_ROUTINE)fn, + param, + 0, + NULL); return (thread *)h; } RTC_API THREAD_RETURN_TYPE @@ -705,4 +714,3 @@ JoinThread(thread *t) #endif #endif -