Merge remote-tracking branch 'refs/remotes/origin/main'

This commit is contained in:
bumbread 2023-02-01 20:34:27 +11:00
commit d5650150b1
10 changed files with 152 additions and 140 deletions

View File

@ -3,11 +3,11 @@
:: 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
:: Compile chkstk
nasm src\os_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
rem cuik src\ciabatta.c -o ciabatta.obj -c -DCIABATTA_WIN -I inc -I src\_win -nostdlib
lib /nologo /out:ciabatta.lib chkstk.o utf8.obj ciabatta.obj

View File

@ -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
);

View File

@ -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 <immintrin.h>
// #include <immintrin.h>
#include <xmmintrin.h>
#include "intrin.h"
@ -65,15 +65,15 @@
// Windows platform functionality
#if defined(CIABATTA_WIN)
#include "os_win/win.h"
#include "os_win/cookie.c"
#include "os_win/assert.c"
#include "os_win/cmdline.c"
#include "os_win/entry.c"
#include "os_win/environment.c"
#include "os_win/heap.c"
#include "os_win/signal.c"
#include "os_win/stdio.c"
#include "os_win/threads.c"
#include "os_win/time.c"
#include "os_win/win.h"
#include "os_win/cookie.c"
#include "os_win/assert.c"
#include "os_win/cmdline.c"
#include "os_win/entry.c"
#include "os_win/environment.c"
#include "os_win/heap.c"
#include "os_win/signal.c"
#include "os_win/stdio.c"
#include "os_win/threads.c"
#include "os_win/time.c"
#endif

View File

@ -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 {

View File

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

View File

@ -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;
}

View File

@ -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) {

View File

@ -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);
}
}*/

View File

@ -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();
static void _close_io();

View File

@ -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
}