1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright 2002 Andi Kleen, SuSE Labs */
4 #include <linux/linkage.h>
5 #include <asm/cpufeatures.h>
6 #include <asm/alternative-asm.h>
7 #include <asm/export.h>
10 * ISO C memset - set a memory block to a byte value. This function uses fast
11 * string to get better performance than the original function. The code is
12 * simpler and shorter than the original function as well.
18 * rax original destination
20 SYM_FUNC_START_WEAK(memset)
21 SYM_FUNC_START(__memset)
23 * Some CPUs support enhanced REP MOVSB/STOSB feature. It is recommended
24 * to use it when possible. If not available, use fast string instructions.
26 * Otherwise, use original memset function.
28 ALTERNATIVE_2 "jmp memset_orig", "", X86_FEATURE_REP_GOOD, \
29 "jmp memset_erms", X86_FEATURE_ERMS
35 /* expand byte value */
37 movabs $0x0101010101010101,%rax
44 SYM_FUNC_END(__memset)
45 SYM_FUNC_END_ALIAS(memset)
47 EXPORT_SYMBOL(__memset)
50 * ISO C memset - set a memory block to a byte value. This function uses
51 * enhanced rep stosb to override the fast string function.
52 * The code is simpler and shorter than the fast string function as well.
58 * rax original destination
60 SYM_FUNC_START_LOCAL(memset_erms)
67 SYM_FUNC_END(memset_erms)
69 SYM_FUNC_START_LOCAL(memset_orig)
72 /* expand byte value */
74 movabs $0x0101010101010101,%rax
81 .Lafter_bad_alignment:
101 /* Handle tail in loops. The loops should be faster than hard
102 to predict jump tables. */
133 movq %rax,(%rdi) /* unaligned store */
138 jmp .Lafter_bad_alignment
140 SYM_FUNC_END(memset_orig)