]> Joshua Wise's Git repositories - netwatch.git/commitdiff
An optimized memcpy() is fine too.
authorJoshua Wise <joshua@nyus.joshuawise.com>
Sat, 6 Dec 2008 11:09:23 +0000 (06:09 -0500)
committerJoshua Wise <joshua@nyus.joshuawise.com>
Sat, 6 Dec 2008 11:09:23 +0000 (06:09 -0500)
lib/minilib.c

index 664a0b42a7abe24229793a7490fd5b5cc934a923..56090ed9389f9a8e1d0657649ca73438fc70203d 100644 (file)
@@ -1,12 +1,59 @@
 #include "console.h"
 #include <minilib.h>
+#include <output.h>
 
-void memcpy(void *dest, const void *src, int bytes)
+/* We have both _memcpy and memcpy, because gcc might be able to do better in lwip.
+ * For small things, gcc inlines its memcpy, but for large things, we call out
+ * to this memcpy.
+ */
+inline void _memcpy(void *dest, const void *src, int bytes)
 {
+       /* I hate everyone */
+       /* Since we otherwise compile with -O0, we might as well manually speed this up a bit. */
+       
        char *cdest = dest;
        const char *csrc = src;
-       while (bytes--)
+       int *idest;
+       const int *isrc;
+       int nwords;
+       
+       /* Align to src (picked arbitrarily; might as well align to something) */
+       while (bytes && ((unsigned int)csrc & 3))
+       {
+               *(cdest++) = *(csrc++);
+               bytes--;
+       }
+       
+       idest = (int *)cdest;
+       isrc = (const int *)csrc;
+       
+       nwords = bytes / 4;
+       bytes -= bytes & ~3;
+       if (nwords != 0)
+               switch(nwords % 8)      /* They see me Duff'sin'.  They hatin'. */
+                       do {
+               case  0:        nwords--; *(idest++) = *(isrc++);
+               case  7:        nwords--; *(idest++) = *(isrc++);
+               case  6:        nwords--; *(idest++) = *(isrc++);
+               case  5:        nwords--; *(idest++) = *(isrc++);
+               case  4:        nwords--; *(idest++) = *(isrc++);
+               case  3:        nwords--; *(idest++) = *(isrc++);
+               case  2:        nwords--; *(idest++) = *(isrc++);
+               case  1:        nwords--; *(idest++) = *(isrc++);
+                       } while (nwords);
+       
+       cdest = (char *)idest;
+       csrc = (const char *)isrc;
+       while (bytes)   /* Clean up the remainder */
+       {
                *(cdest++) = *(csrc++);
+               bytes--;
+       }
+}
+
+void memcpy(void *dest, const void *src, int bytes)
+{
+       _memcpy(dest, src, bytes);
 }
 
 void memset(void *dest, int data, int bytes)
@@ -28,8 +75,7 @@ void memmove(void *dest, void *src, int bytes)
                while (bytes--)
                        *(--cdest) = *(--csrc);
        } else
-               while (bytes--)
-                       *(cdest++) = *(csrc++);
+               memcpy(dest, src, bytes);
 }
 
 int memcmp (const char *a2, const char *a1, int bytes) {
This page took 0.024939 seconds and 4 git commands to generate.