1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/* $NetBSD: memmove.S,v 1.1 2014/09/03 19:34:25 matt Exp $ */
/* stropt/memmove.S, pl_string_common, pl_linux 10/11/04 11:45:37
* ==========================================================================
* Optimized memmove implementation for IBM PowerPC 405/440.
*
* Copyright (c) 2003, IBM Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
* * Neither the name of IBM nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ==========================================================================
*
* Function: Move memory area (handles overlapping regions)
*
* void *memmove(void * dest, const void * src, int n)
*
* Input: r3 - destination address
* r4 - source address
* r5 - byte count
* Output: r11 - destination address
*
* ==========================================================================
*/
#include <machine/asm.h>
#ifdef _BCOPY
/* bcopy = memcpy/memmove with arguments reversed. */
/* LINTSTUB: Func: void bcopy(void *, void *, size_t) */
ENTRY(bcopy)
l.or r6, r3, r0 /* swap src/dst */
l.or r3, r4, r0
l.or r4, r6, r0
#else
/* LINTSTUB: Func: void *memmove(void *, const void *, size_t) */
ENTRY(memmove)
#endif
l.or r11, r3, r0 /* Save dst (return value) */
l.sfges r4, r3 /* Branch to reverse if */
l.bnf .Lreverse /* src < dest. Don't want to */
/* overwrite end of src with */
/* start of dest */
l.addi r4, r4, -4 /* Back up src and dst pointers */
l.addi r3, r3, -4 /* due to auto-update of 'load' */
l.srli r13, r5, 2 /* How many words in total cnt */
l.sfeqi r13, 0
l.bf .Llast1 /* Handle byte by byte if < 4 */
/* bytes total */
l.lwz r7, 4(r4) /* Preload first word */
l.addi r4, r4, 4
l.j .Lg1
l.nop
.Lg0: /* Main loop */
l.lwz r7, 4(r4) /* Load a new word */
l.sw 4(r3), r6 /* Store previous word */
l.addi r4, r4, 4 /* advance */
l.addi r3, r3, 4 /* advance */
.Lg1:
l.addi r13, r13, -1
l.sfeqi r13, 0
l.bf .Llast /* Dec cnt, and branch if just */
l.nop
/* one word to store */
l.lwz r6, 4(r4) /* Load another word */
l.sw 4(r3), r7 /* Store previous word */
l.addi r4, r4, 4 /* advance to next word */
l.addi r3, r3, 4 /* advance to next word */
l.addi r13, r13, -1 /* Decrement count */
l.sfeqi r13, 0 /* last word? */
l.bnf .Lg0 /* no, loop, more words */
l.nop
l.or r7, r6, r0 /* If word count -> 0, then... */
.Llast:
l.sw 4(r3), r7 /* ... store last word */
l.addi r3, r3, 4
.Llast1: /* Byte-by-byte copy */
l.andi r5, r5, 3 /* get remaining byte count */
l.sfeqi r5, 0 /* is it 0? */
l.bf .Ldone /* yes, we're done */
l.nop /* -- delay slot -- */
l.lbz r6, 4(r4) /* 1st byte: update addr by 4 */
l.sb 4(r3), r6 /* since we pre-adjusted by 4 */
l.addi r4, r4, 4 /* advance to next word */
l.addi r3, r3, 4 /* advance to next word */
l.addi r5, r5, -1 /* decrement count */
l.sfeqi r5, 0 /* is it 0? */
l.bf .Ldone /* yes, we're done */
l.nop /* -- delay slot -- */
.Llast2:
l.lbz r6, 1(r4) /* But handle the rest by */
l.sb 1(r3), r6 /* updating addr by 1 */
l.addi r4, r4, 1 /* advance to next word */
l.addi r3, r3, 1 /* advance to next word */
l.addi r5, r5, -1 /* decrement count */
l.sfeqi r5, 0 /* is it 0? */
l.bnf .Llast2 /* yes, we're done */
l.nop /* -- delay slot -- */
.Ldone:
l.jr lr /* return */
l.nop /* -- delay slot -- */
/* We're here since src < dest. Don't want to overwrite end of */
/* src with start of dest */
.Lreverse:
l.add r4, r4, r5 /* Work from end to beginning */
l.add r3, r3, r5 /* so add count to string ptrs */
l.srli r13, r5, 2 /* Words in total count */
l.sfeqi r13, 0
l.bf .Lrlast1 /* Handle byte by byte if < 4 */
/* bytes total */
l.nop
l.lwz r7, -4(r4) /* Preload first word */
l.addi r4, r4, -4 /* update pointer */
l.j .Lrg1
.Lrg0: /* Main loop */
l.lwz r7, -4(r4) /* Load a new word */
l.sw -4(r3), r6 /* Store previous word */
l.addi r4, r4, -4
l.addi r3, r3, -4
.Lrg1:
l.addi r13, r13, -1 /* decrement count */
l.sfeqi r13, 0 /* just one pending word left? */
l.bf .Lrlast /* yes, deal with it */
l.lwz r6, -4(r4) /* Load another word */
l.sw -4(r3), r7 /* Store previous word */
l.addi r4, r4, -4
l.addi r3, r3, -4
l.addi r13, r13, -1 /* decrement count */
l.sfeqi r13, 0 /* just one pending word left? */
l.bnf .Lrg0 /* no, loop again more words */
l.nop
l.or r7, r6, r0 /* If word count -> 0, then... */
.Lrlast:
l.sw -4(r3), r7 /* ... store last word */
l.addi r3, r3, -4 /* update pointer */
.Lrlast1: /* Byte-by-byte copy */
l.andi r5, r5, 3
l.sfeqi r5, 0
l.bf .Lrdone
.Lrlast2:
l.lbz r6, -1(r4) /* Handle the rest, byte by */
l.sb -1(r3), r6 /* byte */
l.addi r4, r4, -1
l.addi r3, r3, -1
l.addi r5, r5, -1 /* decrement count */
l.sfeqi r5, 0 /* is it 0? */
l.bnf .Lrlast2 /* no, loop again */
l.nop
.Lrdone:
l.jr lr
l.nop
#ifdef _BCOPY
END(bcopy)
#else
END(memmove)
#endif
|