1/*        $NetBSD: memset.S,v 1.5 2014/05/23 03:17:31 uebayasi 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <machine/asm.h>
33
34#if defined(LIBC_SCCS)
35          RCSID("$NetBSD: memset.S,v 1.5 2014/05/23 03:17:31 uebayasi Exp $")
36#endif
37
38#ifdef BZERO
39ENTRY(bzero)
40#else
41ENTRY(memset)
42#endif
43#ifdef BZERO
44          movl      8(%esp),%ecx
45          xor       %eax,%eax
46#else
47          movl      12(%esp),%ecx
48          movzbl    8(%esp),%eax                  /* unsigned char, zero extend */
49#endif
50          cmpl      $0x0f,%ecx                    /* avoid mispredicted branch... */
51
52          pushl     %edi
53          movl      8(%esp),%edi
54
55          /*
56           * if the string is too short, it's really not worth the overhead
57           * of aligning to word boundries, etc.  So we jump to a plain
58           * unaligned set.
59           *
60           * NB aligning the transfer is actually pointless on my athlon 700,
61           * It does make a difference to a PII though.
62           *
63           * The PII, PIII and PIV all seem to have a massive performance
64           * drop when the initial target address is an odd multiple of 4.
65           */
66          jbe       .Lby_bytes
67
68#ifndef BZERO
69          movb      %al,%ah                       /* copy char to all bytes in word */
70          movl      %eax,%edx
71          sall      $16,%eax
72          orl       %edx,%eax
73#endif
74
75          movl      %edi,%edx           /* detect misalignment */
76          neg       %edx
77          andl      $7,%edx
78          jnz       .Lalign
79.Laligned:
80          movl      %eax,-4(%edi,%ecx)  /* zap last 4 bytes */
81          shrl      $2,%ecx                       /* zero by words */
82          rep
83          stosl
84.Ldone:
85#ifndef BZERO
86          movl      8(%esp),%eax                  /* return address of buffer */
87#endif
88          pop       %edi
89          ret
90
91.Lalign:
92          movl      %eax,(%edi)                   /* zap first 8 bytes */
93          movl      %eax,4(%edi)
94          subl      %edx,%ecx           /* remove from main count */
95          add       %edx,%edi
96          jmp       .Laligned
97
98.Lby_bytes:
99          rep
100          stosb
101
102#ifndef BZERO
103          movl      8(%esp),%eax                  /* return address of buffer */
104#endif
105          popl      %edi
106          ret
107#ifdef BZERO
108END(bzero)
109#else
110END(memset)
111#endif
112