importing messaging-2008 branch to trunk.
[mono.git] / mcs / class / Mono.Simd / Mono.Simd / Vector4ui.cs
1 // Vector4ui.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 Vector4ui
35         {
36                 private uint x;
37                 private uint y;
38                 private uint z;
39                 private uint w;
40
41                 public uint X { get { return x; } set { x = value; } }
42                 public uint Y { get { return y; } set { y = value; } }
43                 public uint Z { get { return z; } set { z = value; } }
44                 public uint W { get { return w; } set { w = value; } }
45
46                 [System.Runtime.CompilerServices.IndexerName ("Component")]
47                 public unsafe uint this [int index]
48                 {
49                         get {
50                                 if ((index | 0x3) != 0x3) //index < 0 || index > 3
51                                         throw new ArgumentOutOfRangeException ("index");
52                                 fixed (uint *v = &x) {
53                                         return * (v + index);
54                                 }
55                         }
56                         set {
57                                 if ( (index | 0x3) != 0x3) //index < 0 || index > 3
58                                         throw new ArgumentOutOfRangeException ("index");
59                                 fixed (uint *v = &x) {
60                                         * (v + index) = value;
61                                 }
62                         }
63                 }
64
65                 public Vector4ui (uint x, uint y, uint z, uint w)
66                 {
67                         this.x = x;
68                         this.y = y;
69                         this.z = z;
70                         this.w = w;
71                 }
72
73                 [Acceleration (AccelMode.SSE2)]
74                 public static Vector4ui operator + (Vector4ui v1, Vector4ui v2)
75                 {
76                         return new Vector4ui (v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
77                 }
78
79                 [Acceleration (AccelMode.SSE2)]
80                 public static Vector4ui operator - (Vector4ui v1, Vector4ui v2)
81                 {
82                         return new Vector4ui (v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
83                 }
84
85                 [Acceleration (AccelMode.SSE41)]
86                 public static Vector4ui operator * (Vector4ui v1, Vector4ui v2)
87                 {
88                         return new Vector4ui (v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
89                 }
90
91                 [Acceleration (AccelMode.SSE2)]
92                 public static unsafe Vector4ui operator << (Vector4ui v1, int amount)
93                 {
94                         return new Vector4ui (v1.x << amount, v1.y << amount, v1.z << amount, v1.w << amount);
95                 }
96
97                 [Acceleration (AccelMode.SSE2)]
98                 public static unsafe Vector4ui operator >> (Vector4ui v1, int amount)
99                 {
100                         return new Vector4ui (v1.x >> amount, v1.y >> amount, v1.z >> amount, v1.w >> amount);
101                 }
102
103                 [Acceleration (AccelMode.SSE2)]
104                 public static Vector4ui operator & (Vector4ui v1, Vector4ui v2)
105                 {
106                         return new Vector4ui (v1.x & v2.x, v1.y & v2.y, v1.z & v2.z, v1.w & v2.w);
107                 }
108
109                 [Acceleration (AccelMode.SSE2)]
110                 public static Vector4ui operator | (Vector4ui v1, Vector4ui v2)
111                 {
112                         return new Vector4ui (v1.x | v2.x, v1.y | v2.y, v1.z | v2.z, v1.w | v2.w);
113                 }
114
115                 [Acceleration (AccelMode.SSE2)]
116                 public static Vector4ui operator ^ (Vector4ui v1, Vector4ui v2)
117                 {
118                         return new Vector4ui (v1.x ^ v2.x, v1.y ^ v2.y, v1.z ^ v2.z, v1.w ^ v2.w);
119                 }
120
121                 [Acceleration (AccelMode.SSE2)]
122                 public static bool operator ==(Vector4ui v1, Vector4ui v2)
123                 {
124                         return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w;
125                 }
126
127                 [Acceleration (AccelMode.SSE2)]
128                 public static bool operator !=(Vector4ui v1, Vector4ui v2)
129                 {
130                         return v1.x != v2.x || v1.y != v2.y || v1.z != v2.z || v1.w != v2.w;
131                 }
132
133                 [Acceleration (AccelMode.SSE2)]
134                 public static unsafe Vector4ui UnpackLow (Vector4ui v1, Vector4ui v2)
135                 {
136                         return new Vector4ui (v1.x, v2.x, v1.y, v2.y);
137                 }
138
139                 [Acceleration (AccelMode.SSE2)]
140                 public static unsafe Vector4ui UnpackHigh (Vector4ui v1, Vector4ui v2)
141                 {
142                         return new Vector4ui (v1.z, v2.z, v1.w, v2.w);
143                 }
144
145                 [Acceleration (AccelMode.SSE2)]
146                 public static unsafe Vector4ui ArithmeticRightShift (Vector4ui v1, int amount)
147                 {
148                         Vector4ui res = new Vector4ui ();
149                         uint *a = &v1.x;
150                         uint *b = &res.x;
151                         for (int i = 0; i < 4; ++i)
152                                 *b++ = (uint)((int)(*a++) >> amount);
153                         return res;
154                 }
155
156                 [Acceleration (AccelMode.SSE41)]
157                 public static unsafe Vector4ui Max (Vector4ui v1, Vector4ui v2)
158                 {
159                         return new Vector4ui (System.Math.Max (v1.x, v2.x), System.Math.Max (v1.y, v2.y), System.Math.Max (v1.z, v2.z), System.Math.Max (v1.w, v2.w));
160                 }
161
162                 [Acceleration (AccelMode.SSE41)]
163                 public static unsafe Vector4ui Min (Vector4ui v1, Vector4ui v2)
164                 {
165                         return new Vector4ui (System.Math.Min (v1.x, v2.x), System.Math.Min (v1.y, v2.y), System.Math.Min (v1.z, v2.z), System.Math.Min (v1.w, v2.w));
166                 }
167
168                 [Acceleration (AccelMode.SSE2)]
169                 public static unsafe Vector4ui Shuffle (Vector4ui v1, ShuffleSel sel)
170                 {
171                         uint *ptr = (uint*)&v1;
172                         int idx = (int)sel;
173                         return new Vector4ui (*(ptr + ((idx >> 0) & 0x3)),*(ptr + ((idx >> 2) & 0x3)),*(ptr + ((idx >> 4) & 0x3)),*(ptr + ((idx >> 6) & 0x3)));
174                 }
175
176                 [Acceleration (AccelMode.SSE2)]
177                 public static unsafe Vector4ui CompareEqual (Vector4ui v1, Vector4ui v2)
178                 {
179                         return new Vector4ui ((uint)(v1.x ==  v2.x ? -1 : 0), (uint)(v1.y ==  v2.y ? -1 : 0), (uint)(v1.z ==  v2.z ? -1 : 0), (uint)(v1.w ==  v2.w ? -1 : 0));
180                 }
181
182                 /* This function performs a packusdw, which treats the source as a signed value */
183                 [Acceleration (AccelMode.SSE41)]
184                 public static unsafe Vector8us SignedPackWithUnsignedSaturation (Vector4ui va, Vector4ui vb) {
185                         Vector8us res = new Vector8us ();
186                         int *a = (int*)&va;
187                         int *b = (int*)&vb;
188                         ushort *c = (ushort*)&res;
189                         for (int i = 0; i < 4; ++i)
190                                 *c++ = (ushort)System.Math.Max (0, System.Math.Min (*a++, ushort.MaxValue));
191                         for (int i = 0; i < 4; ++i)
192                                 *c++ = (ushort)System.Math.Max (0, System.Math.Min (*b++, ushort.MaxValue));
193                         return res;
194                 }
195
196                 /* This function performs a packssdw, which treats the source as a signed value*/
197                 [Acceleration (AccelMode.SSE2)]
198                 public static unsafe Vector8s SignedPackWithSignedSaturation (Vector4ui va, Vector4ui vb) {
199                         Vector8s res = new Vector8s ();
200                         int *a = (int*)&va;
201                         int *b = (int*)&vb;
202                         short *c = (short*)&res;
203                         for (int i = 0; i < 4; ++i)
204                                 *c++ = (short)System.Math.Max (System.Math.Min ((int)*a++, short.MaxValue), short.MinValue);
205                         for (int i = 0; i < 4; ++i)
206                                 *c++ = (short)System.Math.Max (System.Math.Min ((int)*b++, short.MaxValue), short.MinValue);
207                         return res;
208                 }
209
210                 [Acceleration (AccelMode.SSE1)]
211                 public static unsafe explicit operator Vector2d (Vector4ui v)
212                 {
213                         Vector2d* p = (Vector2d*)&v;
214                         return *p;
215                 }
216
217                 [Acceleration (AccelMode.SSE1)]
218                 public static unsafe explicit operator Vector4f (Vector4ui v)
219                 {
220                         Vector4f* p = (Vector4f*)&v;
221                         return *p;
222                 }
223
224                 [Acceleration (AccelMode.SSE1)]
225                 public static unsafe explicit operator Vector2l (Vector4ui v)
226                 {
227                         Vector2l* p = (Vector2l*)&v;
228                         return *p;
229                 }
230
231                 [Acceleration (AccelMode.SSE1)]
232                 public static unsafe explicit operator Vector2ul (Vector4ui v)
233                 {
234                         Vector2ul* p = (Vector2ul*)&v;
235                         return *p;
236                 }
237
238                 [Acceleration (AccelMode.SSE1)]
239                 public static unsafe explicit operator Vector4i (Vector4ui v)
240                 {
241                         Vector4i* p = (Vector4i*)&v;
242                         return *p;
243                 }
244
245                 [Acceleration (AccelMode.SSE1)]
246                 public static unsafe explicit operator Vector8s (Vector4ui v)
247                 {
248                         Vector8s* p = (Vector8s*)&v;
249                         return *p;
250                 }
251
252                 [Acceleration (AccelMode.SSE1)]
253                 public static unsafe explicit operator Vector8us (Vector4ui v)
254                 {
255                         Vector8us* p = (Vector8us*)&v;
256                         return *p;
257                 }
258
259                 [Acceleration (AccelMode.SSE1)]
260                 public static unsafe explicit operator Vector16sb (Vector4ui v)
261                 {
262                         Vector16sb* p = (Vector16sb*)&v;
263                         return *p;
264                 }
265
266                 [Acceleration (AccelMode.SSE1)]
267                 public static unsafe explicit operator Vector16b (Vector4ui v)
268                 {
269                         Vector16b* p = (Vector16b*)&v;
270                         return *p;
271                 }
272
273                 [Acceleration (AccelMode.SSE1)]
274                 public static Vector4ui LoadAligned (ref Vector4ui v)
275                 {
276                         return v;
277                 }
278
279                 [Acceleration (AccelMode.SSE1)]
280                 public static void StoreAligned (ref Vector4ui res, Vector4ui val)
281                 {
282                         res = val;
283                 }
284
285                 [Acceleration (AccelMode.SSE1)]
286                 public static unsafe Vector4ui LoadAligned (Vector4ui *v)
287                 {
288                         return *v;
289                 }
290
291                 [Acceleration (AccelMode.SSE1)]
292                 public static unsafe void StoreAligned (Vector4ui *res, Vector4ui val)
293                 {
294                         *res = val;
295                 }
296
297                 [Acceleration (AccelMode.SSE1)]
298                 [CLSCompliant(false)]
299                 public static void PrefetchTemporalAllCacheLevels (ref Vector4ui res)
300                 {
301                 }
302
303                 [Acceleration (AccelMode.SSE1)]
304                 [CLSCompliant(false)]
305                 public static void PrefetchTemporal1stLevelCache (ref Vector4ui res)
306                 {
307                 }
308
309                 [Acceleration (AccelMode.SSE1)]
310                 [CLSCompliant(false)]
311                 public static void PrefetchTemporal2ndLevelCache (ref Vector4ui res)
312                 {
313                 }
314
315                 [Acceleration (AccelMode.SSE1)]
316                 [CLSCompliant(false)]
317                 public static void PrefetchNonTemporal (ref Vector4ui res)
318                 {
319                 }
320
321                 [Acceleration (AccelMode.SSE1)]
322                 [CLSCompliant(false)]
323                 public static unsafe void PrefetchTemporalAllCacheLevels (Vector4ui *res)
324                 {
325                 }
326
327                 [Acceleration (AccelMode.SSE1)]
328                 [CLSCompliant(false)]
329                 public static unsafe void PrefetchTemporal1stLevelCache (Vector4ui *res)
330                 {
331                 }
332
333                 [Acceleration (AccelMode.SSE1)]
334                 [CLSCompliant(false)]
335                 public static unsafe void PrefetchTemporal2ndLevelCache (Vector4ui *res)
336                 {
337                 }
338
339                 [Acceleration (AccelMode.SSE1)]
340                 [CLSCompliant(false)]
341                 public static unsafe void PrefetchNonTemporal (Vector4ui *res)
342                 {
343                 }
344         }
345 }