libc: core: implement memcmp and memmove

This commit is contained in:
2026-04-01 18:47:59 +01:00
parent 0d16c300e7
commit 36a486cca6
2 changed files with 33 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
#include <stddef.h>
int memcmp(const void *vl, const void *vr, size_t n)
{
const unsigned char *l = vl, *r = vr;
for (; n && *l == *r; n--, l++, r++)
;
return n ? *l - *r : 0;
}
+24
View File
@@ -0,0 +1,24 @@
#include <stdint.h>
#include <string.h>
static void *memcpy_r(void *dest, const void *src, size_t sz)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (size_t i = 0; i < sz; i++) {
size_t b = sz - i - 1;
d[b] = s[b];
}
return dest;
}
void *memmove(void *dest, const void *src, size_t n)
{
if (dest < src) {
return memcpy(dest, src, n);
} else {
return memcpy_r(dest, src, n);
}
}