]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - lib_generic/string.c
lib_generic memcpy: copy one word at a time if possible
[karo-tx-uboot.git] / lib_generic / string.c
index 1e1e2e33fc32e1d0fb909350dec988da47ac74af..61a45dc94d26c9fc9c42682ee0c67221c26c4a2f 100644 (file)
 #include <linux/ctype.h>
 #include <malloc.h>
 
-#ifdef CONFIG_ARM
-#undef  __HAVE_ARCH_MEMCMP
-#undef  __HAVE_ARCH_MEMCPY
-#undef  __HAVE_ARCH_MEMMOVE
-#undef  __HAVE_ARCH_MEMSET
-#undef  __HAVE_ARCH_BCOPY
-#undef  __HAVE_ARCH_STRCAT
-#undef  __HAVE_ARCH_STRCHR
-#undef  __HAVE_ARCH_STRCMP
-#undef  __HAVE_ARCH_STRCPY
-#undef  __HAVE_ARCH_STRLEN
-#undef  __HAVE_ARCH_STRNCPY
-#else
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCPY
-#define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMSET
-#define __HAVE_ARCH_BCOPY
-#define __HAVE_ARCH_STRCAT
-#define __HAVE_ARCH_STRCMP
-#define __HAVE_ARCH_STRCPY
-#define __HAVE_ARCH_STRLEN
-#define __HAVE_ARCH_STRNCPY
-#endif
 
-#ifndef __HAVE_ARCH_STRNICMP
+#if 0 /* not used - was: #ifndef __HAVE_ARCH_STRNICMP */
 /**
  * strnicmp - Case insensitive, length-limited string comparison
  * @s1: One string
@@ -231,8 +207,8 @@ char * strrchr(const char * s, int c)
 {
        const char *p = s + strlen(s);
        do {
-           if (*p == (char)c)
-               return (char *)p;
+          if (*p == (char)c)
+              return (char *)p;
        } while (--p >= s);
        return NULL;
 }
@@ -287,7 +263,7 @@ char * strdup(const char *s)
 #ifndef __HAVE_ARCH_STRSPN
 /**
  * strspn - Calculate the length of the initial substring of @s which only
- *     contain letters in @accept
+ *     contain letters in @accept
  * @s: The string to be searched
  * @accept: The string to search for
  */
@@ -388,6 +364,34 @@ char * strsep(char **s, const char *ct)
 }
 #endif
 
+#ifndef __HAVE_ARCH_STRSWAB
+/**
+ * strswab - swap adjacent even and odd bytes in %NUL-terminated string
+ * s: address of the string
+ *
+ * returns the address of the swapped string or NULL on error. If
+ * string length is odd, last byte is untouched.
+ */
+char *strswab(const char *s)
+{
+       char *p, *q;
+
+       if ((NULL == s) || ('\0' == *s)) {
+               return (NULL);
+       }
+
+       for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
+               char  tmp;
+
+               tmp = *p;
+               *p  = *q;
+               *q  = tmp;
+       }
+
+       return (char *) s;
+}
+#endif
+
 #ifndef __HAVE_ARCH_MEMSET
 /**
  * memset - Fill a region of memory with the given value
@@ -442,12 +446,23 @@ char * bcopy(const char * src, char * dest, int count)
  * You should not use this function to access IO space, use memcpy_toio()
  * or memcpy_fromio() instead.
  */
-void * memcpy(void * dest,const void *src,size_t count)
+void * memcpy(void *dest, const void *src, size_t count)
 {
-       char *tmp = (char *) dest, *s = (char *) src;
-
+       unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
+       char *d8, *s8;
+
+       /* while all data is aligned (common case), copy a word at a time */
+       if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
+               while (count >= sizeof(*dl)) {
+                       *dl++ = *sl++;
+                       count -= sizeof(*dl);
+               }
+       }
+       /* copy the reset one byte at a time */
+       d8 = (char *)dl;
+       s8 = (char *)sl;
        while (count--)
-               *tmp++ = *s++;
+               *d8++ = *s8++;
 
        return dest;
 }
@@ -522,7 +537,7 @@ void * memscan(void * addr, int c, size_t size)
                p++;
                size--;
        }
-       return (void *) p;
+       return (void *) p;
 }
 #endif
 
@@ -564,7 +579,7 @@ void *memchr(const void *s, int c, size_t n)
 {
        const unsigned char *p = s;
        while (n-- != 0) {
-               if ((unsigned char)c == *p++) {
+               if ((unsigned char)c == *p++) {
                        return (void *)(p-1);
                }
        }