memmove_64.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Normally compiler builtins are used, but sometimes the compiler calls out
  3. * of line code. Based on asm-i386/string.h.
  4. *
  5. * This assembly file is re-written from memmove_64.c file.
  6. * - Copyright 2011 Fenghua Yu <fenghua.yu@intel.com>
  7. */
  8. #include <linux/linkage.h>
  9. #include <asm/cpufeatures.h>
  10. #include <asm/alternative-asm.h>
  11. #include <asm/export.h>
  12. #undef memmove
  13. /*
  14. * Implement memmove(). This can handle overlap between src and dst.
  15. *
  16. * Input:
  17. * rdi: dest
  18. * rsi: src
  19. * rdx: count
  20. *
  21. * Output:
  22. * rax: dest
  23. */
  24. .weak memmove
  25. ENTRY(memmove)
  26. ENTRY(__memmove)
  27. /* Handle more 32 bytes in loop */
  28. mov %rdi, %rax
  29. cmp $0x20, %rdx
  30. jb 1f
  31. /* Decide forward/backward copy mode */
  32. cmp %rdi, %rsi
  33. jge .Lmemmove_begin_forward
  34. mov %rsi, %r8
  35. add %rdx, %r8
  36. cmp %rdi, %r8
  37. jg 2f
  38. .Lmemmove_begin_forward:
  39. ALTERNATIVE "", "movq %rdx, %rcx; rep movsb; retq", X86_FEATURE_ERMS
  40. /*
  41. * movsq instruction have many startup latency
  42. * so we handle small size by general register.
  43. */
  44. cmp $680, %rdx
  45. jb 3f
  46. /*
  47. * movsq instruction is only good for aligned case.
  48. */
  49. cmpb %dil, %sil
  50. je 4f
  51. 3:
  52. sub $0x20, %rdx
  53. /*
  54. * We gobble 32 bytes forward in each loop.
  55. */
  56. 5:
  57. sub $0x20, %rdx
  58. movq 0*8(%rsi), %r11
  59. movq 1*8(%rsi), %r10
  60. movq 2*8(%rsi), %r9
  61. movq 3*8(%rsi), %r8
  62. leaq 4*8(%rsi), %rsi
  63. movq %r11, 0*8(%rdi)
  64. movq %r10, 1*8(%rdi)
  65. movq %r9, 2*8(%rdi)
  66. movq %r8, 3*8(%rdi)
  67. leaq 4*8(%rdi), %rdi
  68. jae 5b
  69. addq $0x20, %rdx
  70. jmp 1f
  71. /*
  72. * Handle data forward by movsq.
  73. */
  74. .p2align 4
  75. 4:
  76. movq %rdx, %rcx
  77. movq -8(%rsi, %rdx), %r11
  78. lea -8(%rdi, %rdx), %r10
  79. shrq $3, %rcx
  80. rep movsq
  81. movq %r11, (%r10)
  82. jmp 13f
  83. .Lmemmove_end_forward:
  84. /*
  85. * Handle data backward by movsq.
  86. */
  87. .p2align 4
  88. 7:
  89. movq %rdx, %rcx
  90. movq (%rsi), %r11
  91. movq %rdi, %r10
  92. leaq -8(%rsi, %rdx), %rsi
  93. leaq -8(%rdi, %rdx), %rdi
  94. shrq $3, %rcx
  95. std
  96. rep movsq
  97. cld
  98. movq %r11, (%r10)
  99. jmp 13f
  100. /*
  101. * Start to prepare for backward copy.
  102. */
  103. .p2align 4
  104. 2:
  105. cmp $680, %rdx
  106. jb 6f
  107. cmp %dil, %sil
  108. je 7b
  109. 6:
  110. /*
  111. * Calculate copy position to tail.
  112. */
  113. addq %rdx, %rsi
  114. addq %rdx, %rdi
  115. subq $0x20, %rdx
  116. /*
  117. * We gobble 32 bytes backward in each loop.
  118. */
  119. 8:
  120. subq $0x20, %rdx
  121. movq -1*8(%rsi), %r11
  122. movq -2*8(%rsi), %r10
  123. movq -3*8(%rsi), %r9
  124. movq -4*8(%rsi), %r8
  125. leaq -4*8(%rsi), %rsi
  126. movq %r11, -1*8(%rdi)
  127. movq %r10, -2*8(%rdi)
  128. movq %r9, -3*8(%rdi)
  129. movq %r8, -4*8(%rdi)
  130. leaq -4*8(%rdi), %rdi
  131. jae 8b
  132. /*
  133. * Calculate copy position to head.
  134. */
  135. addq $0x20, %rdx
  136. subq %rdx, %rsi
  137. subq %rdx, %rdi
  138. 1:
  139. cmpq $16, %rdx
  140. jb 9f
  141. /*
  142. * Move data from 16 bytes to 31 bytes.
  143. */
  144. movq 0*8(%rsi), %r11
  145. movq 1*8(%rsi), %r10
  146. movq -2*8(%rsi, %rdx), %r9
  147. movq -1*8(%rsi, %rdx), %r8
  148. movq %r11, 0*8(%rdi)
  149. movq %r10, 1*8(%rdi)
  150. movq %r9, -2*8(%rdi, %rdx)
  151. movq %r8, -1*8(%rdi, %rdx)
  152. jmp 13f
  153. .p2align 4
  154. 9:
  155. cmpq $8, %rdx
  156. jb 10f
  157. /*
  158. * Move data from 8 bytes to 15 bytes.
  159. */
  160. movq 0*8(%rsi), %r11
  161. movq -1*8(%rsi, %rdx), %r10
  162. movq %r11, 0*8(%rdi)
  163. movq %r10, -1*8(%rdi, %rdx)
  164. jmp 13f
  165. 10:
  166. cmpq $4, %rdx
  167. jb 11f
  168. /*
  169. * Move data from 4 bytes to 7 bytes.
  170. */
  171. movl (%rsi), %r11d
  172. movl -4(%rsi, %rdx), %r10d
  173. movl %r11d, (%rdi)
  174. movl %r10d, -4(%rdi, %rdx)
  175. jmp 13f
  176. 11:
  177. cmp $2, %rdx
  178. jb 12f
  179. /*
  180. * Move data from 2 bytes to 3 bytes.
  181. */
  182. movw (%rsi), %r11w
  183. movw -2(%rsi, %rdx), %r10w
  184. movw %r11w, (%rdi)
  185. movw %r10w, -2(%rdi, %rdx)
  186. jmp 13f
  187. 12:
  188. cmp $1, %rdx
  189. jb 13f
  190. /*
  191. * Move data for 1 byte.
  192. */
  193. movb (%rsi), %r11b
  194. movb %r11b, (%rdi)
  195. 13:
  196. retq
  197. ENDPROC(__memmove)
  198. ENDPROC(memmove)
  199. EXPORT_SYMBOL(__memmove)
  200. EXPORT_SYMBOL(memmove)