string: better memset and memcpy

This commit is contained in:
Matheus
2024-08-21 23:23:58 -03:00
parent e0813e0701
commit fb24745071
+8 -2
View File
@@ -3,20 +3,26 @@
#include <stdint.h>
void *memcpy(void *d, void *s, size_t c) {
#ifdef __x86_64__
asm volatile ("rep movsb" : : "S"(s), "D"(d), "c"(c) : "memory");
#else
uint8_t *sc = (uint8_t *)s;
uint8_t *dc = (uint8_t *)d;
while (c--)
*dc++ = *sc++;
#endif
return d;
}
void *memset(void *ptr, unsigned long value, size_t num) {
#ifdef __x86_64__
asm volatile ("rep stosb" : : "D"(ptr), "a"(value), "c"(num) : "memory");
#else
char *p = (char *)ptr;
while (num--)
*p++ = value;
#endif
return ptr;
}