I have no idea but something for linux

This commit is contained in:
bumbread 2022-06-28 23:57:14 +11:00
parent 328e9f6c35
commit d70de47c01
8 changed files with 74 additions and 41 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
bin
a.out
*.exe
*.lib
*.obj

View File

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

View File

@ -3,6 +3,7 @@
#include <fenv.h>
#include <errno.h>
#include <stdint.h>
#include <stddef.h>
static double LN2 = 0.693147180559945309417232121458176;
static double HALF_PI = 1.570796326794896619231321691639751;

View File

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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
#include <fcntl.h>
#include <unistd.h>
#include <stddef.h>
@ -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;
}

View File

@ -1,9 +1,9 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
// #include <stdio.h>
// #include <errno.h>
// #include <stdlib.h>
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]);
}
}