Author: Rodrigo Kumpera <kumpera@gmail.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                 /*
160                  * XXX Maybe this method doesn't make sense.
161                  * C# doesn't support explicit shifting mode, it is defined by the signed of the type.
162                  */
163                 public static unsafe Vector8us ShiftRightArithmetic (Vector8us va, int amount)\r
164                 {\r
165                         Vector8us res = new Vector8us ();
166                         ushort *a = &va.v0;
167                         ushort *b = &res.v0;
168                         for (int i = 0; i < 8; ++i)
169                                 *b++ = (ushort)(((uint)(*a++)) >> amount);
170                         return res;\r
171                 }
172
173                 public static unsafe Vector8us AddWithSaturation (Vector8us va, Vector8us vb) {
174                         Vector8us res = new Vector8us ();
175                         ushort *a = &va.v0;
176                         ushort *b = &vb.v0;
177                         ushort *c = &res.v0;
178                         for (int i = 0; i < 8; ++i)
179                                 *c++ = (ushort) System.Math.Min (*a++ + *b++, ushort.MaxValue);
180                         return res;
181                 }\r
182
183                 public static unsafe Vector8us SubWithSaturation (Vector8us va, Vector8us vb) {
184                         Vector8us res = new Vector8us ();
185                         ushort *a = &va.v0;
186                         ushort *b = &vb.v0;
187                         ushort *c = &res.v0;
188                         for (int i = 0; i < 8; ++i)
189                                 *c++ = (ushort) System.Math.Max (*a++ - *b++, 0);
190                         return res;
191                 }
192
193                 /*
194          * NOTE: Thou packuswb states that it works with signed words, it works as expected with unsigned ones.
195                  */
196                 public static unsafe Vector16b PackWithUnsignedSaturation (Vector8us va, Vector8us vb)
197                 {
198                         Vector16b res = new Vector16b ();
199                         byte *r = (byte*)&res;
200                         ushort *a = &va.v0;
201                         ushort *b = &vb.v0;
202                         int i;
203                         for (i = 0; i < 8; ++i)
204                                 *r++ = (byte)System.Math.Min (*a++, byte.MaxValue);
205                         for (i = 0; i < 8; ++i)
206                                 *r++ = (byte)System.Math.Min (*b++, byte.MaxValue);
207
208                         return res;
209                 }
210
211                 public static unsafe explicit operator Vector8us(Vector4f v)\r
212                 {\r
213                         Vector8us* p = (Vector8us*)&v;\r
214                         return *p;\r
215                 }\r
216
217                 public static unsafe explicit operator Vector8us(Vector16b v)\r
218                 {\r
219                         Vector8us* p = (Vector8us*)&v;\r
220                         return *p;\r
221                 }
222
223                 public static Vector8us LoadAligned (ref Vector8us v)\r
224                 {\r
225                         return v;\r
226                 }\r
227 \r
228                 public static void StoreAligned (ref Vector8us res, Vector8us val)\r
229                 {\r
230                         res = val;\r
231                 }
232         }\r
233 }\r