forked from bepu/bepuphysics2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector4Wide.cs
269 lines (247 loc) · 10.8 KB
/
Vector4Wide.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace BepuUtilities
{
/// <summary>
/// Four dimensional vector with <see cref="Vector{T}.Count"/> (with generic type argument of <see cref="float"/>) SIMD lanes.
/// </summary>
public struct Vector4Wide
{
public Vector<float> X;
public Vector<float> Y;
public Vector<float> Z;
public Vector<float> W;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Broadcast(in Vector4 source, out Vector4Wide broadcasted)
{
broadcasted.X = new Vector<float>(source.X);
broadcasted.Y = new Vector<float>(source.Y);
broadcasted.Z = new Vector<float>(source.Z);
broadcasted.W = new Vector<float>(source.W);
}
/// <summary>
/// Performs a componentwise add between two vectors.
/// </summary>
/// <param name="a">First vector to add.</param>
/// <param name="b">Second vector to add.</param>
/// <param name="result">Sum of a and b.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add(Vector4Wide a, Vector4Wide b, out Vector4Wide result)
{
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
result.Z = a.Z + b.Z;
result.W = a.W + b.W;
}
/// <summary>
/// Finds the result of adding a scalar to every component of a vector.
/// </summary>
/// <param name="v">Vector to add to.</param>
/// <param name="s">Scalar to add to every component of the vector.</param>
/// <param name="result">Vector with components equal to the input vector added to the input scalar.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add(Vector4Wide v, Vector<float> s, out Vector4Wide result)
{
result.X = v.X + s;
result.Y = v.Y + s;
result.Z = v.Z + s;
result.W = v.W + s;
}
/// <summary>
/// Performs a componentwise add between two vectors.
/// </summary>
/// <param name="a">First vector to add.</param>
/// <param name="b">Second vector to add.</param>
/// <returns>Sum of a and b.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector4Wide operator +(Vector4Wide a, Vector4Wide b)
{
Vector4Wide result;
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
result.Z = a.Z + b.Z;
result.W = a.W + b.W;
return result;
}
/// <summary>
/// Finds the result of adding a scalar to every component of a vector.
/// </summary>
/// <param name="v">Vector to add to.</param>
/// <param name="s">Scalar to add to every component of the vector.</param>
/// <returns>Vector with components equal to the input vector added to the input scalar.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector4Wide operator +(Vector4Wide v, Vector<float> s)
{
Vector4Wide result;
result.X = v.X + s;
result.Y = v.Y + s;
result.Z = v.Z + s;
result.W = v.W + s;
return result;
}
/// <summary>
/// Finds the result of adding a scalar to every component of a vector.
/// </summary>
/// <param name="v">Vector to add to.</param>
/// <param name="s">Scalar to add to every component of the vector.</param>
/// <returns>Vector with components equal to the input vector added to the input scalar.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector4Wide operator +(Vector<float> s, Vector4Wide v)
{
Vector4Wide result;
result.X = v.X + s;
result.Y = v.Y + s;
result.Z = v.Z + s;
result.W = v.W + s;
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Subtract(in Vector4Wide a, in Vector4Wide b, out Vector4Wide result)
{
result.X = a.X - b.X;
result.Y = a.Y - b.Y;
result.Z = a.Z - b.Z;
result.W = a.W - b.W;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Dot(in Vector4Wide a, in Vector4Wide b, out Vector<float> result)
{
result = a.X * b.X + a.Y * b.Y + a.Z * b.Z + a.W * b.W;
}
/// <summary>
/// Computes the per-component minimum of two vectors.
/// </summary>
/// <param name="a">First vector whose components will be compared.</param>
/// <param name="b">Second vector whose components will be compared.</param>
/// <param name="result">Vector with components matching the smaller of the two input vectors.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Min(in Vector4Wide a, in Vector4Wide b, out Vector4Wide result)
{
result.X = Vector.Min(a.X, b.X);
result.Y = Vector.Min(a.Y, b.Y);
result.Z = Vector.Min(a.Z, b.Z);
result.W = Vector.Min(a.W, b.W);
}
/// <summary>
/// Computes the per-component maximum of two vectors.
/// </summary>
/// <param name="a">First vector whose components will be compared.</param>
/// <param name="b">Second vector whose components will be compared.</param>
/// <param name="result">Vector with components matching the larger of the two input vectors.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Max(in Vector4Wide a, in Vector4Wide b, out Vector4Wide result)
{
result.X = Vector.Max(a.X, b.X);
result.Y = Vector.Max(a.Y, b.Y);
result.Z = Vector.Max(a.Z, b.Z);
result.W = Vector.Max(a.W, b.W);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Scale(in Vector4Wide vector, in Vector<float> scalar, out Vector4Wide result)
{
result.X = vector.X * scalar;
result.Y = vector.Y * scalar;
result.Z = vector.Z * scalar;
result.W = vector.W * scalar;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Abs(in Vector4Wide vector, out Vector4Wide result)
{
result.X = Vector.Abs(vector.X);
result.Y = Vector.Abs(vector.Y);
result.Z = Vector.Abs(vector.Z);
result.W = Vector.Abs(vector.Z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Negate(in Vector4Wide v, out Vector4Wide result)
{
result.X = -v.X;
result.Y = -v.Y;
result.Z = -v.Z;
result.W = -v.W;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref Vector4Wide Negate(ref Vector4Wide v)
{
v.X = -v.X;
v.Y = -v.Y;
v.Z = -v.Z;
v.Z = -v.W;
return ref v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LengthSquared(in Vector4Wide v, out Vector<float> lengthSquared)
{
lengthSquared = v.X * v.X + v.Y * v.Y + v.Z * v.Z + v.W * v.W;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Length(in Vector4Wide v, out Vector<float> length)
{
length = Vector.SquareRoot(v.X * v.X + v.Y * v.Y + v.Z * v.Z + v.W * v.W);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Distance(in Vector4Wide a, in Vector4Wide b, out Vector<float> distance)
{
Subtract(b, a, out var offset);
Length(offset, out distance);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Normalize(in Vector4Wide v, out Vector4Wide result)
{
Length(v, out var length);
var scale = Vector<float>.One / length;
Scale(v, scale, out result);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ConditionalSelect(in Vector<int> condition, in Vector4Wide left, in Vector4Wide right, out Vector4Wide result)
{
result.X = Vector.ConditionalSelect(condition, left.X, right.X);
result.Y = Vector.ConditionalSelect(condition, left.Y, right.Y);
result.Z = Vector.ConditionalSelect(condition, left.Z, right.Z);
result.W = Vector.ConditionalSelect(condition, left.W, right.W);
}
/// <summary>
/// Pulls one lane out of the wide representation.
/// </summary>
/// <param name="wide">Source of the lane.</param>
/// <param name="slotIndex">Index of the lane within the wide representation to read.</param>
/// <param name="narrow">Non-SIMD type to store the lane in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReadSlot(ref Vector4Wide wide, int slotIndex, out Vector4 narrow)
{
ref var offset = ref GatherScatter.GetOffsetInstance(ref wide, slotIndex);
ReadFirst(offset, out narrow);
}
/// <summary>
/// Pulls one lane out of the wide representation.
/// </summary>
/// <param name="source">Source of the lane.</param>
/// <param name="target">Non-SIMD type to store the lane in.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReadFirst(in Vector4Wide source, out Vector4 target)
{
target.X = source.X[0];
target.Y = source.Y[0];
target.Z = source.Z[0];
target.W = source.W[0];
}
/// <summary>
/// Gathers values from a vector and places them into the first indices of the target vector.
/// </summary>
/// <param name="source">Vector to copy values from.</param>
/// <param name="targetSlot">Wide vectorto place values into.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteFirst(in Vector4 source, ref Vector4Wide targetSlot)
{
GatherScatter.GetFirst(ref targetSlot.X) = source.X;
GatherScatter.GetFirst(ref targetSlot.Y) = source.Y;
GatherScatter.GetFirst(ref targetSlot.Z) = source.Z;
GatherScatter.GetFirst(ref targetSlot.W) = source.W;
}
public override string ToString()
{
return $"<{X}, {Y}, {Z}, {W}>";
}
}
}