memcpy_64.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright 2002 Andi Kleen */
  2. #include <linux/linkage.h>
  3. #include <asm/cpufeature.h>
  4. #include <asm/dwarf2.h>
  5. #include <asm/alternative-asm.h>
  6. /*
  7. * We build a jump to memcpy_orig by default which gets NOPped out on
  8. * the majority of x86 CPUs which set REP_GOOD. In addition, CPUs which
  9. * have the enhanced REP MOVSB/STOSB feature (ERMS), change those NOPs
  10. * to a jmp to memcpy_erms which does the REP; MOVSB mem copy.
  11. */
  12. .weak memcpy
  13. /*
  14. * memcpy - Copy a memory block.
  15. *
  16. * Input:
  17. * rdi destination
  18. * rsi source
  19. * rdx count
  20. *
  21. * Output:
  22. * rax original destination
  23. */
  24. ENTRY(__memcpy)
  25. ENTRY(memcpy)
  26. ALTERNATIVE_2 "jmp memcpy_orig", "", X86_FEATURE_REP_GOOD, \
  27. "jmp memcpy_erms", X86_FEATURE_ERMS
  28. movq %rdi, %rax
  29. movq %rdx, %rcx
  30. shrq $3, %rcx
  31. andl $7, %edx
  32. rep movsq
  33. movl %edx, %ecx
  34. rep movsb
  35. ret
  36. ENDPROC(memcpy)
  37. ENDPROC(__memcpy)
  38. /*
  39. * memcpy_erms() - enhanced fast string memcpy. This is faster and
  40. * simpler than memcpy. Use memcpy_erms when possible.
  41. */
  42. ENTRY(memcpy_erms)
  43. movq %rdi, %rax
  44. movq %rdx, %rcx
  45. rep movsb
  46. ret
  47. ENDPROC(memcpy_erms)
  48. ENTRY(memcpy_orig)
  49. CFI_STARTPROC
  50. movq %rdi, %rax
  51. cmpq $0x20, %rdx
  52. jb .Lhandle_tail
  53. /*
  54. * We check whether memory false dependence could occur,
  55. * then jump to corresponding copy mode.
  56. */
  57. cmp %dil, %sil
  58. jl .Lcopy_backward
  59. subq $0x20, %rdx
  60. .Lcopy_forward_loop:
  61. subq $0x20, %rdx
  62. /*
  63. * Move in blocks of 4x8 bytes:
  64. */
  65. movq 0*8(%rsi), %r8
  66. movq 1*8(%rsi), %r9
  67. movq 2*8(%rsi), %r10
  68. movq 3*8(%rsi), %r11
  69. leaq 4*8(%rsi), %rsi
  70. movq %r8, 0*8(%rdi)
  71. movq %r9, 1*8(%rdi)
  72. movq %r10, 2*8(%rdi)
  73. movq %r11, 3*8(%rdi)
  74. leaq 4*8(%rdi), %rdi
  75. jae .Lcopy_forward_loop
  76. addl $0x20, %edx
  77. jmp .Lhandle_tail
  78. .Lcopy_backward:
  79. /*
  80. * Calculate copy position to tail.
  81. */
  82. addq %rdx, %rsi
  83. addq %rdx, %rdi
  84. subq $0x20, %rdx
  85. /*
  86. * At most 3 ALU operations in one cycle,
  87. * so append NOPS in the same 16 bytes trunk.
  88. */
  89. .p2align 4
  90. .Lcopy_backward_loop:
  91. subq $0x20, %rdx
  92. movq -1*8(%rsi), %r8
  93. movq -2*8(%rsi), %r9
  94. movq -3*8(%rsi), %r10
  95. movq -4*8(%rsi), %r11
  96. leaq -4*8(%rsi), %rsi
  97. movq %r8, -1*8(%rdi)
  98. movq %r9, -2*8(%rdi)
  99. movq %r10, -3*8(%rdi)
  100. movq %r11, -4*8(%rdi)
  101. leaq -4*8(%rdi), %rdi
  102. jae .Lcopy_backward_loop
  103. /*
  104. * Calculate copy position to head.
  105. */
  106. addl $0x20, %edx
  107. subq %rdx, %rsi
  108. subq %rdx, %rdi
  109. .Lhandle_tail:
  110. cmpl $16, %edx
  111. jb .Lless_16bytes
  112. /*
  113. * Move data from 16 bytes to 31 bytes.
  114. */
  115. movq 0*8(%rsi), %r8
  116. movq 1*8(%rsi), %r9
  117. movq -2*8(%rsi, %rdx), %r10
  118. movq -1*8(%rsi, %rdx), %r11
  119. movq %r8, 0*8(%rdi)
  120. movq %r9, 1*8(%rdi)
  121. movq %r10, -2*8(%rdi, %rdx)
  122. movq %r11, -1*8(%rdi, %rdx)
  123. retq
  124. .p2align 4
  125. .Lless_16bytes:
  126. cmpl $8, %edx
  127. jb .Lless_8bytes
  128. /*
  129. * Move data from 8 bytes to 15 bytes.
  130. */
  131. movq 0*8(%rsi), %r8
  132. movq -1*8(%rsi, %rdx), %r9
  133. movq %r8, 0*8(%rdi)
  134. movq %r9, -1*8(%rdi, %rdx)
  135. retq
  136. .p2align 4
  137. .Lless_8bytes:
  138. cmpl $4, %edx
  139. jb .Lless_3bytes
  140. /*
  141. * Move data from 4 bytes to 7 bytes.
  142. */
  143. movl (%rsi), %ecx
  144. movl -4(%rsi, %rdx), %r8d
  145. movl %ecx, (%rdi)
  146. movl %r8d, -4(%rdi, %rdx)
  147. retq
  148. .p2align 4
  149. .Lless_3bytes:
  150. subl $1, %edx
  151. jb .Lend
  152. /*
  153. * Move data from 1 bytes to 3 bytes.
  154. */
  155. movzbl (%rsi), %ecx
  156. jz .Lstore_1byte
  157. movzbq 1(%rsi), %r8
  158. movzbq (%rsi, %rdx), %r9
  159. movb %r8b, 1(%rdi)
  160. movb %r9b, (%rdi, %rdx)
  161. .Lstore_1byte:
  162. movb %cl, (%rdi)
  163. .Lend:
  164. retq
  165. CFI_ENDPROC
  166. ENDPROC(memcpy_orig)