Merge branch 'cuik-fix'

This commit is contained in:
NeGate 2023-01-27 22:25:26 -05:00
commit fbe47be3c5
10 changed files with 154 additions and 142 deletions

View File

@ -2,12 +2,12 @@
:: Compile UTF-8 resource into .obj file :: Compile UTF-8 resource into .obj file
:: this .obj file has to be linked to the executable using it, NOT archived :: this .obj file has to be linked to the executable using it, NOT archived
:: together with ciabatta.lib. :: together with ciabatta.lib.
windres -o utf8.obj utf8.rc rem windres -o utf8.obj utf8.rc
ld -relocatable -o libwinsane.obj utf8.obj
:: Compile chkstk :: Compile chkstk
nasm src\os_win\chkstk.asm -ochkstk.o -fwin64 nasm src\os_win\chkstk.asm -ochkstk.o -fwin64
:: Compile the rest of the party :: 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 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 lib /nologo /out:ciabatta.lib chkstk.o ciabatta.obj

View File

@ -6,37 +6,37 @@
#define TSS_DTOR_ITERATIONS 32 #define TSS_DTOR_ITERATIONS 32
#if !defined(_timespec_defined) #if !defined(_timespec_defined)
#define _timespec_defined #define _timespec_defined
typedef unsigned long long time_t; typedef unsigned long long time_t;
struct timespec { struct timespec {
time_t tv_sec; time_t tv_sec;
long tv_nsec; long tv_nsec;
}; };
#endif #endif
#if defined(_WIN32) #if defined(_WIN32)
typedef struct cnd_t { typedef struct cnd_t {
int idk_yet; int idk_yet;
} cnd_t; } cnd_t;
typedef struct thrd_t { typedef struct thrd_t {
void* handle; void* handle;
} thrd_t; } thrd_t;
typedef struct tss_t { typedef struct tss_t {
int idk_yet; int idk_yet;
} tss_t; } tss_t;
typedef struct mtx_t { typedef struct mtx_t {
int type; int type;
// Done to handle recursive mutexes // Done to handle recursive mutexes
unsigned long recursion; unsigned long recursion;
unsigned long owner; unsigned long owner;
_Atomic(int) counter; _Atomic(int) counter;
void* semaphore; void* semaphore;
} mtx_t; } mtx_t;
#else #else
#error "Not implemented" #error "Not implemented"
#endif #endif
typedef void(*tss_dtor_t) (void*); typedef void(*tss_dtor_t) (void*);
@ -50,11 +50,11 @@ enum {
}; };
enum { enum {
thrd_success, thrd_success,
thrd_timedout, thrd_timedout,
thrd_busy, thrd_busy,
thrd_error, thrd_error,
thrd_nomem thrd_nomem
}; };
void call_once(once_flag *flag, void (*func)(void)); 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_equal (thrd_t thr0, thrd_t thr1);
int thrd_join (thrd_t thr, int *res); int thrd_join (thrd_t thr, int *res);
void thrd_yield (void); void thrd_yield (void);
int thrd_sleep( int thrd_sleep(
const struct timespec *duration, const struct timespec *duration,
struct timespec *remaining struct timespec *remaining
); );

View File

@ -25,15 +25,15 @@
// Intrinsics // Intrinsics
#if !defined(__FMA__) #if !defined(__FMA__)
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
#error "Get a better CPU (the kind that supports FMA) or enable -mfma" #error "Get a better CPU (the kind that supports FMA) or enable -mfma"
#endif #endif
#endif #endif
// xmmintrin.h depends on mm_malloc.h, which itself includes other CRT headers // xmmintrin.h depends on mm_malloc.h, which itself includes other CRT headers
// Which introduces compiler errors. Actually does it really matter? I would // Which introduces compiler errors. Actually does it really matter? I would
// need to check again // need to check again
#undef __STDC_HOSTED__ #undef __STDC_HOSTED__
#include <immintrin.h> // #include <immintrin.h>
#include <xmmintrin.h> #include <xmmintrin.h>
#include "intrin.h" #include "intrin.h"
@ -65,15 +65,15 @@
// Windows stuff // Windows stuff
#if defined(CIABATTA_WIN) #if defined(CIABATTA_WIN)
#include "os_win/win.h" #include "os_win/win.h"
#include "os_win/cookie.c" #include "os_win/cookie.c"
#include "os_win/assert.c" #include "os_win/assert.c"
#include "os_win/cmdline.c" #include "os_win/cmdline.c"
#include "os_win/entry.c" #include "os_win/entry.c"
#include "os_win/environment.c" #include "os_win/environment.c"
#include "os_win/heap.c" #include "os_win/heap.c"
#include "os_win/signal.c" #include "os_win/signal.c"
#include "os_win/stdio.c" #include "os_win/stdio.c"
#include "os_win/threads.c" #include "os_win/threads.c"
#include "os_win/time.c" #include "os_win/time.c"
#endif #endif

View File

