126234Swpaul/* $OpenBSD: memmove.S,v 1.5 2022/06/10 01:56:02 guenther Exp $ */
229735Scharnier/* $NetBSD: memmove.S,v 1.3 2011/01/15 07:31:12 matt Exp $ */
326234Swpaul
426234Swpaul/* stropt/memmove.S, pl_string_common, pl_linux 10/11/04 11:45:37
550479Speter * ==========================================================================
626234Swpaul * Optimized memmove implementation for IBM PowerPC 405/440.
729735Scharnier *
829735Scharnier *	Copyright (c) 2003, IBM Corporation
929735Scharnier *	All rights reserved.
1029735Scharnier *
1129735Scharnier *	Redistribution and use in source and binary forms, with or
1229735Scharnier *	without modification, are permitted provided that the following
1329735Scharnier *	conditions are met:
1468965Sru *
1529735Scharnier *	* Redistributions of source code must retain the above
1629735Scharnier *	copyright notice, this list of conditions and the following
1729735Scharnier *	disclaimer.
1829735Scharnier *	* Redistributions in binary form must reproduce the above
1929735Scharnier *	copyright notice, this list of conditions and the following
2029735Scharnier *	disclaimer in the documentation and/or other materials
2199968Scharnier *	provided with the distribution.
2299968Scharnier *	* Neither the name of IBM nor the names of its contributors
2399968Scharnier *	may be used to endorse or promote products derived from this
2426234Swpaul *	software without specific prior written permission.
2526234Swpaul *
2626234Swpaul *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
2799968Scharnier *	CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
2899968Scharnier *	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2929735Scharnier *	MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3026234Swpaul *	DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
3129735Scharnier *	BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3226234Swpaul *	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3326234Swpaul *	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3479755Sdd *	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
3529735Scharnier *	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3679755Sdd *	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37101828Sru *	USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3879755Sdd *
3979755Sdd * ==========================================================================
4026234Swpaul */
4129735Scharnier
4229735Scharnier#include "DEFS.h"
4329735Scharnier
4429735Scharnier        .text
4526234Swpaul
4629735Scharnier/* void *memcpy(void *to, const void *from, size_t len) */
4729735Scharnier#if 0
4879755SddENTRY(memcpy)
49101828Sru	RETGUARD_SETUP(memmove, %r11, %r12)
5029735Scharnier	mr	%r8, %r3		/* Save dst (return value)	*/
5126234Swpaul	b	.Lfwd
5229735Scharnier#endif
5326234Swpaul
5429735Scharnier/* void bcopy(void *, void *, size_t) */
5526234SwpaulENTRY_NB(bcopy)
5626234Swpaul	mr	%r6, %r3		/* swap src/dst */
5729735Scharnier	mr	%r3, %r4
5826234Swpaul	mr	%r4, %r6
5929735Scharnier
6026234Swpaul/* void *memmove(void *, const void *, size_t) */
6126234SwpaulENTRY(memmove)
6229735Scharnier	RETGUARD_SETUP(memmove, %r11, %r12)
6326234Swpaul	mr	%r8, %r3		/* Save dst (return value)	*/
6429735Scharnier
6529735Scharnier	cmpw	%r4, %r8		/* Branch to reverse if 	*/
6629735Scharnier	blt	.Lreverse		/* src < dest. Don't want to	*/
6729735Scharnier					/* overwrite end of src with	*/
6829735Scharnier					/* start of dest 		*/
6929735Scharnier
7029735Scharnier.Lfwd:
7129735Scharnier	addi	%r4, %r4, -4		/* Back up src and dst pointers */
7229735Scharnier	addi	%r8, %r8, -4		/* due to auto-update of 'load' */
7399968Scharnier
7429735Scharnier	srwi.	%r9,%r5,2		/* How many words in total cnt	*/
7571898Sru	beq-	.Llast1			/* Handle byte by byte if < 4	*/
7629735Scharnier					/* bytes total 			*/
7729735Scharnier	mtctr	%r9			/* Count of words for loop	*/
7829735Scharnier	lwzu	%r7, 4(%r4)		/* Preload first word		*/
7929735Scharnier
8029735Scharnier	b	.Lg1
81
82.Lg0:					/* Main loop			*/
83
84	lwzu	%r7, 4(%r4)		/* Load a new word		*/
85	stwu	%r6, 4(%r8)		/* Store previous word		*/
86
87.Lg1:
88
89	bdz-	.Llast			/* Dec cnt, and branch if just	*/
90					/* one word to store		*/
91	lwzu	%r6, 4(%r4)		/* Load another word		*/
92	stwu	%r7, 4(%r8)		/* Store previous word		*/
93	bdnz+	.Lg0			/* Dec cnt, and loop again if	*/
94					/* more words			*/
95	mr	%r7, %r6		/* If word count -> 0, then...	*/
96
97.Llast:
98
99	stwu	%r7, 4(%r8)		/* ... store last word		*/
100
101.Llast1:				/* Byte-by-byte copy		*/
102
103	clrlwi.	%r5,%r5,30		/* If count -> 0, then ...	*/
104	beq	.Ldone			/* we're done			*/
105
106	mtctr	%r5			/* else load count for loop	*/
107
108	lbzu	%r6, 4(%r4)		/* 1st byte: update addr by 4	*/
109	stbu	%r6, 4(%r8)		/* since we pre-adjusted by 4	*/
110	bdz-	.Ldone			/* in anticipation of main loop */
111
112.Llast2:
113
114	lbzu	%r6, 1(%r4)		/* But handle the rest by	*/
115	stbu	%r6, 1(%r8)		/* updating addr by 1		*/
116	bdnz+	.Llast2
117	b	.Ldone
118
119	/* We're here since src < dest. Don't want to overwrite end of	*/
120	/* src with start of dest					*/
121
122.Lreverse:
123
124	add	%r4, %r4, %r5		/* Work from end to beginning	*/
125	add	%r8, %r8, %r5 		/* so add count to string ptrs	*/
126	srwi.	%r9,%r5,2		/* Words in total count		*/
127	beq-	.Lrlast1		/* Handle byte by byte if < 4	*/
128					/* bytes total 			*/
129
130	mtctr	%r9			/* Count of words for loop 	*/
131
132	lwzu	%r7, -4(%r4)		/* Preload first word		*/
133	b	.Lrg1
134
135.Lrg0:					/* Main loop			*/
136
137	lwzu	%r7, -4(%r4)		/* Load a new word		*/
138	stwu	%r6, -4(%r8)		/* Store previous word		*/
139
140.Lrg1:
141
142	bdz-	.Lrlast			/* Dec cnt, and branch if just	*/
143					/* one word to store		*/
144
145	lwzu	%r6, -4(%r4)		/* Load another word		*/
146	stwu	%r7, -4(%r8)		/* Store previous word		*/
147
148	bdnz+	.Lrg0			/* Dec cnt, and loop again if	*/
149					/* more words			*/
150
151	mr	%r7, %r6		/* If word count -> 0, then...	*/
152
153.Lrlast:
154
155	stwu	%r7, -4(%r8)		/* ... store last word		*/
156
157.Lrlast1:				/* Byte-by-byte copy		*/
158
159	clrlwi.	%r5,%r5,30		/* If count -> 0, then...	*/
160	beq	.Ldone			/* ... we're done 		*/
161
162	mtctr	%r5			/* else load count for loop 	*/
163
164.Lrlast2:
165
166	lbzu	%r6, -1(%r4)		/* Handle the rest, byte by 	*/
167	stbu	%r6, -1(%r8)		/* byte				*/
168
169	bdnz+	.Lrlast2	 	/* Dec ctr, and branch if more	*/
170					/* bytes left			*/
171.Ldone:
172	RETGUARD_CHECK(memmove, %r11, %r12)
173	blr
174END_STRONG(memmove)
175END_WEAK(bcopy)
176