[PATCH v3 2/3] riscv: optimized memmove

Nick Kossifidis mick at ics.forth.gr
Mon Jun 21 17:46:16 PDT 2021


Στις 2021-06-17 18:27, Matteo Croce έγραψε:
> +
> +/*
> + * Simply check if the buffer overlaps an call memcpy() in case,
> + * otherwise do a simple one byte at time backward copy.
> + */
> +void *__memmove(void *dest, const void *src, size_t count)
> +{
> +	if (dest < src || src + count <= dest)
> +		return memcpy(dest, src, count);
> +
> +	if (dest > src) {
> +		const char *s = src + count;
> +		char *tmp = dest + count;
> +
> +		while (count--)
> +			*--tmp = *--s;
> +	}
> +	return dest;
> +}
> +EXPORT_SYMBOL(__memmove);
> +

Copying backwards byte-per-byte is suboptimal, I understand this is not 
a very common scenario but you could at least check if they are both 
word-aligned e.g. (((src + len) | (dst + len)) & mask), or missaligned 
by the same offset e.g. (((src + len) ^ (dst + len)) & mask) and still 
end up doing word-by-word copying. Ideally it would be great if you 
re-used the same technique you used for forwards copying on your memcpy.

> +void *memmove(void *dest, const void *src, size_t count) __weak
> __alias(__memmove);
> +EXPORT_SYMBOL(memmove);

As I mentioned on your memcpy patch, if you implement memmove, you can 
just alias memcpy to memmove and we won't have to worry about memcpy 
being used on overlapping regions.



More information about the linux-riscv mailing list