]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/amd64/string/memset.S
amd64: sync up libc memset with the kernel version
[FreeBSD/FreeBSD.git] / lib / libc / amd64 / string / memset.S
1 /*-
2  * Copyright (c) 2018 The FreeBSD Foundation
3  *
4  * This software was developed by Mateusz Guzik <mjg@FreeBSD.org>
5  * under sponsorship from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <machine/asm.h>
32 __FBSDID("$FreeBSD$");
33
34 #define ALIGN_TEXT      .p2align 4,0x90 /* 16-byte alignment, nop filled */
35
36 .macro MEMSET erms
37         movq    %rdi,%rax
38         movq    %rdx,%rcx
39         movzbq  %sil,%r8
40         movabs  $0x0101010101010101,%r10
41         imulq   %r8,%r10
42
43         cmpq    $32,%rcx
44         jb      1016f
45
46         cmpq    $256,%rcx
47         ja      1256f
48
49 1032:
50         movq    %r10,(%rdi)
51         movq    %r10,8(%rdi)
52         movq    %r10,16(%rdi)
53         movq    %r10,24(%rdi)
54         leaq    32(%rdi),%rdi
55         subq    $32,%rcx
56         cmpq    $32,%rcx
57         jae     1032b
58         cmpb    $0,%cl
59         je      1000f
60 1016:
61         cmpb    $16,%cl
62         jl      1008f
63         movq    %r10,(%rdi)
64         movq    %r10,8(%rdi)
65         subb    $16,%cl
66         jz      1000f
67         leaq    16(%rdi),%rdi
68 1008:
69         cmpb    $8,%cl
70         jl      1004f
71         movq    %r10,(%rdi)
72         subb    $8,%cl
73         jz      1000f
74         leaq    8(%rdi),%rdi
75 1004:
76         cmpb    $4,%cl
77         jl      1002f
78         movl    %r10d,(%rdi)
79         subb    $4,%cl
80         jz      1000f
81         leaq    4(%rdi),%rdi
82 1002:
83         cmpb    $2,%cl
84         jl      1001f
85         movw    %r10w,(%rdi)
86         subb    $2,%cl
87         jz      1000f
88         leaq    2(%rdi),%rdi
89 1001:
90         cmpb    $1,%cl
91         jl      1000f
92         movb    %r10b,(%rdi)
93 1000:
94         ret
95         ALIGN_TEXT
96 1256:
97         movq    %rdi,%r9
98         movq    %r10,%rax
99         testl   $15,%edi
100         jnz     3f
101 1:
102 .if \erms == 1
103         rep
104         stosb
105         movq    %r9,%rax
106 .else
107         movq    %rcx,%rdx
108         shrq    $3,%rcx
109         rep
110         stosq
111         movq    %r9,%rax
112         andl    $7,%edx
113         jnz     2f
114         ret
115 2:
116         movq    %r10,-8(%rdi,%rdx)
117 .endif
118         ret
119         ALIGN_TEXT
120 3:
121         movq    %r10,(%rdi)
122         movq    %r10,8(%rdi)
123         movq    %rdi,%r8
124         andq    $15,%r8
125         leaq    -16(%rcx,%r8),%rcx
126         neg     %r8
127         leaq    16(%rdi,%r8),%rdi
128         jmp     1b
129 .endm
130
131 ENTRY(memset)
132         MEMSET erms=0
133 END(memset)
134
135         .section .note.GNU-stack,"",%progbits