1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
4 using System.Runtime.CompilerServices;
6 namespace System.Numerics
8 // This file contains the definitions for all of the JIT intrinsic methods and properties that are recognized by the current x64 JIT compiler.
9 // The implementation defined here is used in any circumstance where the JIT fails to recognize these members as intrinsic.
10 // The JIT recognizes these methods and properties by name and signature: if either is changed, the JIT will no longer recognize the member.
11 // Some methods declared here are not strictly intrinsic, but delegate to an intrinsic method. For example, only one overload of CopyTo()
12 // is actually recognized by the JIT, but both are here for simplicity.
14 public partial struct Vector3
17 /// The X component of the vector.
21 /// The Y component of the vector.
25 /// The Z component of the vector.
31 /// Constructs a vector whose elements are all the single specified value.
33 /// <param name="value">The element to fill the vector with.</param>
35 public Vector3(Single value) : this(value, value, value) { }
38 /// Constructs a Vector3 from the given Vector2 and a third value.
40 /// <param name="value">The Vector to extract X and Y components from.</param>
41 /// <param name="z">The Z component.</param>
42 public Vector3(Vector2 value, float z) : this(value.X, value.Y, z) { }
45 /// Constructs a vector with the given individual elements.
47 /// <param name="x">The X component.</param>
48 /// <param name="y">The Y component.</param>
49 /// <param name="z">The Z component.</param>
51 public Vector3(Single x, Single y, Single z)
57 #endregion Constructors
59 #region Public Instance Methods
61 /// Copies the contents of the vector into the given array.
63 [MethodImpl(MethodImplOptions.AggressiveInlining)]
64 public void CopyTo(Single[] array)
70 /// Copies the contents of the vector into the given array, starting from index.
72 /// <exception cref="ArgumentNullException">If array is null.</exception>
73 /// <exception cref="RankException">If array is multidimensional.</exception>
74 /// <exception cref="ArgumentOutOfRangeException">If index is greater than end of the array or index is less than zero.</exception>
75 /// <exception cref="ArgumentException">If number of elements in source vector is greater than those available in destination array.</exception>
77 [MethodImpl(MethodImplOptions.AggressiveInlining)]
78 public void CopyTo(Single[] array, int index)
81 throw new ArgumentNullException("values");
82 if (index < 0 || index >= array.Length)
83 throw new ArgumentOutOfRangeException(SR.GetString("Arg_ArgumentOutOfRangeException", index));
84 if ((array.Length - index) < 3)
85 throw new ArgumentException(SR.GetString("Arg_ElementsInSourceIsGreaterThanDestination", index));
92 /// Returns a boolean indicating whether the given Vector3 is equal to this Vector3 instance.
94 /// <param name="other">The Vector3 to compare this instance to.</param>
95 /// <returns>True if the other Vector3 is equal to this instance; False otherwise.</returns>
97 public bool Equals(Vector3 other)
99 return X == other.X &&
103 #endregion Public Instance Methods
105 #region Public Static Methods
107 /// Returns the dot product of two vectors.
109 /// <param name="vector1">The first vector.</param>
110 /// <param name="vector2">The second vector.</param>
111 /// <returns>The dot product.</returns>
113 [MethodImpl(MethodImplOptions.AggressiveInlining)]
114 public static float Dot(Vector3 vector1, Vector3 vector2)
116 return vector1.X * vector2.X +
117 vector1.Y * vector2.Y +
118 vector1.Z * vector2.Z;
122 /// Returns a vector whose elements are the minimum of each of the pairs of elements in the two source vectors.
124 /// <param name="value1">The first source vector.</param>
125 /// <param name="value2">The second source vector.</param>
126 /// <returns>The minimized vector.</returns>
128 public static Vector3 Min(Vector3 value1, Vector3 value2)
131 (value1.X < value2.X) ? value1.X : value2.X,
132 (value1.Y < value2.Y) ? value1.Y : value2.Y,
133 (value1.Z < value2.Z) ? value1.Z : value2.Z);
137 /// Returns a vector whose elements are the maximum of each of the pairs of elements in the two source vectors.
139 /// <param name="value1">The first source vector.</param>
140 /// <param name="value2">The second source vector.</param>
141 /// <returns>The maximized vector.</returns>
143 [MethodImpl(MethodImplOptions.AggressiveInlining)]
144 public static Vector3 Max(Vector3 value1, Vector3 value2)
147 (value1.X > value2.X) ? value1.X : value2.X,
148 (value1.Y > value2.Y) ? value1.Y : value2.Y,
149 (value1.Z > value2.Z) ? value1.Z : value2.Z);
153 /// Returns a vector whose elements are the absolute values of each of the source vector's elements.
155 /// <param name="value">The source vector.</param>
156 /// <returns>The absolute value vector.</returns>
158 [MethodImpl(MethodImplOptions.AggressiveInlining)]
159 public static Vector3 Abs(Vector3 value)
161 return new Vector3(Math.Abs(value.X), Math.Abs(value.Y), Math.Abs(value.Z));
165 /// Returns a vector whose elements are the square root of each of the source vector's elements.
167 /// <param name="value">The source vector.</param>
168 /// <returns>The square root vector.</returns>
170 [MethodImpl(MethodImplOptions.AggressiveInlining)]
171 public static Vector3 SquareRoot(Vector3 value)
173 return new Vector3((Single)Math.Sqrt(value.X), (Single)Math.Sqrt(value.Y), (Single)Math.Sqrt(value.Z));
175 #endregion Public Static Methods
177 #region Public Static Operators
179 /// Adds two vectors together.
181 /// <param name="left">The first source vector.</param>
182 /// <param name="right">The second source vector.</param>
183 /// <returns>The summed vector.</returns>
185 [MethodImpl(MethodImplOptions.AggressiveInlining)]
186 public static Vector3 operator +(Vector3 left, Vector3 right)
188 return new Vector3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
192 /// Subtracts the second vector from the first.
194 /// <param name="left">The first source vector.</param>
195 /// <param name="right">The second source vector.</param>
196 /// <returns>The difference vector.</returns>
198 [MethodImpl(MethodImplOptions.AggressiveInlining)]
199 public static Vector3 operator -(Vector3 left, Vector3 right)
201 return new Vector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
205 /// Multiplies two vectors together.
207 /// <param name="left">The first source vector.</param>
208 /// <param name="right">The second source vector.</param>
209 /// <returns>The product vector.</returns>
211 [MethodImpl(MethodImplOptions.AggressiveInlining)]
212 public static Vector3 operator *(Vector3 left, Vector3 right)
214 return new Vector3(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
218 /// Multiplies a vector by the given scalar.
220 /// <param name="left">The source vector.</param>
221 /// <param name="right">The scalar value.</param>
222 /// <returns>The scaled vector.</returns>
224 [MethodImpl(MethodImplOptions.AggressiveInlining)]
225 public static Vector3 operator *(Vector3 left, Single right)
227 return left * new Vector3(right);
231 /// Multiplies a vector by the given scalar.
233 /// <param name="left">The scalar value.</param>
234 /// <param name="right">The source vector.</param>
235 /// <returns>The scaled vector.</returns>
237 [MethodImpl(MethodImplOptions.AggressiveInlining)]
238 public static Vector3 operator *(Single left, Vector3 right)
240 return new Vector3(left) * right;
244 /// Divides the first vector by the second.
246 /// <param name="left">The first source vector.</param>
247 /// <param name="right">The second source vector.</param>
248 /// <returns>The vector resulting from the division.</returns>
250 [MethodImpl(MethodImplOptions.AggressiveInlining)]
251 public static Vector3 operator /(Vector3 left, Vector3 right)
253 return new Vector3(left.X / right.X, left.Y / right.Y, left.Z / right.Z);
257 /// Divides the vector by the given scalar.
259 /// <param name="value1">The source vector.</param>
260 /// <param name="value2">The scalar value.</param>
261 /// <returns>The result of the division.</returns>
263 [MethodImpl(MethodImplOptions.AggressiveInlining)]
264 public static Vector3 operator /(Vector3 value1, float value2)
266 float invDiv = 1.0f / value2;
275 /// Negates a given vector.
277 /// <param name="value">The source vector.</param>
278 /// <returns>The negated vector.</returns>
279 [MethodImpl(MethodImplOptions.AggressiveInlining)]
280 public static Vector3 operator -(Vector3 value)
286 /// Returns a boolean indicating whether the two given vectors are equal.
288 /// <param name="left">The first vector to compare.</param>
289 /// <param name="right">The second vector to compare.</param>
290 /// <returns>True if the vectors are equal; False otherwise.</returns>
292 [MethodImpl(MethodImplOptions.AggressiveInlining)]
293 public static bool operator ==(Vector3 left, Vector3 right)
295 return (left.X == right.X &&
301 /// Returns a boolean indicating whether the two given vectors are not equal.
303 /// <param name="left">The first vector to compare.</param>
304 /// <param name="right">The second vector to compare.</param>
305 /// <returns>True if the vectors are not equal; False if they are equal.</returns>
306 [MethodImpl(MethodImplOptions.AggressiveInlining)]
307 public static bool operator !=(Vector3 left, Vector3 right)
309 return (left.X != right.X ||
313 #endregion Public Static Operators