summaryrefslogtreecommitdiff
path: root/arch/x86/machine
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2014-05-23 00:50:11 +0200
committerRichard Braun <rbraun@sceen.net>2014-05-23 00:54:02 +0200
commit532f247e4ecc6a101ae2b5c0acb83fa02380d779 (patch)
treed113fec9d8b4c1f96675d7e41ab9bafd51093345 /arch/x86/machine
parent8bba3406df3eeeb3089cdb1a73fed37a3ce32943 (diff)
x86/string: add some optimized string functions
Diffstat (limited to 'arch/x86/machine')
-rw-r--r--arch/x86/machine/param.h8
-rw-r--r--arch/x86/machine/string.c87
2 files changed, 95 insertions, 0 deletions
diff --git a/arch/x86/machine/param.h b/arch/x86/machine/param.h
index 80c8de9..ce373c8 100644
--- a/arch/x86/machine/param.h
+++ b/arch/x86/machine/param.h
@@ -50,6 +50,14 @@
#define __read_mostly __section(".data.read_mostly")
/*
+ * Provide architecture-specific string functions.
+ */
+#define ARCH_STRING_MEMCPY
+#define ARCH_STRING_MEMMOVE
+#define ARCH_STRING_MEMSET
+#define ARCH_STRING_MEMCMP
+
+/*
* System timer frequency.
*
* The selected value of 200 translates to a period of 5ms, small enough to
diff --git a/arch/x86/machine/string.c b/arch/x86/machine/string.c
new file mode 100644
index 0000000..2ea8392
--- /dev/null
+++ b/arch/x86/machine/string.c
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2014 Richard Braun.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <kern/param.h>
+#include <kern/stddef.h>
+#include <kern/string.h>
+
+#ifdef ARCH_STRING_MEMCPY
+void *
+memcpy(void *dest, const void *src, size_t n)
+{
+ void *orig_dest;
+
+ orig_dest = dest;
+ asm volatile("cld; rep movsb" : "+D" (dest), "+S" (src), "+c" (n));
+ return orig_dest;
+}
+#endif /* ARCH_STRING_MEMCPY */
+
+#ifdef ARCH_STRING_MEMMOVE
+void *
+memmove(void *dest, const void *src, size_t n)
+{
+ void *orig_dest;
+
+ orig_dest = dest;
+
+ if (dest <= src)
+ asm volatile("cld; rep movsb" : "+D" (dest), "+S" (src), "+c" (n));
+ else {
+ dest += n - 1;
+ src += n - 1;
+ asm volatile("std; rep movsb" : "+D" (dest), "+S" (src), "+c" (n));
+ }
+
+ return orig_dest;
+}
+#endif /* ARCH_STRING_MEMMOVE */
+
+#ifdef ARCH_STRING_MEMSET
+void *
+memset(void *s, int c, size_t n)
+{
+ void *orig_s;
+
+ orig_s = s;
+ asm volatile("cld; rep stosb" : "+D" (s), "+c" (n) : "a" (c));
+ return orig_s;
+}
+#endif /* ARCH_STRING_MEMSET */
+
+#ifdef ARCH_STRING_MEMCMP
+int
+memcmp(const void *s1, const void *s2, size_t n)
+{
+ unsigned char zf, c1, c2;
+
+ if (n == 0)
+ return 0;
+
+ asm volatile("cld\n"
+ "repe cmpsb\n"
+ "sete %0\n"
+ : "=g" (zf), "+D" (s1), "+S" (s2), "+c" (n));
+
+ if (zf)
+ return 0;
+
+ c1 = *(((const unsigned char *)s1) - 1);
+ c2 = *(((const unsigned char *)s2) - 1);
+ return (int)c1 - (int)c2;
+}
+#endif /* ARCH_STRING_MEMCMP */