Files
serenity/Kernel/Library/MiniStdLib.cpp
T
Sönke Holz cef72c74c3 Kernel: Use word-sized writes in the generic memset implementation
This decreases the boot time on my x86-64 host system from 15 s to 13 s
for AArch64 QEMU TCG and 33 s to 30 s for RISC-V QEMU TCG!

I additionally measured the performance of this new implementation
with this simple benchmark:
https://gist.github.com/spholz/b06ea737b435ecc181069cf0d911faa4

Based on to this benchmark, an unroll level 8 seems like a good choice
for all tested systems.

Here are the speedups for n=0x10000:
- Raspberry Pi 5:        7.6 ( 81984 ns -> 10748 ns)
- Raspberry Pi 4:        3.3 (131197 ns -> 39704 ns)
- StarFive VisionFive 2: 5.5 (279107 ns -> 50650 ns)
- AArch64 QEMU TCG:      6.8 (374287 ns -> 54847 ns)
- RISC-V QEMU TCG:       6.7 (354195 ns -> 52615 ns)
- x86-64 QEMU KVM:       3.8 ( 32443 ns ->  8542 ns)
2025-12-01 23:02:53 +01:00

174 lines
3.9 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Library/StdLib.h>
extern "C" {
void* memcpy(void* dest_ptr, void const* src_ptr, size_t n)
{
#if ARCH(X86_64)
size_t dest = (size_t)dest_ptr;
size_t src = (size_t)src_ptr;
// FIXME: Support starting at an unaligned address.
if (!(dest & 0x3) && !(src & 0x3) && n >= 12) {
size_t size_ts = n / sizeof(size_t);
n -= size_ts * sizeof(size_t);
asm volatile(
"rep movsq\n"
: "+S"(src), "+D"(dest), "+c"(size_ts)::"memory");
if (n == 0)
return dest_ptr;
}
asm volatile(
"rep movsb\n"
: "+S"(src), "+D"(dest), "+c"(n)::"memory");
#else
u8* pd = (u8*)dest_ptr;
u8 const* ps = (u8 const*)src_ptr;
for (; n--;)
*pd++ = *ps++;
#endif
return dest_ptr;
}
void* memmove(void* dest, void const* src, size_t n)
{
if (dest < src)
return memcpy(dest, src, n);
u8* pd = (u8*)dest;
u8 const* ps = (u8 const*)src;
for (pd += n, ps += n; n--;)
*--pd = *--ps;
return dest;
}
void* memset(void* dest_ptr, int c, size_t n)
{
#if ARCH(X86_64)
size_t dest = (size_t)dest_ptr;
// FIXME: Support starting at an unaligned address.
if (!(dest & 0x3) && n >= 12) {
size_t size_ts = n / sizeof(size_t);
size_t expanded_c = explode_byte((u8)c);
n -= size_ts * sizeof(size_t);
asm volatile(
"rep stosq\n"
: "+D"(dest), "+c"(size_ts)
: "a"(expanded_c)
: "memory");
if (n == 0)
return dest_ptr;
}
asm volatile(
"rep stosb\n"
: "+D"(dest), "+c"(n)
: "a"(c)
: "memory");
#else
auto* dest = static_cast<u8*>(dest_ptr);
auto is_word_aligned = [](auto ptr) {
return reinterpret_cast<FlatPtr>(ptr) % sizeof(FlatPtr) == 0;
};
// Set bytes until destination is word-aligned.
while (n > 0 && !is_word_aligned(dest)) {
*dest++ = static_cast<u8>(c);
n--;
}
// Set in word-sized chunks.
FlatPtr exploded = explode_byte(c);
auto* dest_word = reinterpret_cast<FlatPtr*>(dest);
# pragma GCC unroll 8
while (n >= sizeof(FlatPtr)) {
*dest_word++ = exploded;
n -= sizeof(FlatPtr);
}
dest = reinterpret_cast<u8*>(dest_word);
// Set remaining tail bytes.
while (n > 0) {
*dest++ = static_cast<u8>(c);
n--;
}
#endif
return dest_ptr;
}
size_t strlen(char const* str)
{
size_t len = 0;
while (*(str++))
++len;
return len;
}
size_t strnlen(char const* str, size_t maxlen)
{
size_t len = 0;
for (; len < maxlen && *str; str++)
len++;
return len;
}
int strcmp(char const* s1, char const* s2)
{
for (; *s1 == *s2; ++s1, ++s2) {
if (*s1 == 0)
return 0;
}
return *(u8 const*)s1 < *(u8 const*)s2 ? -1 : 1;
}
int memcmp(void const* v1, void const* v2, size_t n)
{
auto const* s1 = (u8 const*)v1;
auto const* s2 = (u8 const*)v2;
while (n-- > 0) {
if (*s1++ != *s2++)
return s1[-1] < s2[-1] ? -1 : 1;
}
return 0;
}
int strncmp(char const* s1, char const* s2, size_t n)
{
if (!n)
return 0;
do {
if (*s1 != *s2++)
return *(unsigned char const*)s1 - *(unsigned char const*)--s2;
if (*s1++ == 0)
break;
} while (--n);
return 0;
}
char* strstr(char const* haystack, char const* needle)
{
char nch;
char hch;
if ((nch = *needle++) != 0) {
size_t len = strlen(needle);
do {
do {
if ((hch = *haystack++) == 0)
return nullptr;
} while (hch != nch);
} while (strncmp(haystack, needle, len) != 0);
--haystack;
}
return const_cast<char*>(haystack);
}
}