[PATCH 2/2] riscv: Add and use alignment-ignorant memcpy

Richard Henderson richard.henderson@linaro.org
Mon Feb 6 22:05:59 GMT 2023


On 2/6/23 09:48, Evan Green wrote:
> +	/* Remainder is smaller than a page, compute native word count */
> +	beqz a2, 6f
> +	andi a5, a2, ~(SZREG-1)
> +	andi a2, a2, (SZREG-1)
> +	add a3, a1, a5
> +	/* Jump directly to byte copy if no words. */
> +	beqz a5, 4f
> +
> +3:
> +	/* Use single native register copy */
> +	REG_L a4, 0(a1)
> +	addi a1, a1, SZREG
> +	REG_S a4, 0(t6)
> +	addi t6, t6, SZREG
> +	bltu a1, a3, 3b
> +
> +	/* Jump directly out if no more bytes */
> +	beqz a2, 6f
> +
> +4:
> +	/* Copy the last few individual bytes */
> +	add a3, a1, a2
> +5:
> +	lb a4, 0(a1)
> +	addi a1, a1, 1
> +	sb a4, 0(t6)
> +	addi t6, t6, 1
> +	bltu a1, a3, 5b
> +6:
> +	ret

If you know there are at least SZREG bytes in the range, you can avoid the byte loop by 
copying the last word unaligned.  That may copy some bytes twice, but that's ok too. 
Similarly, you can redundantly copy a few bytes at the beginning to align the destination 
(there's usually some cost for unaligned stores, even if it's generally "fast").

For memcpy < SZREG, you don't need a loop; just test the final few bits of len.
Have a look at the tricks in sysdeps/x86_64/multiarch/memmove-ssse3.S for ideas.


r~


More information about the Libc-alpha mailing list