memcpy.S 3.3 KB

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