From 349d2ec1c2605a5cc0e792b073d6bf9f13ebbfe8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 6 Nov 2019 13:07:07 +0100 Subject: [PATCH] Kernel: Link with libgcc This allows us to get rid of all the custom 64-bit division helpers. I wanted to do this ages ago but couldn't get it working. Turns out it was unstable due to libgcc using the regular ABI and the kernel being built with -mregparm=3. Now that we build the kernel with regular calls, we can just link with libgcc and get this stuff for free. :^) --- Kernel/Makefile | 2 +- Kernel/StdLib.cpp | 95 ----------------------------------------------- 2 files changed, 1 insertion(+), 96 deletions(-) diff --git a/Kernel/Makefile b/Kernel/Makefile index 5a745843a8..c237acf423 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -120,7 +120,7 @@ kernel.map: kernel @echo "MKMAP $@"; sh mkmap.sh $(KERNEL): $(OBJS) - @echo "LD $@"; $(LD) $(LDFLAGS) -o $@ $(OBJS) + @echo "LD $@"; $(LD) $(LDFLAGS) -o $@ $(OBJS) -lgcc .cpp.o: @echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $< diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index 491db3d458..6e9e4dbc33 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -174,99 +174,4 @@ char* strstr(const char* haystack, const char* needle) ASSERT_NOT_REACHED(); } -static inline uint32_t divq(uint64_t n, uint32_t d) -{ - uint32_t n1 = n >> 32; - uint32_t n0 = n; - uint32_t q; - uint32_t r; - asm volatile("divl %4" - : "=d"(r), "=a"(q) - : "0"(n1), "1"(n0), "rm"(d)); - return q; -} - -static uint64_t unsigned_divide64(uint64_t n, uint64_t d) -{ - if ((d >> 32) == 0) { - uint64_t b = 1ULL << 32; - uint32_t n1 = n >> 32; - uint32_t n0 = n; - uint32_t d0 = d; - return divq(b * (n1 % d0) + n0, d0) + b * (n1 / d0); - } - if (n < d) - return 0; - uint32_t d1 = d >> 32u; - int s = __builtin_clz(d1); - uint64_t q = divq(n >> 1, (d << s) >> 32) >> (31 - s); - return n - (q - 1) * d < d ? q - 1 : q; -} - -static uint32_t unsigned_modulo64(uint64_t n, uint64_t d) -{ - return n - d * unsigned_divide64(n, d); -} - -static int64_t signed_divide64(int64_t n, int64_t d) -{ - uint64_t n_abs = n >= 0 ? (uint64_t)n : -(uint64_t)n; - uint64_t d_abs = d >= 0 ? (uint64_t)d : -(uint64_t)d; - uint64_t q_abs = unsigned_divide64(n_abs, d_abs); - return (n < 0) == (d < 0) ? (int64_t)q_abs : -(int64_t)q_abs; -} - -static int32_t signed_modulo64(int64_t n, int64_t d) -{ - return n - d * signed_divide64(n, d); -} - -int64_t __divdi3(int64_t n, int64_t d) -{ - return signed_divide64(n, d); -} - -int64_t __moddi3(int64_t n, int64_t d) -{ - return signed_modulo64(n, d); -} - -uint64_t __udivdi3(uint64_t n, uint64_t d) -{ - return unsigned_divide64(n, d); -} - -uint64_t __umoddi3(uint64_t n, uint64_t d) -{ - return unsigned_modulo64(n, d); -} - -uint64_t __udivmoddi4(uint64_t n, uint64_t d, uint64_t* r) -{ - uint64_t q = 0; - uint64_t qbit = 1; - - if (!d) - return 1 / ((unsigned)d); - - while ((int64_t)d >= 0) { - d <<= 1; - qbit <<= 1; - } - - while (qbit) { - if (d <= n) { - n -= d; - q += qbit; - } - d >>= 1; - qbit >>= 1; - } - - if (r) - *r = n; - - return q; -} - }