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