summaryrefslogtreecommitdiffstats
path: root/usr/klibc/memmove.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/klibc/memmove.c')
-rw-r--r--usr/klibc/memmove.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/usr/klibc/memmove.c b/usr/klibc/memmove.c
new file mode 100644
index 0000000..a398cd8
--- /dev/null
+++ b/usr/klibc/memmove.c
@@ -0,0 +1,36 @@
+/*
+ * memmove.c
+ */
+
+#include <string.h>
+
+void *memmove(void *dst, const void *src, size_t n)
+{
+ const char *p = src;
+ char *q = dst;
+#if defined(__i386__) || defined(__x86_64__)
+ if (q < p) {
+ asm volatile("cld; rep; movsb"
+ : "+c" (n), "+S"(p), "+D"(q));
+ } else {
+ p += (n - 1);
+ q += (n - 1);
+ asm volatile("std; rep; movsb; cld"
+ : "+c" (n), "+S"(p), "+D"(q));
+ }
+#else
+ if (q < p) {
+ while (n--) {
+ *q++ = *p++;
+ }
+ } else {
+ p += n;
+ q += n;
+ while (n--) {
+ *--q = *--p;
+ }
+ }
+#endif
+
+ return dst;
+}