1// Copyright 2016 The Fuchsia Authors
2//
3// Use of this source code is governed by a MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT
6
7#include <asm.h>
8#include <lib/code_patching.h>
9
10.text
11
12// memset implementation relying on Intel's Enhanced REP STOSB optimization
13// %rax = memset(%rdi, %rsi, %rdx)
14FUNCTION(memset_erms)
15    // Save return value.
16    mov %rdi, %r11
17
18    mov %sil, %al
19    mov %rdx, %rcx
20    rep stosb // while (rcx-- > 0) *rdi++ = al;
21
22    mov %r11, %rax
23    ret
24END_FUNCTION(memset_erms)
25
26// memset implementation that sets 8 bytes at a time when possible
27// %rax = memset_quad(%rdi, %rsi, %rdx)
28FUNCTION(memset_quad)
29    // Save return value.
30    mov %rdi, %r11
31
32    // Create an 8-byte copy of the pattern
33    mov %rdx, %rcx
34    movzx %sil, %rax
35    movq $0x0101010101010101, %r10
36    mul %r10
37    mov %rcx, %rdx
38
39    // Copy all of the 8 byte chunks we can
40    shr $3, %rcx
41    rep stosq // while (rcx-- > 0) { *rdi++ = rax; /* rdi is uint64_t* */ }
42
43    // Copy the rest
44    mov %rdx, %rcx
45    and $0x7, %rcx
46    rep stosb
47
48    mov %r11, %rax
49    ret
50END_FUNCTION(memset_quad)
51
52FUNCTION(memset)
53    jmp memset_erms
54    APPLY_CODE_PATCH_FUNC_WITH_DEFAULT(x86_memset_select, memset, 2)
55END_FUNCTION(memset)
56