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
85 Single float constructor (expand it to the 4 positions)
86 Replace Shuffle with less bug prone methods
89 [StructLayout(LayoutKind.Explicit, Pack = 0, Size = 16)]
90 public struct Vector4f
101 public float X { get { return x; } set { x = value; } }
102 public float Y { get { return y; } set { y = value; } }
103 public float Z { get { return z; } set { z = value; } }
104 public float W { get { return w; } set { w = value; } }
106 public static Vector4f Pi
108 get { return new Vector4f ((float)System.Math.PI); }
111 public static Vector4f E
113 get { return new Vector4f ((float)System.Math.E); }
116 public static Vector4f One
118 get { return new Vector4f (1); }
121 public static Vector4f Zero
123 get { return new Vector4f (0); }
126 public static Vector4f MinusOne
128 get { return new Vector4f (-1); }
131 [System.Runtime.CompilerServices.IndexerName ("Component")]
132 public unsafe float this [int index]
135 if ((index | 0x3) != 0x3) //index < 0 || index > 3
136 throw new ArgumentOutOfRangeException ("index");
137 fixed (float *v = &x) {
138 return * (v + index);
142 if ( (index | 0x3) != 0x3) //index < 0 || index > 3
143 throw new ArgumentOutOfRangeException ("index");
144 fixed (float *v = &x) {
145 * (v + index) = value;
150 public Vector4f (float x, float y, float z, float w)
158 public Vector4f (float f)
166 [Acceleration (AccelMode.SSE1)]
167 public static unsafe Vector4f operator & (Vector4f v1, Vector4f v2)
169 Vector4f res = new Vector4f ();
180 [Acceleration (AccelMode.SSE1)]
181 public static unsafe Vector4f operator | (Vector4f v1, Vector4f v2)
183 Vector4f res = new Vector4f ();
194 [Acceleration (AccelMode.SSE1)]
195 public static unsafe Vector4f operator ^ (Vector4f v1, Vector4f v2)
197 Vector4f res = new Vector4f ();
208 [Acceleration (AccelMode.SSE1)]
209 public static Vector4f operator + (Vector4f v1, Vector4f v2)
211 return new Vector4f (v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
214 [Acceleration (AccelMode.SSE1)]
215 public static Vector4f operator - (Vector4f v1, Vector4f v2)
217 return new Vector4f (v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
220 [Acceleration (AccelMode.SSE1)]
221 public static Vector4f operator * (Vector4f v1, Vector4f v2)
223 return new Vector4f (v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
226 [Acceleration (AccelMode.SSE1)]
227 public static Vector4f operator / (Vector4f v1, Vector4f v2)
229 return new Vector4f (v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w);
232 [Acceleration (AccelMode.SSE2)]
233 public static bool operator ==(Vector4f v1, Vector4f v2)
235 return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w;
238 [Acceleration (AccelMode.SSE2)]
239 public static bool operator !=(Vector4f v1, Vector4f v2)
241 return v1.x != v2.x || v1.y != v2.y || v1.z != v2.z || v1.w != v2.w;
244 [Acceleration (AccelMode.SSE1)]
245 public static unsafe explicit operator Vector2d (Vector4f v)
247 Vector2d* p = (Vector2d*)&v;
251 [Acceleration (AccelMode.SSE1)]
252 public static unsafe explicit operator Vector2l (Vector4f v)
254 Vector2l* p = (Vector2l*)&v;
258 [Acceleration (AccelMode.SSE1)]
259 [CLSCompliant(false)]
260 public static unsafe explicit operator Vector2ul (Vector4f v)
262 Vector2ul* p = (Vector2ul*)&v;
266 [Acceleration (AccelMode.SSE1)]
267 public static unsafe explicit operator Vector4i (Vector4f v)
269 Vector4i* p = (Vector4i*)&v;
273 [Acceleration (AccelMode.SSE1)]
274 [CLSCompliant(false)]
275 public static unsafe explicit operator Vector4ui (Vector4f v)
277 Vector4ui* p = (Vector4ui*)&v;
281 [Acceleration (AccelMode.SSE1)]
282 public static unsafe explicit operator Vector8s (Vector4f v)
284 Vector8s* p = (Vector8s*)&v;
288 [Acceleration (AccelMode.SSE1)]
289 [CLSCompliant(false)]
290 public static unsafe explicit operator Vector8us (Vector4f v)
292 Vector8us* p = (Vector8us*)&v;
296 [Acceleration (AccelMode.SSE1)]
297 [CLSCompliant(false)]
298 public static unsafe explicit operator Vector16sb (Vector4f v)
300 Vector16sb* p = (Vector16sb*)&v;
304 [Acceleration (AccelMode.SSE1)]
305 public static unsafe explicit operator Vector16b (Vector4f v)
307 Vector16b* p = (Vector16b*)&v;
311 [Acceleration (AccelMode.SSE1)]
312 public static Vector4f LoadAligned (ref Vector4f v)
317 [Acceleration (AccelMode.SSE1)]
318 public static void StoreAligned (ref Vector4f res, Vector4f val)
323 [CLSCompliant(false)]
324 [Acceleration (AccelMode.SSE1)]
325 public static unsafe Vector4f LoadAligned (Vector4f *v)
330 [CLSCompliant(false)]
331 [Acceleration (AccelMode.SSE1)]
332 public static unsafe void StoreAligned (Vector4f *res, Vector4f val)
337 [Acceleration (AccelMode.SSE1)]
338 [CLSCompliant(false)]
339 public static void PrefetchTemporalAllCacheLevels (ref Vector4f res)
343 [Acceleration (AccelMode.SSE1)]
344 [CLSCompliant(false)]
345 public static void PrefetchTemporal1stLevelCache (ref Vector4f res)
349 [Acceleration (AccelMode.SSE1)]
350 [CLSCompliant(false)]
351 public static void PrefetchTemporal2ndLevelCache (ref Vector4f res)
355 [Acceleration (AccelMode.SSE1)]
356 [CLSCompliant(false)]
357 public static void PrefetchNonTemporal (ref Vector4f res)
361 [Acceleration (AccelMode.SSE1)]
362 [CLSCompliant(false)]
363 public static unsafe void PrefetchTemporalAllCacheLevels (Vector4f *res)
367 [Acceleration (AccelMode.SSE1)]
368 [CLSCompliant(false)]
369 public static unsafe void PrefetchTemporal1stLevelCache (Vector4f *res)
373 [Acceleration (AccelMode.SSE1)]
374 [CLSCompliant(false)]
375 public static unsafe void PrefetchTemporal2ndLevelCache (Vector4f *res)
379 [Acceleration (AccelMode.SSE1)]
380 [CLSCompliant(false)]
381 public static unsafe void PrefetchNonTemporal (Vector4f *res)
385 public override string ToString()
387 return "<" + x + ", " + y + ", " + z + ", " + w + ">";