Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic implementation of BoardMoveExecutor #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions Backend/BoardMoveExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Backend.Data;
using Backend.Data.Enum;
using Backend.Data.Struct;

namespace Backend;

public readonly unsafe struct BoardMoveExecutor
{
private readonly struct True { }

private static readonly delegate*<ref BitBoardMap, void>* FPs;

[ModuleInitializer]
internal static void RunCctor()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do this. It's already done by Util.cs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't tell me you're pre-running all cctors...

Copy link
Owner

@TheBlackPlague TheBlackPlague Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am. See:

public static void RunStaticConstructor()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that even work? The ModuleInitializer attribute seems to be missing. Either way, RIP startup perf

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it works? — Not 100% sure, but running it seems to be fine.

The start-up performance isn't actually hurt that much.

{
RuntimeHelpers.RunClassConstructor(typeof(BoardMoveExecutor).TypeHandle);
}

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
private static MethodInfo GetMethodInfo(Delegate Method)
{
return Method.Method;
}

static BoardMoveExecutor()
{
//This should be above, for codegen accuracy
FPs = (delegate*<ref BitBoardMap, void>*) NativeMemory.Alloc(64, 64);

//This is necessary for InASM
if (Assembly.GetExecutingAssembly().GetName().Name.Contains('@'))
{
return;
}

var GMethod = GetMethodInfo(ExecuteMove<True, True, True, True, True, True>).GetGenericMethodDefinition();

const int GArgsCount = 6;

var GArgs = new Type[GArgsCount];

ref var FirstArgOffsetByOne = ref Unsafe.Subtract(ref MemoryMarshal.GetArrayDataReference(GArgs), 1);

ref var LastArg = ref Unsafe.Add(ref FirstArgOffsetByOne, GArgsCount);

for (int I = 0; I < 64; I++)
{
const int ExtractionMask = 1, True = 1; //LSB set

ref var CurrentArg = ref LastArg;

var ID = I;

for (; !Unsafe.AreSame(ref CurrentArg, ref FirstArgOffsetByOne)
; CurrentArg = ref Unsafe.Subtract(ref CurrentArg, 1))
{
var ExtractedState = ID & ExtractionMask;

if (ExtractedState == True)
{
CurrentArg = typeof(True);
}

else
{
CurrentArg = typeof(int); //Any type that isn't True will be treated as false
}

ID >>= 1;
}

var SpecializedMethod = GMethod.MakeGenericMethod(GArgs);

var MH = SpecializedMethod.MethodHandle;

//JIT method before getting FP pointer, allowing FP to point to optimized code
RuntimeHelpers.PrepareMethod(MH);

FPs[I] = (delegate*<ref BitBoardMap, void>) MH.GetFunctionPointer();
}
}

//For testing purposes
public static MethodInfo PopulateGArgsArrayAndCreateSpecializedMethodInstantiation(int ID)
{
var GArgs = new Type[6];

var GMethod = GetMethodInfo(ExecuteMove<True, True, True, True, True, True>).GetGenericMethodDefinition();

const int GArgsCount = 6;

ref var FirstArgOffsetByOne = ref Unsafe.Subtract(ref MemoryMarshal.GetArrayDataReference(GArgs), 1);

ref var CurrentArg = ref Unsafe.Add(ref FirstArgOffsetByOne, GArgsCount);

const int ExtractionMask = 1, True = 1; //LSB set

for (; !Unsafe.AreSame(ref CurrentArg, ref FirstArgOffsetByOne)
; CurrentArg = ref Unsafe.Subtract(ref CurrentArg, 1))
{
var ExtractedState = ID & ExtractionMask;

if (ExtractedState == True)
{
CurrentArg = typeof(True);
}

else
{
CurrentArg = typeof(int); //Any type that isn't True will be treated as false
}

ID >>= 1;
}

var SpecializedMethod = GMethod.MakeGenericMethod(GArgs);

return SpecializedMethod;
}

[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static void ExecuteMove(ref BitBoardMap Map, nint ID)
{
FPs[ID](ref Map);
}

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static void ExecuteMove<IsWhiteTurn, IsEnPassantSet, IsWhiteKCastle, IsWhiteQCastle, IsBlackKCastle, IsBlackQCastle>(ref BitBoardMap Map)
{
if (typeof(IsWhiteTurn) == typeof(True))
{
Console.WriteLine("IsWhiteTurn");
}

if (typeof(IsEnPassantSet) == typeof(True))
{
Console.WriteLine("IsEnPassantSet");
}

if (typeof(IsWhiteKCastle) == typeof(True))
{
Console.WriteLine("IsWhiteKCastle");
}

if (typeof(IsWhiteQCastle) == typeof(True))
{
Console.WriteLine("IsWhiteQCastle");
}

if (typeof(IsBlackKCastle) == typeof(True))
{
Console.WriteLine("IsBlackKCastle");
}

if (typeof(IsBlackQCastle) == typeof(True))
{
Console.WriteLine("IsBlackQCastle");
}
}
}
13 changes: 13 additions & 0 deletions Test/BitBoardMap.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using Backend.Data.Enum;
using Backend.Data.Struct;
using NUnit.Framework;
Expand Down Expand Up @@ -104,4 +105,16 @@ public void UndoEval()
Assert.IsTrue(evalChanged && evalReverted);
}

[Test]
public void DoesRuntimeCrash()
{
ref var NullRef = ref Unsafe.NullRef<Backend.Data.Struct.BitBoardMap>();

for (int I = 0; I < 64; I++)
{
Console.WriteLine($"\nCurrent ID: {I}\n");

Backend.BoardMoveExecutor.ExecuteMove(ref NullRef, I);
}
}
}