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 [CLSCompliant(false)]
\r
34 public struct Vector8us
\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
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; } }
58 public static unsafe Vector8us operator + (Vector8us va, Vector8us vb)
\r
60 Vector8us res = new Vector8us ();
64 for (int i = 0; i < 8; ++i)
65 *c++ = (ushort)(*a++ + *b++);
69 public static unsafe Vector8us operator - (Vector8us va, Vector8us vb)
\r
71 Vector8us res = new Vector8us ();
75 for (int i = 0; i < 8; ++i)
76 *c++ = (ushort)(*a++ - *b++);
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.
84 public static unsafe Vector8us operator * (Vector8us va, Vector8us vb)
\r
86 Vector8us res = new Vector8us ();
90 for (int i = 0; i < 8; ++i)
91 *c++ = (ushort)(*a++ * (*b++));
95 public static unsafe Vector8us operator >> (Vector8us va, int amount)
\r
97 Vector8us res = new Vector8us ();
100 for (int i = 0; i < 8; ++i)
101 *b++ = (ushort)(*a++ >> amount);
106 public static unsafe Vector8us operator << (Vector8us va, int amount)
\r
108 Vector8us res = new Vector8us ();
111 for (int i = 0; i < 8; ++i)
112 *b++ = (ushort)(*a++ << amount);
116 public static unsafe Vector8us operator & (Vector8us va, Vector8us vb)
\r
118 Vector8us res = new Vector8us ();
122 for (int i = 0; i < 8; ++i)
123 *c++ = (ushort)(*a++ & *b++);
127 public static unsafe Vector8us operator | (Vector8us va, Vector8us vb)
\r
129 Vector8us res = new Vector8us ();
133 for (int i = 0; i < 8; ++i)
134 *c++ = (ushort)(*a++ | *b++);
138 public static unsafe Vector8us operator ^ (Vector8us va, Vector8us vb)
\r
140 Vector8us res = new Vector8us ();
144 for (int i = 0; i < 8; ++i)
145 *c++ = (ushort)(*a++ ^ *b++);
149 public static unsafe Vector8us UnpackLow (Vector8us va, Vector8us vb)
151 return new Vector8us (va.v0, vb.v0, va.v1, vb.v1, va.v2, vb.v2, va.v3, vb.v3);
154 public static unsafe Vector8us UnpackHigh (Vector8us va, Vector8us vb)
156 return new Vector8us (va.v4, vb.v4, va.v5, vb.v5, va.v6, vb.v6, va.v7, vb.v7);
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.
163 public static unsafe Vector8us ShiftRightArithmetic (Vector8us va, int amount)
\r
165 Vector8us res = new Vector8us ();
168 for (int i = 0; i < 8; ++i)
169 *b++ = (ushort)(((uint)(*a++)) >> amount);
173 public static unsafe Vector8us AddWithSaturation (Vector8us va, Vector8us vb) {
174 Vector8us res = new Vector8us ();
178 for (int i = 0; i < 8; ++i)
179 *c++ = (ushort) System.Math.Min (*a++ + *b++, ushort.MaxValue);
183 public static unsafe Vector8us SubWithSaturation (Vector8us va, Vector8us vb) {
184 Vector8us res = new Vector8us ();
188 for (int i = 0; i < 8; ++i)
189 *c++ = (ushort) System.Math.Max (*a++ - *b++, 0);
194 * NOTE: Thou packuswb states that it works with signed words, it works as expected with unsigned ones.
196 public static unsafe Vector16b PackWithUnsignedSaturation (Vector8us va, Vector8us vb)
198 Vector16b res = new Vector16b ();
199 byte *r = (byte*)&res;
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);
211 public static unsafe explicit operator Vector8us(Vector4f v)
\r
213 Vector8us* p = (Vector8us*)&v;
\r
217 public static unsafe explicit operator Vector8us(Vector16b v)
\r
219 Vector8us* p = (Vector8us*)&v;
\r
223 public static Vector8us LoadAligned (ref Vector8us v)
\r
228 public static void StoreAligned (ref Vector8us res, Vector8us val)
\r