@ -14,22 +14,22 @@ struct decfloat_t {
static const char DIGIT_TABLE[200] = { static const char DIGIT_TABLE[200] = {
"00010203040506070809101112131415161718192021222324" "00010203040506070809101112131415161718192021222324"
"25262728293031323334353637383940414243444546474849" "25262728293031323334353637383940414243444546474849"
"50515253545556575859606162636465666768697071727374" "50515253545556575859606162636465666768697071727374"
"75767778798081828384858687888990919293949596979899" "75767778798081828384858687888990919293949596979899"
}; };
static inline u32 pow5Factor(u64 value) { static inline u32 pow5Factor(u64 value) {
const u64 m_inv_5 = 14757395258967641293u; // 5 * m_inv_5 = 1 (mod 2^64) 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 const u64 n_div_5 = 3689348814741910323u; // #{ n | n = 0 (mod 2^64) } = 2^64 / 5
u32 count = 0; u32 count = 0;
for (;;) { for (;;) {
value *= m_inv_5; value *= m_inv_5;
if (value > n_div_5) if (value > n_div_5)
break; break;
++count; ++count;
} }
return count; return count;
} }
// Returns true if value is divisible by 5^p. // 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) { 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 u64 high1; // 128
const u64 low1 = umul128(m, mul[1], &high1); // 64 const u64 low1 = umul128(m, mul[1], &high1); // 64
u64 high0; // 64 u64 high0; // 64
umul128(m, mul[0], &high0); // 0 umul128(m, mul[0], &high0); // 0
const u64 sum = high0 + low1; const u64 sum = high0 + low1;
if (sum < high0) { if (sum < high0) {
++high1; // overflow into high1 ++high1; // overflow into high1
@ -143,7 +143,7 @@ static decfloat_t ieee_to_decimal(u64 sign, u64 ieeeMantissa, u32 ieeeExponent)
int32_t e2; int32_t e2;
u64 m2; u64 m2;
if (ieeeExponent == 0) { 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; e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
m2 = ieeeMantissa; m2 = ieeeMantissa;
} else { } else {

View File

@ -75,6 +75,28 @@ long double fminl(long double x, long double y) {
return fmin(x, 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) { double fma(double x, double y, double z) {
__m128d xd = _mm_set_sd(x); __m128d xd = _mm_set_sd(x);
__m128d yd = _mm_set_sd(y); __m128d yd = _mm_set_sd(y);
@ -115,4 +137,4 @@ float sqrtf(float x) {
long double sqrtl(long double x) { long double sqrtl(long double x) {
return sqrt(x); return sqrt(x);
} }
#endif

View File

@ -17,89 +17,89 @@ static int cmdline_to_argv8(const wchar_t *cmd, char **argv) {
} }
switch (state) { switch (state) {
case 0: switch (c) { // outside token case 0: switch (c) { // outside token
case 0x09: case 0x09:
case 0x20: continue; case 0x20: continue;
case 0x22: argv[argc++] = buf; case 0x22: argv[argc++] = buf;
state = 2; state = 2;
continue; continue;
case 0x5c: argv[argc++] = buf; case 0x5c: argv[argc++] = buf;
slash = 1; slash = 1;
state = 3; state = 3;
break; break;
default : argv[argc++] = buf; default : argv[argc++] = buf;
state = 1; state = 1;
} break; } break;
case 1: switch (c) { // inside unquoted token case 1: switch (c) { // inside unquoted token
case 0x09: case 0x09:
case 0x20: *buf++ = 0; case 0x20: *buf++ = 0;
state = 0; state = 0;
continue; continue;
case 0x22: state = 2; case 0x22: state = 2;
continue; continue;
case 0x5c: slash = 1; case 0x5c: slash = 1;
state = 3; state = 3;
break; break;
} break; } break;
case 2: switch (c) { // inside quoted token case 2: switch (c) { // inside quoted token
case 0x22: state = 5; case 0x22: state = 5;
continue; continue;
case 0x5c: slash = 1; case 0x5c: slash = 1;
state = 4; state = 4;
break; break;
} break; } break;
case 3: case 3:
case 4: switch (c) { // backslash sequence case 4: switch (c) { // backslash sequence
case 0x22: buf -= (1 + slash) >> 1; case 0x22: buf -= (1 + slash) >> 1;
if (slash & 1) { if (slash & 1) {
state -= 2; state -= 2;
break; break;
} // fallthrough } // fallthrough
default : cmd--; default : cmd--;
state -= 2; state -= 2;
continue; continue;
case 0x5c: slash++; case 0x5c: slash++;
} break; } break;
case 5: switch (c) { // quoted token exit case 5: switch (c) { // quoted token exit
default : cmd--; default : cmd--;
state = 1; state = 1;
continue; continue;
case 0x22: state = 1; case 0x22: state = 1;
} break; } break;
case 6: switch (c) { // begin argv[0] case 6: switch (c) { // begin argv[0]
case 0x09: case 0x09:
case 0x20: *buf++ = 0; case 0x20: *buf++ = 0;
state = 0; state = 0;
continue; continue;
case 0x22: state = 8; case 0x22: state = 8;
continue; continue;
default : state = 7; default : state = 7;
} break; } break;
case 7: switch (c) { // unquoted argv[0] case 7: switch (c) { // unquoted argv[0]
case 0x09: case 0x09:
case 0x20: *buf++ = 0; case 0x20: *buf++ = 0;
state = 0; state = 0;
continue; continue;
} break; } break;
case 8: switch (c) { // quoted argv[0] case 8: switch (c) { // quoted argv[0]
case 0x22: *buf++ = 0; case 0x22: *buf++ = 0;
state = 0; state = 0;
continue; continue;
} break; } break;
} }
switch (c & 0x1f0880) { // WTF-8/UTF-8 encoding switch (c & 0x1f0880) { // WTF-8/UTF-8 encoding
case 0x00000: *buf++ = 0x00 | ((c >> 0) ); break; case 0x00000: *buf++ = 0x00 | ((c >> 0) ); break;
case 0x00080: *buf++ = 0xc0 | ((c >> 6) ); case 0x00080: *buf++ = 0xc0 | ((c >> 6) );
*buf++ = 0x80 | ((c >> 0) & 63); break; *buf++ = 0x80 | ((c >> 0) & 63); break;
case 0x00800: case 0x00800:
case 0x00880: *buf++ = 0xe0 | ((c >> 12) ); case 0x00880: *buf++ = 0xe0 | ((c >> 12) );
*buf++ = 0x80 | ((c >> 6) & 63); *buf++ = 0x80 | ((c >> 6) & 63);
*buf++ = 0x80 | ((c >> 0) & 63); break; *buf++ = 0x80 | ((c >> 0) & 63); break;
default : *buf++ = 0xf0 | ((c >> 18) ); default : *buf++ = 0xf0 | ((c >> 18) );
*buf++ = 0x80 | ((c >> 12) & 63); *buf++ = 0x80 | ((c >> 12) & 63);
*buf++ = 0x80 | ((c >> 6) & 63); *buf++ = 0x80 | ((c >> 6) & 63);
*buf++ = 0x80 | ((c >> 0) & 63); *buf++ = 0x80 | ((c >> 0) & 63);
} }
} }
@ -108,18 +108,9 @@ static int cmdline_to_argv8(const wchar_t *cmd, char **argv) {
return argc; 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 **get_command_args(int *argc_ptr) {
static char *argv_buffer[CMDLINE_ARGV_MAX]; static char *argv_buffer[CMDLINE_ARGV_MAX];
wchar_t *cmdline = get_wcmdline(); wchar_t *cmdline = GetCommandLineW();
*argc_ptr = cmdline_to_argv8(cmdline, argv_buffer); *argc_ptr = cmdline_to_argv8(cmdline, argv_buffer);
return argv_buffer; return argv_buffer;
} }

