Make the kernel's memcpy() and memset() go fast with dword copies.

Also I learned that the ABI allows us to assume DF=0 on function entry.
This commit is contained in:
Andreas Kling 2019-01-12 18:13:21 +01:00
parent 20156f9ec5
commit 24b2cadb82
Notes: sideshowbarker 2024-07-19 16:03:49 +09:00

View file

@ -5,12 +5,27 @@
extern "C" {
void memcpy(void *dest, const void *src, dword n)
void memcpy(void *dest_ptr, const void *src_ptr, dword n)
{
byte* bdest = (byte*)dest;
const byte* bsrc = (const byte*)src;
for (; n; --n)
*(bdest++) = *(bsrc++);
dword dest = (dword)dest_ptr;
dword src = (dword)src_ptr;
if (n >= 12) {
size_t dwords = n / sizeof(dword);
asm volatile(
"rep movsl\n"
: "=S"(src), "=D"(dest)
: "S"(src), "D"(dest), "c"(dwords)
: "memory"
);
n -= dwords * sizeof(dword);
if (n == 0)
return;
}
asm volatile(
"rep movsb\n"
:: "S"(src), "D"(dest), "c"(n)
: "memory"
);
}
void strcpy(char* dest, const char *src)
@ -21,12 +36,26 @@ void strcpy(char* dest, const char *src)
void* memset(void* dest_ptr, byte c, dword n)
{
dword dest = (dword)dest_ptr;
if (n >= 12) {
size_t dwords = n / sizeof(dword);
dword expanded_c = c;
expanded_c <<= 8;
expanded_c <<= 16;
asm volatile(
"rep stosl\n"
: "=D"(dest)
: "D"(dest), "c"(dwords), "a"(expanded_c)
: "memory"
);
n -= dwords * sizeof(dword);
if (n == 0)
return dest_ptr;
}
asm volatile(
"cld\n"
"rep stosb\n"
: "=D" (dest), "=c" (n)
: "0" (dest), "1" (n), "a" (c)
: "cc", "memory"
: "memory"
);
return dest_ptr;
}