From: "Mihai Donțu" <mihai.dontu@gmail.com>
To: "N. Coesel" <nico@nctdev.nl>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Fast memcpy patch
Date: Wed, 23 Nov 2011 13:45:29 +0200 [thread overview]
Message-ID: <20111123134529.1fb13e13@mdontu-dell.dsd.ro> (raw)
In-Reply-To: <CPSMTPM-CMT101y332G000633f8@CPSMTPM-CMT101.kpnxchange.com>
On Wed, 23 Nov 2011 12:25:46 +0100 N. Coesel wrote:
> 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.
>
Could you run checkpatch.pl on this an fix all the warnings? Or, if you
wish, I could do it for you.
> --- 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
>
--
Mihai Donțu
next prev parent reply other threads:[~2011-11-23 11:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-23 11:25 N. Coesel
2011-11-23 11:45 ` Mihai Donțu [this message]
2011-11-23 12:07 ` N. Coesel
2011-11-23 12:06 ` richard -rw- weinberger
2011-11-23 12:07 ` Cong Wang
2011-11-23 12:10 ` Sasha Levin
2011-11-23 12:51 ` N. Coesel
2011-11-23 13:04 ` Sasha Levin
2011-11-23 20:38 ` N. Coesel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20111123134529.1fb13e13@mdontu-dell.dsd.ro \
--to=mihai.dontu@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=nico@nctdev.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®