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 <spu_intrinsics.h>
34 #include <stddef.h>
35 #include <vec_literal.h>
36 #include "../../string/local.h"
37
38 /* Copy n bytes from memory area src to memory area dest.
39 * The memory areas may not overlap. The memcpy subroutine
40 * returns a pointer to dest.
41 *
42 * Faster implementation of this function can be implemented
43 * either with prior knowledge of the alignment or special
44 * casing specific optimal alignments.
45 */
46 void *
47 __inhibit_loop_to_libcall
memcpy(void * __restrict__ dest,const void * __restrict__ src,size_t n)48 memcpy(void * __restrict__ dest, const void * __restrict__ src, size_t n)
49 {
50 int adjust, delta;
51 unsigned int soffset1, doffset1, doffset2;
52 vec_uchar16 *vSrc, *vDst;
53 vec_uchar16 sdata1, sdata2, sdata, ddata, shuffle;
54 vec_uchar16 mask, mask1, mask2, mask3;
55
56 vSrc = (vec_uchar16 *)(src);
57 vDst = (vec_uchar16 *)(dest);
58
59 /* Handle any leading destination partial quadwords as
60 * well a very short copy (ie, such that the n characters
61 * all reside in a single (destination) quadword.
62 */
63 soffset1 = (unsigned int)(src) & 15;
64 doffset1 = (unsigned int)(dest) & 15;
65 doffset2 = ((unsigned int)(dest) + n) & 15;
66
67 /* Compute a shuffle pattern used to align the source string
68 * with the alignment of the destination string.
69 */
70
71 adjust = (int)spu_extract(spu_cmpgt(spu_promote(doffset1, 0), spu_promote(soffset1, 0)), 0);
72 delta = (int)soffset1 - (int)doffset1;
73 delta += adjust & 16;
74
75 shuffle = (vec_uchar16)spu_add((vec_uint4)spu_splats((unsigned char)delta),
76 VEC_LITERAL(vec_uint4, 0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F));
77
78 vSrc += adjust;
79
80 sdata1 = *vSrc++;
81 sdata2 = *vSrc++;
82
83 ddata = *vDst;
84 sdata = spu_shuffle(sdata1, sdata2, shuffle);
85
86 /* Construct a series of masks used to data insert. The masks
87 * contain 0 when the destination word is unchanged, 1 when it
88 * must be replaced by source bytes.
89 *
90 * mask1 = mask for leading unchanged bytes
91 * mask2 = mask for trailing unchange bytes
92 * mask3 = mask indicating the more than one qword is being changed.
93 */
94 mask = spu_splats((unsigned char)-1);
95 mask1 = spu_rlmaskqwbyte(mask, -doffset1);
96 mask2 = spu_slqwbyte(mask, 16-doffset2);
97 mask3 = (vec_uchar16)spu_cmpgt(spu_splats((unsigned int)(doffset1 + n)), 15);
98
99 *vDst++ = spu_sel(ddata, sdata, spu_and(mask1, spu_or(mask2, mask3)));
100
101 n += doffset1;
102
103 /* Handle complete destination quadwords
104 */
105 while (n > 31) {
106 sdata1 = sdata2;
107 sdata2 = *vSrc++;
108 *vDst++ = spu_shuffle(sdata1, sdata2, shuffle);
109 n -= 16;
110 }
111
112 /* Handle any trailing partial (destination) quadwords
113 */
114 mask = spu_and((vec_uchar16)spu_cmpgt(spu_splats((unsigned int)n), 16), mask2);
115 *vDst = spu_sel(*vDst, spu_shuffle(sdata2, *vSrc, shuffle), mask);
116
117 return (dest);
118 }
119