1/*        $NetBSD: memcpy.S,v 1.4 2014/03/22 19:38:46 jakllsch Exp $  */
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from locore.s.
8 * Optimised by David Laight 2003
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 University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <machine/asm.h>
36
37#if defined(LIBC_SCCS)
38          RCSID("$NetBSD: memcpy.S,v 1.4 2014/03/22 19:38:46 jakllsch Exp $")
39#endif
40
41          /*
42           * (ov)bcopy (src,dst,cnt)
43           *  ws@tools.de     (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
44           */
45
46#ifdef BCOPY
47ENTRY(bcopy)
48#else
49#ifdef MEMMOVE
50ENTRY(memmove)
51#else
52#define MEMCPY
53#define NO_OVERLAP
54ENTRY(memcpy)
55#endif
56#endif
57          push      %esi
58          mov       %edi,%edx
59#if defined(MEMCPY) || defined(MEMMOVE)
60          movl      8(%esp),%edi
61          movl      12(%esp),%esi
62#else
63          movl      8(%esp),%esi
64          movl      12(%esp),%edi
65#endif
66          movl      16(%esp),%ecx
67#if defined(NO_OVERLAP)
68          movl      %ecx,%eax
69#else
70          movl      %edi,%eax
71          subl      %esi,%eax
72          cmpl      %ecx,%eax /* overlapping? */
73          movl      %ecx,%eax
74          jb        .Lbackwards
75#endif
76          /* nope, copy forwards. */
77          shrl      $2,%ecx             /* copy by words */
78          rep
79          movsl
80          and       $3,%eax             /* any bytes left? */
81          jnz       .Ltrailing
82.Ldone:
83#if defined(MEMCPY) || defined(MEMMOVE)
84          movl      8(%esp),%eax
85#endif
86          mov       %edx,%edi
87          pop       %esi
88          ret
89
90.Ltrailing:
91          cmp       $2,%eax
92          jb        1f
93          movw      (%esi),%ax
94          movw      %ax,(%edi)
95          je        .Ldone
96          movb      2(%esi),%al
97          movb      %al,2(%edi)
98          jmp       .Ldone
991:        movb      (%esi),%al
100          movb      %al,(%edi)
101          jmp       .Ldone
102
103#if !defined(NO_OVERLAP)
104.Lbackwards:
105          addl      %ecx,%edi /* copy backwards. */
106          addl      %ecx,%esi
107          and       $3,%eax             /* any fractional bytes? */
108          jnz       .Lback_align
109.Lback_aligned:
110          shrl      $2,%ecx
111          subl      $4,%esi
112          subl      $4,%edi
113          std
114          rep
115          movsl
116          cld
117          jmp       .Ldone
118
119.Lback_align:
120          sub       %eax,%esi
121          sub       %eax,%edi
122          cmp       $2,%eax
123          jb        1f
124          je        2f
125          movb      2(%esi),%al
126          movb      %al,2(%edi)
1272:        movw      (%esi),%ax
128          movw      %ax,(%edi)
129          jmp       .Lback_aligned
1301:        movb      (%esi),%al
131          movb      %al,(%edi)
132          jmp       .Lback_aligned
133#endif
134
135#ifdef BCOPY
136END(bcopy)
137#else
138#ifdef MEMMOVE
139END(memmove)
140#else
141END(memcpy)
142#endif
143#endif
144