xref: /netbsd-src/common/lib/libc/arch/powerpc/string/memcpy.S (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1/* $NetBSD: memcpy.S,v 1.2 2008/03/06 21:17:17 phx Exp $ */
2
3/* stropt/memcpy_440.S, pl_string_common, pl_linux 10/11/04 11:45:36
4 * ==========================================================================
5 * Optimized memcpy implementation for IBM PowerPC 440.
6 *
7 *  Copyright (c) 2003, IBM Corporation
8 *  All rights reserved.
9 *
10 *  Redistribution and use in source and binary forms, with or
11 *  without modification, are permitted provided that the following
12 *  conditions are met:
13 *
14 *    * Redistributions of source code must retain the above
15 *      copyright notice, this list of conditions and the following
16 *      disclaimer.
17 *    * Redistributions in binary form must reproduce the above
18 *      copyright notice, this list of conditions and the following
19 *      disclaimer in the documentation and/or other materials
20 *      provided with the distribution.
21 *    * Neither the name of IBM nor the names of its contributors
22 *      may be used to endorse or promote products derived from this
23 *      software without specific prior written permission.
24 *
25 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
30 *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 *  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
34 *  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * ==========================================================================
39 *
40 * Function: Copy n bytes of the source to the destination. Behavior is
41 *	   undefined for objects that overlap.
42 *
43 *
44 *	   void *memcpy(void * dest, const void * src, int n)
45 *
46 * Input:  r3 - destination address
47 *	 r4 - source address
48 *	 r5 - byte count
49 * Output: r3 - destination address
50 *
51 * ==========================================================================
52 */
53
54#define _NOREGNAMES
55#include <machine/asm.h>
56
57	.text
58	.align 4
59/* LINTSTUB: Func: void *memcpy(void *, const void *, size_t) */
60ENTRY(memcpy)
61	/*
62	 * Check count passed in R5. If zero, return; otherwise continue.
63	 */
64	cmpwi	%r5,0
65	beqlr-
66
67	mr	%r8, %r3		/* Copy dst (return value)	*/
68
69	addi	%r4, %r4, -4		/* Prepare for main loop's auto	*/
70	addi	%r8, %r8, -4		/* update		       */
71
72	srwi.	%r9,%r5,2		/* Word count -> r9 		*/
73	beq-	last1			/* Partial copy if <4 bytes	*/
74
75	mtctr	%r9			/* Word cnt in CTR for loop     */
76	lwzu	%r7, 4(%r4)		/* Preload for main loop	*/
77
78	b	g1
79
80g0:					/* Main loop			*/
81
82	lwzu	%r7, 4(%r4)		/* Load a new word		*/
83	stwu	%r6, 4(%r8)		/* Store previous word		*/
84
85g1:
86
87	bdz-	last			/* Dec ctr and exit loop if no  */
88					/* more words		   */
89	lwzu	%r6, 4(%r4)		/* Load another word		*/
90	stwu	%r7, 4(%r8)		/* Store previous word		*/
91	bdnz+	g0			/* Dec ctr and continue loop if */
92					/* more words		   */
93
94	mr	%r7, %r6
95
96last:
97
98	stwu	%r7, 4(%r8)		/* Store last word		*/
99
100last1:					/* Byte-by-byte copy		*/
101
102	clrlwi.	%r5,%r5,30
103	beqlr
104
105	mtctr	%r5
106
107	lbzu	%r6, 4(%r4)		/* 1st byte: update by word	*/
108	stbu	%r6, 4(%r8)
109	bdzlr-
110
111last2:
112
113	lbzu	%r6, 1(%r4)		/* Handle the rest		*/
114	stbu	%r6, 1(%r8)
115	bdnz+	last2
116
117	blr
118
119