memset_64.S 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright 2002 Andi Kleen, SuSE Labs */
  3. #include <linux/export.h>
  4. #include <linux/linkage.h>
  5. #include <asm/cpufeatures.h>
  6. #include <asm/alternative.h>
  7. .section .noinstr.text, "ax"
  8. /*
  9. * ISO C memset - set a memory block to a byte value. This function uses fast
  10. * string to get better performance than the original function. The code is
  11. * simpler and shorter than the original function as well.
  12. *
  13. * rdi destination
  14. * rsi value (char)
  15. * rdx count (bytes)
  16. *
  17. * rax original destination
  18. *
  19. * The FSRS alternative should be done inline (avoiding the call and
  20. * the disgusting return handling), but that would require some help
  21. * from the compiler for better calling conventions.
  22. *
  23. * The 'rep stosb' itself is small enough to replace the call, but all
  24. * the register moves blow up the code. And two of them are "needed"
  25. * only for the return value that is the same as the source input,
  26. * which the compiler could/should do much better anyway.
  27. */
  28. SYM_FUNC_START(__memset)
  29. ALTERNATIVE "jmp memset_orig", "", X86_FEATURE_FSRS
  30. movq %rdi,%r9
  31. movb %sil,%al
  32. movq %rdx,%rcx
  33. rep stosb
  34. movq %r9,%rax
  35. RET
  36. SYM_FUNC_END(__memset)
  37. EXPORT_SYMBOL(__memset)
  38. SYM_FUNC_ALIAS_MEMFUNC(memset, __memset)
  39. EXPORT_SYMBOL(memset)
  40. SYM_FUNC_START_LOCAL(memset_orig)
  41. movq %rdi,%r10
  42. /* expand byte value */
  43. movzbl %sil,%ecx
  44. movabs $0x0101010101010101,%rax
  45. imulq %rcx,%rax
  46. /* align dst */
  47. movl %edi,%r9d
  48. andl $7,%r9d
  49. jnz .Lbad_alignment
  50. .Lafter_bad_alignment:
  51. movq %rdx,%rcx
  52. shrq $6,%rcx
  53. jz .Lhandle_tail
  54. .p2align 4
  55. .Lloop_64:
  56. decq %rcx
  57. movq %rax,(%rdi)
  58. movq %rax,8(%rdi)
  59. movq %rax,16(%rdi)
  60. movq %rax,24(%rdi)
  61. movq %rax,32(%rdi)
  62. movq %rax,40(%rdi)
  63. movq %rax,48(%rdi)
  64. movq %rax,56(%rdi)
  65. leaq 64(%rdi),%rdi
  66. jnz .Lloop_64
  67. /* Handle tail in loops. The loops should be faster than hard
  68. to predict jump tables. */
  69. .p2align 4
  70. .Lhandle_tail:
  71. movl %edx,%ecx
  72. andl $63&(~7),%ecx
  73. jz .Lhandle_7
  74. shrl $3,%ecx
  75. .p2align 4
  76. .Lloop_8:
  77. decl %ecx
  78. movq %rax,(%rdi)
  79. leaq 8(%rdi),%rdi
  80. jnz .Lloop_8
  81. .Lhandle_7:
  82. andl $7,%edx
  83. jz .Lende
  84. .p2align 4
  85. .Lloop_1:
  86. decl %edx
  87. movb %al,(%rdi)
  88. leaq 1(%rdi),%rdi
  89. jnz .Lloop_1
  90. .Lende:
  91. movq %r10,%rax
  92. RET
  93. .Lbad_alignment:
  94. cmpq $7,%rdx
  95. jbe .Lhandle_7
  96. movq %rax,(%rdi) /* unaligned store */
  97. movq $8,%r8
  98. subq %r9,%r8
  99. addq %r8,%rdi
  100. subq %r8,%rdx
  101. jmp .Lafter_bad_alignment
  102. .Lfinal:
  103. SYM_FUNC_END(memset_orig)