159191Skris/*
259191Skris * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
359191Skris *
459191Skris * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5109998Smarkm *
6109998Smarkm * This file contains Original Code and/or Modifications of Original Code
759191Skris * as defined in and that are subject to the Apple Public Source License
859191Skris * Version 2.0 (the 'License'). You may not use this file except in
959191Skris * compliance with the License. The rights granted to you under the License
1068651Skris * may not be used to create, or enable the creation or redistribution of,
11109998Smarkm * unlawful or unlicensed copies of an Apple operating system, or to
1259191Skris * circumvent, violate, or enable the circumvention or violation of, any
13109998Smarkm * terms of an Apple operating system software license agreement.
1459191Skris *
15109998Smarkm * Please obtain a copy of the License at
1659191Skris * http://www.opensource.apple.com/apsl/ and read it before using this file.
17109998Smarkm *
1859191Skris * The Original Code and all software distributed under the License are
19109998Smarkm * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
2059191Skris * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
2159191Skris * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2259191Skris * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
2359191Skris * Please see the License for the specific language governing rights and
2459191Skris * limitations under the License.
2559191Skris *
2659191Skris * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27109998Smarkm */
28109998Smarkm/*
29109998Smarkm * @OSF_COPYRIGHT@
3059191Skris */
31109998Smarkm/*
32109998Smarkm * Mach Operating System
3359191Skris * Copyright (c) 1991,1990 Carnegie Mellon University
34109998Smarkm * All Rights Reserved.
35109998Smarkm *
36109998Smarkm * Permission to use, copy, modify and distribute this software and its
3759191Skris * documentation is hereby granted, provided that both the copyright
3859191Skris * notice and this permission notice appear in all copies of the
39194206Ssimon * software, derivative works or modified versions, and any portions
40109998Smarkm * thereof, and that both notices appear in supporting documentation.
41109998Smarkm *
4259191Skris * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43109998Smarkm * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44109998Smarkm * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45109998Smarkm *
46109998Smarkm * Carnegie Mellon requests users of this software to return to
47109998Smarkm *
48109998Smarkm *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
49109998Smarkm *  School of Computer Science
50109998Smarkm *  Carnegie Mellon University
5159191Skris *  Pittsburgh PA 15213-3890
52109998Smarkm *
53109998Smarkm * any improvements or extensions that they make and grant Carnegie Mellon
54109998Smarkm * the rights to redistribute these changes.
55109998Smarkm */
5659191Skris/*
5759191Skris */
5859191Skris
5959191Skris#include <i386/asm.h>
6059191Skris
6159191Skris/* void *memcpy((void *) to, (const void *) from, (size_t) bcount) */
6259191Skris/*			rdi,	 	     rsi,	      rdx   */
6359191Skris/*
6459191Skris * Note: memcpy does not support overlapping copies
6559191Skris */
6659191SkrisENTRY(memcpy)
6759191Skris	movq	%rdx,%rcx
6859191Skris	shrq	$3,%rcx				/* copy by 64-bit words */
6959191Skris	cld					/* copy forwards */
7059191Skris	rep
7159191Skris	movsq
7259191Skris	movq	%rdx,%rcx
7359191Skris	andq	$7,%rcx				/* any bytes left? */
7459191Skris	rep
7559191Skris	movsb
7659191Skris	ret
7759191Skris
7859191Skris/* void bcopy((const char *) from, (char *) to, (unsigned int) count) */
7959191Skris/*			      rdi, 	   rsi,			 rdx  */
8059191Skris
8159191SkrisENTRY(bcopy_no_overwrite)
8259191Skris	xchgq	%rsi,%rdi
8359191Skris	jmp	EXT(memcpy)
8459191Skris
8559191Skris/*
8659191Skris * bcopy(src, dst, cnt)
8759191Skris *       rdi, rsi, rdx
8859191Skris *  ws@tools.de     (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
8959191Skris */
9059191SkrisENTRY(bcopy)
9159191Skris	xchgq	%rsi,%rdi
9259191Skris	movq	%rdx,%rcx
9359191Skris
9459191Skris	movq	%rdi,%rax
9559191Skris	subq	%rsi,%rax
9659191Skris	cmpq	%rcx,%rax			/* overlapping && src < dst? */
9759191Skris	jb	1f
9859191Skris
9959191Skris	shrq	$3,%rcx				/* copy by 64-bit words */
10059191Skris	cld					/* nope, copy forwards */
101109998Smarkm	rep
102109998Smarkm	movsq
10359191Skris	movq	%rdx,%rcx
10459191Skris	andq	$7,%rcx				/* any bytes left? */
10559191Skris	rep
106109998Smarkm	movsb
107109998Smarkm	ret
108109998Smarkm
10959191Skris	/* ALIGN_TEXT */
110109998Smarkm1:
11159191Skris	addq	%rcx,%rdi			/* copy backwards */
112109998Smarkm	addq	%rcx,%rsi
11359191Skris	decq	%rdi
114109998Smarkm	decq	%rsi
115109998Smarkm	andq	$7,%rcx				/* any fractional bytes? */
116109998Smarkm	std
117109998Smarkm	rep
118109998Smarkm	movsb
119109998Smarkm	movq	%rdx,%rcx			/* copy remainder by 32-bit words */
120109998Smarkm	shrq	$3,%rcx
121109998Smarkm	subq	$7,%rsi
122109998Smarkm	subq	$7,%rdi
123109998Smarkm	rep
12459191Skris	movsq
12559191Skris	cld
12659191Skris	ret
12759191Skris