From d70de47c0187f4a8c73971e21e28342b5e1e911b Mon Sep 17 00:00:00 2001 From: bumbread Date: Tue, 28 Jun 2022 23:57:14 +1100 Subject: [PATCH] I have no idea but something for linux --- .gitignore | 1 + Makefile | 25 ++++++++++++++++-------- src/code/math/gen_math.c | 1 + src/code/math/round.c | 36 +++++++++++++++++------------------ src/code/stdlib/conversion.c | 2 +- src/code/stdlib/multibyte.c | 8 ++++---- src/linux/linux_environment.c | 32 ++++++++++++++++++++++++++----- test/test_entry.c | 10 +++++----- 8 files changed, 74 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index 1607e61..7cb052c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ bin +a.out *.exe *.lib *.obj diff --git a/Makefile b/Makefile index 230c046..86465bc 100644 --- a/Makefile +++ b/Makefile @@ -18,36 +18,45 @@ endif # If we're compiling under windows we'll link to these libraries ifeq ($(PLATFORM),win) - LIBS := -lDbghelp -lkernel32 -luser32 -lshell32 + LIBS := -lDbghelp -lkernel32 -luser32 -lshell32 + MKDIR_P_FLAG := + NUL_FILE := NUL +else + LIBS := -lrt -lpthread -ldl + MKDIR_P_FLAG := '-p' + NUL_FILE := /dev/null endif # Compiler flags ifeq ($(CC), clang) - CFLAGS=$(GNUFLAGS) -Werror -Wall -msse2 $(IFLAGS) + CFLAGS=$(GNUFLAGS) -Wall -msse2 -mfma $(IFLAGS) else echo BAD CC exit 1 endif # Figure out what we want to compile at the end -SRC_FILES := $(wildcard $(SRC_DIR)/code/*.c) $(wildcard $(SRC_DIR)/$(PLATFORM)/*.c) +SRC_FILES := \ + $(wildcard $(SRC_DIR)/code/*.c) \ + $(wildcard $(SRC_DIR)/code/**/*.c) \ + $(wildcard $(SRC_DIR)/$(PLATFORM)/*.c) \ + $(wildcard $(SRC_DIR)/$(PLATFORM)/**/*.c) OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.obj,$(SRC_FILES)) $(OBJ_DIR)/%.obj: $(SRC_DIR)/%.c + @-mkdir $(MKDIR_P_FLAG) "$(dir $@)" 2> $(NUL_FILE) $(CC) $(CFLAGS) -c -o $@ $< ciabatta.lib: $(OBJ_FILES) llvm-ar rc $@ $^ test: ciabatta.lib - clang test/test_$(test).c ciabatta.lib -std=c11 $(LIBS) -nostdlib -Iinc + clang -g test/test_$(test).c ciabatta.lib -std=c11 $(LIBS) -nostdlib -Iinc clean: rd/s/q bin || true rm -Rf bin || true - mkdir bin - mkdir bin/code - mkdir bin/win - mkdir bin/linux + rm -f ciabatta.lib || true + del ciabatta.lib || true .PHONY: ciabatta.lib test diff --git a/src/code/math/gen_math.c b/src/code/math/gen_math.c index 7a353d2..206d8cc 100644 --- a/src/code/math/gen_math.c +++ b/src/code/math/gen_math.c @@ -3,6 +3,7 @@ #include #include #include +#include static double LN2 = 0.693147180559945309417232121458176; static double HALF_PI = 1.570796326794896619231321691639751; diff --git a/src/code/math/round.c b/src/code/math/round.c index 230d138..28f0480 100644 --- a/src/code/math/round.c +++ b/src/code/math/round.c @@ -9,9 +9,9 @@ #define asdouble(x) ((union {double f; uint64_t i;}){x}).f #if defined(__GNUC__) || defined(__clang__) - #define just_do_it(t) __attribute__((unused)) volatile t + #define just_do_it(v) do{__attribute__((unused)) volatile double t = v;}while(0) #else - #define just_do_it(t) volatile t + #define just_do_it(v) do{volatile double t = v;}while(0) #endif double nearbyint(double x) { @@ -53,9 +53,9 @@ double nextafter(double x, double y) { } e = ux.i >> 52 & 0x7ff; /* raise overflow if ux.f is infinite and x is finite */ - if (e == 0x7ff) just_do_it(float) _x = x+x; + if (e == 0x7ff) just_do_it(x+x); /* raise underflow if ux.f is subnormal or zero */ - if (e == 0) just_do_it(float) _x = x*x + ux.f*ux.f; + if (e == 0) just_do_it(x*x + ux.f*ux.f); return ux.f; } @@ -78,9 +78,9 @@ float nextafterf(float x, float y) { } e = ux.i & 0x7f800000; /* raise overflow if ux.f is infinite and x is finite */ - if (e == 0x7f800000) just_do_it(float) _x = x+x; + if (e == 0x7f800000) just_do_it(x+x); /* raise underflow if ux.f is subnormal or zero */ - if (e == 0) just_do_it(float) _x = x*x + ux.f*ux.f; + if (e == 0) just_do_it(x*x + ux.f*ux.f); return ux.f; } @@ -109,9 +109,9 @@ float nexttowardf(float x, long double y) { } e = ux.i & 0x7f800000; /* raise overflow if ux.f is infinite and x is finite */ - if (e == 0x7f800000) just_do_it(float) _x = x+x; + if (e == 0x7f800000) just_do_it(x+x); /* raise underflow if ux.f is subnormal or zero */ - if (e == 0) just_do_it(float) _x = x*x + ux.f*ux.f; + if (e == 0) just_do_it(x*x + ux.f*ux.f); return ux.f; } @@ -210,7 +210,7 @@ double round(double x) { if (u.i >> 63) x = -x; if (e < 0x3ff-1) { /* raise inexact if x!=0 */ - just_do_it(float) _x = x + toint; + just_do_it(x + toint); return 0*u.f; } y = x + toint - toint - x; @@ -229,7 +229,7 @@ float roundf(float x) { if (e >= 0x7f+23) return x; if (u.i >> 31) x = -x; if (e < 0x7f-1) { - just_do_it(float) _x = x + toint; + just_do_it(x + toint); return 0*u.f; } y = x + toint - toint - x; @@ -283,7 +283,7 @@ double ceil(double x) { y = x + toint - toint - x; /* special case because of non-nearest rounding modes */ if (e <= 0x3ff-1) { - just_do_it(double) _x = y; + just_do_it(y); return u.i >> 63 ? -0.0 : 1; } if (y < 0) @@ -302,12 +302,12 @@ float ceilf(float x) { m = 0x007fffff >> e; if ((u.i & m) == 0) return x; - just_do_it(float) _x = (x + 0x1p120f); + just_do_it(x + 0x1p120f); if (u.i >> 31 == 0) u.i += m; u.i &= ~m; } else { - just_do_it(float) _x = (x + 0x1p120f); + just_do_it(x + 0x1p120f); if (u.i >> 31) u.f = -0.0; else if (u.i << 1) @@ -334,7 +334,7 @@ double floor(double x) { y = x + toint - toint - x; /* special case because of non-nearest rounding modes */ if (e <= 0x3ff-1) { - just_do_it(double) _x = (y); + just_do_it(y); return u.i >> 63 ? -1 : 0; } if (y > 0) @@ -353,12 +353,12 @@ float floorf(float x) { m = 0x007fffff >> e; if ((u.i & m) == 0) return x; - just_do_it(float) _x = (x + 0x1p120f); + just_do_it(x + 0x1p120f); if (u.i >> 31) u.i += m; u.i &= ~m; } else { - just_do_it(float) _x = (x + 0x1p120f); + just_do_it(x + 0x1p120f); if (u.i >> 31 == 0) u.i = 0; else if (u.i << 1) @@ -383,7 +383,7 @@ double trunc(double x) { m = -1ULL >> e; if ((u.i & m) == 0) return x; - just_do_it(double) _x = (x + 0x1p120f); + just_do_it(x + 0x1p120f); u.i &= ~m; return u.f; } @@ -400,7 +400,7 @@ float truncf(float x) { m = -1U >> e; if ((u.i & m) == 0) return x; - just_do_it(float) _x = (x + 0x1p120f); + just_do_it(x + 0x1p120f); u.i &= ~m; return u.f; } diff --git a/src/code/stdlib/conversion.c b/src/code/stdlib/conversion.c index 6c42d34..334a9f7 100644 --- a/src/code/stdlib/conversion.c +++ b/src/code/stdlib/conversion.c @@ -139,7 +139,7 @@ static intull strtoi_generic(const char *restrict nptr, errno = ERANGE; value = int_max; goto finish; - finish: + finish:; // If no conversion is performed we return the value of 0 and *endptr // is set to the nptr. bool conv_performed = (digits_read > 0); diff --git a/src/code/stdlib/multibyte.c b/src/code/stdlib/multibyte.c index 561617b..0bdbef1 100644 --- a/src/code/stdlib/multibyte.c +++ b/src/code/stdlib/multibyte.c @@ -6,18 +6,18 @@ int mblen(const char *s, size_t n) { } int mbtowc(wchar_t *restrict pwc, const char *restrict s, size_t n) { - + return 0; } size_t mbstowcs(wchar_t *restrict pwcs, const char *restrict s, size_t n) { - + return 0; } int wctomb(char *s, wchar_t wchar) { - + return 0; } size_t wcstombs(char *restrict s, const wchar_t *restrict pwcs, size_t n) { - + return 0; } diff --git a/src/linux/linux_environment.c b/src/linux/linux_environment.c index bafdb13..1ce59c1 100644 --- a/src/linux/linux_environment.c +++ b/src/linux/linux_environment.c @@ -1,4 +1,5 @@ +#include #include #include @@ -14,17 +15,23 @@ static void (*atqexit_funcs[ATQEXIT_FUNC_COUNT])(void); static int atexit_func_count; static int atqexit_func_count; +static char **get_command_args(int *argcp); + +// TODO: Instead of using static arrays, allocate this memory dynamically +static char cmdline[4096]; +static char *cmdargs[1024]; extern int main(int argc, char** argv); + void _start() { - srand(0); setlocale(LC_ALL, "C"); - int argc = 0; - char *argv[1] = {NULL}; - int code = main(argc, argv); - _exit(code); + int argc; + char **argv = get_command_args(&argc); + int exit_code = main(argc, argv); + + exit(exit_code); } _Noreturn void quick_exit(int status) { @@ -66,3 +73,18 @@ int at_quick_exit(void (*func)(void)) { atqexit_funcs[atqexit_func_count++] = func; return 1; } + +static char **get_command_args(int *argcp) { + int fd = open("/proc/self/cmdline", O_RDONLY); + ssize_t size = read(fd, cmdline, sizeof cmdline); + ssize_t i = 0; + ssize_t cmd_idx = 0; + while(i != size) { + cmdargs[cmd_idx] = &cmdline[i]; + while(cmdline[i] != 0) { + ++i; + } + ++i; + } + return cmdargs; +} diff --git a/test/test_entry.c b/test/test_entry.c index 9c240ce..e7d1adb 100644 --- a/test/test_entry.c +++ b/test/test_entry.c @@ -1,9 +1,9 @@ -#include -#include -#include +// #include +// #include +// #include int main(int argc, char** argv) { - for (int i = 0; i < argv; i++) { - printf("[%d] = %s\n", i, argv[i]); + for (int i = 0; i < argc; i++) { + // printf("[%d] = %s\n", i, argv[i]); } }