memmove.S 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #include <linux/linkage.h>
  4. /*
  5. void *memmove(void *dst, const void *src, int n);
  6. dst: $r0
  7. src: $r1
  8. n : $r2
  9. ret: $r0 - pointer to the memory area dst.
  10. */
  11. .text
  12. ENTRY(memmove)
  13. move $r5, $r0 ! Set return value = det
  14. beq $r0, $r1, exit_memcpy ! Exit when det = src
  15. beqz $r2, exit_memcpy ! Exit when n = 0
  16. pushm $t0, $t1 ! Save reg
  17. srli $p1, $r2, #2 ! $p1 is how many words to copy
  18. ! Avoid data lost when memory overlap
  19. ! Copy data reversely when src < dst
  20. slt $p0, $r0, $r1 ! check if $r0 < $r1
  21. beqz $p0, do_reverse ! branch if dst > src
  22. ! No reverse, dst < src
  23. andi $r2, $r2, #3 ! How many bytes are less than a word
  24. li $t0, #1 ! Determining copy direction in byte_cpy
  25. beqz $p1, byte_cpy ! When n is less than a word
  26. word_cpy:
  27. lmw.bim $p0, [$r1], $p0 ! Read a word from src
  28. addi $p1, $p1, #-1 ! How many words left to copy
  29. smw.bim $p0, [$r0], $p0 ! Copy the word to det
  30. bnez $p1, word_cpy ! If remained words > 0
  31. beqz $r2, end_memcpy ! No left bytes to copy
  32. b byte_cpy
  33. do_reverse:
  34. add $r0, $r0, $r2 ! Start with the end of $r0
  35. add $r1, $r1, $r2 ! Start with the end of $r1
  36. andi $r2, $r2, #3 ! How many bytes are less than a word
  37. li $t0, #-1 ! Determining copy direction in byte_cpy
  38. beqz $p1, reverse_byte_cpy ! When n is less than a word
  39. reverse_word_cpy:
  40. lmw.adm $p0, [$r1], $p0 ! Read a word from src
  41. addi $p1, $p1, #-1 ! How many words left to copy
  42. smw.adm $p0, [$r0], $p0 ! Copy the word to det
  43. bnez $p1, reverse_word_cpy ! If remained words > 0
  44. beqz $r2, end_memcpy ! No left bytes to copy
  45. reverse_byte_cpy:
  46. addi $r0, $r0, #-1
  47. addi $r1, $r1, #-1
  48. byte_cpy: ! Less than 4 bytes to copy now
  49. lb.bi $p0, [$r1], $t0 ! Read a byte from src
  50. addi $r2, $r2, #-1 ! How many bytes left to copy
  51. sb.bi $p0, [$r0], $t0 ! copy the byte to det
  52. bnez $r2, byte_cpy ! If remained bytes > 0
  53. end_memcpy:
  54. popm $t0, $t1
  55. exit_memcpy:
  56. move $r0, $r5
  57. ret
  58. ENDPROC(memmove)