1 /* $NetBSD: memmove.S,v 1.1 2014/09/03 19:34:25 matt Exp $ */
3 /* stropt/memmove.S, pl_string_common, pl_linux 10/11/04 11:45:37
4 * ==========================================================================
5 * Optimized memmove implementation for IBM PowerPC 405/440.
7 * Copyright (c) 2003, IBM Corporation
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * * Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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.
38 * ==========================================================================
40 * Function: Move memory area (handles overlapping regions)
42 * void *memmove(void * dest, const void * src, int n)
44 * Input: r3 - destination address
47 * Output: r11 - destination address
49 * ==========================================================================
52 #include <machine/asm.h>
55 /* bcopy = memcpy/memmove with arguments reversed. */
56 /* LINTSTUB: Func: void bcopy(void *, void *, size_t) */
58 l.or r6, r3, r0 /* swap src/dst */
62 /* LINTSTUB: Func: void *memmove(void *, const void *, size_t) */
66 l.or r11, r3, r0 /* Save dst (return value) */
68 l.sfges r4, r3 /* Branch to reverse if */
69 l.bnf .Lreverse /* src < dest. Don't want to */
70 /* overwrite end of src with */
73 l.addi r4, r4, -4 /* Back up src and dst pointers */
74 l.addi r3, r3, -4 /* due to auto-update of 'load' */
76 l.srli r13, r5, 2 /* How many words in total cnt */
78 l.bf .Llast1 /* Handle byte by byte if < 4 */
80 l.lwz r7, 4(r4) /* Preload first word */
88 l.lwz r7, 4(r4) /* Load a new word */
89 l.sw 4(r3), r6 /* Store previous word */
90 l.addi r4, r4, 4 /* advance */
91 l.addi r3, r3, 4 /* advance */
97 l.bf .Llast /* Dec cnt, and branch if just */
99 /* one word to store */
100 l.lwz r6, 4(r4) /* Load another word */
101 l.sw 4(r3), r7 /* Store previous word */
102 l.addi r4, r4, 4 /* advance to next word */
103 l.addi r3, r3, 4 /* advance to next word */
104 l.addi r13, r13, -1 /* Decrement count */
105 l.sfeqi r13, 0 /* last word? */
106 l.bnf .Lg0 /* no, loop, more words */
109 l.or r7, r6, r0 /* If word count -> 0, then... */
113 l.sw 4(r3), r7 /* ... store last word */
116 .Llast1: /* Byte-by-byte copy */
118 l.andi r5, r5, 3 /* get remaining byte count */
119 l.sfeqi r5, 0 /* is it 0? */
120 l.bf .Ldone /* yes, we're done */
121 l.nop /* -- delay slot -- */
123 l.lbz r6, 4(r4) /* 1st byte: update addr by 4 */
124 l.sb 4(r3), r6 /* since we pre-adjusted by 4 */
125 l.addi r4, r4, 4 /* advance to next word */
126 l.addi r3, r3, 4 /* advance to next word */
127 l.addi r5, r5, -1 /* decrement count */
128 l.sfeqi r5, 0 /* is it 0? */
129 l.bf .Ldone /* yes, we're done */
130 l.nop /* -- delay slot -- */
134 l.lbz r6, 1(r4) /* But handle the rest by */
135 l.sb 1(r3), r6 /* updating addr by 1 */
136 l.addi r4, r4, 1 /* advance to next word */
137 l.addi r3, r3, 1 /* advance to next word */
138 l.addi r5, r5, -1 /* decrement count */
139 l.sfeqi r5, 0 /* is it 0? */
140 l.bnf .Llast2 /* yes, we're done */
141 l.nop /* -- delay slot -- */
144 l.nop /* -- delay slot -- */
146 /* We're here since src < dest. Don't want to overwrite end of */
147 /* src with start of dest */
151 l.add r4, r4, r5 /* Work from end to beginning */
152 l.add r3, r3, r5 /* so add count to string ptrs */
153 l.srli r13, r5, 2 /* Words in total count */
155 l.bf .Lrlast1 /* Handle byte by byte if < 4 */
159 l.lwz r7, -4(r4) /* Preload first word */
160 l.addi r4, r4, -4 /* update pointer */
164 .Lrg0: /* Main loop */
166 l.lwz r7, -4(r4) /* Load a new word */
167 l.sw -4(r3), r6 /* Store previous word */
173 l.addi r13, r13, -1 /* decrement count */
174 l.sfeqi r13, 0 /* just one pending word left? */
175 l.bf .Lrlast /* yes, deal with it */
177 l.lwz r6, -4(r4) /* Load another word */
178 l.sw -4(r3), r7 /* Store previous word */
182 l.addi r13, r13, -1 /* decrement count */
183 l.sfeqi r13, 0 /* just one pending word left? */
184 l.bnf .Lrg0 /* no, loop again more words */
187 l.or r7, r6, r0 /* If word count -> 0, then... */
191 l.sw -4(r3), r7 /* ... store last word */
192 l.addi r3, r3, -4 /* update pointer */
194 .Lrlast1: /* Byte-by-byte copy */
202 l.lbz r6, -1(r4) /* Handle the rest, byte by */
203 l.sb -1(r3), r6 /* byte */
206 l.addi r5, r5, -1 /* decrement count */
207 l.sfeqi r5, 0 /* is it 0? */
208 l.bnf .Lrlast2 /* no, loop again */