2008-10-24 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / Mono.Simd / Mono.Simd / Vector8s.cs
1 // Vector8s.cs
2 //
3 // Author:
4 //   Rodrigo Kumpera (rkumpera@novell.com)
5 //
6 // (C) 2008 Novell, Inc. (http://www.novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 using System;\r
28 using System.Runtime.InteropServices;\r
29 \r
30 namespace Mono.Simd\r
31 {\r
32         [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 16)]
33         public struct Vector8s\r
34         {\r
35                 private short v0, v1, v2, v3, v4, v5, v6, v7;\r\r
36                 public Vector8s (short v0, short v1, short v2, short v3, short v4, short v5, short v6, short v7)\r
37                 {\r
38                         this.v0 = v0;\r
39                         this.v1 = v1;\r
40                         this.v2 = v2;\r
41                         this.v3 = v3;\r
42                         this.v4 = v4;\r
43                         this.v5 = v5;\r
44                         this.v6 = v6;\r
45                         this.v7 = v7;\r
46                 }\r
47
48                 public short V0 { get { return v0; } set { v0 = value; } }
49                 public short V1 { get { return v1; } set { v1 = value; } }
50                 public short V2 { get { return v2; } set { v2 = value; } }
51                 public short V3 { get { return v3; } set { v3 = value; } }
52                 public short V4 { get { return v4; } set { v4 = value; } }
53                 public short V5 { get { return v5; } set { v5 = value; } }
54                 public short V6 { get { return v6; } set { v6 = value; } }
55                 public short V7 { get { return v7; } set { v7 = value; } }
56 \r
57                 public static unsafe Vector8s operator + (Vector8s va, Vector8s vb)\r
58                 {\r
59                         Vector8s res = new Vector8s ();
60                         short *a = &va.v0;
61                         short *b = &vb.v0;
62                         short *c = &res.v0;
63                         for (int i = 0; i < 8; ++i)
64                                 *c++ = (short)(*a++ + *b++);
65                         return res;\r
66                 }\r
67
68                 public static unsafe Vector8s operator - (Vector8s va, Vector8s vb)\r
69                 {\r
70                         Vector8s res = new Vector8s ();
71                         short *a = &va.v0;
72                         short *b = &vb.v0;
73                         short *c = &res.v0;
74                         for (int i = 0; i < 8; ++i)
75                                 *c++ = (short)(*a++ - *b++);
76                         return res;\r
77                 }
78
79                 public static unsafe Vector8s operator * (Vector8s va, Vector8s vb)\r
80                 {\r
81                         Vector8s res = new Vector8s ();
82                         short *a = &va.v0;
83                         short *b = &vb.v0;
84                         short *c = &res.v0;
85                         for (int i = 0; i < 8; ++i)
86                                 *c++ = (short)(*a++ * (*b++));
87                         return res;\r
88                 }
89
90                 public static unsafe Vector8s operator >> (Vector8s va, int amount)\r
91                 {\r
92                         Vector8s res = new Vector8s ();
93                         short *a = &va.v0;
94                         short *b = &res.v0;
95                         for (int i = 0; i < 8; ++i)
96                                 *b++ = (short)(*a++ >> amount);
97                         return res;\r
98                 }
99
100
101                 public static unsafe Vector8s operator << (Vector8s va, int amount)\r
102                 {\r
103                         Vector8s res = new Vector8s ();
104                         short *a = &va.v0;
105                         short *b = &res.v0;
106                         for (int i = 0; i < 8; ++i)
107                                 *b++ = (short)(*a++ << amount);
108                         return res;\r
109                 }
110
111                 public static unsafe Vector8s operator & (Vector8s va, Vector8s vb)\r
112                 {\r
113                         Vector8s res = new Vector8s ();
114                         short *a = &va.v0;
115                         short *b = &vb.v0;
116                         short *c = &res.v0;
117                         for (int i = 0; i < 8; ++i)
118                                 *c++ = (short)(*a++ & *b++);
119                         return res;\r
120                 }
121
122                 public static unsafe Vector8s operator | (Vector8s va, Vector8s vb)\r
123                 {\r
124                         Vector8s res = new Vector8s ();
125                         short *a = &va.v0;
126                         short *b = &vb.v0;
127                         short *c = &res.v0;
128                         for (int i = 0; i < 8; ++i)
129                                 *c++ = (short)(*a++ | *b++);
130                         return res;\r
131                 }
132
133                 public static unsafe Vector8s operator ^ (Vector8s va, Vector8s vb)\r
134                 {\r
135                         Vector8s res = new Vector8s ();
136                         short *a = &va.v0;
137                         short *b = &vb.v0;
138                         short *c = &res.v0;
139                         for (int i = 0; i < 8; ++i)
140                                 *c++ = (short)(*a++ ^ *b++);
141                         return res;\r
142                 }
143
144                 public static unsafe Vector8s UnpackLow (Vector8s va, Vector8s vb)
145                 {
146                         return new Vector8s (va.v0, vb.v0, va.v1, vb.v1, va.v2, vb.v2, va.v3, vb.v3);
147                 }
148
149                 public static unsafe Vector8s UnpackHigh (Vector8s va, Vector8s vb)
150                 {
151                         return new Vector8s (va.v4, vb.v4, va.v5, vb.v5, va.v6, vb.v6, va.v7, vb.v7);
152                 }
153
154                 public static unsafe Vector8s ShiftRightLogic (Vector8s va, int amount)\r
155                 {\r
156                         Vector8s res = new Vector8s ();
157                         short *a = &va.v0;
158                         short *b = &res.v0;
159                         for (int i = 0; i < 8; ++i)
160                                 *b++ = (short)((ushort)(*a++) >> amount);
161                         return res;\r
162                 }
163
164                 public static unsafe Vector8s AddWithSaturation (Vector8s va, Vector8s vb) {
165                         Vector8s res = new Vector8s ();
166                         short *a = &va.v0;
167                         short *b = &vb.v0;
168                         short *c = &res.v0;
169                         for (int i = 0; i < 8; ++i)
170                                 *c++ = (short) System.Math.Max (System.Math.Min (*a++ + *b++, short.MaxValue), short.MinValue); 
171                         return res;
172                 }\r
173
174                 public static unsafe Vector8s SubWithSaturation (Vector8s va, Vector8s vb) {
175                         Vector8s res = new Vector8s ();
176                         short *a = &va.v0;
177                         short *b = &vb.v0;
178                         short *c = &res.v0;
179                         for (int i = 0; i < 8; ++i)
180                                 *c++ = (short) System.Math.Max (System.Math.Min (*a++ - *b++, short.MaxValue), short.MinValue); ;
181                         return res;
182                 }
183
184                 public static unsafe Vector8s Max (Vector8s va, Vector8s vb) {
185                         Vector8s res = new Vector8s ();
186                         short *a = &va.v0;
187                         short *b = &vb.v0;
188                         short *c = &res.v0;
189                         for (int i = 0; i < 8; ++i)
190                                 *c++ = (short) System.Math.Max (*a++, *b++);
191                         return res;
192                 }
193
194                 public static unsafe Vector8s Min (Vector8s va, Vector8s vb) {
195                         Vector8s res = new Vector8s ();
196                         short *a = &va.v0;
197                         short *b = &vb.v0;
198                         short *c = &res.v0;
199                         for (int i = 0; i < 8; ++i)
200                                 *c++ = (short) System.Math.Min (*a++, *b++);
201                         return res;
202                 }
203
204                 public static unsafe int ExtractByteMask (Vector8s va) {
205                         int res = 0;
206                         byte *a = (byte*)&va;
207                         for (int i = 0; i < 16; ++i)
208                                 res |= (*a++ & 0x80) >> 7 << i;
209                         return res;
210                 }
211
212                 public static unsafe Vector8s ShuffleHigh (Vector8s va, ShuffleSel sel)\r
213                 {
214                         short *ptr = ((short*)&va) + 4;
215                         int idx = (int)sel;\r
216                         return new Vector8s (va.v0, va.v1, va.v2, va.v3, *(ptr + ((idx >> 0) & 0x3)), *(ptr + ((idx >> 2) & 0x3)), *(ptr + ((idx >> 4) & 0x3)), *(ptr + ((idx >> 6) & 0x3)));\r
217                 }
218
219                 public static unsafe Vector8s ShuffleLow (Vector8s va, ShuffleSel sel)\r
220                 {
221                         short *ptr = ((short*)&va);
222                         int idx = (int)sel;\r
223                         return new Vector8s (*(ptr + ((idx >> 0) & 0x3)), *(ptr + ((idx >> 2) & 0x3)), *(ptr + ((idx >> 4) & 0x3)), *(ptr + ((idx >> 6) & 0x3)), va.v4, va.v5, va.v6, va.v7);\r
224                 }
225
226                 public static unsafe Vector8s CompareEqual (Vector8s va, Vector8s vb) {
227                         Vector8s res = new Vector8s ();
228                         short *a = &va.v0;
229                         short *b = &vb.v0;
230                         short *c = &res.v0;
231                         for (int i = 0; i < 8; ++i)
232                                 *c++ = (short) (*a++ == *b++ ? -1 : 0);
233                         return res;
234                 }
235
236                 public static unsafe Vector8s CompareGreaterThan (Vector8s va, Vector8s vb) {
237                         Vector8s res = new Vector8s ();
238                         short *a = &va.v0;
239                         short *b = &vb.v0;
240                         short *c = &res.v0;
241                         for (int i = 0; i < 8; ++i)
242                                 *c++ = (short) (*a++ > *b++ ? -1 : 0);
243                         return res;
244                 }
245
246                 public static unsafe Vector8s MultiplyStoreHigh (Vector8s va, Vector8s vb) {
247                         Vector8s res = new Vector8s ();
248                         short *a = &va.v0;
249                         short *b = &vb.v0;
250                         short *c = &res.v0;
251                         for (int i = 0; i < 8; ++i)
252                                 *c++ = (short)((int)*a++ * (int)*b++ >> 16);
253                         return res;
254                 }
255
256                 [CLSCompliant(false)]\r
257                 public static unsafe Vector16b PackWithUnsignedSaturation (Vector8s va, Vector8s vb) {
258                         Vector16b res = new Vector16b ();
259                         short *a = (short*)&va;
260                         short *b = (short*)&vb;
261                         byte *c = (byte*)&res;
262                         for (int i = 0; i < 8; ++i)
263                                 *c++ = (byte)System.Math.Max (0, System.Math.Min ((int)*a++, byte.MaxValue));
264                         for (int i = 0; i < 8; ++i)
265                                 *c++ = (byte)System.Math.Max (0, System.Math.Min ((int)*b++, byte.MaxValue));
266                         return res;
267                 }
268
269                 public static unsafe explicit operator Vector4f(Vector8s v)\r
270                 {\r
271                         Vector4f* p = (Vector4f*)&v;\r
272                         return *p;\r
273                 }\r
274
275                 [CLSCompliant(false)]\r
276                 public static unsafe explicit operator Vector4ui(Vector8s v)\r
277                 {\r
278                         Vector4ui* p = (Vector4ui*)&v;\r
279                         return *p;\r
280                 }\r
281
282                 [CLSCompliant(false)]\r
283                 public static unsafe explicit operator Vector16b(Vector8s v)\r
284                 {\r
285                         Vector16b* p = (Vector16b*)&v;\r
286                         return *p;\r
287                 }\r
288
289                 public static Vector8s LoadAligned (ref Vector8s v)\r
290                 {\r
291                         return v;\r
292                 }\r
293 \r
294                 public static void StoreAligned (ref Vector8s res, Vector8s val)\r
295                 {\r
296                         res = val;\r
297                 }
298
299                 [CLSCompliant(false)]\r
300                 public static unsafe Vector8s LoadAligned (Vector8s *v)\r
301                 {\r
302                         return *v;\r
303                 }\r
304 \r
305                 [CLSCompliant(false)]\r
306                 public static unsafe void StoreAligned (Vector8s *res, Vector8s val)\r
307                 {\r
308                         *res = val;\r
309                 }
310         }\r
311 }\r