xref: /netbsd-src/common/lib/libc/arch/powerpc/string/memcpy.S (revision a00f9ab046124bbf3081dffae806f57bb67807b9)
1/* $NetBSD: memcpy.S,v 1.4 2014/03/03 15:30:31 macallan 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#include <machine/asm.h>
55#ifdef _KERNEL
56#include "opt_ppcarch.h"
57#endif
58
59	.text
60	.align 4
61/* LINTSTUB: Func: void *memcpy(void *, const void *, size_t) */
62ENTRY(memcpy)
63	/*
64	 * Check count passed in R5. If zero, return; otherwise continue.
65	 */
66	cmpwi	%r5,0
67	beqlr-
68
69#if defined(_KERNEL) && defined(PPC_OEA601)
70	/*
71	* 601 will generate alignment exceptions if operand crosses
72	 * 4k page boundary, so do byte copy when exception handler
73	 * not available.  Maybe want to have a different memcpy for 601
74	 * that checks for page boundaries/word alignment...
75	 */
76	mfspr   %r6, 287		/* mfpvbr %r6 PVR = 287       */
77	srwi    %r6, %r6, 0x10		/* get version field from PVR */
78	cmpwi   %r6, 0x1		/* 601 CPU = 0x0001           */
79	bne     bnorm			/* skip byte-only unless 601  */
80
81bcpy:
82	mtctr   %r5			/* byte copy everything */
83	li      %r6, 0
84bloop:
85	lbzx    %r7, %r4, %r6
86	stbx    %r7, %r3, %r6
87	addi    %r6, %r6, 1
88	bdnz    bloop
89	blr
90
91bnorm:
92
93#endif
94
95	mr	%r8, %r3		/* Copy dst (return value)	*/
96
97	addi	%r4, %r4, -4		/* Prepare for main loop's auto	*/
98	addi	%r8, %r8, -4		/* update		       */
99
100	srwi.	%r9,%r5,2		/* Word count -> r9 		*/
101	beq-	last1			/* Partial copy if <4 bytes	*/
102
103	mtctr	%r9			/* Word cnt in CTR for loop     */
104	lwzu	%r7, 4(%r4)		/* Preload for main loop	*/
105
106	b	g1
107
108g0:					/* Main loop			*/
109
110	lwzu	%r7, 4(%r4)		/* Load a new word		*/
111	stwu	%r6, 4(%r8)		/* Store previous word		*/
112
113g1:
114
115	bdz-	last			/* Dec ctr and exit loop if no  */
116					/* more words		   */
117	lwzu	%r6, 4(%r4)		/* Load another word		*/
118	stwu	%r7, 4(%r8)		/* Store previous word		*/
119	bdnz+	g0			/* Dec ctr and continue loop if */
120					/* more words		   */
121
122	mr	%r7, %r6
123
124last:
125
126	stwu	%r7, 4(%r8)		/* Store last word		*/
127
128last1:					/* Byte-by-byte copy		*/
129
130	clrlwi.	%r5,%r5,30
131	beqlr
132
133	mtctr	%r5
134
135	lbzu	%r6, 4(%r4)		/* 1st byte: update by word	*/
136	stbu	%r6, 4(%r8)
137	bdzlr-
138
139last2:
140
141	lbzu	%r6, 1(%r4)		/* Handle the rest		*/
142	stbu	%r6, 1(%r8)
143	bdnz+	last2
144
145	blr
146END(memcpy)
147