xref: /netbsd-src/common/lib/libc/arch/i386/string/memset.S (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1/*	$NetBSD: memset.S,v 1.1 2005/12/20 19:28:49 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Laight.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The NetBSD Foundation nor the names of its
19 *    contributors may be used to endorse or promote products derived
20 *    from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <machine/asm.h>
36
37#if defined(LIBC_SCCS)
38	RCSID("$NetBSD: memset.S,v 1.1 2005/12/20 19:28:49 christos Exp $")
39#endif
40
41#ifdef BZERO
42ENTRY(bzero)
43#else
44ENTRY(memset)
45#endif
46#ifdef BZERO
47	movl	8(%esp),%ecx
48	xor	%eax,%eax
49#else
50	movl	12(%esp),%ecx
51	movzbl	8(%esp),%eax		/* unsigned char, zero extend */
52#endif
53	cmpl	$0x0f,%ecx		/* avoid mispredicted branch... */
54
55	pushl	%edi
56	movl	8(%esp),%edi
57
58	cld				/* set fill direction forward */
59
60	/*
61	 * if the string is too short, it's really not worth the overhead
62	 * of aligning to word boundries, etc.  So we jump to a plain
63	 * unaligned set.
64	 *
65	 * NB aligning the transfer is actually pointless on my athlon 700,
66	 * It does make a difference to a PII though.
67	 *
68	 * The PII, PIII and PIV all seem to have a massive performance
69	 * drop when the initial target address is an odd multiple of 4.
70	 */
71	jbe	by_bytes
72
73#ifndef BZERO
74	movb	%al,%ah			/* copy char to all bytes in word */
75	movl	%eax,%edx
76	sall	$16,%eax
77	orl	%edx,%eax
78#endif
79
80	movl	%edi,%edx		/* detect misalignment */
81	neg	%edx
82	andl	$7,%edx
83	jnz	align
84aligned:
85	movl	%eax,-4(%edi,%ecx)	/* zap last 4 bytes */
86	shrl	$2,%ecx			/* zero by words */
87	rep
88	stosl
89done:
90#ifndef BZERO
91	movl	8(%esp),%eax		/* return address of buffer */
92#endif
93	pop	%edi
94	ret
95
96align:
97	movl	%eax,(%edi)		/* zap first 8 bytes */
98	movl	%eax,4(%edi)
99	subl	%edx,%ecx		/* remove from main count */
100	add	%edx,%edi
101	jmp	aligned
102
103by_bytes:
104	rep
105	stosb
106
107#ifndef BZERO
108	movl	8(%esp),%eax		/* return address of buffer */
109#endif
110	popl	%edi
111	ret
112