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