memset.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* memset/bzero -- set memory area to CH/0
  2. Optimized version for x86-64.
  3. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Andreas Jaeger <aj@suse.de>.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. #include "_glibc_inc.h"
  19. /* BEWARE: `#ifdef memset' means that memset is redefined as `bzero' */
  20. #define BZERO_P (defined __memset)
  21. /* This is somehow experimental and could made dependend on the cache
  22. size. */
  23. #define LARGE $120000
  24. .text
  25. #if !BZERO_P && defined PIC && !defined NOT_IN_libc
  26. ENTRY (__memset_chk)
  27. cmpq %rdx, %rcx
  28. jb HIDDEN_JUMPTARGET (__chk_fail)
  29. END (__memset_chk)
  30. #endif
  31. ENTRY (__memset)
  32. #if BZERO_P
  33. mov %rsi,%rdx /* Adjust parameter. */
  34. xorl %esi,%esi /* Fill with 0s. */
  35. #endif
  36. cmp $0x7,%rdx /* Check for small length. */
  37. mov %rdi,%rcx /* Save ptr as return value. */
  38. jbe 7f
  39. #if BZERO_P
  40. mov %rsi,%r8 /* Just copy 0. */
  41. #else
  42. /* Populate 8 bit data to full 64-bit. */
  43. movabs $0x0101010101010101,%r8
  44. movzbl %sil,%eax
  45. imul %rax,%r8
  46. #endif
  47. test $0x7,%edi /* Check for alignment. */
  48. je 2f
  49. .p2align 4
  50. 1: /* Align ptr to 8 byte. */
  51. mov %sil,(%rcx)
  52. dec %rdx
  53. inc %rcx
  54. test $0x7,%ecx
  55. jne 1b
  56. 2: /* Check for really large regions. */
  57. mov %rdx,%rax
  58. shr $0x6,%rax
  59. je 4f
  60. cmp LARGE, %rdx
  61. jae 11f
  62. .p2align 4
  63. 3: /* Copy 64 bytes. */
  64. mov %r8,(%rcx)
  65. mov %r8,0x8(%rcx)
  66. mov %r8,0x10(%rcx)
  67. mov %r8,0x18(%rcx)
  68. mov %r8,0x20(%rcx)
  69. mov %r8,0x28(%rcx)
  70. mov %r8,0x30(%rcx)
  71. mov %r8,0x38(%rcx)
  72. add $0x40,%rcx
  73. dec %rax
  74. jne 3b
  75. 4: /* Copy final bytes. */
  76. and $0x3f,%edx
  77. mov %rdx,%rax
  78. shr $0x3,%rax
  79. je 6f
  80. 5: /* First in chunks of 8 bytes. */
  81. mov %r8,(%rcx)
  82. add $0x8,%rcx
  83. dec %rax
  84. jne 5b
  85. 6:
  86. and $0x7,%edx
  87. 7:
  88. test %rdx,%rdx
  89. je 9f
  90. 8: /* And finally as bytes (up to 7). */
  91. mov %sil,(%rcx)
  92. inc %rcx
  93. dec %rdx
  94. jne 8b
  95. 9:
  96. #if BZERO_P
  97. nop
  98. #else
  99. /* Load result (only if used as memset). */
  100. mov %rdi,%rax /* start address of destination is result */
  101. #endif
  102. retq
  103. .p2align 4
  104. 11: /* Copy 64 bytes without polluting the cache. */
  105. /* We could use movntdq %xmm0,(%rcx) here to further
  106. speed up for large cases but let's not use XMM registers. */
  107. movnti %r8,(%rcx)
  108. movnti %r8,0x8(%rcx)
  109. movnti %r8,0x10(%rcx)
  110. movnti %r8,0x18(%rcx)
  111. movnti %r8,0x20(%rcx)
  112. movnti %r8,0x28(%rcx)
  113. movnti %r8,0x30(%rcx)
  114. movnti %r8,0x38(%rcx)
  115. add $0x40,%rcx
  116. dec %rax
  117. jne 11b
  118. jmp 4b
  119. END (__memset)
  120. #if !BZERO_P
  121. strong_alias(__memset,memset)
  122. #endif
  123. #if !BZERO_P && defined PIC && !defined NOT_IN_libc
  124. strong_alias (__memset_chk, __memset_zero_constant_len_parameter)
  125. #endif