xref: /netbsd-src/common/lib/libc/arch/i386/string/memset.S (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1/*	$NetBSD: memset.S,v 1.3 2007/11/12 18:41:59 ad 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.3 2007/11/12 18:41:59 ad 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	/*
59	 * if the string is too short, it's really not worth the overhead
60	 * of aligning to word boundries, etc.  So we jump to a plain
61	 * unaligned set.
62	 *
63	 * NB aligning the transfer is actually pointless on my athlon 700,
64	 * It does make a difference to a PII though.
65	 *
66	 * The PII, PIII and PIV all seem to have a massive performance
67	 * drop when the initial target address is an odd multiple of 4.
68	 */
69	jbe	.Lby_bytes
70
71#ifndef BZERO
72	movb	%al,%ah			/* copy char to all bytes in word */
73	movl	%eax,%edx
74	sall	$16,%eax
75	orl	%edx,%eax
76#endif
77
78	movl	%edi,%edx		/* detect misalignment */
79	neg	%edx
80	andl	$7,%edx
81	jnz	.Lalign
82.Laligned:
83	movl	%eax,-4(%edi,%ecx)	/* zap last 4 bytes */
84	shrl	$2,%ecx			/* zero by words */
85	rep
86	stosl
87.Ldone:
88#ifndef BZERO
89	movl	8(%esp),%eax		/* return address of buffer */
90#endif
91	pop	%edi
92	ret
93
94.Lalign:
95	movl	%eax,(%edi)		/* zap first 8 bytes */
96	movl	%eax,4(%edi)
97	subl	%edx,%ecx		/* remove from main count */
98	add	%edx,%edi
99	jmp	.Laligned
100
101.Lby_bytes:
102	rep
103	stosb
104
105#ifndef BZERO
106	movl	8(%esp),%eax		/* return address of buffer */
107#endif
108	popl	%edi
109	ret
110