18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// Copyright (C) 2005-2017 Andes Technology Corporation
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/linkage.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/*
78c2ecf20Sopenharmony_ci  void *memmove(void *dst, const void *src, int n);
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci  dst: $r0
108c2ecf20Sopenharmony_ci  src: $r1
118c2ecf20Sopenharmony_ci  n  : $r2
128c2ecf20Sopenharmony_ci  ret: $r0 - pointer to the memory area dst.
138c2ecf20Sopenharmony_ci*/
148c2ecf20Sopenharmony_ci	.text
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciENTRY(memmove)
178c2ecf20Sopenharmony_ci	move	$r5, $r0		! Set return value = det
188c2ecf20Sopenharmony_ci	beq	$r0, $r1, exit_memcpy	! Exit when det = src
198c2ecf20Sopenharmony_ci	beqz	$r2, exit_memcpy	! Exit when n = 0
208c2ecf20Sopenharmony_ci	pushm	$t0, $t1		! Save reg
218c2ecf20Sopenharmony_ci	srli	$p1, $r2, #2		! $p1 is how many words to copy
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	! Avoid data lost when memory overlap
248c2ecf20Sopenharmony_ci	! Copy data reversely when src < dst
258c2ecf20Sopenharmony_ci	slt	$p0, $r0, $r1		! check if $r0 < $r1
268c2ecf20Sopenharmony_ci	beqz	$p0, do_reverse		! branch if dst > src
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	! No reverse, dst < src
298c2ecf20Sopenharmony_ci	andi	$r2, $r2, #3		! How many bytes are less than a word
308c2ecf20Sopenharmony_ci	li	$t0, #1			! Determining copy direction in byte_cpy
318c2ecf20Sopenharmony_ci	beqz	$p1, byte_cpy		! When n is less than a word
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciword_cpy:
348c2ecf20Sopenharmony_ci	lmw.bim	$p0, [$r1], $p0		! Read a word from src
358c2ecf20Sopenharmony_ci	addi	$p1, $p1, #-1		! How many words left to copy
368c2ecf20Sopenharmony_ci	smw.bim	$p0, [$r0], $p0		! Copy the word to det
378c2ecf20Sopenharmony_ci	bnez	$p1, word_cpy		! If remained words > 0
388c2ecf20Sopenharmony_ci	beqz	$r2, end_memcpy		! No left bytes to copy
398c2ecf20Sopenharmony_ci	b	byte_cpy
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cido_reverse:
428c2ecf20Sopenharmony_ci	add	$r0, $r0, $r2		! Start with the end of $r0
438c2ecf20Sopenharmony_ci	add	$r1, $r1, $r2		! Start with the end of $r1
448c2ecf20Sopenharmony_ci	andi	$r2, $r2, #3		! How many bytes are less than a word
458c2ecf20Sopenharmony_ci	li	$t0, #-1		! Determining copy direction in byte_cpy
468c2ecf20Sopenharmony_ci	beqz	$p1, reverse_byte_cpy	! When n is less than a word
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cireverse_word_cpy:
498c2ecf20Sopenharmony_ci	lmw.adm	$p0, [$r1], $p0		! Read a word from src
508c2ecf20Sopenharmony_ci	addi	$p1, $p1, #-1		! How many words left to copy
518c2ecf20Sopenharmony_ci	smw.adm	$p0, [$r0], $p0		! Copy the word to det
528c2ecf20Sopenharmony_ci	bnez	$p1, reverse_word_cpy	! If remained words > 0
538c2ecf20Sopenharmony_ci	beqz	$r2, end_memcpy		! No left bytes to copy
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cireverse_byte_cpy:
568c2ecf20Sopenharmony_ci	addi	$r0, $r0, #-1
578c2ecf20Sopenharmony_ci	addi	$r1, $r1, #-1
588c2ecf20Sopenharmony_cibyte_cpy:				! Less than 4 bytes to copy now
598c2ecf20Sopenharmony_ci	lb.bi	$p0, [$r1], $t0		! Read a byte from src
608c2ecf20Sopenharmony_ci	addi	$r2, $r2, #-1		! How many bytes left to copy
618c2ecf20Sopenharmony_ci	sb.bi	$p0, [$r0], $t0		! copy the byte to det
628c2ecf20Sopenharmony_ci	bnez	$r2, byte_cpy		! If remained bytes > 0
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ciend_memcpy:
658c2ecf20Sopenharmony_ci	popm	$t0, $t1
668c2ecf20Sopenharmony_ciexit_memcpy:
678c2ecf20Sopenharmony_ci	move	$r0, $r5
688c2ecf20Sopenharmony_ci	ret
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciENDPROC(memmove)
71