From 79ac5ba9b12835ea660b1c325f81bc0b23aec73b Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Tue, 26 Jun 2018 20:53:48 +0900 Subject: [PATCH] Fix several Python things --- Makefile | 5 ++++- base/usr/include/inttypes.h | 4 ++++ base/usr/include/math.h | 1 + base/usr/include/sys/types.h | 5 +++++ base/usr/include/sys/wait.h | 9 ++++++++ libc/math/bad.c | 4 ++++ libc/math/math.c | 18 ++++++++++++++- libc/stdio/printf.c | 1 + libc/stdlib/strtod.c | 43 ++++++++++++++++++++++++++++-------- libc/time/localtime.c | 6 ++--- util/lm.c | 3 +++ 11 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 util/lm.c diff --git a/Makefile b/Makefile index 97eeaa3e..0d7eb533 100644 --- a/Makefile +++ b/Makefile @@ -133,6 +133,9 @@ base/lib/libc.a: ${LIBC_OBJS} | dirs crts base/lib/libc.so: ${LIBC_OBJS} | dirs crts $(CC) -nodefaultlibs -o $@ $(CFLAGS) -shared -fPIC $^ -lgcc +base/lib/libm.so: util/lm.c | dirs crts + $(CC) -nodefaultlibs -o $@ $(CFLAGS) -shared -fPIC $^ -lgcc + # Userspace Linker/Loader base/lib/ld.so: linker/linker.c base/lib/libc.a | dirs @@ -162,7 +165,7 @@ endif # Ramdisk -cdrom/ramdisk.img: ${APPS_X} ${LIBS_X} base/lib/ld.so $(shell find base) Makefile | dirs +cdrom/ramdisk.img: ${APPS_X} ${LIBS_X} base/lib/ld.so base/lib/libm.so $(shell find base) Makefile | dirs genext2fs -B 4096 -d base -D util/devtable -U -b `util/calc-size.sh` -N 2048 cdrom/ramdisk.img # CD image diff --git a/base/usr/include/inttypes.h b/base/usr/include/inttypes.h index 5eea35a5..c087963c 100644 --- a/base/usr/include/inttypes.h +++ b/base/usr/include/inttypes.h @@ -2,3 +2,7 @@ #include #include + +#define PRIi8 "i" +#define PRIi16 "i" +#define PRIi32 "i" diff --git a/base/usr/include/math.h b/base/usr/include/math.h index 75e15c9e..8c243d02 100644 --- a/base/usr/include/math.h +++ b/base/usr/include/math.h @@ -27,5 +27,6 @@ extern double log2(double x); extern double sinh(double x); extern double tan(double x); extern double tanh(double x); +extern double atan(double x); extern double modf(double x, double *iptr); diff --git a/base/usr/include/sys/types.h b/base/usr/include/sys/types.h index 86836777..a6e82990 100644 --- a/base/usr/include/sys/types.h +++ b/base/usr/include/sys/types.h @@ -24,3 +24,8 @@ typedef unsigned long useconds_t; typedef long suseconds_t; typedef int pid_t; +#define FD_SETSIZE 64 /* compatibility with newlib */ +typedef long fd_mask; +typedef struct _fd_set { + fd_mask fds_bits[2]; /* should be 64 bits */ +} fd_set; diff --git a/base/usr/include/sys/wait.h b/base/usr/include/sys/wait.h index f1db33de..cf4e9010 100644 --- a/base/usr/include/sys/wait.h +++ b/base/usr/include/sys/wait.h @@ -5,5 +5,14 @@ #define WNOHANG 1 #define WUNTRACED 2 +/* This were taken from newlib, but they remain true */ +#define WIFEXITED(w) (((w) & 0xff) == 0) +#define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f)) +#define WIFSTOPPED(w) (((w) & 0xff) == 0x7f) +#define WEXITSTATUS(w) (((w) >> 8) & 0xff) +#define WTERMSIG(w) ((w) & 0x7f) +#define WSTOPSIG WEXITSTATUS + + extern pid_t wait(int*); extern pid_t waitpid(pid_t, int *, int); diff --git a/libc/math/bad.c b/libc/math/bad.c index 3da551e0..6204d6da 100644 --- a/libc/math/bad.c +++ b/libc/math/bad.c @@ -8,6 +8,10 @@ double asin(double x) { return 0.0; } +double atan(double x) { + return 0.0; +} + double atan2(double y, double x) { return 0.0; } diff --git a/libc/math/math.c b/libc/math/math.c index 4d855cb6..76afab02 100644 --- a/libc/math/math.c +++ b/libc/math/math.c @@ -27,7 +27,23 @@ int abs(int j) { } double pow(double x, double y) { - return __builtin_pow(x,y); + double out; + asm volatile ( + "fyl2x;" + "fld %%st;" + "frndint;" + "fsub %%st,%%st(1);" + "fxch;" + "fchs;" + "f2xm1;" + "fld1;" + "faddp;" + "fxch;" + "fld1;" + "fscale;" + "fstp %%st(1);" + "fmulp;" : "=t"(out) : "0"(x),"u"(y) : "st(1)" ); + return out; } double fabs(double x) { diff --git a/libc/stdio/printf.c b/libc/stdio/printf.c index c0b494c5..d250958d 100644 --- a/libc/stdio/printf.c +++ b/libc/stdio/printf.c @@ -209,6 +209,7 @@ size_t xvasprintf(char * buf, const char * fmt, va_list args) { } b = buf + i; break; + case 'g': /* supposed to also support e */ case 'f': { double val = (double)va_arg(args, double); diff --git a/libc/stdlib/strtod.c b/libc/stdlib/strtod.c index 387608c6..a405f889 100644 --- a/libc/stdlib/strtod.c +++ b/libc/stdlib/strtod.c @@ -1,4 +1,6 @@ #include +#include +#include double strtod(const char *nptr, char **endptr) { int sign = 1; @@ -11,10 +13,7 @@ double strtod(const char *nptr, char **endptr) { while (*nptr && *nptr != '.') { if (*nptr < '0' || *nptr > '9') { - if (endptr) { - *endptr = (char *)nptr; - } - return 0.0; + break; } decimal_part *= 10LL; decimal_part += (long long)(*nptr - '0'); @@ -29,10 +28,7 @@ double strtod(const char *nptr, char **endptr) { while (*nptr) { if (*nptr < '0' || *nptr > '9') { - if (endptr) { - *endptr = (char *)nptr; - } - return ((double)decimal_part) * (double)(sign); + break; } sub_part += multiplier * (*nptr - '0'); @@ -41,9 +37,38 @@ double strtod(const char *nptr, char **endptr) { } } + double expn = (double)sign; + + if (*nptr == 'e' || *nptr == 'E') { + nptr++; + + int exponent_sign = 1; + + if (*nptr == '+') { + nptr++; + } else if (*nptr == '-') { + exponent_sign = -1; + nptr++; + } + + int exponent = 0; + + while (*nptr) { + if (*nptr < '0' || *nptr > '9') { + break; + } + exponent *= 10; + exponent += (*nptr - '0'); + nptr++; + } + + expn = pow(10.0,(double)(exponent * exponent_sign)); + } + if (endptr) { *endptr = (char *)nptr; } - return ((double)decimal_part + sub_part) * (double)(sign); + double result = ((double)decimal_part + sub_part) * expn; + return result; } diff --git a/libc/time/localtime.c b/libc/time/localtime.c index 1245a081..3da3a34c 100644 --- a/libc/time/localtime.c +++ b/libc/time/localtime.c @@ -58,7 +58,7 @@ static long secs_of_month(int months, int year) { struct tm *localtime_r(const time_t *timep, struct tm * _timevalue) { - fprintf(stderr, "Hello world?\n"); + fprintf(stderr, "Hello world? %p %d\n", _timevalue, *timep); long seconds = 0; // this needs to be bigger, but whatever @@ -70,7 +70,7 @@ struct tm *localtime_r(const time_t *timep, struct tm * _timevalue) { fprintf(stderr, "adding %d...\n", added); long secs = added * 86400; - if (seconds + secs >= *timep) { + if (seconds + secs >= *timep + 1) { _timevalue->tm_year = year - 1900; year_sec = seconds; fprintf(stderr, "The year is %d, year_sec=%d\n", year, year_sec); @@ -102,7 +102,7 @@ struct tm *localtime_r(const time_t *timep, struct tm * _timevalue) { seconds += secs; } } - fprintf(stderr, "Failed but this is definitely the right day, returning NULL\n"); + fprintf(stderr, "Failed but this is definitely the right day, returning NULL (seconds = %dbut need %d)\n", seconds, *timep); return NULL; } else { seconds += secs; diff --git a/util/lm.c b/util/lm.c new file mode 100644 index 00000000..a131395d --- /dev/null +++ b/util/lm.c @@ -0,0 +1,3 @@ +int __libm_dummy(void) { + return 42; +}