xref: /openbsd-src/lib/libc/arch/amd64/string/memset.S (revision 631951aab24dc7dce7719dfa2487185ea5ef120f)
15b859c19Sderaadt/*
25b859c19Sderaadt * Written by J.T. Conklin <jtc@netbsd.org>.
35b859c19Sderaadt * Public domain.
45b859c19Sderaadt * Adapted for NetBSD/x86_64 by Frank van der Linden <fvdl@wasabisystems.com>
55b859c19Sderaadt */
65b859c19Sderaadt
7b4551fdeSguenther#include "DEFS.h"
85b859c19Sderaadt
95b859c19SderaadtENTRY(memset)
10*631951aaSmortimer	RETGUARD_SETUP(memset, r10)
115b859c19Sderaadt	movq	%rsi,%rax
125b859c19Sderaadt	andq	$0xff,%rax
135b859c19Sderaadt	movq	%rdx,%rcx
145b859c19Sderaadt	movq	%rdi,%r11
155b859c19Sderaadt
165b859c19Sderaadt	cld				/* set fill direction forward */
175b859c19Sderaadt
185b859c19Sderaadt	/*
195b859c19Sderaadt	 * if the string is too short, it's really not worth the overhead
205b859c19Sderaadt	 * of aligning to word boundaries, etc.  So we jump to a plain
215b859c19Sderaadt	 * unaligned set.
225b859c19Sderaadt	 */
235b859c19Sderaadt	cmpq	$0x0f,%rcx
245b859c19Sderaadt	jle	L1
255b859c19Sderaadt
265b859c19Sderaadt	movb	%al,%ah			/* copy char to all bytes in word */
275b859c19Sderaadt	movl	%eax,%edx
285b859c19Sderaadt	sall	$16,%eax
295b859c19Sderaadt	orl	%edx,%eax
305b859c19Sderaadt
315b859c19Sderaadt	movl	%eax,%edx
325b859c19Sderaadt	salq	$32,%rax
335b859c19Sderaadt	orq	%rdx,%rax
345b859c19Sderaadt
355b859c19Sderaadt	movq	%rdi,%rdx		/* compute misalignment */
365b859c19Sderaadt	negq	%rdx
375b859c19Sderaadt	andq	$7,%rdx
385b859c19Sderaadt	movq	%rcx,%r8
395b859c19Sderaadt	subq	%rdx,%r8
405b859c19Sderaadt
415b859c19Sderaadt	movq	%rdx,%rcx		/* set until word aligned */
425b859c19Sderaadt	rep
435b859c19Sderaadt	stosb
445b859c19Sderaadt
455b859c19Sderaadt	movq	%r8,%rcx
465b859c19Sderaadt	shrq	$3,%rcx			/* set by words */
475b859c19Sderaadt	rep
485b859c19Sderaadt	stosq
495b859c19Sderaadt
505b859c19Sderaadt	movq	%r8,%rcx		/* set remainder by bytes */
515b859c19Sderaadt	andq	$7,%rcx
525b859c19SderaadtL1:	rep
535b859c19Sderaadt	stosb
545b859c19Sderaadt	movq	%r11,%rax
555b859c19Sderaadt
56*631951aaSmortimer	RETGUARD_CHECK(memset, r10)
575b859c19Sderaadt	ret
58ea6088e7SguentherEND_BUILTIN(memset)
59