2008-11-12 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;
28 using System.Runtime.InteropServices;
29
30 namespace Mono.Simd
31 {
32         [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 16)]
33         public struct Vector8s
34         {
35                 private short v0, v1, v2, v3, v4, v5, v6, v7;
36                 public Vector8s (short v0, short v1, short v2, short v3, short v4, short v5, short v6, short v7)
37                 {
38                         this.v0 = v0;
39                         this.v1 = v1;
40                         this.v2 = v2;
41                         this.v3 = v3;
42                         this.v4 = v4;
43                         this.v5 = v5;
44                         this.v6 = v6;
45                         this.v7 = v7;
46                 }
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
57                 [System.Runtime.CompilerServices.IndexerName ("Component")]
58                 public unsafe short this [int index]
59                 {
60                         get {
61                                 if ((index | 0x7) != 0x7) //index < 0 || index > 7
62                                         throw new ArgumentOutOfRangeException ("index");
63                                 fixed (short *v = &v0) {
64                                         return * (v + index);
65                                 }
66                         }
67                         set {
68                                 if ( (index | 0x7) != 0x7) //index < 0 || index > 7
69                                         throw new ArgumentOutOfRangeException ("index");
70                                 fixed (short *v = &v0) {
71                                         * (v + index) = value;
72                                 }
73                         }
74                 }
75
76                 [Acceleration (AccelMode.SSE2)]
77                 public static unsafe Vector8s operator + (Vector8s va, Vector8s vb)
78                 {
79                         Vector8s res = new Vector8s ();
80                         short *a = &va.v0;
81                         short *b = &vb.v0;
82                         short *c = &res.v0;
83                         for (int i = 0; i < 8; ++i)
84                                 *c++ = (short)(*a++ + *b++);
85                         return res;
86                 }
87
88                 [Acceleration (AccelMode.SSE2)]
89                 public static unsafe Vector8s operator - (Vector8s va, Vector8s vb)
90                 {
91                         Vector8s res = new Vector8s ();
92                         short *a = &va.v0;
93                         short *b = &vb.v0;
94                         short *c = &res.v0;
95                         for (int i = 0; i < 8; ++i)
96                                 *c++ = (short)(*a++ - *b++);
97                         return res;
98                 }
99
100                 [Acceleration (AccelMode.SSE2)]
101                 public static unsafe Vector8s operator * (Vector8s va, Vector8s vb)
102                 {
103                         Vector8s res = new Vector8s ();
104                         short *a = &va.v0;
105                         short *b = &vb.v0;
106                         short *c = &res.v0;
107                         for (int i = 0; i < 8; ++i)
108                                 *c++ = (short)(*a++ * (*b++));
109                         return res;
110                 }
111
112                 [Acceleration (AccelMode.SSE2)]
113                 public static unsafe Vector8s operator >> (Vector8s va, int amount)
114                 {
115                         Vector8s res = new Vector8s ();
116                         short *a = &va.v0;
117                         short *b = &res.v0;
118                         for (int i = 0; i < 8; ++i)
119                                 *b++ = (short)(*a++ >> amount);
120                         return res;
121                 }
122
123                 [Acceleration (AccelMode.SSE2)]
124                 public static unsafe Vector8s operator << (Vector8s va, int amount)
125                 {
126                         Vector8s res = new Vector8s ();
127                         short *a = &va.v0;
128                         short *b = &res.v0;
129                         for (int i = 0; i < 8; ++i)
130                                 *b++ = (short)(*a++ << amount);
131                         return res;
132                 }
133
134                 [Acceleration (AccelMode.SSE2)]
135                 public static unsafe Vector8s operator & (Vector8s va, Vector8s vb)
136                 {
137                         Vector8s res = new Vector8s ();
138                         ulong *a = (ulong*) &va.v0;
139                         ulong *b = (ulong*) &vb.v0;
140                         ulong *c = (ulong*) &res.v0;
141                         *c++ = (ulong)(*a++ & *b++);
142                         *c = (ulong)(*a & *b);
143                         return res;
144                 }
145
146                 [Acceleration (AccelMode.SSE2)]
147                 public static unsafe Vector8s operator | (Vector8s va, Vector8s vb)
148                 {
149                         Vector8s res = new Vector8s ();
150                         ulong *a = (ulong*) &va.v0;
151                         ulong *b = (ulong*) &vb.v0;
152                         ulong *c = (ulong*) &res.v0;
153                         *c++ = (ulong)(*a++ | *b++);
154                         *c = (ulong)(*a | *b);
155                         return res;
156                 }
157
158                 [Acceleration (AccelMode.SSE2)]
159                 public static unsafe Vector8s operator ^ (Vector8s va, Vector8s vb)
160                 {
161                         Vector8s res = new Vector8s ();
162                         ulong *a = (ulong*) &va.v0;
163                         ulong *b = (ulong*) &vb.v0;
164                         ulong *c = (ulong*) &res.v0;
165                         *c++ = (ulong)(*a++ ^ *b++);
166                         *c = (ulong)(*a ^ *b);
167                         return res;
168                 }
169
170                 [Acceleration (AccelMode.SSE2)]
171                 public static unsafe Vector8s UnpackLow (Vector8s va, Vector8s vb)
172                 {
173                         return new Vector8s (va.v0, vb.v0, va.v1, vb.v1, va.v2, vb.v2, va.v3, vb.v3);
174                 }
175
176                 [Acceleration (AccelMode.SSE2)]
177                 public static unsafe Vector8s UnpackHigh (Vector8s va, Vector8s vb)
178                 {
179                         return new Vector8s (va.v4, vb.v4, va.v5, vb.v5, va.v6, vb.v6, va.v7, vb.v7);
180                 }
181
182                 [Acceleration (AccelMode.SSE2)]
183                 public static unsafe Vector8s LogicalRightShift (Vector8s va, int amount)
184                 {
185                         Vector8s res = new Vector8s ();
186                         short *a = &va.v0;
187                         short *b = &res.v0;
188                         for (int i = 0; i < 8; ++i)
189                                 *b++ = (short)((ushort)(*a++) >> amount);
190                         return res;
191                 }
192
193                 [Acceleration (AccelMode.SSE2)]
194                 public static unsafe Vector8s AddWithSaturation (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.Max (System.Math.Min (*a++ + *b++, short.MaxValue), short.MinValue); 
201                         return res;
202                 }
203
204                 [Acceleration (AccelMode.SSE2)]
205                 public static unsafe Vector8s SubtractWithSaturation (Vector8s va, Vector8s vb) {
206                         Vector8s res = new Vector8s ();
207                         short *a = &va.v0;
208                         short *b = &vb.v0;
209                         short *c = &res.v0;
210                         for (int i = 0; i < 8; ++i)
211                                 *c++ = (short) System.Math.Max (System.Math.Min (*a++ - *b++, short.MaxValue), short.MinValue); ;
212                         return res;
213                 }
214
215                 [Acceleration (AccelMode.SSE2)]
216                 public static unsafe Vector8s Max (Vector8s va, Vector8s vb) {
217                         Vector8s res = new Vector8s ();
218                         short *a = &va.v0;
219                         short *b = &vb.v0;
220                         short *c = &res.v0;
221                         for (int i = 0; i < 8; ++i)
222                                 *c++ = (short) System.Math.Max (*a++, *b++);
223                         return res;
224                 }
225
226                 [Acceleration (AccelMode.SSE2)]
227                 public static unsafe Vector8s Min (Vector8s va, Vector8s vb) {
228                         Vector8s res = new Vector8s ();
229                         short *a = &va.v0;
230                         short *b = &vb.v0;
231                         short *c = &res.v0;
232                         for (int i = 0; i < 8; ++i)
233                                 *c++ = (short) System.Math.Min (*a++, *b++);
234                         return res;
235                 }
236
237                 [Acceleration (AccelMode.SSE2)]
238                 public static unsafe int ExtractByteMask (Vector8s va) {
239                         int res = 0;
240                         byte *a = (byte*)&va;
241                         for (int i = 0; i < 16; ++i)
242                                 res |= (*a++ & 0x80) >> 7 << i;
243                         return res;
244                 }
245
246                 [Acceleration (AccelMode.SSE2)]
247                 public static unsafe Vector8s ShuffleHigh (Vector8s va, ShuffleSel sel)
248                 {
249                         short *ptr = ((short*)&va) + 4;
250                         int idx = (int)sel;
251                         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)));
252                 }
253
254                 [Acceleration (AccelMode.SSE2)]
255                 public static unsafe Vector8s ShuffleLow (Vector8s va, ShuffleSel sel)
256                 {
257                         short *ptr = ((short*)&va);
258                         int idx = (int)sel;
259                         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);
260                 }
261
262                 [Acceleration (AccelMode.SSE2)]
263                 public static unsafe Vector8s CompareEqual (Vector8s va, Vector8s vb) {
264                         Vector8s res = new Vector8s ();
265                         short *a = &va.v0;
266                         short *b = &vb.v0;
267                         short *c = &res.v0;
268                         for (int i = 0; i < 8; ++i)
269                                 *c++ = (short) (*a++ == *b++ ? -1 : 0);
270                         return res;
271                 }
272
273                 [Acceleration (AccelMode.SSE2)]
274                 public static unsafe Vector8s CompareGreaterThan (Vector8s va, Vector8s vb) {
275                         Vector8s res = new Vector8s ();
276                         short *a = &va.v0;
277                         short *b = &vb.v0;
278                         short *c = &res.v0;
279                         for (int i = 0; i < 8; ++i)
280                                 *c++ = (short) (*a++ > *b++ ? -1 : 0);
281                         return res;
282                 }
283
284                 [Acceleration (AccelMode.SSE2)]
285                 public static unsafe Vector8s MultiplyStoreHigh (Vector8s va, Vector8s vb) {
286                         Vector8s res = new Vector8s ();
287                         short *a = &va.v0;
288                         short *b = &vb.v0;
289                         short *c = &res.v0;
290                         for (int i = 0; i < 8; ++i)
291                                 *c++ = (short)((int)*a++ * (int)*b++ >> 16);
292                         return res;
293                 }
294
295                 [Acceleration (AccelMode.SSE2)]
296                 public static unsafe Vector16b PackWithUnsignedSaturation (Vector8s va, Vector8s vb) {
297                         Vector16b res = new Vector16b ();
298                         short *a = (short*)&va;
299                         short *b = (short*)&vb;
300                         byte *c = (byte*)&res;
301                         for (int i = 0; i < 8; ++i)
302                                 *c++ = (byte)System.Math.Max (0, System.Math.Min ((int)*a++, byte.MaxValue));
303                         for (int i = 0; i < 8; ++i)
304                                 *c++ = (byte)System.Math.Max (0, System.Math.Min ((int)*b++, byte.MaxValue));
305                         return res;
306                 }
307
308                 [CLSCompliant(false)]
309                 [Acceleration (AccelMode.SSE2)]
310                 public static unsafe Vector16sb PackWithSignedSaturation (Vector8s va, Vector8s vb) {
311                         Vector16sb res = new Vector16sb ();
312                         short *a = (short*)&va;
313                         short *b = (short*)&vb;
314                         sbyte *c = (sbyte*)&res;
315                         for (int i = 0; i < 8; ++i)
316                                 *c++ = (sbyte)System.Math.Max (System.Math.Min ((int)*a++, sbyte.MaxValue), sbyte.MinValue);
317                         for (int i = 0; i < 8; ++i)
318                                 *c++ = (sbyte)System.Math.Max (System.Math.Min ((int)*b++, sbyte.MaxValue), sbyte.MinValue);
319                         return res;
320                 }
321
322                 [Acceleration (AccelMode.SSE1)]
323                 public static unsafe explicit operator Vector2d (Vector8s v)
324                 {
325                         Vector2d* p = (Vector2d*)&v;
326                         return *p;
327                 }
328
329                 [Acceleration (AccelMode.SSE1)]
330                 public static unsafe explicit operator Vector4f (Vector8s v)
331                 {
332                         Vector4f* p = (Vector4f*)&v;
333                         return *p;
334                 }
335
336                 [Acceleration (AccelMode.SSE1)]
337                 public static unsafe explicit operator Vector2l (Vector8s v)
338                 {
339                         Vector2l* p = (Vector2l*)&v;
340                         return *p;
341                 }
342
343                 [Acceleration (AccelMode.SSE1)]
344                 [CLSCompliant(false)]
345                 public static unsafe explicit operator Vector2ul (Vector8s v)
346                 {
347                         Vector2ul* p = (Vector2ul*)&v;
348                         return *p;
349                 }
350
351                 [Acceleration (AccelMode.SSE1)]
352                 public static unsafe explicit operator Vector4i (Vector8s v)
353                 {
354                         Vector4i* p = (Vector4i*)&v;
355                         return *p;
356                 }
357
358                 [Acceleration (AccelMode.SSE1)]
359                 [CLSCompliant(false)]
360                 public static unsafe explicit operator Vector4ui (Vector8s v)
361                 {
362                         Vector4ui* p = (Vector4ui*)&v;
363                         return *p;
364                 }
365
366                 [Acceleration (AccelMode.SSE1)]
367                 [CLSCompliant(false)]
368                 public static unsafe explicit operator Vector8us (Vector8s v)
369                 {
370                         Vector8us* p = (Vector8us*)&v;
371                         return *p;
372                 }
373
374                 [Acceleration (AccelMode.SSE1)]
375                 [CLSCompliant(false)]
376                 public static unsafe explicit operator Vector16sb (Vector8s v)
377                 {
378                         Vector16sb* p = (Vector16sb*)&v;
379                         return *p;
380                 }
381
382                 [Acceleration (AccelMode.SSE1)]
383                 public static unsafe explicit operator Vector16b (Vector8s v)
384                 {
385                         Vector16b* p = (Vector16b*)&v;
386                         return *p;
387                 }
388
389
390                 [Acceleration (AccelMode.SSE1)]
391                 public static Vector8s LoadAligned (ref Vector8s v)
392                 {
393                         return v;
394                 }
395
396                 [Acceleration (AccelMode.SSE1)]
397                 public static void StoreAligned (ref Vector8s res, Vector8s val)
398                 {
399                         res = val;
400                 }
401
402                 [CLSCompliant(false)]
403                 [Acceleration (AccelMode.SSE1)]
404                 public static unsafe Vector8s LoadAligned (Vector8s *v)
405                 {
406                         return *v;
407                 }
408
409                 [CLSCompliant(false)]
410                 [Acceleration (AccelMode.SSE1)]
411                 public static unsafe void StoreAligned (Vector8s *res, Vector8s val)
412                 {
413                         *res = val;
414                 }
415
416                 [Acceleration (AccelMode.SSE1)]
417                 [CLSCompliant(false)]
418                 public static void PrefetchTemporalAllCacheLevels (ref Vector8s res)
419                 {
420                 }
421
422                 [Acceleration (AccelMode.SSE1)]
423                 [CLSCompliant(false)]
424                 public static void PrefetchTemporal1stLevelCache (ref Vector8s res)
425                 {
426                 }
427
428                 [Acceleration (AccelMode.SSE1)]
429                 [CLSCompliant(false)]
430                 public static void PrefetchTemporal2ndLevelCache (ref Vector8s res)
431                 {
432                 }
433
434                 [Acceleration (AccelMode.SSE1)]
435                 [CLSCompliant(false)]
436                 public static void PrefetchNonTemporal (ref Vector8s res)
437                 {
438                 }
439
440                 [Acceleration (AccelMode.SSE1)]
441                 [CLSCompliant(false)]
442                 public static unsafe void PrefetchTemporalAllCacheLevels (Vector8s *res)
443                 {
444                 }
445
446                 [Acceleration (AccelMode.SSE1)]
447                 [CLSCompliant(false)]
448                 public static unsafe void PrefetchTemporal1stLevelCache (Vector8s *res)
449                 {
450                 }
451
452                 [Acceleration (AccelMode.SSE1)]
453                 [CLSCompliant(false)]
454                 public static unsafe void PrefetchTemporal2ndLevelCache (Vector8s *res)
455                 {
456                 }
457
458                 [Acceleration (AccelMode.SSE1)]
459                 [CLSCompliant(false)]
460                 public static unsafe void PrefetchNonTemporal (Vector8s *res)
461                 {
462                 }
463         }
464 }