memset.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 defined __PIC__ && !defined NOT_IN_libc
  26. ENTRY (__memset_chk)
  27. cmpq %rdx, %rcx
  28. #if defined __UCLIBC_HAS_SSP__
  29. jb HIDDEN_JUMPTARGET (__chk_fail)
  30. #endif
  31. END (__memset_chk)
  32. #endif
  33. ENTRY (memset)
  34. #if BZERO_P
  35. mov %rsi,%rdx /* Adjust parameter. */
  36. xorl %esi,%esi /* Fill with 0s. */
  37. #endif
  38. cmp $0x7,%rdx /* Check for small length. */
  39. mov %rdi,%rcx /* Save ptr as return value. */
  40. jbe 7f
  41. #if BZERO_P
  42. mov %rsi,%r8 /* Just copy 0. */
  43. #else
  44. /* Populate 8 bit data to full 64-bit. */
  45. movabs $0x0101010101010101,%r8
  46. movzbl %sil,%eax
  47. imul %rax,%r8
  48. #endif
  49. test $0x7,%edi /* Check for alignment. */
  50. jz 2f
  51. /* Next 3 insns are 9 bytes total, make sure we decode them in one go */
  52. .p2align 4,,9
  53. 1:
  54. /* Align ptr to 8 byte. */
  55. mov %sil,(%rcx)
  56. dec %rdx
  57. inc %rcx
  58. test $0x7,%cl
  59. jnz 1b
  60. 2: /* Check for really large regions. */
  61. mov %rdx,%rax
  62. shr $0x6,%rax
  63. je 4f
  64. cmp LARGE, %rdx
  65. jae 11f
  66. /* Next 3 insns are 11 bytes total, make sure we decode them in one go */
  67. .p2align 4,,11
  68. 3:
  69. /* Fill 64 bytes. */
  70. mov %r8,(%rcx)
  71. mov %r8,0x8(%rcx)
  72. mov %r8,0x10(%rcx)
  73. mov %r8,0x18(%rcx)
  74. mov %r8,0x20(%rcx)
  75. mov %r8,0x28(%rcx)
  76. mov %r8,0x30(%rcx)
  77. mov %r8,0x38(%rcx)
  78. add $0x40,%rcx
  79. dec %rax
  80. jne 3b
  81. 4: /* Fill final bytes. */
  82. and $0x3f,%edx
  83. mov %rdx,%rax
  84. shr $0x3,%rax
  85. je 6f
  86. 5: /* First in chunks of 8 bytes. */
  87. mov %r8,(%rcx)
  88. add $0x8,%rcx
  89. dec %rax
  90. jne 5b
  91. 6:
  92. and $0x7,%edx
  93. 7:
  94. test %rdx,%rdx
  95. je 9f
  96. 8: /* And finally as bytes (up to 7). */
  97. mov %sil,(%rcx)
  98. inc %rcx
  99. dec %rdx
  100. jne 8b
  101. 9:
  102. #if BZERO_P
  103. /* nothing */
  104. #else
  105. /* Load result (only if used as memset). */
  106. mov %rdi,%rax /* start address of destination is result */
  107. #endif
  108. retq
  109. /* Next 3 insns are 14 bytes total, make sure we decode them in one go */
  110. .p2align 4,,14
  111. 11:
  112. /* Fill 64 bytes without polluting the cache. */
  113. /* We could use movntdq %xmm0,(%rcx) here to further
  114. speed up for large cases but let's not use XMM registers. */
  115. movnti %r8,(%rcx)
  116. movnti %r8,0x8(%rcx)
  117. movnti %r8,0x10(%rcx)
  118. movnti %r8,0x18(%rcx)
  119. movnti %r8,0x20(%rcx)
  120. movnti %r8,0x28(%rcx)
  121. movnti %r8,0x30(%rcx)
  122. movnti %r8,0x38(%rcx)
  123. add $0x40,%rcx
  124. dec %rax
  125. jne 11b
  126. jmp 4b
  127. END (memset)
  128. #if !BZERO_P
  129. libc_hidden_def(memset)
  130. #endif
  131. #if !BZERO_P && defined __PIC__ && !defined NOT_IN_libc
  132. strong_alias (__memset_chk, __memset_zero_constant_len_parameter)
  133. #endif