xref: /freebsd/lib/libc/amd64/string/memset.S (revision e5d258c9e599d2b2fe642e678091cac5da8a10d1)
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
29#include <machine/asm.h>
30__FBSDID("$FreeBSD$");
31
32/*
33 * Note: this routine was written with kernel use in mind (read: no simd),
34 * it is only present in userspace as a temporary measure until something
35 * better gets imported.
36 */
37
38#define ALIGN_TEXT      .p2align 4,0x90 /* 16-byte alignment, nop filled */
39
40.macro MEMSET erms
41	movq	%rdi,%rax
42	movq	%rdx,%rcx
43	movzbq	%sil,%r8
44	movabs	$0x0101010101010101,%r10
45	imulq	%r8,%r10
46
47	cmpq	$32,%rcx
48	jbe	101632f
49
50	cmpq	$256,%rcx
51	ja	1256f
52
53	ALIGN_TEXT
54103200:
55	movq	%r10,(%rdi)
56	movq	%r10,8(%rdi)
57	movq	%r10,16(%rdi)
58	movq	%r10,24(%rdi)
59	leaq	32(%rdi),%rdi
60	subq	$32,%rcx
61	cmpq	$32,%rcx
62	ja	103200b
63	cmpb	$16,%cl
64	ja	201632f
65	movq	%r10,-16(%rdi,%rcx)
66	movq	%r10,-8(%rdi,%rcx)
67	ret
68	ALIGN_TEXT
69101632:
70	cmpb	$16,%cl
71	jl	100816f
72201632:
73	movq	%r10,(%rdi)
74	movq	%r10,8(%rdi)
75	movq	%r10,-16(%rdi,%rcx)
76	movq	%r10,-8(%rdi,%rcx)
77	ret
78	ALIGN_TEXT
79100816:
80	cmpb	$8,%cl
81	jl	100408f
82	movq	%r10,(%rdi)
83	movq	%r10,-8(%rdi,%rcx)
84	ret
85	ALIGN_TEXT
86100408:
87	cmpb	$4,%cl
88	jl	100204f
89	movl	%r10d,(%rdi)
90	movl	%r10d,-4(%rdi,%rcx)
91	ret
92	ALIGN_TEXT
93100204:
94	cmpb	$2,%cl
95	jl	100001f
96	movw	%r10w,(%rdi)
97	movw	%r10w,-2(%rdi,%rcx)
98	ret
99	ALIGN_TEXT
100100001:
101	cmpb	$0,%cl
102	je	100000f
103	movb	%r10b,(%rdi)
104100000:
105	ret
106	ALIGN_TEXT
1071256:
108	movq	%rdi,%r9
109	movq	%r10,%rax
110	testl	$15,%edi
111	jnz	3f
1121:
113.if \erms == 1
114	rep
115	stosb
116	movq	%r9,%rax
117.else
118	movq	%rcx,%rdx
119	shrq	$3,%rcx
120	rep
121	stosq
122	movq	%r9,%rax
123	andl	$7,%edx
124	jnz	2f
125	ret
1262:
127	movq	%r10,-8(%rdi,%rdx)
128.endif
129	ret
130	ALIGN_TEXT
1313:
132	movq	%r10,(%rdi)
133	movq	%r10,8(%rdi)
134	movq	%rdi,%r8
135	andq	$15,%r8
136	leaq	-16(%rcx,%r8),%rcx
137	neg	%r8
138	leaq	16(%rdi,%r8),%rdi
139	jmp	1b
140.endm
141
142
143ENTRY(memset)
144	MEMSET erms=0
145END(memset)
146
147	.section .note.GNU-stack,"",%progbits
148