	.section ".text.hot.memcpy","ax"

	.balign 4
	.globl	mempcpy
mempcpy:
	mv	a3, a0
	add	a0, a0, a2
	j	__memxcpy
	.type mempcpy, @function
	.size mempcpy, . - mempcpy

	.balign 4
	.globl	memcpy
memcpy:
	mv	a3, a0
	.type memcpy, @function
	.size memcpy, . - memcpy

	.globl	__memxcpy
__memxcpy:
	or	a5, a3, a1
	or	a5, a5, a2
	andi	a5, a5, 3
	bnez	a5, __memxcpy_bytewise

	.type	__memxcpy, @function
	.size	__memxcpy, . - __memxcpy

	.globl __memxcpy_aligned
__memxcpy_aligned:
	add	a4, a3, a2

	andi	a2, a2, 7*4
	.option	norelax
	sub	a5, zero, a2
	jr	a5, %lo(.L_case0)
	.option	relax

	.balign	4
.L_aligned_loop:
	.option norvc
	li	a2, 32
	.option rvc

	c.lw	a5, 28(a1)
	c.sw	a5, 28(a3)
.L_case7:
	c.lw	a5, 24(a1)
	c.sw	a5, 24(a3)
.L_case6:
	c.lw	a5, 20(a1)
	c.sw	a5, 20(a3)
.L_case5:
	c.lw	a5, 16(a1)
	c.sw	a5, 16(a3)
.L_case4:
	c.lw	a5, 12(a1)
	c.sw	a5, 12(a3)
.L_case3:
	c.lw	a5,  8(a1)
	c.sw	a5,  8(a3)
.L_case2:
	c.lw	a5,  4(a1)
	c.sw	a5,  4(a3)
.L_case1:
	c.lw	a5,  0(a1)
	c.sw	a5,  0(a3)
.L_case0:
	add	a1, a1, a2
	add	a3, a3, a2
	bltu	a3, a4, .L_aligned_loop

.L_empty:
	ret

	.type	__memxcpy_aligned, @function
	.size	__memxcpy_aligned, . - __memxcpy_aligned

	//
	// These can be used by I/O devices that need bytewise access
	//
	.balign 4
memcpy_bytewise:
	.option	norvc
	mv	a0, a3
	.option	rvc

	.type	memcpy_bytewise, @function
	.size	memcpy_bytewise, . - memcpy_bytewise

	.balign 4
__memxcpy_bytewise:
	.option norvc
	add	a4, a3, a2
	.option	rvc

	.balign 4
.L_bytewise_loop:
	lbu	a5, 0(a1)
	sb	a5, 0(a3)
	addi	a1, a1, 1
	addi	a3, a3, 1
	bltu	a3, a4, .L_bytewise_loop

	ret

	.type	__memxcpy_bytewise, @function
	.size	__memxcpy_bytewise, . - __memxcpy_bytewise
