From 4287d9f601a592dcde6387b1a52d307729653555 Mon Sep 17 00:00:00 2001 From: NeGate Date: Fri, 27 Jan 2023 01:50:37 -0500 Subject: [PATCH] Variety of changes (got Cuik compiling with it) --- bake.cmd | 8 +-- inc/threads.h | 60 ++++++++--------- src/_win/cmdline.c | 125 ++++++++++++++++------------------- src/_win/cookie.c | 4 +- src/_win/entry.c | 8 +-- src/_win/win.h | 3 +- src/ciabatta.c | 30 ++++----- src/conv/decfloat/decfloat.c | 32 ++++----- src/math/basic.c | 24 ++++++- test/test_threads.c | 2 +- 10 files changed, 154 insertions(+), 142 deletions(-) diff --git a/bake.cmd b/bake.cmd index 7bc983c..655d480 100644 --- a/bake.cmd +++ b/bake.cmd @@ -2,12 +2,12 @@ :: Compile UTF-8 resource into .obj file :: this .obj file has to be linked to the executable using it, NOT archived :: together with ciabatta.lib. -windres -o utf8.obj utf8.rc -ld -relocatable -o libwinsane.obj utf8.obj +rem windres -o utf8.obj utf8.rc :: Compile chkstk nasm src\_win\chkstk.asm -ochkstk.o -fwin64 :: Compile the rest of the party -clang -Wall src\ciabatta.c -o ciabatta.obj -c -DCIABATTA_WIN -I inc -I src\_win -nodefaultlibs -g -mfma -lib /nologo /out:ciabatta.lib chkstk.o ciabatta.obj \ No newline at end of file +rem clang -Wall src\ciabatta.c -o ciabatta.obj -c -DCIABATTA_WIN -I inc -I src\_win -nodefaultlibs -g -mfma +cuik src\ciabatta.c -o ciabatta.obj -c -DCIABATTA_WIN -I inc -I src\_win -nostdlib +lib /nologo /out:ciabatta.lib chkstk.o ciabatta.obj diff --git a/inc/threads.h b/inc/threads.h index b533268..096dcd2 100644 --- a/inc/threads.h +++ b/inc/threads.h @@ -6,37 +6,37 @@ #define TSS_DTOR_ITERATIONS 32 #if !defined(_timespec_defined) - #define _timespec_defined - typedef unsigned long long time_t; - struct timespec { - time_t tv_sec; - long tv_nsec; - }; +#define _timespec_defined +typedef unsigned long long time_t; +struct timespec { + time_t tv_sec; + long tv_nsec; +}; #endif #if defined(_WIN32) - typedef struct cnd_t { - int idk_yet; - } cnd_t; +typedef struct cnd_t { + int idk_yet; +} cnd_t; - typedef struct thrd_t { - void* handle; - } thrd_t; +typedef struct thrd_t { + void* handle; +} thrd_t; - typedef struct tss_t { - int idk_yet; - } tss_t; +typedef struct tss_t { + int idk_yet; +} tss_t; - typedef struct mtx_t { - int type; - // Done to handle recursive mutexes - unsigned long recursion; - unsigned long owner; - _Atomic(int) counter; - void* semaphore; - } mtx_t; +typedef struct mtx_t { + int type; + // Done to handle recursive mutexes + unsigned long recursion; + unsigned long owner; + _Atomic(int) counter; + void* semaphore; +} mtx_t; #else - #error "Not implemented" +#error "Not implemented" #endif typedef void(*tss_dtor_t) (void*); @@ -50,11 +50,11 @@ enum { }; enum { - thrd_success, - thrd_timedout, - thrd_busy, - thrd_error, - thrd_nomem + thrd_success, + thrd_timedout, + thrd_busy, + thrd_error, + thrd_nomem }; void call_once(once_flag *flag, void (*func)(void)); @@ -86,7 +86,7 @@ int thrd_detach (thrd_t thr); int thrd_equal (thrd_t thr0, thrd_t thr1); int thrd_join (thrd_t thr, int *res); void thrd_yield (void); -int thrd_sleep( +int thrd_sleep( const struct timespec *duration, struct timespec *remaining ); diff --git a/src/_win/cmdline.c b/src/_win/cmdline.c index 5826e33..99ec0c5 100644 --- a/src/_win/cmdline.c +++ b/src/_win/cmdline.c @@ -17,89 +17,89 @@ static int cmdline_to_argv8(const wchar_t *cmd, char **argv) { } switch (state) { - case 0: switch (c) { // outside token + case 0: switch (c) { // outside token case 0x09: case 0x20: continue; case 0x22: argv[argc++] = buf; - state = 2; - continue; + state = 2; + continue; case 0x5c: argv[argc++] = buf; - slash = 1; - state = 3; - break; + slash = 1; + state = 3; + break; default : argv[argc++] = buf; - state = 1; - } break; - case 1: switch (c) { // inside unquoted token + state = 1; + } break; + case 1: switch (c) { // inside unquoted token case 0x09: case 0x20: *buf++ = 0; - state = 0; - continue; + state = 0; + continue; case 0x22: state = 2; - continue; + continue; case 0x5c: slash = 1; - state = 3; - break; - } break; - case 2: switch (c) { // inside quoted token + state = 3; + break; + } break; + case 2: switch (c) { // inside quoted token case 0x22: state = 5; - continue; + continue; case 0x5c: slash = 1; - state = 4; - break; - } break; - case 3: - case 4: switch (c) { // backslash sequence + state = 4; + break; + } break; + case 3: + case 4: switch (c) { // backslash sequence case 0x22: buf -= (1 + slash) >> 1; - if (slash & 1) { - state -= 2; - break; - } // fallthrough + if (slash & 1) { + state -= 2; + break; + } // fallthrough default : cmd--; - state -= 2; - continue; + state -= 2; + continue; case 0x5c: slash++; - } break; - case 5: switch (c) { // quoted token exit + } break; + case 5: switch (c) { // quoted token exit default : cmd--; - state = 1; - continue; + state = 1; + continue; case 0x22: state = 1; - } break; - case 6: switch (c) { // begin argv[0] + } break; + case 6: switch (c) { // begin argv[0] case 0x09: case 0x20: *buf++ = 0; - state = 0; - continue; + state = 0; + continue; case 0x22: state = 8; - continue; + continue; default : state = 7; - } break; - case 7: switch (c) { // unquoted argv[0] + } break; + case 7: switch (c) { // unquoted argv[0] case 0x09: case 0x20: *buf++ = 0; - state = 0; - continue; - } break; - case 8: switch (c) { // quoted argv[0] + state = 0; + continue; + } break; + case 8: switch (c) { // quoted argv[0] case 0x22: *buf++ = 0; - state = 0; - continue; - } break; + state = 0; + continue; + } break; } switch (c & 0x1f0880) { // WTF-8/UTF-8 encoding - case 0x00000: *buf++ = 0x00 | ((c >> 0) ); break; - case 0x00080: *buf++ = 0xc0 | ((c >> 6) ); - *buf++ = 0x80 | ((c >> 0) & 63); break; - case 0x00800: - case 0x00880: *buf++ = 0xe0 | ((c >> 12) ); - *buf++ = 0x80 | ((c >> 6) & 63); - *buf++ = 0x80 | ((c >> 0) & 63); break; - default : *buf++ = 0xf0 | ((c >> 18) ); - *buf++ = 0x80 | ((c >> 12) & 63); - *buf++ = 0x80 | ((c >> 6) & 63); - *buf++ = 0x80 | ((c >> 0) & 63); + case 0x00000: *buf++ = 0x00 | ((c >> 0) ); break; + case 0x00080: *buf++ = 0xc0 | ((c >> 6) ); + *buf++ = 0x80 | ((c >> 0) & 63); break; + case 0x00800: + case 0x00880: *buf++ = 0xe0 | ((c >> 12) ); + *buf++ = 0x80 | ((c >> 6) & 63); + *buf++ = 0x80 | ((c >> 0) & 63); break; + default : *buf++ = 0xf0 | ((c >> 18) ); + *buf++ = 0x80 | ((c >> 12) & 63); + *buf++ = 0x80 | ((c >> 6) & 63); + *buf++ = 0x80 | ((c >> 0) & 63); } } @@ -108,18 +108,9 @@ static int cmdline_to_argv8(const wchar_t *cmd, char **argv) { return argc; } -static wchar_t *get_wcmdline() { - // That's right, that's where windows hid the command line - TEB *teb = (TEB *)__readgsqword(offsetof(NT_TIB, Self)); - PEB *peb = teb->ProcessEnvironmentBlock; - RTL_USER_PROCESS_PARAMETERS *params = peb->ProcessParameters; - UNICODE_STRING command_line_str = params->CommandLine; - return command_line_str.Buffer; -} - static char **get_command_args(int *argc_ptr) { static char *argv_buffer[CMDLINE_ARGV_MAX]; - wchar_t *cmdline = get_wcmdline(); + wchar_t *cmdline = GetCommandLineW(); *argc_ptr = cmdline_to_argv8(cmdline, argv_buffer); return argv_buffer; } diff --git a/src/_win/cookie.c b/src/_win/cookie.c index 2833bf5..3efa641 100644 --- a/src/_win/cookie.c +++ b/src/_win/cookie.c @@ -1,10 +1,10 @@ u64 __security_cookie; -static void __security_init_cookie() { +void __security_init_cookie() { // They say it's a random number so I generated // one using numbergenerator.org - __security_cookie = 0xb26e04cc62ba48a; + __security_cookie = 0xb26e04cc62ba48aULL; } void __security_check_cookie(u64 retrieved) { diff --git a/src/_win/entry.c b/src/_win/entry.c index 2b679a4..dc8cfac 100644 --- a/src/_win/entry.c +++ b/src/_win/entry.c @@ -4,8 +4,8 @@ #pragma weak WinMain int main(int argc, char** argv); -int wmain(int argc, wchar_t** argv, wchar_t **envp); -int WinMain(HINSTANCE inst, HINSTANCE pinst, LPSTR cmdline, int showcmd); +// int wmain(int argc, wchar_t** argv, wchar_t **envp); +// int WinMain(HINSTANCE inst, HINSTANCE pinst, LPSTR cmdline, int showcmd); _Noreturn void mainCRTStartup() { _setup_eh(); @@ -24,7 +24,7 @@ _Noreturn void mainCRTStartup() { exit(exit_code); } -_Noreturn void WinMainCRTStartup() { +/*_Noreturn void WinMainCRTStartup() { _setup_eh(); _setup_heap(); _setup_timer(); @@ -39,4 +39,4 @@ _Noreturn void WinMainCRTStartup() { int exit_code = WinMain(inst, 0, cmdline, SW_SHOWDEFAULT); exit(exit_code); -} +}*/ diff --git a/src/_win/win.h b/src/_win/win.h index 1f24001..adc591c 100644 --- a/src/_win/win.h +++ b/src/_win/win.h @@ -11,5 +11,4 @@ static void _setup_timer(void); static void _setup_eh(); static void _setup_heap(); static void _setup_io(); -static void _close_io(); -static void __security_init_cookie(); \ No newline at end of file +static void _close_io(); \ No newline at end of file diff --git a/src/ciabatta.c b/src/ciabatta.c index c38a1ab..5a3e70f 100644 --- a/src/ciabatta.c +++ b/src/ciabatta.c @@ -25,15 +25,15 @@ // Intrinsics #if !defined(__FMA__) - #if !defined(_MSC_VER) - #error "Get a better CPU (the kind that supports FMA) or enable -mfma" - #endif +#if !defined(_MSC_VER) +#error "Get a better CPU (the kind that supports FMA) or enable -mfma" +#endif #endif // xmmintrin.h depends on mm_malloc.h, which itself includes other CRT headers // Which introduces compiler errors. Actually does it really matter? I would // need to check again #undef __STDC_HOSTED__ -#include +// #include #include #include "intrin.h" @@ -65,15 +65,15 @@ // Windows stuff #if defined(CIABATTA_WIN) - #include "_win/win.h" - #include "_win/cookie.c" - #include "_win/assert.c" - #include "_win/cmdline.c" - #include "_win/entry.c" - #include "_win/environment.c" - #include "_win/heap.c" - #include "_win/signal.c" - #include "_win/stdio.c" - #include "_win/threads.c" - #include "_win/time.c" +#include "_win/win.h" +#include "_win/cookie.c" +#include "_win/assert.c" +#include "_win/cmdline.c" +#include "_win/entry.c" +#include "_win/environment.c" +#include "_win/heap.c" +#include "_win/signal.c" +#include "_win/stdio.c" +#include "_win/threads.c" +#include "_win/time.c" #endif diff --git a/src/conv/decfloat/decfloat.c b/src/conv/decfloat/decfloat.c index bbaebd2..a8857c4 100644 --- a/src/conv/decfloat/decfloat.c +++ b/src/conv/decfloat/decfloat.c @@ -14,22 +14,22 @@ struct decfloat_t { static const char DIGIT_TABLE[200] = { "00010203040506070809101112131415161718192021222324" - "25262728293031323334353637383940414243444546474849" - "50515253545556575859606162636465666768697071727374" - "75767778798081828384858687888990919293949596979899" + "25262728293031323334353637383940414243444546474849" + "50515253545556575859606162636465666768697071727374" + "75767778798081828384858687888990919293949596979899" }; static inline u32 pow5Factor(u64 value) { - const u64 m_inv_5 = 14757395258967641293u; // 5 * m_inv_5 = 1 (mod 2^64) - const u64 n_div_5 = 3689348814741910323u; // #{ n | n = 0 (mod 2^64) } = 2^64 / 5 - u32 count = 0; - for (;;) { - value *= m_inv_5; - if (value > n_div_5) - break; - ++count; - } - return count; + const u64 m_inv_5 = 14757395258967641293u; // 5 * m_inv_5 = 1 (mod 2^64) + const u64 n_div_5 = 3689348814741910323u; // #{ n | n = 0 (mod 2^64) } = 2^64 / 5 + u32 count = 0; + for (;;) { + value *= m_inv_5; + if (value > n_div_5) + break; + ++count; + } + return count; } // Returns true if value is divisible by 5^p. @@ -59,11 +59,11 @@ static inline u64 shiftright128(const u64 lo, const u64 hi, const u32 dist) { } static inline u64 mulShift64(const u64 m, const u64* const mul, const int32_t j) { - // m is maximum 55 bits + // m is maximum 55 bits u64 high1; // 128 const u64 low1 = umul128(m, mul[1], &high1); // 64 u64 high0; // 64 - umul128(m, mul[0], &high0); // 0 + umul128(m, mul[0], &high0); // 0 const u64 sum = high0 + low1; if (sum < high0) { ++high1; // overflow into high1 @@ -143,7 +143,7 @@ static decfloat_t ieee_to_decimal(u64 sign, u64 ieeeMantissa, u32 ieeeExponent) int32_t e2; u64 m2; if (ieeeExponent == 0) { - // We subtract 2 so that the bounds computation has 2 additional bits. + // We subtract 2 so that the bounds computation has 2 additional bits. e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2; m2 = ieeeMantissa; } else { diff --git a/src/math/basic.c b/src/math/basic.c index 74efc84..8f6cd31 100644 --- a/src/math/basic.c +++ b/src/math/basic.c @@ -75,6 +75,28 @@ long double fminl(long double x, long double y) { return fmin(x, y); } +#ifdef __CUIK__ +#warning "Cuik doesn't support the FMA intrinsics... fix that NeGate" +double fma(double x, double y, double z) { + return (x * y) + z; +} + +float fmaf(float x, float y, float z) { + return (x * y) + z; +} + +long double fmal(long double x, long double y, long double z) { + return (x * y) + z; +} + +double sqrt(double x) { + return 0.0; +} + +float sqrtf(float x) { + return 0.0; +} +#else double fma(double x, double y, double z) { __m128d xd = _mm_set_sd(x); __m128d yd = _mm_set_sd(y); @@ -115,4 +137,4 @@ float sqrtf(float x) { long double sqrtl(long double x) { return sqrt(x); } - +#endif diff --git a/test/test_threads.c b/test/test_threads.c index fcec016..401e917 100644 --- a/test/test_threads.c +++ b/test/test_threads.c @@ -7,7 +7,7 @@ int cnt; int f(void* thr_data) { - for(int n = 0; n < 1000; ++n) { + for(int n = 0; n < 100000; ++n) { atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed); // atomic ++cnt; // undefined behavior, in practice some updates missed }