-
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 1 commit
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 readonly unsafe 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; | ||
} | ||
|
||
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"); | ||
} | ||
} | ||
} |
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.