From: Chris Moore <moore@free.fr>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH V2 1/3] memcpy: copy one word at a time if possible
Date: Fri, 09 Oct 2009 06:42:01 +0200 [thread overview]
Message-ID: <4ACEBF19.4010902@free.fr> (raw)
In-Reply-To: <20091008204431.07341E8B31D@gemini.denx.de>
Wolfgang Denk a ?crit :
> I think we should change this if-else into a plain if, something like
> that:
>
> void * memcpy(void *dest, const void *src, size_t count)
> {
> char *tmp = (char *) dest, *s = (char *) src;
> char *d8 = (char *)dest, *s8 = (char *)src;
> unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
>
> /* while all data is aligned (common case), copy a word at a time */
> if ( (((int)dest | (int)src | count) & (sizeof(long) - 1)) == 0) {
> while (count) {
> *dl++ = *sl++;
> count -= sizeof(unsigned long);
> }
> }
> while (count--)
> *d8++ = *s8++;
>
> return dest;
> }
>
> This way we can have both - the "long" copy of a potential aligne
> dfirst part, and the byte copy of any trailing (or unaligned) part.
>
>
I agree wholeheartedly with the idea but shouldn't it be more like this
(untested) code :
void * memcpy(void *dest, const void *src, size_t count)
{
char *d8, *s8;
unsigned long *dl = dest, *sl = src;
/* while all data is aligned (common case), copy multiple bytes at a time */
if ( (((int)(long)dest | (int)(long)src) & (sizeof(*dl) - 1)) == 0) {
while (count >= sizeof(*dl)) {
*dl++ = *sl++;
count -= sizeof(*dl);
}
}
d8 = (char *)dl;
s8 = (char *)sl;
/* copy any remaining data byte by byte */
while (count--)
*d8++ = *s8++;
return dest;
}
Remarks :
1) My curious (int) (long) pointer casts are intended to avoid compiler
warnings while avoiding unnecessary calculations in long.
On some architectures long calculations are less efficient than int ones.
In fact I wonder whether, on such architectures, it might not also be
better to perform the copy with int size chunks.
2) Personally I prefer sizeof(*dl) to sizeof(unsigned long) as there is
less risk of error if the type of the chunks is changed.
3) In C (but not in C++) I think the casts from void * to unsigned long
* are unnecessary.
But as I said all this is completely untested :(
Cheers,
Chris
next prev parent reply other threads:[~2009-10-09 4:42 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-08 11:29 [U-Boot] [PATCH V2 0/3] make memcpy and memset faster Alessandro Rubini
2009-10-08 11:30 ` [U-Boot] [PATCH V2 1/3] memcpy: copy one word at a time if possible Alessandro Rubini
2009-10-08 15:12 ` Peter Tyser
2009-10-08 16:00 ` Alessandro Rubini
2009-10-08 16:30 ` Peter Tyser
2009-10-08 18:23 ` Alessandro Rubini
2009-10-08 19:09 ` Peter Tyser
2009-10-08 19:17 ` Alessandro Rubini
2009-10-08 20:40 ` Wolfgang Denk
2009-10-08 20:47 ` Wolfgang Denk
2009-10-08 19:14 ` Mike Frysinger
2009-10-08 20:44 ` Wolfgang Denk
2009-10-09 4:42 ` Chris Moore [this message]
2009-10-09 10:11 ` Mark Jackson
2009-10-09 10:26 ` Mike Frysinger
2009-10-11 7:06 ` Chris Moore
2009-10-09 11:12 ` Wolfgang Denk
2009-10-08 11:30 ` [U-Boot] [PATCH V2 2/3] memset: fill " Alessandro Rubini
2009-10-08 20:46 ` Wolfgang Denk
2009-10-08 11:30 ` [U-Boot] [PATCH V2 3/3] lcd: remove '#if 0' 32-bit scroll, now memcpy does it Alessandro Rubini
2009-11-22 22:34 ` Wolfgang Denk
2009-11-24 23:04 ` Anatolij Gustschin
2009-10-08 20:36 ` [U-Boot] [PATCH V2 0/3] make memcpy and memset faster Wolfgang Denk
2009-10-08 21:30 ` Mike Frysinger
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=4ACEBF19.4010902@free.fr \
--to=moore@free.fr \
--cc=u-boot@lists.denx.de \
/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