memcpy.S
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:2k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Copyright 2002 Andi Kleen */
  2. /*
  3.  * memcpy - Copy a memory block.
  4.  *
  5.  * Input:
  6.  * rdi destination
  7.  * rsi source
  8.  * rdx count
  9.  * 
  10.  * Output:
  11.  * rax original destination
  12.  */
  13.  // #define FIX_ALIGNMENT
  14.   .globl __memcpy
  15. .globl memcpy
  16. .p2align
  17. __memcpy:
  18. memcpy:
  19. pushq %rbx
  20. movq %rdi,%rax
  21. #ifdef FIX_ALIGNMENT
  22. movl %edi,%ecx
  23. andl $7,%ecx
  24. jnz  bad_alignment
  25. after_bad_alignment:
  26. #endif
  27. movq %rdx,%rcx
  28. movl $64,%ebx
  29. shrq $6,%rcx
  30. jz handle_tail
  31. loop_64:
  32. movq (%rsi),%r11
  33. movq 8(%rsi),%r8
  34. movq 2*8(%rsi),%r9
  35. movq 3*8(%rsi),%r10
  36. movq %r11,(%rdi)
  37. movq %r8,1*8(%rdi)
  38. movq %r9,2*8(%rdi)
  39. movq %r10,3*8(%rdi)
  40. movq 4*8(%rsi),%r11
  41. movq 5*8(%rsi),%r8
  42. movq 6*8(%rsi),%r9
  43. movq 7*8(%rsi),%r10
  44. movq %r11,4*8(%rdi)
  45. movq %r8,5*8(%rdi)
  46. movq %r9,6*8(%rdi)
  47. movq %r10,7*8(%rdi)
  48. addq %rbx,%rsi
  49. addq %rbx,%rdi
  50. decl %ecx
  51. jnz  loop_64
  52. handle_tail:
  53. movl %edx,%ecx
  54. andl $63,%ecx
  55. shrl $3,%ecx
  56. jz   handle_7
  57. movl $8,%ebx
  58. loop_8: 
  59. movq (%rsi),%r8
  60. movq %r8,(%rdi) 
  61. addq %rbx,%rdi
  62. addq %rbx,%rsi
  63. decl %ecx
  64. jnz  loop_8
  65. handle_7:
  66. movl %edx,%ecx
  67. andl $7,%ecx
  68. jz ende
  69. loop_1:
  70. movb (%rsi),%r8b
  71. movb %r8b,(%rdi) 
  72. incq %rdi
  73. incq %rsi
  74. decl %ecx
  75. jnz loop_1
  76. ende: 
  77. sfence
  78. popq %rbx
  79. ret
  80. #ifdef FIX_ALIGNMENT
  81. /* align destination */
  82. /* This is simpleminded. For bigger blocks it may make sense to align
  83.    src and dst to their aligned subset and handle the rest separately */
  84. bad_alignment:
  85. movl $8,%r9d
  86. subl %ecx,%r9d
  87. movl %r9d,%ecx
  88. subq %r9,%rdx
  89. js   small_alignment
  90. jz   small_alignment
  91. align_1:
  92. movb (%rsi),%r8b
  93. movb %r8b,(%rdi) 
  94. incq %rdi
  95. incq %rsi
  96. decl %ecx
  97. jnz  align_1
  98. jmp after_bad_alignment
  99. small_alignment:
  100. addq %r9,%rdx
  101. jmp handle_7
  102. #endif