Replace memcpy() with a routine written in assembly which minimizes stores
authorH. Peter Anvin <hpa@zytor.com>
Thu, 14 Sep 2006 19:01:48 +0000 (12:01 -0700)
committerH. Peter Anvin <hpa@zytor.com>
Thu, 14 Sep 2006 19:01:48 +0000 (12:01 -0700)
com32/lib/memcpy.S [new file with mode: 0644]
com32/lib/memcpy.c [deleted file]

diff --git a/com32/lib/memcpy.S b/com32/lib/memcpy.S
new file mode 100644 (file)
index 0000000..ed8e20e
--- /dev/null
@@ -0,0 +1,36 @@
+#
+# memcpy.S
+#
+
+       .text
+       .globl  memcpy
+       .type   memcpy, @function
+memcpy:
+       pushl   %esi
+       pushl   %edi
+#ifdef REGPARM
+       movl    %edx, %esi
+#else
+       movl    12(%esp), %eax
+       movl    16(%esp), %esi
+       movl    20(%esp), %ecx
+#endif
+       movl    %eax, %edi
+       movl    %ecx, %edx
+
+       shrl    $2, %ecx
+       cld
+       rep ; movsl
+
+       jnc     1f              # The shrl had carry out if odd word count
+       movsw
+1:
+       testb   $1, %dl
+       jz      2f
+       movsb
+2:
+       popl    %edi
+       popl    %esi
+       ret
+
+       .size   memcpy, .-memcpy
diff --git a/com32/lib/memcpy.c b/com32/lib/memcpy.c
deleted file mode 100644 (file)
index b9171c3..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * memcpy.c
- */
-
-#include <string.h>
-#include <stdint.h>
-
-void *memcpy(void *dst, const void *src, size_t n)
-{
-  const char *p = src;
-  char *q = dst;
-#if defined(__i386__)
-  size_t nl = n >> 2;
-  asm volatile("cld ; rep ; movsl ; movl %3,%0 ; rep ; movsb"
-              : "+c" (nl), "+S" (p), "+D" (q)
-              : "r" (n & 3));
-#elif defined(__x86_64__)
-  size_t nq = n >> 3;
-  asm volatile("cld ; rep ; movsq ; movl %3,%%ecx ; rep ; movsb"
-              : "+c" (nq), "+S" (p), "+D" (q)
-              : "r" ((uint32_t)(n & 7)));
-#else
-  while ( n-- ) {
-    *q++ = *p++;
-  }
-#endif
-
-  return dst;
-}