summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-27 16:32:13 +0200
committerAnton Kling <anton@kling.gg>2023-10-30 21:49:48 +0100
commit8eede5fd642db2c56a1f5185973815fbd2b0afa4 (patch)
tree0a75f0debcedd3657cf0509e7973cad70fbc2262
parent5026f823fa2708404302aa59d03401635a435c0b (diff)
LibC: Optimize memcpy function
Instead of copying byte by byte it now copies in 64, 32, 16, 8 bit chunks.
-rw-r--r--libc/string/memcpy.c13
-rw-r--r--userland/libc/string/memcpy.c10
2 files changed, 21 insertions, 2 deletions
diff --git a/libc/string/memcpy.c b/libc/string/memcpy.c
index 70a60c1..5c04407 100644
--- a/libc/string/memcpy.c
+++ b/libc/string/memcpy.c
@@ -1,10 +1,19 @@
#include "../include/string.h"
-void *__attribute__((optimize("O0")))
+void *
memcpy(void *dest, const void *src, uint32_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
- // for(;n--;) *d++ = *s++;
+
+ for (; n >= 8; n -= 8, d += 8, s += 8)
+ *(uint64_t *)d = *(uint64_t *)s;
+
+ for (; n >= 4; n -= 4, d += 4, s += 4)
+ *(uint32_t *)d = *(uint32_t *)s;
+
+ for (; n >= 2; n -= 2, d += 2, s += 2)
+ *(uint16_t *)d = *(uint16_t *)s;
+
for (; n; n--)
*d++ = *s++;
return dest;
diff --git a/userland/libc/string/memcpy.c b/userland/libc/string/memcpy.c
index e19dec9..7c86b3a 100644
--- a/userland/libc/string/memcpy.c
+++ b/userland/libc/string/memcpy.c
@@ -3,6 +3,16 @@
void *memcpy(void *dest, const void *src, uint32_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
+
+ for (; n >= 8; n -= 8, d += 8, s += 8)
+ *(uint64_t *)d = *(uint64_t *)s;
+
+ for (; n >= 4; n -= 4, d += 4, s += 4)
+ *(uint32_t *)d = *(uint32_t *)s;
+
+ for (; n >= 2; n -= 2, d += 2, s += 2)
+ *(uint16_t *)d = *(uint16_t *)s;
+
for (; n; n--)
*d++ = *s++;
return dest;