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