]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/bcopy.s
e55a9b04943105b8930eaed3cef417eba475ab40
[apple/xnu.git] / osfmk / i386 / bcopy.s
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 /*
59 */
60
61 #include <i386/asm.h>
62
63 /* void *memcpy((void *) to, (const void *) from, (size_t) bcount) */
64
65 ENTRY(memcpy)
66 pushl %edi
67 pushl %esi
68 movl 8+ 4(%esp),%edi /* to */
69 movl %edi,%eax /* returns its first argument */
70 movl 8+ 8(%esp),%esi /* from */
71 memcpy_common:
72 movl 8+ 12(%esp),%edx /* number of bytes */
73 cld
74 /* move longs*/
75 movl %edx,%ecx
76 sarl $2,%ecx
77 rep
78 movsl
79 /* move bytes*/
80 movl %edx,%ecx
81 andl $3,%ecx
82 rep
83 movsb
84 popl %esi
85 popl %edi
86 ret
87
88 /* void bcopy((const char *) from, (char *) to, (unsigned int) count) */
89
90 ENTRY(bcopy_no_overwrite)
91 pushl %edi
92 pushl %esi
93 movl 8+ 8(%esp),%edi /* to */
94 movl 8+ 4(%esp),%esi /* from */
95 jmp memcpy_common
96
97 /* bcopy16(from, to, bcount) using word moves */
98
99 ENTRY(bcopy16)
100 pushl %edi
101 pushl %esi
102 movl 8+12(%esp),%edx /* 8 for the two pushes above */
103 movl 8+ 8(%esp),%edi
104 movl 8+ 4(%esp),%esi
105 /* move words */
106 0: cld
107 movl %edx,%ecx
108 sarl $1,%ecx
109 rep
110 movsw
111 /* move bytes */
112 movl %edx,%ecx
113 andl $1,%ecx
114 rep
115 movsb
116 popl %esi
117 popl %edi
118 ret
119
120
121 /*
122 * Based on NetBSD's bcopy.S from their libc.
123 * bcopy(src, dst, cnt)
124 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
125 */
126 ENTRY(bcopy)
127 pushl %esi
128 pushl %edi
129 movl 12(%esp),%esi
130 movl 16(%esp),%edi
131 movl 20(%esp),%ecx
132
133 movl %edi,%edx
134 subl %esi,%edx
135 cmpl %ecx,%edx /* overlapping && src < dst? */
136 movl %ecx,%edx
137 jb 1f
138
139 shrl $2,%ecx /* copy by 32-bit words */
140 cld /* nope, copy forwards */
141 rep
142 movsl
143 movl %edx,%ecx
144 andl $3,%ecx /* any bytes left? */
145 rep
146 movsb
147 popl %edi
148 popl %esi
149 ret
150
151
152 1:
153 addl %ecx,%edi /* copy backwards */
154 addl %ecx,%esi
155 decl %edi
156 decl %esi
157 andl $3,%ecx /* any fractional bytes? */
158 std
159 rep
160 movsb
161 movl %edx,%ecx /* copy remainder by 32-bit words */
162 shrl $2,%ecx
163 subl $3,%esi
164 subl $3,%edi
165 rep
166 movsl
167 popl %edi
168 popl %esi
169 cld
170 ret