-
Notifications
You must be signed in to change notification settings - Fork 7
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
budgetdevv
wants to merge
4
commits into
TheBlackPlague:master
Choose a base branch
from
budgetdevv:BoardMoveExecutor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 unsafe readonly struct BoardMoveExecutor | ||
{ | ||
private readonly struct True { } | ||
|
||
private static readonly delegate*<ref BitBoardMap, void>* FPs; | ||
|
||
[ModuleInitializer] | ||
internal static void RunCctor() | ||
{ | ||
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; | ||
} | ||
|
||
MethodInfo gMethod = GetMethodInfo(ExecuteMove<True, True, True, True, True, True>).GetGenericMethodDefinition(); | ||
|
||
const int gArgsCount = 6; | ||
|
||
Type[] gArgs = new Type[gArgsCount]; | ||
|
||
ref Type firstArgOffsetByOne = ref Unsafe.Subtract(ref MemoryMarshal.GetArrayDataReference(gArgs), 1); | ||
|
||
ref Type lastArg = ref Unsafe.Add(ref firstArgOffsetByOne, gArgsCount); | ||
|
||
for (int I = 0; I < 64; I++) | ||
{ | ||
const int extractionMask = 1, @true = 1; //LSB set | ||
|
||
ref Type currentArg = ref lastArg; | ||
|
||
int id = I; | ||
|
||
for (; !Unsafe.AreSame(ref currentArg, ref firstArgOffsetByOne) | ||
; currentArg = ref Unsafe.Subtract(ref currentArg, 1)) | ||
{ | ||
int 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; | ||
} | ||
|
||
MethodInfo specializedMethod = gMethod.MakeGenericMethod(gArgs); | ||
|
||
RuntimeMethodHandle 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) | ||
{ | ||
Type[] gArgs = new Type[6]; | ||
|
||
MethodInfo gMethod = GetMethodInfo(ExecuteMove<True, True, True, True, True, True>).GetGenericMethodDefinition(); | ||
|
||
const int gArgsCount = 6; | ||
|
||
ref Type firstArgOffsetByOne = ref Unsafe.Subtract(ref MemoryMarshal.GetArrayDataReference(gArgs), 1); | ||
|
||
ref Type 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)) | ||
{ | ||
int 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; | ||
} | ||
|
||
MethodInfo specializedMethod = gMethod.MakeGenericMethod(gArgs); | ||
|
||
return specializedMethod; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] | ||
public static void ExecuteMove(ref BitBoardMap map, nint id) //Do NOT reorder params! It will incur an additional reg to reg mov! | ||
{ | ||
FPs[id](ref map); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveOptimization)] | ||
private static void ExecuteMove<TIsWhiteTurn, TIsEnPassantSet, TIsWhiteKCastle, TIsWhiteQCastle, TIsBlackKCastle, TIsBlackQCastle>(ref BitBoardMap map) | ||
{ | ||
if (typeof(TIsWhiteTurn) == typeof(True)) | ||
{ | ||
Console.WriteLine("IsWhiteTurn"); | ||
} | ||
|
||
if (typeof(TIsEnPassantSet) == typeof(True)) | ||
{ | ||
Console.WriteLine("IsEnPassantSet"); | ||
} | ||
|
||
if (typeof(TIsWhiteKCastle) == typeof(True)) | ||
{ | ||
Console.WriteLine("IsWhiteKCastle"); | ||
} | ||
|
||
if (typeof(TIsWhiteQCastle) == typeof(True)) | ||
{ | ||
Console.WriteLine("IsWhiteQCastle"); | ||
} | ||
|
||
if (typeof(TIsBlackKCastle) == typeof(True)) | ||
{ | ||
Console.WriteLine("IsBlackKCastle"); | ||
} | ||
|
||
if (typeof(TIsBlackQCastle) == typeof(True)) | ||
{ | ||
Console.WriteLine("IsBlackQCastle"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am. See:
StockNemo/Backend/Util.cs
Line 13 in 4a89176
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.