diff --git a/Makefile b/Makefile index 312a81e..a5acc0b 100644 --- a/Makefile +++ b/Makefile @@ -13,3 +13,4 @@ include make/libkb.mk include make/libaux.mk include make/libarena.mk include make/libioutil.mk +include make/libmath.mk diff --git a/aux/compiledb.sh b/aux/compiledb.sh index 8c28bb2..3dbbd14 100755 --- a/aux/compiledb.sh +++ b/aux/compiledb.sh @@ -12,3 +12,4 @@ make -B all_compiledb_libkb make -B all_compiledb_libaux make -B all_compiledb_libarena make -B all_compiledb_libioutil +make -B all_compiledb_libmath diff --git a/aux/devel.sh b/aux/devel.sh index 66d9561..91f70f8 100755 --- a/aux/devel.sh +++ b/aux/devel.sh @@ -19,6 +19,7 @@ make -B all_libkb "$bt" make -B all_libaux "$bt" make -B all_libarena "$bt" make -B all_libioutil "$bt" +make -B all_libmath "$bt" make -B all_apps "$bt" make -B all_dist ./aux/limine_iso_amd64.sh diff --git a/aux/docs.sh b/aux/docs.sh index 2d727f8..cbd69df 100755 --- a/aux/docs.sh +++ b/aux/docs.sh @@ -13,5 +13,6 @@ make -B docs_libkb make -B docs_libaux make -B docs_libarena make -B docs_libioutil +make -B docs_libmath mkdocs build diff --git a/aux/format.sh b/aux/format.sh index 8253fab..d1faf8b 100755 --- a/aux/format.sh +++ b/aux/format.sh @@ -12,4 +12,5 @@ make -B format_libkb make -B format_libaux make -B format_libarena make -B format_libioutil +make -B format_libmath make -B format_apps diff --git a/libaux/amd64/.gitignore b/libaux/amd64/.gitignore new file mode 100644 index 0000000..5761abc --- /dev/null +++ b/libaux/amd64/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/libaux/amd64/setjmp.c b/libaux/amd64/setjmp.c new file mode 100644 index 0000000..f2a9077 --- /dev/null +++ b/libaux/amd64/setjmp.c @@ -0,0 +1,34 @@ +#include + +/* https://nullprogram.com/blog/2023/02/12 */ + +__attribute__ ((naked, returns_twice)) int setjmp (jmp_buf buf) { + __asm__ volatile ("mov (%rsp), %rax\n" + "mov %rax, 0(%rcx)\n" + "lea 8(%rsp), %rax\n" + "mov %rax, 8(%rcx)\n" + "mov %rbp, 16(%rcx)\n" + "mov %rbx, 24(%rcx)\n" + "mov %rdi, 32(%rcx)\n" + "mov %rsi, 40(%rcx)\n" + "mov %r12, 48(%rcx)\n" + "mov %r13, 56(%rcx)\n" + "mov %r14, 64(%rcx)\n" + "mov %r15, 72(%rcx)\n" + "xor %eax, %eax\n" + "ret\n"); +} + +__attribute__ ((naked, noreturn)) void longjmp (jmp_buf buf, int ret) { + __asm__ volatile ("mov 72(%rcx), %r15\n" + "mov 64(%rcx), %r14\n" + "mov 56(%rcx), %r13\n" + "mov 48(%rcx), %r12\n" + "mov 40(%rcx), %rsi\n" + "mov 32(%rcx), %rdi\n" + "mov 24(%rcx), %rbx\n" + "mov 16(%rcx), %rbp\n" + "mov 8(%rcx), %rsp\n" + "mov %edx, %eax\n" + "jmp *0(%rcx)\n"); +} diff --git a/libaux/amd64/src.mk b/libaux/amd64/src.mk new file mode 100644 index 0000000..1fd23a6 --- /dev/null +++ b/libaux/amd64/src.mk @@ -0,0 +1,3 @@ +c += amd64/setjmp.c + +o += amd64/setjmp.o diff --git a/libaux/assert.h b/libaux/assert.h new file mode 100644 index 0000000..5d1fca0 --- /dev/null +++ b/libaux/assert.h @@ -0,0 +1,12 @@ +#ifndef _LIBAUX_ASSERT_H +#define _LIBAUX_ASSERT_H + +#include + +#define assert(x) \ + do { \ + if (!!(x)) \ + quit (); \ + } while (0) + +#endif // _LIBAUX_ASSERT_H diff --git a/libaux/setjmp.h b/libaux/setjmp.h new file mode 100644 index 0000000..81efbb7 --- /dev/null +++ b/libaux/setjmp.h @@ -0,0 +1,12 @@ +#ifndef _LIBAUX_SETJMP_H +#define _LIBAUX_SETJMP_H + +#if defined(__x86_64__) +typedef void* jmp_buf[10]; +#endif + +__attribute__ ((naked, returns_twice)) int setjmp (jmp_buf buf); + +__attribute__ ((naked, noreturn)) void longjmp (jmp_buf buf, int ret); + +#endif // _LIBAUX_SETJMP_H diff --git a/libaux/src.mk b/libaux/src.mk index 204de35..483f18d 100644 --- a/libaux/src.mk +++ b/libaux/src.mk @@ -1,3 +1,5 @@ +include $(platform)/src.mk + c += printf.c \ path.c diff --git a/libmath/.gitignore b/libmath/.gitignore new file mode 100644 index 0000000..de276e3 --- /dev/null +++ b/libmath/.gitignore @@ -0,0 +1,4 @@ +*.o +*.json +docs/ +.cache/ diff --git a/libmath/Makefile b/libmath/Makefile new file mode 100644 index 0000000..71ea81f --- /dev/null +++ b/libmath/Makefile @@ -0,0 +1,5 @@ +include ../make/ufuncs.mk + +libname := libmath + +include ../make/lib.mk diff --git a/libmath/build/.gitignore b/libmath/build/.gitignore new file mode 100644 index 0000000..10301e2 --- /dev/null +++ b/libmath/build/.gitignore @@ -0,0 +1 @@ +*.a diff --git a/libmath/math.c b/libmath/math.c new file mode 100644 index 0000000..5b76979 --- /dev/null +++ b/libmath/math.c @@ -0,0 +1,5 @@ +#include + +double fmod (double a, double b) { return a - ((double)((long long)(a / b))) * b; } + +float fmodf (float a, float b) { return a - ((float)((long)(a / b))) * b; } diff --git a/libmath/math.h b/libmath/math.h new file mode 100644 index 0000000..53ba2ce --- /dev/null +++ b/libmath/math.h @@ -0,0 +1,8 @@ +#ifndef _LIBMATH_MATH_H +#define _LIBMATH_MATH_H + +double fmod (double a, double b); + +float fmodf (float a, float b); + +#endif // _LIBMATH_MATH_H diff --git a/libmath/src.mk b/libmath/src.mk new file mode 100644 index 0000000..f4df5ac --- /dev/null +++ b/libmath/src.mk @@ -0,0 +1,3 @@ +c += math.c + +o += math.o diff --git a/libstring/string.c b/libstring/string.c index ccca77f..01d5e8d 100644 --- a/libstring/string.c +++ b/libstring/string.c @@ -132,33 +132,59 @@ char* strcat (char* dest, const char* src) { return rdest; } -int isalnum (int c) { return isalpha (c) || isdigit (c); } - -int isalpha (int c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); } - -int iscntrl (int c) { return (c >= 0 && c <= 32) || (c == 127); } - -int isdigit (int c) { return (c >= '0' && c <= '9'); } - -int isgraph (int c) { return (c > 32 && c <= 126); } - -int islower (int c) { return (c >= 'A' && c <= 'z'); } - -int isprint (int c) { return (c >= 32 && c <= 126); } - -int ispunct (int c) { return isgraph (c) && !isalnum (c); } - -int isspace (int c) { - return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'); +int strncmp (const char* s1, const char* s2, size_t n) { + while (n && *s1 && (*s1 == *s2)) { + ++s1; + ++s2; + --n; + } + if (n == 0) { + return 0; + } else { + return (*(unsigned char*)s1 - *(unsigned char*)s2); + } } -int isupper (int c) { return (c >= 'A' && c <= 'Z'); } +/* https://stackoverflow.com/questions/49131175/recreate-the-strstr-function */ +char* strstr (const char* str, const char* substring) { + const char* a; + const char* b; -int isxdigit (int c) { return isdigit (c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); } + b = substring; -int isascii (int c) { return (c >= 0 && c <= 127); } + if (*b == 0) { + return (char*)str; + } -int isblank (int c) { return (c == ' ' || c == '\t'); } + for (; *str != 0; str += 1) { + if (*str != *b) { + continue; + } + + a = str; + while (1) { + if (*b == 0) { + return (char*)str; + } + if (*a++ != *b++) { + break; + } + } + b = substring; + } + + return NULL; +} + +/* https://github.com/gcc-mirror/gcc/blob/master/libiberty/strchr.c */ +char* strchr (register const char* s, int c) { + do { + if (*s == c) { + return (char*)s; + } + } while (*s++); + return (0); +} /* SOURCE: https://aticleworld.com/memmove-function-implementation-in-c/ */ void* memmove (void* dest, const void* src, unsigned int n) { @@ -194,3 +220,43 @@ char* strncat (char* dest, const char* src, size_t n) { *ptr = '\0'; return dest; } + +/* https://stackoverflow.com/questions/14476627/strcpy-implementation-in-c */ +char* strcpy (char* strDest, const char* strSrc) { + char* temp = strDest; + while ((*strDest++ = *strSrc++)) + ; + return temp; +} + +int isalnum (int c) { return isalpha (c) || isdigit (c); } + +int isalpha (int c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); } + +int iscntrl (int c) { return (c >= 0 && c <= 32) || (c == 127); } + +int isdigit (int c) { return (c >= '0' && c <= '9'); } + +int isgraph (int c) { return (c > 32 && c <= 126); } + +int islower (int c) { return (c >= 'A' && c <= 'z'); } + +int isprint (int c) { return (c >= 32 && c <= 126); } + +int ispunct (int c) { return isgraph (c) && !isalnum (c); } + +int isspace (int c) { + return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'); +} + +int isupper (int c) { return (c >= 'A' && c <= 'Z'); } + +int isxdigit (int c) { return isdigit (c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); } + +int isascii (int c) { return (c >= 0 && c <= 127); } + +int isblank (int c) { return (c == ' ' || c == '\t'); } + +int tolower (int chr) { return (chr >= 'A' && chr <= 'Z') ? (chr + 32) : (chr); } + +int toupper (int chr) { return (chr >= 'a' && chr <= 'z') ? (chr - 32) : (chr); } diff --git a/libstring/string.h b/libstring/string.h index cd3f702..41a3c3f 100644 --- a/libstring/string.h +++ b/libstring/string.h @@ -39,6 +39,14 @@ void* memchr (const void* s, unsigned char c, size_t n); void str_split_lines (const char* str, size_t total_len, void* ctx, strtokenize_cb_func_t cb); +char* strcpy (char* strDest, const char* strSrc); + +int strncmp (const char* s1, const char* s2, size_t n); + +char* strstr (const char* str, const char* substring); + +char* strchr (register const char* s, int c); + int isalnum (int c); int isalpha (int c); @@ -65,4 +73,8 @@ int isascii (int c); int isblank (int c); +int tolower (int chr); + +int toupper (int chr); + #endif // _LIBSTRING_STRING_H diff --git a/make/apps.mk b/make/apps.mk index 65ec7eb..5078c0b 100644 --- a/make/apps.mk +++ b/make/apps.mk @@ -1,7 +1,7 @@ apps := \ init \ spin \ - ce + ce \ all_apps: @for d in $(apps); do make -C $$d platform=$(platform) all; done diff --git a/make/libmath.mk b/make/libmath.mk new file mode 100644 index 0000000..58257b7 --- /dev/null +++ b/make/libmath.mk @@ -0,0 +1,16 @@ +all_libmath: + make -C libmath platform=$(platform) all + +all_compiledb_libmath: + bear --output libmath/compile_commands.json -- make -C libmath platform=$(platform) all + +clean_libmath: + make -C libmath platform=$(platform) clean + +format_libmath: + make -C libmath platform=$(platform) format + +docs_libmath: + make -C libmath platform=$(platform) docs + +.PHONY: all_libmath clean_libmath format_libmath docs_libmath all_compiledb_libmath diff --git a/make/user.mk b/make/user.mk index 301b294..3d2c7f2 100644 --- a/make/user.mk +++ b/make/user.mk @@ -4,16 +4,19 @@ o := c := ldflags += -L ../libsystem/build -l:libsystem.a -L ../liballoc/build -l:liballoc.a cflags += -isystem ../libsystem -isystem ../liballoc +extra_deps += include src.mk include app.mk include ../generic/flags.mk include ../$(platform)/flags.mk +FORMAT ?= clang-format -i $$(git ls-files '*.c' '*.h') + all: $(app) -$(app): $(o) - $(ld) -o $@ $(ldflags) -T ../$(platform)/link.ld $^ +$(app): $(extra_deps) $(o) + $(ld) -o $@ $(ldflags) -T ../$(platform)/link.ld $(o) %.o: %.c $(cc) -c -o $@ $(cflags) $< @@ -25,6 +28,6 @@ clean: rm -f $(o) $(app) format: - clang-format -i $$(git ls-files '*.c' '*.h') + $(FORMAT) .PHONY: all clean format