xref: /freebsd/lib/libc/amd64/string/memset.S (revision 79ac3c12a714bcd3f2354c52d948aed9575c46d6)
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/*
35 * Note: this routine was written with kernel use in mind (read: no simd),
36 * it is only present in userspace as a temporary measure until something
37 * better gets imported.
38 */
39
40#define ALIGN_TEXT      .p2align 4,0x90 /* 16-byte alignment, nop filled */
41
42.macro MEMSET erms
43	movq	%rdi,%rax
44	movq	%rdx,%rcx
45	movzbq	%sil,%r8
46	movabs	$0x0101010101010101,%r10
47	imulq	%r8,%r10
48
49	cmpq	$32,%rcx
50	jbe	101632f
51
52	cmpq	$256,%rcx
53	ja	1256f
54
55	ALIGN_TEXT
56103200:
57	movq	%r10,(%rdi)
58	movq	%r10,8(%rdi)
59	movq	%r10,16(%rdi)
60	movq	%r10,24(%rdi)
61	leaq	32(%rdi),%rdi
62	subq	$32,%rcx
63	cmpq	$32,%rcx
64	ja	103200b
65	cmpb	$16,%cl
66	ja	201632f
67	movq	%r10,-16(%rdi,%rcx)
68	movq	%r10,-8(%rdi,%rcx)
69	ret
70	ALIGN_TEXT
71101632:
72	cmpb	$16,%cl
73	jl	100816f
74201632:
75	movq	%r10,(%rdi)
76	movq	%r10,8(%rdi)
77	movq	%r10,-16(%rdi,%rcx)
78	movq	%r10,-8(%rdi,%rcx)
79	ret
80	ALIGN_TEXT
81100816:
82	cmpb	$8,%cl
83	jl	100408f
84	movq	%r10,(%rdi)
85	movq	%r10,-8(%rdi,%rcx)
86	ret
87	ALIGN_TEXT
88100408:
89	cmpb	$4,%cl
90	jl	100204f
91	movl	%r10d,(%rdi)
92	movl	%r10d,-4(%rdi,%rcx)
93	ret
94	ALIGN_TEXT
95100204:
96	cmpb	$2,%cl
97	jl	100001f
98	movw	%r10w,(%rdi)
99	movw	%r10w,-2(%rdi,%rcx)
100	ret
101	ALIGN_TEXT
102100001:
103	cmpb	$0,%cl
104	je	100000f
105	movb	%r10b,(%rdi)
106100000:
107	ret
108	ALIGN_TEXT
1091256:
110	movq	%rdi,%r9
111	movq	%r10,%rax
112	testl	$15,%edi
113	jnz	3f
1141:
115.if \erms == 1
116	rep
117	stosb
118	movq	%r9,%rax
119.else
120	movq	%rcx,%rdx
121	shrq	$3,%rcx
122	rep
123	stosq
124	movq	%r9,%rax
125	andl	$7,%edx
126	jnz	2f
127	ret
1282:
129	movq	%r10,-8(%rdi,%rdx)
130.endif
131	ret
132	ALIGN_TEXT
1333:
134	movq	%r10,(%rdi)
135	movq	%r10,8(%rdi)
136	movq	%rdi,%r8
137	andq	$15,%r8
138	leaq	-16(%rcx,%r8),%rcx
139	neg	%r8
140	leaq	16(%rdi,%r8),%rdi
141	jmp	1b
142.endm
143
144
145ENTRY(memset)
146	MEMSET erms=0
147END(memset)
148
149	.section .note.GNU-stack,"",%progbits
150