SIMD optimizations
This commit is contained in:
@@ -90,9 +90,7 @@ jobs:
|
|||||||
- name: Setup .NET
|
- name: Setup .NET
|
||||||
uses: actions/setup-dotnet@v4
|
uses: actions/setup-dotnet@v4
|
||||||
with:
|
with:
|
||||||
dotnet-version: |
|
dotnet-version: '8.0'
|
||||||
8.0
|
|
||||||
9.0
|
|
||||||
|
|
||||||
- name: Download Dalamud
|
- name: Download Dalamud
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
+6
-15
@@ -5,9 +5,11 @@ using System.Runtime.Intrinsics;
|
|||||||
using System.Runtime.Intrinsics.X86;
|
using System.Runtime.Intrinsics.X86;
|
||||||
|
|
||||||
namespace Craftimizer.Solver;
|
namespace Craftimizer.Solver;
|
||||||
|
|
||||||
|
[SkipLocalsInit]
|
||||||
|
[Pure]
|
||||||
internal static class Intrinsics
|
internal static class Intrinsics
|
||||||
{
|
{
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
// https://stackoverflow.com/a/73439472
|
// https://stackoverflow.com/a/73439472
|
||||||
private static Vector128<float> HMax(Vector256<float> v1)
|
private static Vector128<float> HMax(Vector256<float> v1)
|
||||||
@@ -21,7 +23,6 @@ internal static class Intrinsics
|
|||||||
return v7;
|
return v7;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static int HMaxIndexScalar(Vector256<float> v, int len)
|
private static int HMaxIndexScalar(Vector256<float> v, int len)
|
||||||
{
|
{
|
||||||
@@ -34,7 +35,6 @@ internal static class Intrinsics
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static Vector256<float> ClearLastN(Vector256<float> data, int len)
|
private static Vector256<float> ClearLastN(Vector256<float> data, int len)
|
||||||
{
|
{
|
||||||
@@ -43,7 +43,6 @@ internal static class Intrinsics
|
|||||||
return Avx.And(Avx2.CompareGreaterThan(threshold, index).AsSingle(), data);
|
return Avx.And(Avx2.CompareGreaterThan(threshold, index).AsSingle(), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
// https://stackoverflow.com/a/23592221
|
// https://stackoverflow.com/a/23592221
|
||||||
private static int HMaxIndexAVX2(Vector256<float> v, int len)
|
private static int HMaxIndexAVX2(Vector256<float> v, int len)
|
||||||
@@ -57,21 +56,17 @@ internal static class Intrinsics
|
|||||||
|
|
||||||
// Find the highest index with that value, respecting len
|
// Find the highest index with that value, respecting len
|
||||||
var vcmp = Avx.CompareEqual(vfilt, vmax);
|
var vcmp = Avx.CompareEqual(vfilt, vmax);
|
||||||
var mask = unchecked((uint)Avx2.MoveMask(vcmp.AsByte()));
|
var mask = unchecked((uint)Avx.MoveMask(vcmp));
|
||||||
|
|
||||||
var inverseIdx = BitOperations.LeadingZeroCount(mask << ((8 - len) << 2)) >> 2;
|
return BitOperations.TrailingZeroCount(mask);
|
||||||
|
|
||||||
return len - 1 - inverseIdx;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static int HMaxIndex(Vector256<float> v, int len) =>
|
public static int HMaxIndex(Vector256<float> v, int len) =>
|
||||||
Avx2.IsSupported ?
|
Avx2.IsSupported ?
|
||||||
HMaxIndexAVX2(v, len) :
|
HMaxIndexAVX2(v, len) :
|
||||||
HMaxIndexScalar(v, len);
|
HMaxIndexScalar(v, len);
|
||||||
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static int NthBitSetScalar(ulong value, int n)
|
private static int NthBitSetScalar(ulong value, int n)
|
||||||
{
|
{
|
||||||
@@ -101,12 +96,10 @@ internal static class Intrinsics
|
|||||||
return _base;
|
return _base;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static int NthBitSetBMI2(ulong value, int n) =>
|
private static int NthBitSetBMI2(ulong value, int n) =>
|
||||||
BitOperations.TrailingZeroCount(Bmi2.X64.ParallelBitDeposit(1ul << n, value));
|
BitOperations.TrailingZeroCount(Bmi2.X64.ParallelBitDeposit(1ul << n, value));
|
||||||
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static int NthBitSet(ulong value, int n)
|
public static int NthBitSet(ulong value, int n)
|
||||||
{
|
{
|
||||||
@@ -118,12 +111,10 @@ internal static class Intrinsics
|
|||||||
NthBitSetScalar(value, n);
|
NthBitSetScalar(value, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
[SkipLocalsInit]
|
|
||||||
public static Vector256<float> ReciprocalSqrt(Vector256<float> data)
|
public static Vector256<float> ReciprocalSqrt(Vector256<float> data)
|
||||||
{
|
{
|
||||||
if (Avx.IsSupported && Vector256<float>.Count >= Vector256<float>.Count)
|
if (Avx.IsSupported)
|
||||||
return Avx.ReciprocalSqrt(data);
|
return Avx.ReciprocalSqrt(data);
|
||||||
|
|
||||||
Unsafe.SkipInit(out Vector256<float> ret);
|
Unsafe.SkipInit(out Vector256<float> ret);
|
||||||
|
|||||||
@@ -5,13 +5,11 @@ namespace Craftimizer.Solver;
|
|||||||
[StructLayout(LayoutKind.Auto)]
|
[StructLayout(LayoutKind.Auto)]
|
||||||
public sealed class RootScores
|
public sealed class RootScores
|
||||||
{
|
{
|
||||||
public float ScoreSum;
|
|
||||||
public float MaxScore;
|
public float MaxScore;
|
||||||
public int Visits;
|
public int Visits;
|
||||||
|
|
||||||
public void Visit(float score)
|
public void Visit(float score)
|
||||||
{
|
{
|
||||||
ScoreSum += score;
|
|
||||||
MaxScore = Math.Max(MaxScore, score);
|
MaxScore = Math.Max(MaxScore, score);
|
||||||
Visits++;
|
Visits++;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user