lkml.org 
[lkml]   [2011]   [Nov]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectFast memcpy patch
Dear readers,
I noticed the Linux kernel still uses a byte-by-byte copy method for
memcpy. Since most memory allocations are aligned to the integer size
of a cpu it is often faster to copy by using the CPU's native word
size. The patch below does that. The code is already at work in many
16 and 32 bit embedded products. It should also work for 64 bit
platforms. So far I only tested 16 and 32 bit platforms.


--- lib/string.c.orig 2010-08-20 20:55:55.000000000 +0200
+++ lib/string.c 2011-11-23 12:29:02.000000000 +0100
@@ -565,14 +565,47 @@ EXPORT_SYMBOL(memset);
* 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 *dst, const void *src, size_t length)
{
- char *tmp = dest;
- const char *s = src;
+ void *p=dst;

- while (count--)
- *tmp++ = *s++;
- return dest;
+ //check alignment
+ if (( (int) dst & (sizeof(int) -1)) != ( (int) src &
(sizeof(int) -1) ))
+ {
+ //unaligned. This will never align so copy byte-by-byte
+ goto copyrest;
+ }
+
+ //seek aligment (lower bits should become 0). Because
+ //we already tested the lower bits are equal, we only need
+ //to test source or destination for matching alignment.
+ while ( (length !=0) && (((int) src & (sizeof(int)-1 ))!=0) )
+ {
+
+ *((char*) dst++)=*((char*)src++);
+ length--;
+ }
+
+ //copy words
+ while(length> (sizeof(int)-1) )
+ {
+ *((int*) dst)=*((int*)src);
+ dst+=sizeof(int);
+ src+=sizeof(int);
+ length-=sizeof(int);
+ }
+
+copyrest:
+
+ //now copy the rest byte-by-byte
+ while(length !=0)
+ {
+ *((char*) dst++)=*((char*) src++);
+ length--;
+ }
+
+ return p;
}
EXPORT_SYMBOL(memcpy);
#endif

Signed of by: Nico Coesel nico@nctdev.nl


o---------------------------------------------------------------o
| N C T Developments |
|Innovative embedded solutions |
o---------------------------------------------------------------o


\
 
 \ /
  Last update: 2011-11-23 12:35    [W:1.445 / U:0.028 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site