View File

@ -1,10 +1,10 @@
u64 __security_cookie; u64 __security_cookie;
static void __security_init_cookie() { void __security_init_cookie() {
// They say it's a random number so I generated // They say it's a random number so I generated
// one using numbergenerator.org // one using numbergenerator.org
__security_cookie = 0xb26e04cc62ba48a; __security_cookie = 0xb26e04cc62ba48aULL;
} }
void __security_check_cookie(u64 retrieved) { void __security_check_cookie(u64 retrieved) {

View File

@ -4,8 +4,8 @@
#pragma weak WinMain #pragma weak WinMain
int main(int argc, char** argv); int main(int argc, char** argv);
int wmain(int argc, wchar_t** argv, wchar_t **envp); // int wmain(int argc, wchar_t** argv, wchar_t **envp);
int WinMain(HINSTANCE inst, HINSTANCE pinst, LPSTR cmdline, int showcmd); // int WinMain(HINSTANCE inst, HINSTANCE pinst, LPSTR cmdline, int showcmd);
_Noreturn void mainCRTStartup() { _Noreturn void mainCRTStartup() {
_setup_eh(); _setup_eh();
@ -24,7 +24,7 @@ _Noreturn void mainCRTStartup() {
exit(exit_code); exit(exit_code);
} }
_Noreturn void WinMainCRTStartup() { /*_Noreturn void WinMainCRTStartup() {
_setup_eh(); _setup_eh();
_setup_heap(); _setup_heap();
_setup_timer(); _setup_timer();
@ -39,4 +39,4 @@ _Noreturn void WinMainCRTStartup() {
int exit_code = WinMain(inst, 0, cmdline, SW_SHOWDEFAULT); int exit_code = WinMain(inst, 0, cmdline, SW_SHOWDEFAULT);
exit(exit_code); exit(exit_code);
} }*/

View File

@ -12,4 +12,3 @@ static void _setup_eh();
static void _setup_heap(); static void _setup_heap();
static void _setup_io(); static void _setup_io();
static void _close_io(); static void _close_io();
static void __security_init_cookie();

View File

@ -7,7 +7,7 @@ int cnt;
int f(void* thr_data) 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 atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed); // atomic
++cnt; // undefined behavior, in practice some updates missed ++cnt; // undefined behavior, in practice some updates missed
} }