memcpy.S 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Highly optimized version for x86-64.
  2. Copyright (C) 1997, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Based on i586 version contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "_glibc_inc.h"
  17. /* BEWARE: `#ifdef memcpy' means that memcpy is redefined as `mempcpy',
  18. and the return value is the byte after the last one copied in
  19. the destination. */
  20. #define MEMPCPY_P (defined memcpy)
  21. .text
  22. #if defined __PIC__ && !defined NOT_IN_libc && defined __UCLIBC_HAS_FORTIFY__
  23. ENTRY (__memcpy_chk)
  24. cmpq %rdx, %rcx
  25. jb HIDDEN_JUMPTARGET (__chk_fail)
  26. END (__memcpy_chk)
  27. #endif
  28. ENTRY (BP_SYM (memcpy))
  29. /* Cutoff for the big loop is a size of 32 bytes since otherwise
  30. the loop will never be entered. */
  31. cmpq $32, %rdx
  32. movq %rdx, %rcx
  33. #if !MEMPCPY_P
  34. movq %rdi, %r10 /* Save value. */
  35. #endif
  36. /* We need this in any case. */
  37. cld
  38. jbe 1f
  39. /* Align destination. */
  40. movq %rdi, %rax
  41. negq %rax
  42. andq $7, %rax
  43. subq %rax, %rcx
  44. xchgq %rax, %rcx
  45. rep; movsb
  46. movq %rax, %rcx
  47. subq $32, %rcx
  48. js 2f
  49. /* Next 3 insns are 11 bytes total, make sure we decode them in one go */
  50. .p2align 4,,11
  51. 3:
  52. /* Now correct the loop counter. Please note that in the following
  53. code the flags are not changed anymore. */
  54. subq $32, %rcx
  55. movq (%rsi), %rax
  56. movq 8(%rsi), %rdx
  57. movq 16(%rsi), %r8
  58. movq 24(%rsi), %r9
  59. movq %rax, (%rdi)
  60. movq %rdx, 8(%rdi)
  61. movq %r8, 16(%rdi)
  62. movq %r9, 24(%rdi)
  63. leaq 32(%rsi), %rsi
  64. leaq 32(%rdi), %rdi
  65. jns 3b
  66. /* Correct extra loop counter modification. */
  67. 2: addq $32, %rcx
  68. 1: rep; movsb
  69. #if MEMPCPY_P
  70. movq %rdi, %rax /* Set return value. */
  71. #else
  72. movq %r10, %rax /* Set return value. */
  73. #endif
  74. ret
  75. END (BP_SYM (memcpy))
  76. #if !MEMPCPY_P
  77. libc_hidden_def(memcpy)
  78. #endif