memset_64.S 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright 2002 Andi Kleen, SuSE Labs */
  2. #include <linux/linkage.h>
  3. #include <asm/cpufeatures.h>
  4. #include <asm/alternative-asm.h>
  5. #include <asm/export.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. EXPORT_SYMBOL(memset)
  44. EXPORT_SYMBOL(__memset)
  45. /*
  46. * ISO C memset - set a memory block to a byte value. This function uses
  47. * enhanced rep stosb to override the fast string function.
  48. * The code is simpler and shorter than the fast string function as well.
  49. *
  50. * rdi destination
  51. * rsi value (char)
  52. * rdx count (bytes)
  53. *
  54. * rax original destination
  55. */
  56. ENTRY(memset_erms)
  57. movq %rdi,%r9
  58. movb %sil,%al
  59. movq %rdx,%rcx
  60. rep stosb
  61. movq %r9,%rax
  62. ret
  63. ENDPROC(memset_erms)
  64. ENTRY(memset_orig)
  65. movq %rdi,%r10
  66. /* expand byte value */
  67. movzbl %sil,%ecx
  68. movabs $0x0101010101010101,%rax
  69. imulq %rcx,%rax
  70. /* align dst */
  71. movl %edi,%r9d
  72. andl $7,%r9d
  73. jnz .Lbad_alignment
  74. .Lafter_bad_alignment:
  75. movq %rdx,%rcx
  76. shrq $6,%rcx
  77. jz .Lhandle_tail
  78. .p2align 4
  79. .Lloop_64:
  80. decq %rcx
  81. movq %rax,(%rdi)
  82. movq %rax,8(%rdi)
  83. movq %rax,16(%rdi)
  84. movq %rax,24(%rdi)
  85. movq %rax,32(%rdi)
  86. movq %rax,40(%rdi)
  87. movq %rax,48(%rdi)
  88. movq %rax,56(%rdi)
  89. leaq 64(%rdi),%rdi
  90. jnz .Lloop_64
  91. /* Handle tail in loops. The loops should be faster than hard
  92. to predict jump tables. */
  93. .p2align 4
  94. .Lhandle_tail:
  95. movl %edx,%ecx
  96. andl $63&(~7),%ecx
  97. jz .Lhandle_7
  98. shrl $3,%ecx
  99. .p2align 4
  100. .Lloop_8:
  101. decl %ecx
  102. movq %rax,(%rdi)
  103. leaq 8(%rdi),%rdi
  104. jnz .Lloop_8
  105. .Lhandle_7:
  106. andl $7,%edx
  107. jz .Lende
  108. .p2align 4
  109. .Lloop_1:
  110. decl %edx
  111. movb %al,(%rdi)
  112. leaq 1(%rdi),%rdi
  113. jnz .Lloop_1
  114. .Lende:
  115. movq %r10,%rax
  116. ret
  117. .Lbad_alignment:
  118. cmpq $7,%rdx
  119. jbe .Lhandle_7
  120. movq %rax,(%rdi) /* unaligned store */
  121. movq $8,%r8
  122. subq %r9,%r8
  123. addq %r8,%rdi
  124. subq %r8,%rdx
  125. jmp .Lafter_bad_alignment
  126. .Lfinal:
  127. ENDPROC(memset_orig)