-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathf8.cs
313 lines (228 loc) · 14.1 KB
/
f8.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Runtime.InteropServices;
namespace Ara3D.F8
{
/// <summary>
/// SIMD types can provide up to and beyond 8x improvements over traditional operations involving floats, by using extra wide
/// registers on the system, and providing special opcodes for operating on them.
///
/// A specific set of opcodes that can operate on 8 floats at a time known as AVX (Advanced Vector Extensions) is
/// widely available on the CPUs of most modern laptop and desktop computers.
/// https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#CPUs_with_AVX
///
/// Working with SIMD types and intrinsics C# however can be quite confusing for the uninitiated.
/// There are over a thousand intrinsic opcodes, and many are poorly documented, and scattered across dozens of classes.
/// ChatGPT is unaware of recent introductions of utility functions .NET 9 that make working with SIMD types much easier.
///
/// This class provides a wrapper around the Vector256<float> type and provides many of the basic math operations
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly struct f8
{
public readonly Vector256<float> Value;
//-------------------------------------------------------------------------------------
// Constructors
//-------------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8(Vector256<float> value) => Value = value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8(float scalar) => Value = Vector256.Create(scalar);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8(float f0, float f1, float f2, float f3, float f4, float f5, float f6, float f7)
=> Value = Vector256.Create(f0, f1, f2, f3, f4, f5, f6, f7);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8(Vector128<float> upper, Vector128<float> lower) => Value = Vector256.Create(lower, upper);
//-------------------------------------------------------------------------------------
// Implicit operators
//-------------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector256<float>(f8 value) => value.Value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator f8(Vector256<float> value) => new(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator f8(float value) => new(value);
//-------------------------------------------------------------------------------------
// Constants
//-------------------------------------------------------------------------------------
public static f8 Zero = new(0);
public static f8 One = new(1);
public static f8 AllBitsSet = new(Vector256<float>.AllBitsSet);
public static f8 SignMask = Vector256.Create(0x80000000u).AsSingle();
public static f8 Indices => Vector256<float>.Indices;
//-------------------------------------------------------------------------------------
// Indexer
//-------------------------------------------------------------------------------------
public float this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Value.GetElement(index);
}
public int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => 8;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float GetElement(int index) => Value.GetElement(index);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector128<float> GetLower() => Value.GetLower();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector128<float> GetUpper() => Value.GetUpper();
//-------------------------------------------------------------------------------------
// Operator Overloads
//-------------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator +(f8 left, f8 right) => Vector256.Add(left.Value, right.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator -(f8 left, f8 right) => Vector256.Subtract(left.Value, right.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator *(f8 left, f8 right) => Vector256.Multiply(left.Value, right.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator *(f8 left, float scalar) => Vector256.Multiply(left.Value, scalar);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator *(float scalar, f8 right) => Vector256.Multiply(scalar, right.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator /(f8 left, f8 right) => Vector256.Divide(left.Value, right.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator /(f8 left, float scalar) => Vector256.Divide(left.Value, scalar);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator /(float scalar, f8 right) => Vector256.Divide(new f8(scalar), right.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator -(f8 value) => Vector256.Negate(value.Value);
//-------------------------------------------------------------------------------------
// Bitwise functions
//-------------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 AndNot(f8 a, f8 b) => Vector256.AndNot(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator &(f8 a, f8 b) => Vector256.BitwiseAnd(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator |(f8 a, f8 b) => Vector256.BitwiseOr(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator ~(f8 a) => Vector256.OnesComplement(a.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator^(f8 a, f8 b) => Vector256.Xor(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 ConditionalSelect(f8 condition, f8 a, f8 b) => Vector256.ConditionalSelect(condition.Value, a.Value, b.Value);
//-------------------------------------------------------------------------------------
// Comparison operators
//-------------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator==(f8 a, f8 b) => Vector256.Equals(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator!=(f8 a, f8 b) => ~Vector256.Equals(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator<(f8 a, f8 b) => Vector256.LessThan(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator<=(f8 a, f8 b) => Vector256.LessThanOrEqual(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator>(f8 a, f8 b) => Vector256.GreaterThan(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 operator>=(f8 a, f8 b) => Vector256.GreaterThanOrEqual(a.Value, b.Value);
//-------------------------------------------------------------------------------------
// Comparison functions
//-------------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 Max(f8 a, f8 b) => Vector256.Max(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 Min(f8 a, f8 b) => Vector256.Min(a.Value, b.Value);
//-------------------------------------------------------------------------------------
// Basic math functions
//-------------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Sin() => Vector256.Sin(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Cos() => Vector256.Cos(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (f8, f8) SinCos() => Vector256.SinCos(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Abs() => Vector256.Abs(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Ceiling() => Vector256.Ceiling(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Clamp(f8 min, f8 max) => Vector256.Clamp(Value, min.Value, max.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 DegreesToRadians() => Vector256.DegreesToRadians(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 CopySign(f8 value, f8 sign) => Vector256.CopySign(value.Value, sign.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(f8 a, f8 b) => Vector256.Dot(a.Value, b.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Exp() => Vector256.Exp(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Floor() => Vector256.Floor(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 Hypot(f8 x, f8 y) => Vector256.Hypot(x.Value, y.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 IsNaN() => Vector256.IsNaN(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 IsNegative() => Vector256.IsNegative(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 IsPositive() => Vector256.IsPositive(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 IsPositiveInfinity() => Vector256.IsPositiveInfinity(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 IsZero() => Vector256.IsZero(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static f8 Lerp(f8 a, f8 b, f8 t) => Vector256.Lerp(a.Value, b.Value, t.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Log() => Vector256.Log(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Log2() => Vector256.Log2(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 RadiansToDegrees() => Vector256.RadiansToDegrees(Value);
/// <summary>Reciprocal (1/x) of each element</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Reciprocal() => Avx.Reciprocal(Value);
/// <summary>Approximate reciprocal of the square root of each element: 1 / sqrt(x)</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 ReciprocalSqrt() => Avx.ReciprocalSqrt(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Round(MidpointRounding mr) => Vector256.Round(Value, mr);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Sign() => Vector256.CopySign(One.Value, Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Sqrt() => Vector256.Sqrt(Value);
/// <summary>Square each element</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Square() => this * this;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Sum() => Vector256.Sum(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Tan()
{
var (a, b) = SinCos();
return a / b;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float FirstElement() => Vector256.ToScalar(Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 Truncate() => Vector256.Truncate(Value);
//-------------------------------------------------------------------------------------
// Pseudo-mutation operators
//-------------------------------------------------------------------------------------
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 WithElement(int i, float f) => Vector256.WithElement(this, i, f);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 WithLower(Vector128<float> lower) => Vector256.WithLower(this, lower);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public f8 WithUpper(Vector128<float> upper) => Vector256.WithUpper(this, upper);
//-------------------------------------------------------------------------------------
// Overrides
//-------------------------------------------------------------------------------------
public override string ToString()
=> $"[{this[0]}, {this[1]}, {this[2]}, {this[3]}, {this[4]}, {this[5]}, {this[6]}, {this[7]}]";
public override bool Equals(object? obj)
=> obj is f8 other && Vector256.EqualsAll(Value, other.Value);
public override int GetHashCode()
{
// Combine hash codes from each element
int hash = 17;
for (int i = 0; i < 8; i++)
hash = hash * 31 + this[i].GetHashCode();
return hash;
}
}
}