xref: /freebsd/lib/libc/amd64/string/memset.S (revision bdafb02fcb88389fd1ab684cfe734cb429d35618)
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.macro MEMSET bzero erms
35.if \bzero == 1
36	movq	%rsi,%rcx
37	movq	%rsi,%rdx
38	xorl	%eax,%eax
39.else
40	movq	%rdi,%r9
41	movq	%rdx,%rcx
42	movzbq	%sil,%r8
43	movabs	$0x0101010101010101,%rax
44	imulq	%r8,%rax
45.endif
46
47	cmpq	$32,%rcx
48	jb	1016f
49
50	cmpq	$256,%rcx
51	ja	1256f
52
531032:
54	movq	%rax,(%rdi)
55	movq	%rax,8(%rdi)
56	movq	%rax,16(%rdi)
57	movq	%rax,24(%rdi)
58	leaq	32(%rdi),%rdi
59	subq	$32,%rcx
60	cmpq	$32,%rcx
61	jae	1032b
62	cmpb	$0,%cl
63	je	1000f
641016:
65	cmpb	$16,%cl
66	jl	1008f
67	movq	%rax,(%rdi)
68	movq	%rax,8(%rdi)
69	subb	$16,%cl
70	jz	1000f
71	leaq	16(%rdi),%rdi
721008:
73	cmpb	$8,%cl
74	jl	1004f
75	movq	%rax,(%rdi)
76	subb	$8,%cl
77	jz	1000f
78	leaq	8(%rdi),%rdi
791004:
80	cmpb	$4,%cl
81	jl	1002f
82	movl	%eax,(%rdi)
83	subb	$4,%cl
84	jz	1000f
85	leaq	4(%rdi),%rdi
861002:
87	cmpb	$2,%cl
88	jl	1001f
89	movw	%ax,(%rdi)
90	subb	$2,%cl
91	jz	1000f
92	leaq	2(%rdi),%rdi
931001:
94	cmpb	$1,%cl
95	jl	1000f
96	movb	%al,(%rdi)
971000:
98.if \bzero == 0
99	movq	%r9,%rax
100.endif
101	ret
102
1031256:
104.if \erms == 1
105	rep
106	stosb
107.else
108	shrq	$3,%rcx
109	rep
110	stosq
111	movq	%rdx,%rcx
112	andb	$7,%cl
113	jne	1004b
114.endif
115.if \bzero == 0
116	movq	%r9,%rax
117.endif
118	ret
119.endm
120
121#ifndef BZERO
122ENTRY(memset)
123	MEMSET bzero=0 erms=0
124END(memset)
125#else
126ENTRY(bzero)
127	MEMSET bzero=1 erms=0
128END(bzero)
129#endif
130
131	.section .note.GNU-stack,"",%progbits
132