memset_64.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright 2002 Andi Kleen, SuSE Labs */
  3. #include <linux/linkage.h>
  4. #include <asm/cpufeatures.h>
  5. #include <asm/alternative-asm.h>
  6. .weak memset
  7. /*
  8. * ISO C memset - set a memory block to a byte value. This function uses fast
  9. * string to get better performance than the original function. The code is
  10. * simpler and shorter than the original function as well.
  11. *
  12. * rdi destination
  13. * rsi value (char)
  14. * rdx count (bytes)
  15. *
  16. * rax original destination
  17. */
  18. ENTRY(memset)
  19. ENTRY(__memset)
  20. /*
  21. * Some CPUs support enhanced REP MOVSB/STOSB feature. It is recommended
  22. * to use it when possible. If not available, use fast string instructions.
  23. *
  24. * Otherwise, use original memset function.
  25. */
  26. ALTERNATIVE_2 "jmp memset_orig", "", X86_FEATURE_REP_GOOD, \
  27. "jmp memset_erms", X86_FEATURE_ERMS
  28. movq %rdi,%r9
  29. movq %rdx,%rcx
  30. andl $7,%edx
  31. shrq $3,%rcx
  32. /* expand byte value */
  33. movzbl %sil,%esi
  34. movabs $0x0101010101010101,%rax
  35. imulq %rsi,%rax
  36. rep stosq
  37. movl %edx,%ecx
  38. rep stosb
  39. movq %r9,%rax
  40. ret
  41. ENDPROC(memset)
  42. ENDPROC(__memset)
  43. /*
  44. * ISO C memset - set a memory block to a byte value. This function uses
  45. * enhanced rep stosb to override the fast string function.
  46. * The code is simpler and shorter than the fast string function as well.
  47. *
  48. * rdi destination
  49. * rsi value (char)
  50. * rdx count (bytes)
  51. *
  52. * rax original destination
  53. */
  54. ENTRY(memset_erms)
  55. movq %rdi,%r9
  56. movb %sil,%al
  57. movq %rdx,%rcx
  58. rep stosb
  59. movq %r9,%rax
  60. ret
  61. ENDPROC(memset_erms)
  62. ENTRY(memset_orig)
  63. movq %rdi,%r10
  64. /* expand byte value */
  65. movzbl %sil,%ecx
  66. movabs $0x0101010101010101,%rax
  67. imulq %rcx,%rax
  68. /* align dst */
  69. movl %edi,%r9d
  70. andl $7,%r9d
  71. jnz .Lbad_alignment
  72. .Lafter_bad_alignment:
  73. movq %rdx,%rcx
  74. shrq $6,%rcx
  75. jz .Lhandle_tail
  76. .p2align 4
  77. .Lloop_64:
  78. decq %rcx
  79. movq %rax,(%rdi)
  80. movq %rax,8(%rdi)
  81. movq %rax,16(%rdi)
  82. movq %rax,24(%rdi)
  83. movq %rax,32(%rdi)
  84. movq %rax,40(%rdi)
  85. movq %rax,48(%rdi)
  86. movq %rax,56(%rdi)
  87. leaq 64(%rdi),%rdi
  88. jnz .Lloop_64
  89. /* Handle tail in loops. The loops should be faster than hard
  90. to predict jump tables. */
  91. .p2align 4
  92. .Lhandle_tail:
  93. movl %edx,%ecx
  94. andl $63&(~7),%ecx
  95. jz .Lhandle_7
  96. shrl $3,%ecx
  97. .p2align 4
  98. .Lloop_8:
  99. decl %ecx
  100. movq %rax,(%rdi)
  101. leaq 8(%rdi),%rdi
  102. jnz .Lloop_8
  103. .Lhandle_7:
  104. andl $7,%edx
  105. jz .Lende
  106. .p2align 4
  107. .Lloop_1:
  108. decl %edx
  109. movb %al,(%rdi)
  110. leaq 1(%rdi),%rdi
  111. jnz .Lloop_1
  112. .Lende:
  113. movq %r10,%rax
  114. ret
  115. .Lbad_alignment:
  116. cmpq $7,%rdx
  117. jbe .Lhandle_7
  118. movq %rax,(%rdi) /* unaligned store */
  119. movq $8,%r8
  120. subq %r9,%r8
  121. addq %r8,%rdi
  122. subq %r8,%rdx
  123. jmp .Lafter_bad_alignment
  124. .Lfinal:
  125. ENDPROC(memset_orig)