4 // Rodrigo Kumpera (rkumpera@novell.com)
6 // (C) 2008 Novell, Inc. (http://www.novell.com)
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
28 using System.Runtime.InteropServices;
\r
32 [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 16)]
33 public struct Vector8s
\r
35 private short v0, v1, v2, v3, v4, v5, v6, v7;
\r\r
36 public Vector8s (short v0, short v1, short v2, short v3, short v4, short v5, short v6, short v7)
\r
48 public short V0 { get { return v0; } set { v0 = value; } }
49 public short V1 { get { return v1; } set { v1 = value; } }
50 public short V2 { get { return v2; } set { v2 = value; } }
51 public short V3 { get { return v3; } set { v3 = value; } }
52 public short V4 { get { return v4; } set { v4 = value; } }
53 public short V5 { get { return v5; } set { v5 = value; } }
54 public short V6 { get { return v6; } set { v6 = value; } }
55 public short V7 { get { return v7; } set { v7 = value; } }
57 public static unsafe Vector8s operator + (Vector8s va, Vector8s vb)
\r
59 Vector8s res = new Vector8s ();
63 for (int i = 0; i < 8; ++i)
64 *c++ = (short)(*a++ + *b++);
68 public static unsafe Vector8s operator - (Vector8s va, Vector8s vb)
\r
70 Vector8s res = new Vector8s ();
74 for (int i = 0; i < 8; ++i)
75 *c++ = (short)(*a++ - *b++);
79 public static unsafe Vector8s operator * (Vector8s va, Vector8s vb)
\r
81 Vector8s res = new Vector8s ();
85 for (int i = 0; i < 8; ++i)
86 *c++ = (short)(*a++ * (*b++));
90 public static unsafe Vector8s operator >> (Vector8s va, int amount)
\r
92 Vector8s res = new Vector8s ();
95 for (int i = 0; i < 8; ++i)
96 *b++ = (short)(*a++ >> amount);
101 public static unsafe Vector8s operator << (Vector8s va, int amount)
\r
103 Vector8s res = new Vector8s ();
106 for (int i = 0; i < 8; ++i)
107 *b++ = (short)(*a++ << amount);
111 public static unsafe Vector8s operator & (Vector8s va, Vector8s vb)
\r
113 Vector8s res = new Vector8s ();
117 for (int i = 0; i < 8; ++i)
118 *c++ = (short)(*a++ & *b++);
122 public static unsafe Vector8s operator | (Vector8s va, Vector8s vb)
\r
124 Vector8s res = new Vector8s ();
128 for (int i = 0; i < 8; ++i)
129 *c++ = (short)(*a++ | *b++);
133 public static unsafe Vector8s operator ^ (Vector8s va, Vector8s vb)
\r
135 Vector8s res = new Vector8s ();
139 for (int i = 0; i < 8; ++i)
140 *c++ = (short)(*a++ ^ *b++);
144 public static unsafe Vector8s UnpackLow (Vector8s va, Vector8s vb)
146 return new Vector8s (va.v0, vb.v0, va.v1, vb.v1, va.v2, vb.v2, va.v3, vb.v3);
149 public static unsafe Vector8s UnpackHigh (Vector8s va, Vector8s vb)
151 return new Vector8s (va.v4, vb.v4, va.v5, vb.v5, va.v6, vb.v6, va.v7, vb.v7);
154 public static unsafe Vector8s ShiftRightLogic (Vector8s va, int amount)
\r
156 Vector8s res = new Vector8s ();
159 for (int i = 0; i < 8; ++i)
160 *b++ = (short)((ushort)(*a++) >> amount);
164 public static unsafe Vector8s AddWithSaturation (Vector8s va, Vector8s vb) {
165 Vector8s res = new Vector8s ();
169 for (int i = 0; i < 8; ++i)
170 *c++ = (short) System.Math.Max (System.Math.Min (*a++ + *b++, short.MaxValue), short.MinValue);
174 public static unsafe Vector8s SubWithSaturation (Vector8s va, Vector8s vb) {
175 Vector8s res = new Vector8s ();
179 for (int i = 0; i < 8; ++i)
180 *c++ = (short) System.Math.Max (System.Math.Min (*a++ - *b++, short.MaxValue), short.MinValue); ;
184 public static unsafe Vector8s Max (Vector8s va, Vector8s vb) {
185 Vector8s res = new Vector8s ();
189 for (int i = 0; i < 8; ++i)
190 *c++ = (short) System.Math.Max (*a++, *b++);
194 public static unsafe Vector8s Min (Vector8s va, Vector8s vb) {
195 Vector8s res = new Vector8s ();
199 for (int i = 0; i < 8; ++i)
200 *c++ = (short) System.Math.Min (*a++, *b++);
204 public static unsafe int ExtractByteMask (Vector8s va) {
206 byte *a = (byte*)&va;
207 for (int i = 0; i < 16; ++i)
208 res |= (*a++ & 0x80) >> 7 << i;
212 public static unsafe Vector8s ShuffleHigh (Vector8s va, ShuffleSel sel)
\r
214 short *ptr = ((short*)&va) + 4;
215 int idx = (int)sel;
\r
216 return new Vector8s (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
219 public static unsafe Vector8s ShuffleLow (Vector8s va, ShuffleSel sel)
\r
221 short *ptr = ((short*)&va);
222 int idx = (int)sel;
\r
223 return new Vector8s (*(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
226 public static unsafe Vector8s CompareEqual (Vector8s va, Vector8s vb) {
227 Vector8s res = new Vector8s ();
231 for (int i = 0; i < 8; ++i)
232 *c++ = (short) (*a++ == *b++ ? -1 : 0);
236 public static unsafe Vector8s CompareGreaterThan (Vector8s va, Vector8s vb) {
237 Vector8s res = new Vector8s ();
241 for (int i = 0; i < 8; ++i)
242 *c++ = (short) (*a++ > *b++ ? -1 : 0);
246 public static unsafe Vector8s MultiplyStoreHigh (Vector8s va, Vector8s vb) {
247 Vector8s res = new Vector8s ();
251 for (int i = 0; i < 8; ++i)
252 *c++ = (short)((int)*a++ * (int)*b++ >> 16);
256 [CLSCompliant(false)]
\r
257 public static unsafe Vector16b PackWithUnsignedSaturation (Vector8s va, Vector8s vb) {
258 Vector16b res = new Vector16b ();
259 short *a = (short*)&va;
260 short *b = (short*)&vb;
261 byte *c = (byte*)&res;
262 for (int i = 0; i < 8; ++i)
263 *c++ = (byte)System.Math.Max (0, System.Math.Min ((int)*a++, byte.MaxValue));
264 for (int i = 0; i < 8; ++i)
265 *c++ = (byte)System.Math.Max (0, System.Math.Min ((int)*b++, byte.MaxValue));
269 public static unsafe explicit operator Vector4f(Vector8s v)
\r
271 Vector4f* p = (Vector4f*)&v;
\r
275 [CLSCompliant(false)]
\r
276 public static unsafe explicit operator Vector4ui(Vector8s v)
\r
278 Vector4ui* p = (Vector4ui*)&v;
\r
282 [CLSCompliant(false)]
\r
283 public static unsafe explicit operator Vector16b(Vector8s v)
\r
285 Vector16b* p = (Vector16b*)&v;
\r
289 public static Vector8s LoadAligned (ref Vector8s v)
\r
294 public static void StoreAligned (ref Vector8s res, Vector8s val)
\r
299 [CLSCompliant(false)]
\r
300 public static unsafe Vector8s LoadAligned (Vector8s *v)
\r
305 [CLSCompliant(false)]
\r
306 public static unsafe void StoreAligned (Vector8s *res, Vector8s val)
\r