Browse Source

Fix several Python things

K. Lange 2 years ago
parent
commit
79ac5ba9b1

+ 4 - 1
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

+ 4 - 0
base/usr/include/inttypes.h

@@ -2,3 +2,7 @@
 
 #include <stdint.h>
 #include <stddef.h>
+
+#define PRIi8 "i"
+#define PRIi16 "i"
+#define PRIi32 "i"

+ 1 - 0
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);

+ 5 - 0
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;

+ 9 - 0
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);

+ 4 - 0
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;
 }

+ 17 - 1
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) {

+ 1 - 0
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);

+ 34 - 9
libc/stdlib/strtod.c

@@ -1,4 +1,6 @@
 #include <stdlib.h>
+#include <math.h>
+#include <stdio.h>
 
 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;
 }
 

+ 3 - 3
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;

+ 3 - 0
util/lm.c

@@ -0,0 +1,3 @@
+int __libm_dummy(void) {
+	return 42;
+}