memcpy.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2013 Regents of the University of California
  4. */
  5. #include <linux/linkage.h>
  6. #include <asm/asm.h>
  7. /* void *memcpy(void *, const void *, size_t) */
  8. ENTRY(__memcpy)
  9. WEAK(memcpy)
  10. beq a0, a1, .copy_end
  11. /* Save for return value */
  12. mv t6, a0
  13. /*
  14. * Register allocation for code below:
  15. * a0 - start of uncopied dst
  16. * a1 - start of uncopied src
  17. * t0 - end of uncopied dst
  18. */
  19. add t0, a0, a2
  20. /*
  21. * Use bytewise copy if too small.
  22. *
  23. * This threshold must be at least 2*SZREG to ensure at least one
  24. * wordwise copy is performed. It is chosen to be 16 because it will
  25. * save at least 7 iterations of bytewise copy, which pays off the
  26. * fixed overhead.
  27. */
  28. li a3, 16
  29. bltu a2, a3, .Lbyte_copy_tail
  30. /*
  31. * Bytewise copy first to align a0 to word boundary.
  32. */
  33. addi a2, a0, SZREG-1
  34. andi a2, a2, ~(SZREG-1)
  35. beq a0, a2, 2f
  36. 1:
  37. lb a5, 0(a1)
  38. addi a1, a1, 1
  39. sb a5, 0(a0)
  40. addi a0, a0, 1
  41. bne a0, a2, 1b
  42. 2:
  43. /*
  44. * Now a0 is word-aligned. If a1 is also word aligned, we could perform
  45. * aligned word-wise copy. Otherwise we need to perform misaligned
  46. * word-wise copy.
  47. */
  48. andi a3, a1, SZREG-1
  49. bnez a3, .Lmisaligned_word_copy
  50. /* Unrolled wordwise copy */
  51. addi t0, t0, -(16*SZREG-1)
  52. bgeu a0, t0, 2f
  53. 1:
  54. REG_L a2, 0(a1)
  55. REG_L a3, SZREG(a1)
  56. REG_L a4, 2*SZREG(a1)
  57. REG_L a5, 3*SZREG(a1)
  58. REG_L a6, 4*SZREG(a1)
  59. REG_L a7, 5*SZREG(a1)
  60. REG_L t1, 6*SZREG(a1)
  61. REG_L t2, 7*SZREG(a1)
  62. REG_L t3, 8*SZREG(a1)
  63. REG_L t4, 9*SZREG(a1)
  64. REG_L t5, 10*SZREG(a1)
  65. REG_S a2, 0(a0)
  66. REG_S a3, SZREG(a0)
  67. REG_S a4, 2*SZREG(a0)
  68. REG_S a5, 3*SZREG(a0)
  69. REG_S a6, 4*SZREG(a0)
  70. REG_S a7, 5*SZREG(a0)
  71. REG_S t1, 6*SZREG(a0)
  72. REG_S t2, 7*SZREG(a0)
  73. REG_S t3, 8*SZREG(a0)
  74. REG_S t4, 9*SZREG(a0)
  75. REG_S t5, 10*SZREG(a0)
  76. REG_L a2, 11*SZREG(a1)
  77. REG_L a3, 12*SZREG(a1)
  78. REG_L a4, 13*SZREG(a1)
  79. REG_L a5, 14*SZREG(a1)
  80. REG_L a6, 15*SZREG(a1)
  81. addi a1, a1, 16*SZREG
  82. REG_S a2, 11*SZREG(a0)
  83. REG_S a3, 12*SZREG(a0)
  84. REG_S a4, 13*SZREG(a0)
  85. REG_S a5, 14*SZREG(a0)
  86. REG_S a6, 15*SZREG(a0)
  87. addi a0, a0, 16*SZREG
  88. bltu a0, t0, 1b
  89. 2:
  90. /* Post-loop increment by 16*SZREG-1 and pre-loop decrement by SZREG-1 */
  91. addi t0, t0, 15*SZREG
  92. /* Wordwise copy */
  93. bgeu a0, t0, 2f
  94. 1:
  95. REG_L a5, 0(a1)
  96. addi a1, a1, SZREG
  97. REG_S a5, 0(a0)
  98. addi a0, a0, SZREG
  99. bltu a0, t0, 1b
  100. 2:
  101. addi t0, t0, SZREG-1
  102. .Lbyte_copy_tail:
  103. /*
  104. * Bytewise copy anything left.
  105. */
  106. beq a0, t0, 2f
  107. 1:
  108. lb a5, 0(a1)
  109. addi a1, a1, 1
  110. sb a5, 0(a0)
  111. addi a0, a0, 1
  112. bne a0, t0, 1b
  113. 2:
  114. mv a0, t6
  115. .copy_end:
  116. ret
  117. .Lmisaligned_word_copy:
  118. /*
  119. * Misaligned word-wise copy.
  120. * For misaligned copy we still perform word-wise copy, but we need to
  121. * use the value fetched from the previous iteration and do some shifts.
  122. * This is safe because we wouldn't access more words than necessary.
  123. */
  124. /* Calculate shifts */
  125. slli t3, a3, 3
  126. sub t4, x0, t3 /* negate is okay as shift will only look at LSBs */
  127. /* Load the initial value and align a1 */
  128. andi a1, a1, ~(SZREG-1)
  129. REG_L a5, 0(a1)
  130. addi t0, t0, -(SZREG-1)
  131. /* At least one iteration will be executed here, no check */
  132. 1:
  133. srl a4, a5, t3
  134. REG_L a5, SZREG(a1)
  135. addi a1, a1, SZREG
  136. sll a2, a5, t4
  137. or a2, a2, a4
  138. REG_S a2, 0(a0)
  139. addi a0, a0, SZREG
  140. bltu a0, t0, 1b
  141. /* Update pointers to correct value */
  142. addi t0, t0, SZREG-1
  143. add a1, a1, a3
  144. j .Lbyte_copy_tail
  145. END(__memcpy)