2008-10-24 Rodrigo Kumpera <rkumpera@novell.com>
[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;\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         [CLSCompliant(false)]\r
34         public struct Vector8us\r
35         {\r
36                 private ushort v0, v1, v2, v3, v4, v5, v6, v7;\r\r
37                 public Vector8us (ushort v0, ushort v1, ushort v2, ushort v3, ushort v4, ushort v5, ushort v6, ushort v7)\r
38                 {\r
39                         this.v0 = v0;\r
40                         this.v1 = v1;\r
41                         this.v2 = v2;\r
42                         this.v3 = v3;\r
43                         this.v4 = v4;\r
44                         this.v5 = v5;\r
45                         this.v6 = v6;\r
46                         this.v7 = v7;\r
47                 }\r
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 \r
58                 public static unsafe Vector8us operator + (Vector8us va, Vector8us vb)\r
59                 {\r
60                         Vector8us res = new Vector8us ();
61                         ushort *a = &va.v0;
62                         ushort *b = &vb.v0;
63                         ushort *c = &res.v0;
64                         for (int i = 0; i < 8; ++i)
65                                 *c++ = (ushort)(*a++ + *b++);
66                         return res;\r
67                 }\r
68
69                 public static unsafe Vector8us operator - (Vector8us va, Vector8us vb)\r
70                 {\r
71                         Vector8us res = new Vector8us ();
72                         ushort *a = &va.v0;
73                         ushort *b = &vb.v0;
74                         ushort *c = &res.v0;
75                         for (int i = 0; i < 8; ++i)
76                                 *c++ = (ushort)(*a++ - *b++);
77                         return res;\r
78                 }
79
80                 /*
81                  * NOTE: Thou pmullw states it does signed multiplication, it works for unsigned numbers
82                  * if only the lower part is considered and the flags disregarded.
83                  */
84                 public static unsafe Vector8us operator * (Vector8us va, Vector8us vb)\r
85                 {\r
86                         Vector8us res = new Vector8us ();
87                         ushort *a = &va.v0;
88                         ushort *b = &vb.v0;
89                         ushort *c = &res.v0;
90                         for (int i = 0; i < 8; ++i)
91                                 *c++ = (ushort)(*a++ * (*b++));
92                         return res;\r
93                 }
94
95                 public static unsafe Vector8us operator >> (Vector8us va, int amount)\r
96                 {\r
97                         Vector8us res = new Vector8us ();
98                         ushort *a = &va.v0;
99                         ushort *b = &res.v0;
100                         for (int i = 0; i < 8; ++i)
101                                 *b++ = (ushort)(*a++ >> amount);
102                         return res;\r
103                 }
104
105
106                 public static unsafe Vector8us operator << (Vector8us va, int amount)\r
107                 {\r
108                         Vector8us res = new Vector8us ();
109                         ushort *a = &va.v0;
110                         ushort *b = &res.v0;
111                         for (int i = 0; i < 8; ++i)
112                                 *b++ = (ushort)(*a++ << amount);
113                         return res;\r
114                 }
115
116                 public static unsafe Vector8us operator & (Vector8us va, Vector8us vb)\r
117                 {\r
118                         Vector8us res = new Vector8us ();
119                         ushort *a = &va.v0;
120                         ushort *b = &vb.v0;
121                         ushort *c = &res.v0;
122                         for (int i = 0; i < 8; ++i)
123                                 *c++ = (ushort)(*a++ & *b++);
124                         return res;\r
125                 }
126
127                 public static unsafe Vector8us operator | (Vector8us va, Vector8us vb)\r
128                 {\r
129                         Vector8us res = new Vector8us ();
130                         ushort *a = &va.v0;
131                         ushort *b = &vb.v0;
132                         ushort *c = &res.v0;
133                         for (int i = 0; i < 8; ++i)
134                                 *c++ = (ushort)(*a++ | *b++);
135                         return res;\r
136                 }
137
138                 public static unsafe Vector8us operator ^ (Vector8us va, Vector8us vb)\r
139                 {\r
140                         Vector8us res = new Vector8us ();
141                         ushort *a = &va.v0;
142                         ushort *b = &vb.v0;
143                         ushort *c = &res.v0;
144                         for (int i = 0; i < 8; ++i)
145                                 *c++ = (ushort)(*a++ ^ *b++);
146                         return res;\r
147                 }
148
149                 public static unsafe Vector8us UnpackLow (Vector8us va, Vector8us vb)
150                 {
151                         return new Vector8us (va.v0, vb.v0, va.v1, vb.v1, va.v2, vb.v2, va.v3, vb.v3);
152                 }
153
154                 public static unsafe Vector8us UnpackHigh (Vector8us va, Vector8us vb)
155                 {
156                         return new Vector8us (va.v4, vb.v4, va.v5, vb.v5, va.v6, vb.v6, va.v7, vb.v7);
157                 }
158
159                 public static unsafe Vector8us ShiftRightArithmetic (Vector8us va, int amount)\r
160                 {\r
161                         Vector8us res = new Vector8us ();
162                         ushort *a = &va.v0;
163                         ushort *b = &res.v0;
164                         for (int i = 0; i < 8; ++i)
165                                 *b++ = (ushort)((short)(*a++) >> amount);
166                         return res;\r
167                 }
168
169                 public static unsafe Vector8us AddWithSaturation (Vector8us va, Vector8us vb) {
170                         Vector8us res = new Vector8us ();
171                         ushort *a = &va.v0;
172                         ushort *b = &vb.v0;
173                         ushort *c = &res.v0;
174                         for (int i = 0; i < 8; ++i)
175                                 *c++ = (ushort) System.Math.Min (*a++ + *b++, ushort.MaxValue);
176                         return res;
177                 }\r
178
179                 public static unsafe Vector8us SubWithSaturation (Vector8us va, Vector8us vb) {
180                         Vector8us res = new Vector8us ();
181                         ushort *a = &va.v0;
182                         ushort *b = &vb.v0;
183                         ushort *c = &res.v0;
184                         for (int i = 0; i < 8; ++i)
185                                 *c++ = (ushort) System.Math.Max (*a++ - *b++, 0);
186                         return res;
187                 }
188
189                 public static unsafe Vector8us Average (Vector8us va, Vector8us vb) {
190                         Vector8us res = new Vector8us ();
191                         ushort *a = &va.v0;
192                         ushort *b = &vb.v0;
193                         ushort *c = &res.v0;
194                         for (int i = 0; i < 8; ++i)
195                                 *c++ = (ushort) ((*a++ + *b++ + 1) >> 1);
196                         return res;
197                 }
198
199                 /*Requires SSE 4.1*/
200                 public static unsafe Vector8us Max (Vector8us va, Vector8us vb) {
201                         Vector8us res = new Vector8us ();
202                         ushort *a = &va.v0;
203                         ushort *b = &vb.v0;
204                         ushort *c = &res.v0;
205                         for (int i = 0; i < 8; ++i)
206                                 *c++ = (ushort) System.Math.Max (*a++, *b++);
207                         return res;
208                 }
209
210                 /*Requires SSE 4.1*/
211                 public static unsafe Vector8us Min (Vector8us va, Vector8us vb) {
212                         Vector8us res = new Vector8us ();
213                         ushort *a = &va.v0;
214                         ushort *b = &vb.v0;
215                         ushort *c = &res.v0;
216                         for (int i = 0; i < 8; ++i)
217                                 *c++ = (ushort) System.Math.Min (*a++, *b++);
218                         return res;
219                 }
220
221                 public static unsafe int ExtractByteMask (Vector8us va) {
222                         int res = 0;
223                         byte *a = (byte*)&va;
224                         for (int i = 0; i < 16; ++i)
225                                 res |= (*a++ & 0x80) >> 7 << i;
226                         return res;
227                 }
228
229                 public static unsafe Vector8us ShuffleHigh (Vector8us va, ShuffleSel sel)\r
230                 {
231                         ushort *ptr = ((ushort*)&va) + 4;
232                         int idx = (int)sel;\r
233                         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)));\r
234                 }
235
236                 public static unsafe Vector8us ShuffleLow (Vector8us va, ShuffleSel sel)\r
237                 {
238                         ushort *ptr = ((ushort*)&va);
239                         int idx = (int)sel;\r
240                         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);\r
241                 }
242
243                 public static unsafe Vector8us CompareEqual (Vector8us va, Vector8us vb) {
244                         Vector8us res = new Vector8us ();
245                         ushort *a = &va.v0;
246                         ushort *b = &vb.v0;
247                         ushort *c = &res.v0;
248                         for (int i = 0; i < 8; ++i)
249                                 *c++ = (ushort) (*a++ == *b++ ? -1 : 0);
250                         return res;
251                 }
252
253                 public static unsafe Vector8us MultiplyStoreHigh (Vector8us va, Vector8us vb) {
254                         Vector8us res = new Vector8us ();
255                         ushort *a = &va.v0;
256                         ushort *b = &vb.v0;
257                         ushort *c = &res.v0;
258                         for (int i = 0; i < 8; ++i)
259                                 *c++ = (ushort)((uint)*a++ * (uint)*b++ >> 16);
260                         return res;
261                 }
262
263                 /*This function performs a packuswb, which treats the source as a signed value */
264                 public static unsafe Vector16b SignedPackWithUnsignedSaturation (Vector8us va, Vector8us vb) {
265                         Vector16b res = new Vector16b ();
266                         short *a = (short*)&va;
267                         short *b = (short*)&vb;
268                         byte *c = (byte*)&res;
269                         for (int i = 0; i < 8; ++i)
270                                 *c++ = (byte)System.Math.Max (0, System.Math.Min ((int)*a++, byte.MaxValue));
271                         for (int i = 0; i < 8; ++i)
272                                 *c++ = (byte)System.Math.Max (0, System.Math.Min ((int)*b++, byte.MaxValue));
273                         return res;
274                 }
275
276
277                 public static unsafe explicit operator Vector4f(Vector8us v)\r
278                 {\r
279                         Vector4f* p = (Vector4f*)&v;\r
280                         return *p;\r
281                 }\r
282
283                 public static unsafe explicit operator Vector4ui(Vector8us v)\r
284                 {\r
285                         Vector4ui* p = (Vector4ui*)&v;\r
286                         return *p;\r
287                 }\r
288
289                 public static unsafe explicit operator Vector16b(Vector8us v)\r
290                 {\r
291                         Vector16b* p = (Vector16b*)&v;\r
292                         return *p;\r
293                 }\r
294
295                 public static Vector8us LoadAligned (ref Vector8us v)\r
296                 {\r
297                         return v;\r
298                 }\r
299 \r
300                 public static void StoreAligned (ref Vector8us res, Vector8us val)\r
301                 {\r
302                         res = val;\r
303                 }
304
305                 public static unsafe Vector8us LoadAligned (Vector8us *v)\r
306                 {\r
307                         return *v;\r
308                 }\r
309 \r
310                 public static unsafe void StoreAligned (Vector8us *res, Vector8us val)\r
311                 {\r
312                         *res = val;\r
313                 }
314         }\r
315 }\r