memset.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "_glibc_inc.h"
  18. /* BEWARE: `#ifdef memset' means that memset is redefined as `bzero' */
  19. #define BZERO_P (defined memset)
  20. /* This is somehow experimental and could made dependend on the cache
  21. size. */
  22. #define LARGE $120000
  23. .text
  24. #endif
  25. ENTRY (memset)
  26. #if BZERO_P
  27. mov %rsi,%rdx /* Adjust parameter. */
  28. xorl %esi,%esi /* Fill with 0s. */
  29. #endif
  30. cmp $0x7,%rdx /* Check for small length. */
  31. mov %rdi,%rcx /* Save ptr as return value. */
  32. jbe 7f
  33. #if BZERO_P
  34. mov %rsi,%r8 /* Just copy 0. */
  35. #else
  36. /* Populate 8 bit data to full 64-bit. */
  37. movabs $0x0101010101010101,%r8
  38. movzbl %sil,%eax
  39. imul %rax,%r8
  40. #endif
  41. test $0x7,%edi /* Check for alignment. */
  42. jz 2f
  43. /* Next 3 insns are 9 bytes total, make sure we decode them in one go */
  44. .p2align 4,,9
  45. 1:
  46. /* Align ptr to 8 byte. */
  47. mov %sil,(%rcx)
  48. dec %rdx
  49. inc %rcx
  50. test $0x7,%cl
  51. jnz 1b
  52. 2: /* Check for really large regions. */
  53. mov %rdx,%rax
  54. shr $0x6,%rax
  55. je 4f
  56. cmp LARGE, %rdx
  57. jae 11f
  58. /* Next 3 insns are 11 bytes total, make sure we decode them in one go */
  59. .p2align 4,,11
  60. 3:
  61. /* Fill 64 bytes. */
  62. mov %r8,(%rcx)
  63. mov %r8,0x8(%rcx)
  64. mov %r8,0x10(%rcx)
  65. mov %r8,0x18(%rcx)
  66. mov %r8,0x20(%rcx)
  67. mov %r8,0x28(%rcx)
  68. mov %r8,0x30(%rcx)
  69. mov %r8,0x38(%rcx)
  70. add $0x40,%rcx
  71. dec %rax
  72. jne 3b
  73. 4: /* Fill final bytes. */
  74. and $0x3f,%edx
  75. mov %rdx,%rax
  76. shr $0x3,%rax
  77. je 6f
  78. 5: /* First in chunks of 8 bytes. */
  79. mov %r8,(%rcx)
  80. add $0x8,%rcx
  81. dec %rax
  82. jne 5b
  83. 6:
  84. and $0x7,%edx
  85. 7:
  86. test %rdx,%rdx
  87. je 9f
  88. 8: /* And finally as bytes (up to 7). */
  89. mov %sil,(%rcx)
  90. inc %rcx
  91. dec %rdx
  92. jne 8b
  93. 9:
  94. #if BZERO_P
  95. /* nothing */
  96. #else
  97. /* Load result (only if used as memset). */
  98. mov %rdi,%rax /* start address of destination is result */
  99. #endif
  100. retq
  101. /* Next 3 insns are 14 bytes total, make sure we decode them in one go */
  102. .p2align 4,,14
  103. 11:
  104. /* Fill 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. libc_hidden_def(memset)
  122. #endif