diff options
-rw-r--r-- | stage3/font.c | 14 | ||||
-rw-r--r-- | stage3/font.h | 1 | ||||
-rw-r--r-- | stage3/math.h | 62 | ||||
-rw-r--r-- | stage3/shell.c | 9 |
4 files changed, 78 insertions, 8 deletions
diff --git a/stage3/font.c b/stage3/font.c index f47c9c9..3bbba86 100644 --- a/stage3/font.c +++ b/stage3/font.c @@ -3,6 +3,8 @@ #include "gfx.h" #include "heap.h" #include "memory.h" +#include "math.h" + #include "font_builtin.c" // important: must be a multiple of 2, else code won't work @@ -197,3 +199,15 @@ void print_hex(u64 x) print_num(x, 16); } +void print_dbl(double d, u8 points) +{ + if (d < 0) { + print_char('-'); + d = -d; + } + + long i = d; + print_dec(i); + print_char('.'); + print_num_pad((d - (double) i) * (double) ipow(10, points), 10, points, '0'); +} diff --git a/stage3/font.h b/stage3/font.h index 154b58e..03772db 100644 --- a/stage3/font.h +++ b/stage3/font.h @@ -16,5 +16,6 @@ void print_dec(u64 x); void print_hex(u64 x); void print_num(u64 x, u8 base); void print_num_pad(u64 x, u8 base, u8 pad_len, char pad_char); +void print_dbl(double d, u8 points); #endif diff --git a/stage3/math.h b/stage3/math.h new file mode 100644 index 0000000..6f7ae58 --- /dev/null +++ b/stage3/math.h @@ -0,0 +1,62 @@ +#ifndef MATH_H +#define MATH_H + +// for now, stuff will be added in this file as it is needed + +#define PI 3.14159265358979323846 + +/*double get_pi() +{ + double pi; + asm("fldpi; fstpl %0":"=m"(pi)); + return pi; +}*/ + +static inline double sin(double x) +{ + asm("fldl %1; fsin; fstpl %0":"=m"(x):"m"(x)); + return x; +} + +static inline double rad(double x) +{ + return PI / 180.0 * x; +} + +static inline double deg(double x) +{ + return 180.0 / PI * x; +} + +static inline double fabs(double x) +{ + asm("fldl %1; fabs; fstpl %0":"=m"(x):"m"(x)); + return x; +} + +static inline double atan2(double x, double y) +{ + asm("fldl %1; fldl %2; fpatan; fstpl %0":"=m"(x):"m"(x),"m"(y)); + return x; +} + +static inline double sqrt(double x) +{ + asm("fldl %0; fsqrt; fstpl %0":"=m"(x):"m"(x)); + return x; +} + +static inline double acos(double x) +{ + return atan2(sqrt(1-x*x),x); +} + +static inline long ipow(long b, unsigned long exp) +{ + long x = 1; + for (unsigned long i = 0; i < exp; i++) + x *= b; + return x; +} + +#endif diff --git a/stage3/shell.c b/stage3/shell.c index 0f56a04..055c296 100644 --- a/stage3/shell.c +++ b/stage3/shell.c @@ -7,6 +7,7 @@ #include "pci.h" #include "memory.h" #include "io.h" +#include "math.h" static void cmd_echo(str arg) { @@ -177,14 +178,6 @@ static void cmd_uname(str arg) print(S("\n")); } -static u64 ipow(u64 b, u64 exp) -{ - u64 x = 1; - for (u64 i = 0; i < exp; i++) - x *= b; - return x; -} - static void print_bytes(usize bytes) { static char fmt[] = { ' ', 'K', 'M', 'G', 'T' }; |