-
Notifications
You must be signed in to change notification settings - Fork 1
/
CPUMath.cs
54 lines (48 loc) · 1.53 KB
/
CPUMath.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
using System.Runtime.Intrinsics.X86;
using System.Runtime.Intrinsics.Arm;
namespace Saket.Particles
{
public unsafe static class CPUMath
{
public static void Add(float[] a, float[] b, int startIndex, int endIndex)
{
if (Avx.IsSupported)
{
fixed (float* ptrA = a, ptrB = b)
{
for (int i = startIndex; i < endIndex; i += 8)
{
Avx.Store(&ptrA[i], Avx.Add(Avx.LoadVector256(&ptrA[i]), Avx.LoadVector256(&ptrB[i])));
}
}
}
else if (Sse.IsSupported)
{
fixed (float* ptrA = a, ptrB = b)
{
for (int i = startIndex; i < endIndex; i += 4)
{
Sse.Store(&ptrA[i], Sse.Add(Sse.LoadVector128(&ptrA[i]), Sse.LoadVector128(&ptrB[i])));
}
}
}
else if (AdvSimd.IsSupported)
{
fixed (float* ptrA = a, ptrB = b)
{
for (int i = startIndex; i < endIndex; i += 4)
{
AdvSimd.Store(&ptrA[i], AdvSimd.Add(AdvSimd.LoadVector128(&ptrA[i]), AdvSimd.LoadVector128(&ptrB[i])));
}
}
}
else
{
for (int i = startIndex; i < endIndex; i += 1)
{
a[i] += b[i];
}
}
}
}
}