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.
29 using System.Runtime.InteropServices;
33 public enum ShuffleSel
55 /*Expand a single element into all elements*/
56 ExpandX = XFromX | YFromX | ZFromX | WFromX,
57 ExpandY = XFromY | YFromY | ZFromY | WFromY,
58 ExpandZ = XFromZ | YFromZ | ZFromZ | WFromZ,
59 ExpandW = XFromW | YFromW | ZFromW | WFromW,
61 /*Expand a pair of elements (x,y,z,w) -> (x,x,y,y)*/
62 ExpandXY = XFromX | YFromX | ZFromY | WFromY,
63 ExpandZW = XFromZ | YFromZ | ZFromW | WFromW,
65 /*Expand interleaving elements (x,y,z,w) -> (x,y,x,y)*/
66 ExpandInterleavedXY = XFromX | YFromY | ZFromX | WFromY,
67 ExpandInterleavedZW = XFromZ | YFromW | ZFromZ | WFromW,
70 RotateRight = XFromY | YFromZ | ZFromW | WFromX,
71 RotateLeft = XFromW | YFromX | ZFromY | WFromZ,
74 Swap = XFromW | YFromZ | ZFromY | WFromX,
79 Unary - (implemented as mulps [-1,-1,-1,-1])
80 Abs (implemented as pand [7fffffff,...] )
82 Mask extraction function
84 Replace Shuffle with less bug prone methods
87 [StructLayout(LayoutKind.Explicit, Pack = 0, Size = 16)]
88 public struct Vector4f
99 public float X { get { return x; } set { x = value; } }
100 public float Y { get { return y; } set { y = value; } }
101 public float Z { get { return z; } set { z = value; } }
102 public float W { get { return w; } set { w = value; } }
104 public static Vector4f Pi
106 get { return new Vector4f ((float)System.Math.PI); }
109 public static Vector4f E
111 get { return new Vector4f ((float)System.Math.E); }
114 public static Vector4f One
116 get { return new Vector4f (1); }
119 public static Vector4f Zero
121 get { return new Vector4f (0); }
124 public static Vector4f MinusOne
126 get { return new Vector4f (-1); }
129 [System.Runtime.CompilerServices.IndexerName ("Component")]
130 public unsafe float this [int index]
133 if ((index | 0x3) != 0x3) //index < 0 || index > 3
134 throw new ArgumentOutOfRangeException ("index");
135 fixed (float *v = &x) {
136 return * (v + index);
140 if ( (index | 0x3) != 0x3) //index < 0 || index > 3
141 throw new ArgumentOutOfRangeException ("index");
142 fixed (float *v = &x) {
143 * (v + index) = value;
148 public Vector4f (float x, float y, float z, float w)
156 public Vector4f (float f)
164 [Acceleration (AccelMode.SSE1)]
165 public static unsafe Vector4f operator & (Vector4f v1, Vector4f v2)
167 Vector4f res = new Vector4f ();
178 [Acceleration (AccelMode.SSE1)]
179 public static unsafe Vector4f operator | (Vector4f v1, Vector4f v2)
181 Vector4f res = new Vector4f ();
192 [Acceleration (AccelMode.SSE1)]
193 public static unsafe Vector4f operator ^ (Vector4f v1, Vector4f v2)
195 Vector4f res = new Vector4f ();
206 [Acceleration (AccelMode.SSE1)]
207 public static Vector4f operator + (Vector4f v1, Vector4f v2)
209 return new Vector4f (v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
212 [Acceleration (AccelMode.SSE1)]
213 public static Vector4f operator - (Vector4f v1, Vector4f v2)
215 return new Vector4f (v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
218 [Acceleration (AccelMode.SSE1)]
219 public static Vector4f operator * (Vector4f v1, Vector4f v2)
221 return new Vector4f (v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
224 [Acceleration (AccelMode.SSE1)]
225 public static Vector4f operator * (float scalar, Vector4f v)
227 return new Vector4f (scalar * v.x, scalar * v.y, scalar * v.z, scalar * v.w);
230 [Acceleration (AccelMode.SSE1)]
231 public static Vector4f operator * (Vector4f v, float scalar)
233 return new Vector4f (scalar * v.x, scalar * v.y, scalar * v.z, scalar * v.w);
236 [Acceleration (AccelMode.SSE1)]
237 public static Vector4f operator / (Vector4f v1, Vector4f v2)
239 return new Vector4f (v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w);
242 [Acceleration (AccelMode.SSE2)]
243 public static bool operator ==(Vector4f v1, Vector4f v2)
245 return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w;
248 [Acceleration (AccelMode.SSE2)]
249 public static bool operator !=(Vector4f v1, Vector4f v2)
251 return v1.x != v2.x || v1.y != v2.y || v1.z != v2.z || v1.w != v2.w;
254 [Acceleration (AccelMode.SSE1)]
255 public static unsafe explicit operator Vector2d (Vector4f v)
257 Vector2d* p = (Vector2d*)&v;
261 [Acceleration (AccelMode.SSE1)]
262 public static unsafe explicit operator Vector2l (Vector4f v)
264 Vector2l* p = (Vector2l*)&v;
268 [Acceleration (AccelMode.SSE1)]
269 [CLSCompliant(false)]
270 public static unsafe explicit operator Vector2ul (Vector4f v)
272 Vector2ul* p = (Vector2ul*)&v;
276 [Acceleration (AccelMode.SSE1)]
277 public static unsafe explicit operator Vector4i (Vector4f v)
279 Vector4i* p = (Vector4i*)&v;
283 [Acceleration (AccelMode.SSE1)]
284 [CLSCompliant(false)]
285 public static unsafe explicit operator Vector4ui (Vector4f v)
287 Vector4ui* p = (Vector4ui*)&v;
291 [Acceleration (AccelMode.SSE1)]
292 public static unsafe explicit operator Vector8s (Vector4f v)
294 Vector8s* p = (Vector8s*)&v;
298 [Acceleration (AccelMode.SSE1)]
299 [CLSCompliant(false)]
300 public static unsafe explicit operator Vector8us (Vector4f v)
302 Vector8us* p = (Vector8us*)&v;
306 [Acceleration (AccelMode.SSE1)]
307 [CLSCompliant(false)]
308 public static unsafe explicit operator Vector16sb (Vector4f v)
310 Vector16sb* p = (Vector16sb*)&v;
314 [Acceleration (AccelMode.SSE1)]
315 public static unsafe explicit operator Vector16b (Vector4f v)
317 Vector16b* p = (Vector16b*)&v;
321 [Acceleration (AccelMode.SSE1)]
322 public static Vector4f LoadAligned (ref Vector4f v)
327 [Acceleration (AccelMode.SSE1)]
328 public static void StoreAligned (ref Vector4f res, Vector4f val)
333 [CLSCompliant(false)]
334 [Acceleration (AccelMode.SSE1)]
335 public static unsafe Vector4f LoadAligned (Vector4f *v)
340 [CLSCompliant(false)]
341 [Acceleration (AccelMode.SSE1)]
342 public static unsafe void StoreAligned (Vector4f *res, Vector4f val)
347 [Acceleration (AccelMode.SSE1)]
348 [CLSCompliant(false)]
349 public static void PrefetchTemporalAllCacheLevels (ref Vector4f res)
353 [Acceleration (AccelMode.SSE1)]
354 [CLSCompliant(false)]
355 public static void PrefetchTemporal1stLevelCache (ref Vector4f res)
359 [Acceleration (AccelMode.SSE1)]
360 [CLSCompliant(false)]
361 public static void PrefetchTemporal2ndLevelCache (ref Vector4f res)
365 [Acceleration (AccelMode.SSE1)]
366 [CLSCompliant(false)]
367 public static void PrefetchNonTemporal (ref Vector4f res)
371 [Acceleration (AccelMode.SSE1)]
372 [CLSCompliant(false)]
373 public static unsafe void PrefetchTemporalAllCacheLevels (Vector4f *res)
377 [Acceleration (AccelMode.SSE1)]
378 [CLSCompliant(false)]
379 public static unsafe void PrefetchTemporal1stLevelCache (Vector4f *res)
383 [Acceleration (AccelMode.SSE1)]
384 [CLSCompliant(false)]
385 public static unsafe void PrefetchTemporal2ndLevelCache (Vector4f *res)
389 [Acceleration (AccelMode.SSE1)]
390 [CLSCompliant(false)]
391 public static unsafe void PrefetchNonTemporal (Vector4f *res)
395 public override string ToString()
397 return "<" + x + ", " + y + ", " + z + ", " + w + ">";