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;
32 [StructLayout(LayoutKind.Sequential, Pack = 0, Size = 16)]
34 public struct Vector4ui
41 public uint X { get { return x; } set { x = value; } }
42 public uint Y { get { return y; } set { y = value; } }
43 public uint Z { get { return z; } set { z = value; } }
44 public uint W { get { return w; } set { w = value; } }
46 [System.Runtime.CompilerServices.IndexerName ("Component")]
47 public unsafe uint this [int index]
50 if ((index | 0x3) != 0x3) //index < 0 || index > 3
51 throw new ArgumentOutOfRangeException ("index");
52 fixed (uint *v = &x) {
57 if ( (index | 0x3) != 0x3) //index < 0 || index > 3
58 throw new ArgumentOutOfRangeException ("index");
59 fixed (uint *v = &x) {
60 * (v + index) = value;
65 public Vector4ui (uint x, uint y, uint z, uint w)
73 [Acceleration (AccelMode.SSE2)]
74 public static Vector4ui operator + (Vector4ui v1, Vector4ui v2)
76 return new Vector4ui (v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
79 [Acceleration (AccelMode.SSE2)]
80 public static Vector4ui operator - (Vector4ui v1, Vector4ui v2)
82 return new Vector4ui (v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
85 [Acceleration (AccelMode.SSE41)]
86 public static Vector4ui operator * (Vector4ui v1, Vector4ui v2)
88 return new Vector4ui (v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
91 [Acceleration (AccelMode.SSE2)]
92 public static unsafe Vector4ui operator << (Vector4ui v1, int amount)
94 return new Vector4ui (v1.x << amount, v1.y << amount, v1.z << amount, v1.w << amount);
97 [Acceleration (AccelMode.SSE2)]
98 public static unsafe Vector4ui operator >> (Vector4ui v1, int amount)
100 return new Vector4ui (v1.x >> amount, v1.y >> amount, v1.z >> amount, v1.w >> amount);
103 [Acceleration (AccelMode.SSE2)]
104 public static Vector4ui operator & (Vector4ui v1, Vector4ui v2)
106 return new Vector4ui (v1.x & v2.x, v1.y & v2.y, v1.z & v2.z, v1.w & v2.w);
109 [Acceleration (AccelMode.SSE2)]
110 public static Vector4ui operator | (Vector4ui v1, Vector4ui v2)
112 return new Vector4ui (v1.x | v2.x, v1.y | v2.y, v1.z | v2.z, v1.w | v2.w);
115 [Acceleration (AccelMode.SSE2)]
116 public static Vector4ui operator ^ (Vector4ui v1, Vector4ui v2)
118 return new Vector4ui (v1.x ^ v2.x, v1.y ^ v2.y, v1.z ^ v2.z, v1.w ^ v2.w);
121 [Acceleration (AccelMode.SSE2)]
122 public static bool operator ==(Vector4ui v1, Vector4ui v2)
124 return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w;
127 [Acceleration (AccelMode.SSE2)]
128 public static bool operator !=(Vector4ui v1, Vector4ui v2)
130 return v1.x != v2.x || v1.y != v2.y || v1.z != v2.z || v1.w != v2.w;
133 [Acceleration (AccelMode.SSE2)]
134 public static unsafe Vector4ui UnpackLow (Vector4ui v1, Vector4ui v2)
136 return new Vector4ui (v1.x, v2.x, v1.y, v2.y);
139 [Acceleration (AccelMode.SSE2)]
140 public static unsafe Vector4ui UnpackHigh (Vector4ui v1, Vector4ui v2)
142 return new Vector4ui (v1.z, v2.z, v1.w, v2.w);
145 [Acceleration (AccelMode.SSE2)]
146 public static unsafe Vector4ui ArithmeticRightShift (Vector4ui v1, int amount)
148 Vector4ui res = new Vector4ui ();
151 for (int i = 0; i < 4; ++i)
152 *b++ = (uint)((int)(*a++) >> amount);
156 [Acceleration (AccelMode.SSE41)]
157 public static unsafe Vector4ui Max (Vector4ui v1, Vector4ui v2)
159 return new Vector4ui (System.Math.Max (v1.x, v2.x), System.Math.Max (v1.y, v2.y), System.Math.Max (v1.z, v2.z), System.Math.Max (v1.w, v2.w));
162 [Acceleration (AccelMode.SSE41)]
163 public static unsafe Vector4ui Min (Vector4ui v1, Vector4ui v2)
165 return new Vector4ui (System.Math.Min (v1.x, v2.x), System.Math.Min (v1.y, v2.y), System.Math.Min (v1.z, v2.z), System.Math.Min (v1.w, v2.w));
168 [Acceleration (AccelMode.SSE2)]
169 public static unsafe Vector4ui Shuffle (Vector4ui v1, ShuffleSel sel)
171 uint *ptr = (uint*)&v1;
173 return new Vector4ui (*(ptr + ((idx >> 0) & 0x3)),*(ptr + ((idx >> 2) & 0x3)),*(ptr + ((idx >> 4) & 0x3)),*(ptr + ((idx >> 6) & 0x3)));
176 [Acceleration (AccelMode.SSE2)]
177 public static unsafe Vector4ui CompareEqual (Vector4ui v1, Vector4ui v2)
179 return new Vector4ui ((uint)(v1.x == v2.x ? -1 : 0), (uint)(v1.y == v2.y ? -1 : 0), (uint)(v1.z == v2.z ? -1 : 0), (uint)(v1.w == v2.w ? -1 : 0));
182 /* This function performs a packusdw, which treats the source as a signed value */
183 [Acceleration (AccelMode.SSE41)]
184 public static unsafe Vector8us SignedPackWithUnsignedSaturation (Vector4ui va, Vector4ui vb) {
185 Vector8us res = new Vector8us ();
188 ushort *c = (ushort*)&res;
189 for (int i = 0; i < 4; ++i)
190 *c++ = (ushort)System.Math.Max (0, System.Math.Min (*a++, ushort.MaxValue));
191 for (int i = 0; i < 4; ++i)
192 *c++ = (ushort)System.Math.Max (0, System.Math.Min (*b++, ushort.MaxValue));
196 /* This function performs a packssdw, which treats the source as a signed value*/
197 [Acceleration (AccelMode.SSE2)]
198 public static unsafe Vector8s SignedPackWithSignedSaturation (Vector4ui va, Vector4ui vb) {
199 Vector8s res = new Vector8s ();
202 short *c = (short*)&res;
203 for (int i = 0; i < 4; ++i)
204 *c++ = (short)System.Math.Max (System.Math.Min ((int)*a++, short.MaxValue), short.MinValue);
205 for (int i = 0; i < 4; ++i)
206 *c++ = (short)System.Math.Max (System.Math.Min ((int)*b++, short.MaxValue), short.MinValue);
210 [Acceleration (AccelMode.SSE1)]
211 public static unsafe explicit operator Vector2d (Vector4ui v)
213 Vector2d* p = (Vector2d*)&v;
217 [Acceleration (AccelMode.SSE1)]
218 public static unsafe explicit operator Vector4f (Vector4ui v)
220 Vector4f* p = (Vector4f*)&v;
224 [Acceleration (AccelMode.SSE1)]
225 public static unsafe explicit operator Vector2l (Vector4ui v)
227 Vector2l* p = (Vector2l*)&v;
231 [Acceleration (AccelMode.SSE1)]
232 public static unsafe explicit operator Vector2ul (Vector4ui v)
234 Vector2ul* p = (Vector2ul*)&v;
238 [Acceleration (AccelMode.SSE1)]
239 public static unsafe explicit operator Vector4i (Vector4ui v)
241 Vector4i* p = (Vector4i*)&v;
245 [Acceleration (AccelMode.SSE1)]
246 public static unsafe explicit operator Vector8s (Vector4ui v)
248 Vector8s* p = (Vector8s*)&v;
252 [Acceleration (AccelMode.SSE1)]
253 public static unsafe explicit operator Vector8us (Vector4ui v)
255 Vector8us* p = (Vector8us*)&v;
259 [Acceleration (AccelMode.SSE1)]
260 public static unsafe explicit operator Vector16sb (Vector4ui v)
262 Vector16sb* p = (Vector16sb*)&v;
266 [Acceleration (AccelMode.SSE1)]
267 public static unsafe explicit operator Vector16b (Vector4ui v)
269 Vector16b* p = (Vector16b*)&v;
273 [Acceleration (AccelMode.SSE1)]
274 public static Vector4ui LoadAligned (ref Vector4ui v)
279 [Acceleration (AccelMode.SSE1)]
280 public static void StoreAligned (ref Vector4ui res, Vector4ui val)
285 [Acceleration (AccelMode.SSE1)]
286 public static unsafe Vector4ui LoadAligned (Vector4ui *v)
291 [Acceleration (AccelMode.SSE1)]
292 public static unsafe void StoreAligned (Vector4ui *res, Vector4ui val)
297 [Acceleration (AccelMode.SSE1)]
298 [CLSCompliant(false)]
299 public static void PrefetchTemporalAllCacheLevels (ref Vector4ui res)
303 [Acceleration (AccelMode.SSE1)]
304 [CLSCompliant(false)]
305 public static void PrefetchTemporal1stLevelCache (ref Vector4ui res)
309 [Acceleration (AccelMode.SSE1)]
310 [CLSCompliant(false)]
311 public static void PrefetchTemporal2ndLevelCache (ref Vector4ui res)
315 [Acceleration (AccelMode.SSE1)]
316 [CLSCompliant(false)]
317 public static void PrefetchNonTemporal (ref Vector4ui res)
321 [Acceleration (AccelMode.SSE1)]
322 [CLSCompliant(false)]
323 public static unsafe void PrefetchTemporalAllCacheLevels (Vector4ui *res)
327 [Acceleration (AccelMode.SSE1)]
328 [CLSCompliant(false)]
329 public static unsafe void PrefetchTemporal1stLevelCache (Vector4ui *res)
333 [Acceleration (AccelMode.SSE1)]
334 [CLSCompliant(false)]
335 public static unsafe void PrefetchTemporal2ndLevelCache (Vector4ui *res)
339 [Acceleration (AccelMode.SSE1)]
340 [CLSCompliant(false)]
341 public static unsafe void PrefetchNonTemporal (Vector4ui *res)