xref: /minix3/common/lib/libc/arch/x86_64/string/memset.S (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc/*	$NetBSD: memset.S,v 1.5 2014/05/22 16:47:31 pooka Exp $	*/
2b6cbf720SGianluca Guida
3b6cbf720SGianluca Guida/*-
4b6cbf720SGianluca Guida * Copyright (c) 2009 The NetBSD Foundation, Inc.
5b6cbf720SGianluca Guida * All rights reserved.
6b6cbf720SGianluca Guida *
7b6cbf720SGianluca Guida * This code is derived from software contributed to The NetBSD Foundation
8b6cbf720SGianluca Guida * by David Laight.
9b6cbf720SGianluca Guida *
10b6cbf720SGianluca Guida * Redistribution and use in source and binary forms, with or without
11b6cbf720SGianluca Guida * modification, are permitted provided that the following conditions
12b6cbf720SGianluca Guida * are met:
13b6cbf720SGianluca Guida * 1. Redistributions of source code must retain the above copyright
14b6cbf720SGianluca Guida *    notice, this list of conditions and the following disclaimer.
15b6cbf720SGianluca Guida * 2. Redistributions in binary form must reproduce the above copyright
16b6cbf720SGianluca Guida *    notice, this list of conditions and the following disclaimer in the
17b6cbf720SGianluca Guida *    documentation and/or other materials provided with the distribution.
18b6cbf720SGianluca Guida *
19b6cbf720SGianluca Guida * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b6cbf720SGianluca Guida * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b6cbf720SGianluca Guida * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b6cbf720SGianluca Guida * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b6cbf720SGianluca Guida * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b6cbf720SGianluca Guida * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b6cbf720SGianluca Guida * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b6cbf720SGianluca Guida * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b6cbf720SGianluca Guida * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b6cbf720SGianluca Guida * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b6cbf720SGianluca Guida * POSSIBILITY OF SUCH DAMAGE.
30b6cbf720SGianluca Guida */
31b6cbf720SGianluca Guida
32b6cbf720SGianluca Guida#include <machine/asm.h>
33b6cbf720SGianluca Guida
34b6cbf720SGianluca Guida#if defined(LIBC_SCCS)
35*0a6a1f1dSLionel Sambuc	RCSID("$NetBSD: memset.S,v 1.5 2014/05/22 16:47:31 pooka Exp $")
36b6cbf720SGianluca Guida#endif
37b6cbf720SGianluca Guida
38b6cbf720SGianluca Guida#ifndef _KERNEL
39b6cbf720SGianluca Guida/* bzero, %rdi is buffer, %rsi length */
40b6cbf720SGianluca Guida
41b6cbf720SGianluca GuidaENTRY(bzero)
42b6cbf720SGianluca Guida	mov	%rsi,%rdx		/* length */
43b6cbf720SGianluca Guida	xor	%eax,%eax		/* value to write */
44b6cbf720SGianluca Guida	jmp	1f
45b6cbf720SGianluca Guida#endif
46b6cbf720SGianluca Guida
47b6cbf720SGianluca Guida/* memset, %rdi is buffer, %rsi char to fill, %rdx length */
48b6cbf720SGianluca Guida
49b6cbf720SGianluca GuidaENTRY(memset)
50b6cbf720SGianluca Guida	movzbq	%sil,%rax		/* byte value to fill */
51b6cbf720SGianluca Guida	mov	%rdx,%rsi		/* copy of length */
52b6cbf720SGianluca Guida	mov	$0x0101010101010101,%r9
53b6cbf720SGianluca Guida	imul	%r9,%rax		/* fill value in all bytes */
54b6cbf720SGianluca Guida
55b6cbf720SGianluca Guida1:
56b6cbf720SGianluca Guida	mov	%rdi,%r9		/* Need to return buffer address */
57b6cbf720SGianluca Guida	or	%edi,%edx		/* address | length */
58b6cbf720SGianluca Guida	mov	%rsi,%rcx
59b6cbf720SGianluca Guida	cmp	$7,%rsi
60b6cbf720SGianluca Guida	jbe	10f			/* jump if short fill */
61b6cbf720SGianluca Guida	test	$7,%dl			/* check for misaligned fill */
62b6cbf720SGianluca Guida	jnz	20f			/* jump if misaligned */
63b6cbf720SGianluca Guida
64b6cbf720SGianluca Guida/* Target aligned and length multiple of 8 */
65b6cbf720SGianluca Guida2:
66b6cbf720SGianluca Guida	shr	$3,%rcx
67b6cbf720SGianluca Guida	rep	stosq
68b6cbf720SGianluca Guida	mov	%r9,%rax
69b6cbf720SGianluca Guida	ret
70b6cbf720SGianluca Guida
71b6cbf720SGianluca Guida/*
72b6cbf720SGianluca Guida * Short transfer, any faffing here will generate mispredicted branches.
73b6cbf720SGianluca Guida * So we keep it simple.
74b6cbf720SGianluca Guida */
75b6cbf720SGianluca Guida10:	rep	stosb
76b6cbf720SGianluca Guida	mov	%r9,%rax
77b6cbf720SGianluca Guida	ret
78b6cbf720SGianluca Guida
79b6cbf720SGianluca Guida/*
80b6cbf720SGianluca Guida * Buffer or length misaligned.
81b6cbf720SGianluca Guida * Write pattern to first and last word of buffer, then fill middle.
82b6cbf720SGianluca Guida * (This writes to some bytes more than once - possibly three times!.)
83b6cbf720SGianluca Guida */
84b6cbf720SGianluca Guida20:
85b6cbf720SGianluca Guida	mov	%rax,(%rdi)
86b6cbf720SGianluca Guida	movzbq	%dil,%rdx		/* low address for alignment */
87b6cbf720SGianluca Guida	mov	%rax,-8(%rcx,%rdi)
88b6cbf720SGianluca Guida	and	$7,%dl			/* offset in word */
89b6cbf720SGianluca Guida	sub	%rdx,%rcx		/* adjust length ... */
90b6cbf720SGianluca Guida	add	%rdx,%rdi		/* ... and target */
91b6cbf720SGianluca Guida	jmp	2b
92*0a6a1f1dSLionel SambucEND(memset)
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc#ifndef _KERNEL
95*0a6a1f1dSLionel SambucEND(bzero)
96*0a6a1f1dSLionel Sambuc#endif
97