2008-10-20 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                 public static unsafe Vector8us Max (Vector8us va, Vector8us vb) {
200                         Vector8us res = new Vector8us ();
201                         ushort *a = &va.v0;
202                         ushort *b = &vb.v0;
203                         ushort *c = &res.v0;
204                         for (int i = 0; i < 8; ++i)
205                                 *c++ = (ushort) System.Math.Max (*a++, *b++);
206                         return res;
207                 }
208
209                 public static unsafe Vector8us Min (Vector8us va, Vector8us vb) {
210                         Vector8us res = new Vector8us ();
211                         ushort *a = &va.v0;
212                         ushort *b = &vb.v0;
213                         ushort *c = &res.v0;
214                         for (int i = 0; i < 8; ++i)
215                                 *c++ = (ushort) System.Math.Min (*a++, *b++);
216                         return res;
217                 }
218
219                 public static unsafe int ExtractByteMask (Vector8us va) {
220                         int res = 0;
221                         byte *a = (byte*)&va;
222                         for (int i = 0; i < 16; ++i)
223                                 res |= (*a++ & 0x80) >> 7 << i;
224                         return res;
225                 }
226
227                 public static unsafe Vector8us ShuffleHigh (Vector8us va, ShuffleSel sel)\r
228                 {
229                         ushort *ptr = ((ushort*)&va) + 4;
230                         int idx = (int)sel;\r
231                         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
232                 }
233
234                 public static unsafe Vector8us ShuffleLow (Vector8us va, ShuffleSel sel)\r
235                 {
236                         ushort *ptr = ((ushort*)&va);
237                         int idx = (int)sel;\r
238                         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
239                 }
240
241                 public static unsafe Vector8us CompareEqual (Vector8us va, Vector8us vb) {
242                         Vector8us res = new Vector8us ();
243                         ushort *a = &va.v0;
244                         ushort *b = &vb.v0;
245                         ushort *c = &res.v0;
246                         for (int i = 0; i < 8; ++i)
247                                 *c++ = (ushort) (*a++ == *b++ ? -1 : 0);
248                         return res;
249                 }
250
251                 public static unsafe Vector8us MultiplyStoreHigh (Vector8us va, Vector8us vb) {
252                         Vector8us res = new Vector8us ();
253                         ushort *a = &va.v0;
254                         ushort *b = &vb.v0;
255                         ushort *c = &res.v0;
256                         for (int i = 0; i < 8; ++i)
257                                 *c++ = (ushort)((uint)*a++ * (uint)*b++ >> 16);
258                         return res;
259                 }
260
261                 public static unsafe explicit operator Vector4f(Vector8us v)\r
262                 {\r
263                         Vector4f* p = (Vector4f*)&v;\r
264                         return *p;\r
265                 }\r
266
267                 public static Vector8us LoadAligned (ref Vector8us v)\r
268                 {\r
269                         return v;\r
270                 }\r
271 \r
272                 public static void StoreAligned (ref Vector8us res, Vector8us val)\r
273                 {\r
274                         res = val;\r
275                 }
276         }\r
277 }\r