1 /*
2 (C) Copyright 2001,2006,
3 International Business Machines Corporation,
4 Sony Computer Entertainment, Incorporated,
5 Toshiba Corporation,
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 * Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of their
18 contributors may be used to endorse or promote products derived from this
19 software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32 */
33 #include <picolibc.h>
34
35 #include <spu_intrinsics.h>
36 #include <stddef.h>
37 #include "vec_literal.h"
38
39 /* Copy n bytes from memory area src to memory area dest.
40 * Copying is performed as if the n characters pointed to
41 * by src are first copied into a temporary array that does
42 * not overlap the src and dest arrays. Then the n characters
43 * of the temporary array are copied into the destination
44 * array. The memmove subroutine returns a pointer to dest.
45 */
46
memmove(void * __restrict__ dest,const void * __restrict__ src,size_t n)47 void * memmove(void * __restrict__ dest, const void * __restrict__ src, size_t n)
48 {
49 int adjust, delta;
50 unsigned int soffset1, soffset2, doffset1, doffset2;
51 vec_uchar16 *vSrc, *vDst;
52 vec_uchar16 sdata1, sdata2, sdata, ddata, shuffle;
53 vec_uchar16 mask, mask1, mask2, mask3, one = spu_splats((unsigned char)-1);
54
55 soffset1 = (unsigned int)(src) & 15;
56 doffset1 = (unsigned int)(dest) & 15;
57 doffset2 = ((unsigned int)(dest) + n) & 15;
58
59 /* Construct a series of masks used to data insert. The masks
60 * contains 0 bit when the destination word is unchanged, 1 when it
61 * must be replaced by source bits.
62 *
63 * mask1 = mask for leading unchanged bytes
64 * mask2 = mask for trailing unchange bytes
65 * mask3 = mask indicating the more than one qword is being changed.
66 */
67 mask = one;
68 mask1 = spu_rlmaskqwbyte(mask, -doffset1);
69 mask2 = spu_slqwbyte(mask, 16-doffset2);
70 mask3 = (vec_uchar16)spu_cmpgt(spu_splats((unsigned int)(doffset1 + n)), 15);
71
72 vDst = (vec_uchar16 *)(dest);
73
74 delta = (int)soffset1 - (int)doffset1;
75
76 /* The follow check only works if the SPU addresses are not
77 * wrapped. No provisions have been made to correct for this
78 * limitation.
79 */
80 if (((unsigned int)dest - (unsigned int)src) >= (unsigned int)n) {
81 /* Forward copy. Perform a memcpy.
82 *
83 * Handle any leading destination partial quadwords as
84 * well a very short copy (ie, such that the n characters
85 * all reside in a single (destination) quadword.
86 */
87 vSrc = (vec_uchar16 *)(src);
88 vDst = (vec_uchar16 *)(dest);
89
90 /* Handle any leading destination partial quadwords as
91 * well a very short copy (ie, such that the n characters
92 * all reside in a single (destination) quadword.
93 */
94 soffset1 = (unsigned int)(src) & 15;
95 doffset1 = (unsigned int)(dest) & 15;
96 doffset2 = ((unsigned int)(dest) + n) & 15;
97
98 /* Compute a shuffle pattern used to align the source string
99 * with the alignment of the destination string.
100 */
101
102 adjust = (int)spu_extract(spu_cmpgt(spu_promote(doffset1, 0), spu_promote(soffset1, 0)), 0);
103 delta = (int)soffset1 - (int)doffset1;
104 delta += adjust & 16;
105
106 shuffle = (vec_uchar16)spu_add((vec_uint4)spu_splats((unsigned char)delta),
107 VEC_LITERAL(vec_uint4, 0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F));
108
109 vSrc += adjust;
110
111 sdata1 = *vSrc++;
112 sdata2 = *vSrc++;
113
114 ddata = *vDst;
115 sdata = spu_shuffle(sdata1, sdata2, shuffle);
116
117 /* Construct a series of masks used to data insert. The masks
118 * contain 0 when the destination word is unchanged, 1 when it
119 * must be replaced by source bytes.
120 *
121 * mask1 = mask for leading unchanged bytes
122 * mask2 = mask for trailing unchange bytes
123 * mask3 = mask indicating the more than one qword is being changed.
124 */
125 mask = one;
126 mask1 = spu_rlmaskqwbyte(mask, -doffset1);
127 mask2 = spu_slqwbyte(mask, 16-doffset2);
128 mask3 = (vec_uchar16)spu_cmpgt(spu_splats((unsigned int)(doffset1 + n)), 15);
129
130 *vDst++ = spu_sel(ddata, sdata, spu_and(mask1, spu_or(mask2, mask3)));
131
132 n += doffset1;
133
134 /* Handle complete destination quadwords
135 */
136 while (n > 31) {
137 sdata1 = sdata2;
138 sdata2 = *vSrc++;
139 *vDst++ = spu_shuffle(sdata1, sdata2, shuffle);
140 n -= 16;
141 }
142
143 /* Handle any trailing partial (destination) quadwords
144 */
145 mask = spu_and((vec_uchar16)spu_cmpgt(spu_splats((unsigned int)n), 16), mask2);
146 *vDst = spu_sel(*vDst, spu_shuffle(sdata2, *vSrc, shuffle), mask);
147
148 } else {
149 /* Backward copy.
150 *
151 * Handle any leading destination partial quadwords as
152 * well a very short copy (ie, such that the n characters
153 * all reside in a single (destination) quadword.
154 */
155 vSrc = (vec_uchar16 *)((unsigned int)src + n-1);
156 vDst = (vec_uchar16 *)((unsigned int)dest + n-1);
157
158 /* Handle any leading destination partial quadwords as
159 * well a very short copy (ie, such that the n characters
160 * all reside in a single (destination) quadword.
161 */
162 soffset1 = (unsigned int)(src) & 15;
163 soffset2 = (unsigned int)(vSrc) & 15;
164 doffset1 = (unsigned int)(dest) & 15;
165 doffset2 = (unsigned int)(vDst) & 15;
166
167 /* Compute a shuffle pattern used to align the source string
168 * with the alignment of the destination string.
169 */
170 adjust = (int)spu_extract(spu_cmpgt(spu_promote(soffset2, 0), spu_promote(doffset2, 0)), 0);
171 delta = (int)doffset2 - (int)soffset2;
172 delta += adjust & 16;
173
174 shuffle = (vec_uchar16)spu_sub(VEC_LITERAL(vec_uint4, 0x10111213, 0x14151617, 0x18191A1B, 0x1C1D1E1F),
175 (vec_uint4)spu_splats((unsigned char)delta));
176
177 vSrc -= adjust;
178
179 sdata2 = *vSrc--;
180 sdata1 = *vSrc--;
181
182 ddata = *vDst;
183 sdata = spu_shuffle(sdata1, sdata2, shuffle);
184
185 /* Construct a series of masks used to data insert. The masks
186 * contain 0 when the destination word is unchanged, 1 when it
187 * must be replaced by source bytes.
188 *
189 * mask1 = mask for leading unchanged bytes
190 * mask2 = mask for trailing unchange bytes
191 * mask3 = mask indicating the more than one qword is being changed.
192 */
193 mask = one;
194 mask1 = spu_rlmaskqwbyte(mask, -doffset1);
195 mask2 = spu_slqwbyte(mask, 15-doffset2);
196 mask3 = (vec_uchar16)spu_cmpgt(spu_splats((int)(doffset2 - n)), -2);
197
198 *vDst-- = spu_sel(ddata, sdata, spu_and(mask2, spu_orc(mask1, mask3)));
199
200 n -= doffset2 + 1;
201
202 /* Handle complete destination quadwords
203 */
204 while ((int)n > 15) {
205 sdata2 = sdata1;
206 sdata1 = *vSrc--;
207 *vDst-- = spu_shuffle(sdata1, sdata2, shuffle);
208 n -= 16;
209 }
210
211 /* Handle any trailing partial (destination) quadwords
212 */
213 mask = spu_and((vec_uchar16)spu_cmpgt(spu_splats((int)n), 0), mask1);
214 *vDst = spu_sel(*vDst, spu_shuffle(*vSrc, sdata1, shuffle), mask);
215 }
216 return (dest);
217 }
218
219