[U-Boot] [PATCH V2 1/3] memcpy: copy one word at a time if possible

Wolfgang Denk wd at denx.de
Thu Oct 8 22:44:31 CEST 2009


Dear Alessandro Rubini,

In message <45d5e3a574bf4844f46f50b2c88054a5b28f973b.1255000877.git.rubini@ unipv.it> you wrote:
> From: Alessandro Rubini <rubini at unipv.it>
> 
> Signed-off-by: Alessandro Rubini <rubini at unipv.it>
> Acked-by: Andrea Gallo <andrea.gallo at stericsson.com>
> ---
>  lib_generic/string.c |   17 +++++++++++++----
>  1 files changed, 13 insertions(+), 4 deletions(-)
> 
> diff --git a/lib_generic/string.c b/lib_generic/string.c
> index 181eda6..9911941 100644
> --- a/lib_generic/string.c
> +++ b/lib_generic/string.c
> @@ -446,12 +446,21 @@ 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;
> +	char *d8 = (char *)dest, *s8 = (char *)src;
> +	unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
>  
> +	/* if all data is aligned (common case), copy a word at a time */
> +	if ( (((int)dest | (int)src | count) & (sizeof(long) - 1)) == 0) {
> +		count /= sizeof(unsigned long);
> +		while (count--)
> +			*dl++ = *sl++;
> +		return dest;
> +	}
> +	/* else, use 1-byte copy */
>  	while (count--)
> -		*tmp++ = *s++;
> +		*d8++ = *s8++;
>  
>  	return dest;

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.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Computers make excellent and efficient servants, but I have  no  wish
to  serve under them. Captain, a starship also runs on loyalty to one
man. And nothing can replace it or him.
	-- Spock, "The Ultimate Computer", stardate 4729.4


More information about the U-Boot mailing list