Use mask for ActionPool

This commit is contained in:
Asriel Camora
2024-02-29 02:14:46 -08:00
parent 2c15b23f48
commit 85922b225c
4 changed files with 123 additions and 64 deletions
+34 -55
View File
@@ -1,5 +1,6 @@
using Craftimizer.Simulator.Actions; using Craftimizer.Simulator.Actions;
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@@ -39,77 +40,55 @@ public readonly struct ActionPool
}); });
public const int MaskSize = 32; public const int MaskSize = 32;
public const int EnumSize = 37;
private unsafe struct EnumBuffer // Bitmask of accepted actions
{ private readonly ulong acceptedActions;
private fixed byte data[MaskSize];
public ActionType this[int index] => (ActionType)data[index]; internal ActionType[] AcceptedActions => GetActions();
internal int Count => BitOperations.PopCount(acceptedActions);
public EnumBuffer(ReadOnlySpan<ActionType> actions)
{
fixed (byte* dataPtr = data)
actions.CopyTo(new Span<ActionType>(dataPtr, MaskSize));
}
public readonly ActionType[] ToArray(int size)
{
fixed (byte* dataPtr = data)
return new Span<ActionType>(dataPtr, size).ToArray();
}
}
private unsafe struct LUTBuffer
{
private fixed byte data[EnumSize];
public byte this[ActionType index] => data[(byte)index];
public LUTBuffer(ReadOnlySpan<ActionType> actions)
{
for (var i = 0; i < EnumSize; i++)
data[i] = 0xFF;
for (var i = 0; i < actions.Length; i++)
data[(byte)actions[i]] = (byte)i;
}
}
// List of accepted actions (max 32)
private readonly EnumBuffer acceptedActions;
// Lookup table for accepted actions (ActionType as idx -> idx in acceptedActions)
private readonly LUTBuffer acceptedActionsLUT;
private readonly byte size;
internal ActionType[] AcceptedActions => acceptedActions.ToArray(size);
public ActionPool(ReadOnlySpan<ActionType> actions) public ActionPool(ReadOnlySpan<ActionType> actions)
{ {
if (actions.Length > MaskSize) acceptedActions = 0;
throw new ArgumentOutOfRangeException(nameof(actions), actions.Length, $"ActionPool only supports up to {MaskSize} actions"); foreach (var action in actions)
acceptedActions |= 1ul << ((byte)action);
acceptedActions = new(actions); if (Count > MaskSize)
acceptedActionsLUT = new(actions); throw new ArgumentOutOfRangeException(nameof(actions), actions.Length, $"ActionPool only supports up to {MaskSize} actions");
size = (byte)actions.Length;
} }
[Pure] private ActionType[] GetActions()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal byte FromAction(ActionType action)
{ {
var ret = acceptedActionsLUT[action]; var ret = new ActionType[Count];
if (ret == 0xFF) var i = 0;
throw new ArgumentOutOfRangeException(nameof(action), action, $"Action {action} is unsupported in this pool."); foreach (var v in (byte[])Enum.GetValuesAsUnderlyingType<ActionType>())
{
if ((acceptedActions & (1ul << v)) != 0)
ret[i++] = (ActionType)v;
}
return ret; return ret;
} }
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ActionType ToAction(byte index) internal int FromAction(ActionType action)
{ {
if (index < 0 || index >= size) if ((acceptedActions & (1ul << (byte)action)) == 0)
throw new ArgumentOutOfRangeException(nameof(action), action, "Action is not accepted by this pool.");
// Get number of 1s before action
return BitOperations.PopCount(acceptedActions & ((1ul << (byte)action) - 1));
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ActionType ToAction(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), index, $"Index {index} is out of range for this pool."); throw new ArgumentOutOfRangeException(nameof(index), index, $"Index {index} is out of range for this pool.");
return acceptedActions[index];
// Get index of (index+1)th 1 in set
return (ActionType)Intrinsics.NthBitSet(acceptedActions, index);
} }
[Pure] [Pure]
+1 -1
View File
@@ -34,7 +34,7 @@ public struct ActionSet
public readonly bool HasAction(in ActionPool pool, ActionType action) => (bits & pool.ToMask(action)) != 0; public readonly bool HasAction(in ActionPool pool, ActionType action) => (bits & pool.ToMask(action)) != 0;
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType ElementAt(in ActionPool pool, int index) => pool.ToAction((byte)(Intrinsics.NthBitSet(bits, index) - 1)); public readonly ActionType ElementAt(in ActionPool pool, int index) => pool.ToAction(Intrinsics.NthBitSet(bits, index) - 1);
[Pure] [Pure]
public readonly int Count => BitOperations.PopCount(bits); public readonly int Count => BitOperations.PopCount(bits);
+47
View File
@@ -92,11 +92,46 @@ internal static class Intrinsics
return _base; return _base;
} }
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int NthBitSetScalar(ulong value, int n)
{
var mask = 0x00000000FFFFFFFFul;
var size = 32;
var _base = 0;
if (n++ >= BitOperations.PopCount(value))
return 64;
while (size > 0)
{
var count = BitOperations.PopCount(value & mask);
if (n > count)
{
_base += size;
size >>= 1;
mask |= mask << size;
}
else
{
size >>= 1;
mask >>= size;
}
}
return _base;
}
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int NthBitSetBMI2(uint value, int n) => private static int NthBitSetBMI2(uint value, int n) =>
BitOperations.TrailingZeroCount(Bmi2.ParallelBitDeposit(1u << n, value)); BitOperations.TrailingZeroCount(Bmi2.ParallelBitDeposit(1u << n, value));
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int NthBitSetBMI2(ulong value, int n) =>
BitOperations.TrailingZeroCount(Bmi2.X64.ParallelBitDeposit(1ul << n, value));
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int NthBitSet(uint value, int n) public static int NthBitSet(uint value, int n)
@@ -109,6 +144,18 @@ internal static class Intrinsics
NthBitSetScalar(value, n); NthBitSetScalar(value, n);
} }
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int NthBitSet(ulong value, int n)
{
if (n >= BitOperations.PopCount(value))
return 64;
return Bmi2.X64.IsSupported ?
NthBitSetBMI2(value, n) :
NthBitSetScalar(value, n);
}
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector<float> ReciprocalSqrt(Vector<float> data) public static Vector<float> ReciprocalSqrt(Vector<float> data)
+41 -8
View File
@@ -10,16 +10,49 @@ public class ActionSetTests
[TestMethod] [TestMethod]
public void TestActionPoolSize() public void TestActionPoolSize()
{ {
Assert.AreEqual(ActionPool.EnumSize, Enum.GetValues<ActionType>().Length);
Assert.AreEqual(ActionPool.MaskSize, Unsafe.SizeOf<ActionSet>() * 8); Assert.AreEqual(ActionPool.MaskSize, Unsafe.SizeOf<ActionSet>() * 8);
} }
[TestMethod]
public void TestActionPoolConstructor()
{
CollectionAssert.AreEquivalent(new ActionType[]
{
ActionType.StandardTouchCombo,
ActionType.AdvancedTouchCombo,
ActionType.FocusedTouchCombo,
ActionType.FocusedSynthesisCombo,
ActionType.TrainedFinesse,
ActionType.PrudentSynthesis,
ActionType.Groundwork,
ActionType.AdvancedTouch,
ActionType.CarefulSynthesis,
ActionType.TrainedEye,
ActionType.DelicateSynthesis,
ActionType.PreparatoryTouch,
ActionType.Reflect,
ActionType.PrudentTouch,
ActionType.Manipulation,
ActionType.MuscleMemory,
ActionType.ByregotsBlessing,
ActionType.WasteNot2,
ActionType.BasicSynthesis,
ActionType.Innovation,
ActionType.GreatStrides,
ActionType.StandardTouch,
ActionType.Veneration,
ActionType.WasteNot,
ActionType.MastersMend,
ActionType.BasicTouch,
}, pool.AcceptedActions);
}
[TestMethod] [TestMethod]
public void TestAcceptedActions() public void TestAcceptedActions()
{ {
foreach (var i in Enum.GetValues<ActionType>()) foreach (var i in Enum.GetValues<ActionType>())
{ {
byte idx; int idx;
try try
{ {
idx = pool.FromAction(i); idx = pool.FromAction(i);
@@ -101,18 +134,18 @@ public class ActionSetTests
Assert.AreEqual(4, set.Count); Assert.AreEqual(4, set.Count);
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(in pool, 0)); Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(in pool, 0));
Assert.AreEqual(ActionType.Reflect, set.ElementAt(in pool, 1)); Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(in pool, 1));
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(in pool, 2)); Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(in pool, 2));
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(in pool, 3)); Assert.AreEqual(ActionType.Reflect, set.ElementAt(in pool, 3));
set.RemoveAction(in pool, ActionType.Reflect); set.RemoveAction(in pool, ActionType.Reflect);
Assert.AreEqual(3, set.Count); Assert.AreEqual(3, set.Count);
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(in pool, 0)); Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(in pool, 0));
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(in pool, 1)); Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(in pool, 1));
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(in pool, 2)); Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(in pool, 2));
} }
[TestMethod] [TestMethod]