1/*
2 * Written by J.T. Conklin <jtc@netbsd.org>.
3 * Public domain.
4 * Adapted for NetBSD/x86_64 by Frank van der Linden <fvdl@wasabisystems.com>
5 */
6
7#include "DEFS.h"
8
9ENTRY(memset)
10	RETGUARD_SETUP(memset, r10)
11	movq	%rsi,%rax
12	andq	$0xff,%rax
13	movq	%rdx,%rcx
14	movq	%rdi,%r11
15
16	cld				/* set fill direction forward */
17
18	/*
19	 * if the string is too short, it's really not worth the overhead
20	 * of aligning to word boundaries, etc.  So we jump to a plain
21	 * unaligned set.
22	 */
23	cmpq	$0x0f,%rcx
24	jle	L1
25
26	movb	%al,%ah			/* copy char to all bytes in word */
27	movl	%eax,%edx
28	sall	$16,%eax
29	orl	%edx,%eax
30
31	movl	%eax,%edx
32	salq	$32,%rax
33	orq	%rdx,%rax
34
35	movq	%rdi,%rdx		/* compute misalignment */
36	negq	%rdx
37	andq	$7,%rdx
38	movq	%rcx,%r8
39	subq	%rdx,%r8
40
41	movq	%rdx,%rcx		/* set until word aligned */
42	rep
43	stosb
44
45	movq	%r8,%rcx
46	shrq	$3,%rcx			/* set by words */
47	rep
48	stosq
49
50	movq	%r8,%rcx		/* set remainder by bytes */
51	andq	$7,%rcx
52L1:	rep
53	stosb
54	movq	%r11,%rax
55
56	RETGUARD_CHECK(memset, r10)
57	ret
58END_BUILTIN(memset)
59