Skip to content

Commit

Permalink
Default to non-AOT compilation (liamt19#52)
Browse files Browse the repository at this point in the history
bench: 5609869
  • Loading branch information
liamt19 authored Jul 13, 2024
1 parent 0f104d9 commit 3581897
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 274 deletions.
1 change: 0 additions & 1 deletion GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@
global using Color = Lizard.Logic.Data.Color;
global using Debug = System.Diagnostics.Debug;
global using Stopwatch = System.Diagnostics.Stopwatch;
global using MethodImpl = System.Runtime.CompilerServices.MethodImplAttribute;
global using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
global using Unsafe = System.Runtime.CompilerServices.Unsafe;
2 changes: 1 addition & 1 deletion Lizard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<!-- The "instruction-set" argument is required for AOT to generate code with intrinsics -->
<ItemGroup>
<ilcArg Include="--Ot" />
<ilcArg Include="--instruction-set=native" />
<IlcArg Condition="$(IlcInstructionSet) == ''" Include="--instruction-set=native" />
</ItemGroup>

<!-- If "-p:EVALFILE=(path to network here...)" is passed during the MSBuild, this will set the EvalFile
Expand Down
7 changes: 6 additions & 1 deletion Logic/Core/Bitboard.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Lizard.Logic.Core
using System.Runtime.CompilerServices;

namespace Lizard.Logic.Core
{
/// <summary>
/// Manages the bitboards for a position, which are 64-bit number arrays for each piece type and color.
Expand Down Expand Up @@ -125,6 +127,7 @@ public void MoveSimple(int from, int to, int pieceColor, int pieceType)
/// <summary>
/// Returns the <see cref="Color"/> of the piece on the square <paramref name="idx"/>
/// </summary>
[MethodImpl(Inline)]
public int GetColorAtIndex(int idx)
{
return ((Colors[Color.White] & SquareBB[idx]) != 0) ? Color.White : Color.Black;
Expand All @@ -133,6 +136,7 @@ public int GetColorAtIndex(int idx)
/// <summary>
/// Returns the type of the <see cref="Piece"/> on the square <paramref name="idx"/>
/// </summary>
[MethodImpl(Inline)]
public int GetPieceAtIndex(int idx)
{
return PieceTypes[idx];
Expand All @@ -142,6 +146,7 @@ public int GetPieceAtIndex(int idx)
/// <summary>
/// Returns the index of the square that the <see cref="Color"/> <paramref name="pc"/>'s king is on.
/// </summary>
[MethodImpl(Inline)]
public int KingIndex(int pc)
{
return lsb(Colors[pc] & Pieces[Piece.King]);
Expand Down
27 changes: 9 additions & 18 deletions Logic/Core/MoveGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ public int GenPawns<GenType>(ScoredMove* list, ulong targets, int size) where Ge
int from = poplsb(&mask);

ref Move m = ref list[size++].Move;
m.SetNew(from, State->EPSquare);
m.EnPassant = true;
m.SetNewEnPassant(from, State->EPSquare);
}
}

Expand Down Expand Up @@ -229,17 +228,15 @@ int GenCastlingMoves(ScoredMove* list, int size)
&& (bb.Pieces[Rook] & SquareBB[CastlingRookSquares[(int)CastlingStatus.WK]] & us) != 0)
{
ref Move m = ref list[size++].Move;
m.SetNew(ourKing, CastlingRookSquares[(int)CastlingStatus.WK]);
m.Castle = true;
m.SetNewCastle(ourKing, CastlingRookSquares[(int)CastlingStatus.WK]);
}

if (State->CastleStatus.HasFlag(CastlingStatus.WQ)
&& (occ & CastlingRookPaths[(int)CastlingStatus.WQ]) == 0
&& (bb.Pieces[Rook] & SquareBB[CastlingRookSquares[(int)CastlingStatus.WQ]] & us) != 0)
{
ref Move m = ref list[size++].Move;
m.SetNew(ourKing, CastlingRookSquares[(int)CastlingStatus.WQ]);
m.Castle = true;
m.SetNewCastle(ourKing, CastlingRookSquares[(int)CastlingStatus.WQ]);
}
}
else if (ToMove == Black && (ourKing == E8 || IsChess960))
Expand All @@ -249,17 +246,15 @@ int GenCastlingMoves(ScoredMove* list, int size)
&& (bb.Pieces[Rook] & SquareBB[CastlingRookSquares[(int)CastlingStatus.BK]] & us) != 0)
{
ref Move m = ref list[size++].Move;
m.SetNew(ourKing, CastlingRookSquares[(int)CastlingStatus.BK]);
m.Castle = true;
m.SetNewCastle(ourKing, CastlingRookSquares[(int)CastlingStatus.BK]);
}

if (State->CastleStatus.HasFlag(CastlingStatus.BQ)
&& (occ & CastlingRookPaths[(int)CastlingStatus.BQ]) == 0
&& (bb.Pieces[Rook] & SquareBB[CastlingRookSquares[(int)CastlingStatus.BQ]] & us) != 0)
{
ref Move m = ref list[size++].Move;
m.SetNew(ourKing, CastlingRookSquares[(int)CastlingStatus.BQ]);
m.Castle = true;
m.SetNewCastle(ourKing, CastlingRookSquares[(int)CastlingStatus.BQ]);
}
}

Expand Down Expand Up @@ -455,17 +450,15 @@ int GenCastlingMoves(ScoredMove* list, int size)
&& HasCastlingRook(us, CastlingStatus.WK))
{
ref Move m = ref list[size++].Move;
m.SetNew(ourKing, CastlingRookSquares[(int)CastlingStatus.WK]);
m.Castle = true;
m.SetNewCastle(ourKing, CastlingRookSquares[(int)CastlingStatus.WK]);
}

if (State->CastleStatus.HasFlag(CastlingStatus.WQ)
&& !CastlingImpeded(us, CastlingStatus.WQ)
&& HasCastlingRook(us, CastlingStatus.WQ))
{
ref Move m = ref list[size++].Move;
m.SetNew(ourKing, CastlingRookSquares[(int)CastlingStatus.WQ]);
m.Castle = true;
m.SetNewCastle(ourKing, CastlingRookSquares[(int)CastlingStatus.WQ]);
}
}
else if (ToMove == Black && (ourKing == E8 || IsChess960))
Expand All @@ -475,17 +468,15 @@ int GenCastlingMoves(ScoredMove* list, int size)
&& HasCastlingRook(us, CastlingStatus.BK))
{
ref Move m = ref list[size++].Move;
m.SetNew(ourKing, CastlingRookSquares[(int)CastlingStatus.BK]);
m.Castle = true;
m.SetNewCastle(ourKing, CastlingRookSquares[(int)CastlingStatus.BK]);
}

if (State->CastleStatus.HasFlag(CastlingStatus.BQ)
&& !CastlingImpeded(us, CastlingStatus.BQ)
&& HasCastlingRook(us, CastlingStatus.BQ))
{
ref Move m = ref list[size++].Move;
m.SetNew(ourKing, CastlingRookSquares[(int)CastlingStatus.BQ]);
m.Castle = true;
m.SetNewCastle(ourKing, CastlingRookSquares[(int)CastlingStatus.BQ]);
}
}

Expand Down
69 changes: 36 additions & 33 deletions Logic/Core/Position.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

using Lizard.Logic.Data;
Expand Down Expand Up @@ -90,8 +91,10 @@ public unsafe partial class Position

public bool IsChess960 = false;


[MethodImpl(Inline)]
public bool CastlingImpeded(ulong occ, CastlingStatus cr) => (occ & CastlingRookPaths[(int)cr]) != 0;

[MethodImpl(Inline)]
public bool HasCastlingRook(ulong us, CastlingStatus cr) => (bb.Pieces[Rook] & SquareBB[CastlingRookSquares[(int)cr]] & us) != 0;


Expand Down Expand Up @@ -255,8 +258,8 @@ public void MakeMove(Move move)
FullMoves++;
}

int moveFrom = move.From;
int moveTo = move.To;
int moveFrom = move.GetFrom();
int moveTo = move.GetTo();

int ourPiece = bb.GetPieceAtIndex(moveFrom);
int ourColor = ToMove;
Expand All @@ -266,19 +269,19 @@ public void MakeMove(Move move)

Assert(ourPiece != None, $"Move {move.ToString()} in FEN '{GetFEN()}' doesn't have a piece on the From square!");
Assert(theirPiece != King, $"Move {move.ToString()} in FEN '{GetFEN()}' captures a king!");
Assert(theirPiece == None || (ourColor != bb.GetColorAtIndex(moveTo) || move.Castle),
Assert(theirPiece == None || (ourColor != bb.GetColorAtIndex(moveTo) || move.GetCastle()),
$"Move {move.ToString()} in FEN '{GetFEN()}' captures our own {PieceToString(theirPiece)} on {IndexToString(moveTo)}");

if (ourPiece == King)
{
if (move.Castle)
if (move.GetCastle())
{
// Castling moves are KxR, so "theirPiece" is actually our own rook.
theirPiece = None;

// Move our rook and update the hash
DoCastling(ourColor, moveFrom, moveTo, undo: false);
State->KingSquares[ourColor] = move.CastlingKingSquare;
State->KingSquares[ourColor] = move.CastlingKingSquare();
}
else
{
Expand Down Expand Up @@ -328,7 +331,7 @@ public void MakeMove(Move move)

if (ourPiece == Pawn)
{
if (move.EnPassant)
if (move.GetEnPassant())
{

int idxPawn = ((bb.Pieces[Pawn] & SquareBB[tempEPSquare - 8]) != 0) ? tempEPSquare - 8 : tempEPSquare + 8;
Expand All @@ -354,24 +357,24 @@ public void MakeMove(Move move)
State->HalfmoveClock = 0;
}

if (!move.Castle)
if (!move.GetCastle())
{
bb.MoveSimple(moveFrom, moveTo, ourColor, ourPiece);
State->Hash.ZobristMove(moveFrom, moveTo, ourColor, ourPiece);
}

if (move.Promotion)
if (move.GetPromotion())
{
// Get rid of the pawn we just put there
bb.RemovePiece(moveTo, ourColor, ourPiece);

// And replace it with the promotion piece
bb.AddPiece(moveTo, ourColor, move.PromotionTo);
bb.AddPiece(moveTo, ourColor, move.GetPromotionTo());

State->Hash.ZobristToggleSquare(ourColor, ourPiece, moveTo);
State->Hash.ZobristToggleSquare(ourColor, move.PromotionTo, moveTo);
State->Hash.ZobristToggleSquare(ourColor, move.GetPromotionTo(), moveTo);

MaterialCountNonPawn[ourColor] += GetPieceValue(move.PromotionTo);
MaterialCountNonPawn[ourColor] += GetPieceValue(move.GetPromotionTo());
}

State->Hash.ZobristChangeToMove();
Expand Down Expand Up @@ -402,8 +405,8 @@ public void MakeMove(Move move)

public void UnmakeMove(Move move)
{
int moveFrom = move.From;
int moveTo = move.To;
int moveFrom = move.GetFrom();
int moveTo = move.GetTo();

// Assume that "we" just made the last move, and "they" are undoing it.
int ourPiece = bb.GetPieceAtIndex(moveTo);
Expand All @@ -412,7 +415,7 @@ public void UnmakeMove(Move move)

GamePly--;

if (move.Promotion)
if (move.GetPromotion())
{
// Remove the promotion piece and replace it with a pawn
bb.RemovePiece(moveTo, ourColor, ourPiece);
Expand All @@ -421,15 +424,15 @@ public void UnmakeMove(Move move)

bb.AddPiece(moveTo, ourColor, ourPiece);

MaterialCountNonPawn[ourColor] -= GetPieceValue(move.PromotionTo);
MaterialCountNonPawn[ourColor] -= GetPieceValue(move.GetPromotionTo());
}
else if (move.Castle)
else if (move.GetCastle())
{
// Put both pieces back
DoCastling(ourColor, moveFrom, moveTo, undo: true);
}

if (!move.Castle)
if (!move.GetCastle())
{
// Put our piece back to the square it came from.
bb.MoveSimple(moveTo, moveFrom, ourColor, ourPiece);
Expand All @@ -438,7 +441,7 @@ public void UnmakeMove(Move move)
if (State->CapturedPiece != Piece.None)
{
// CapturedPiece is set for captures and en passant, so check which it was
if (move.EnPassant)
if (move.GetEnPassant())
{
// If the move was an en passant, put the captured pawn back

Expand Down Expand Up @@ -614,7 +617,7 @@ public void SetCheckInfo()
State->CheckSquares[King] = 0;
}

[MethodImpl(Inline)]

public void SetCastlingStatus(int c, int rfrom)
{
int kfrom = bb.KingIndex(c);
Expand Down Expand Up @@ -646,8 +649,8 @@ public ulong HashAfter(Move m)
{
ulong hash = State->Hash;

int from = m.From;
int to = m.To;
int from = m.GetFrom();
int to = m.GetTo();
int us = bb.GetColorAtIndex(from);
int ourPiece = bb.GetPieceAtIndex(from);

Expand All @@ -672,8 +675,8 @@ public ulong HashAfter(Move m)
/// </summary>
public bool IsPseudoLegal(in Move move)
{
int moveTo = move.To;
int moveFrom = move.From;
int moveTo = move.GetTo();
int moveFrom = move.GetFrom();

int pt = bb.GetPieceAtIndex(moveFrom);
if (pt == None)
Expand All @@ -689,7 +692,7 @@ public bool IsPseudoLegal(in Move move)
return false;
}

if (bb.GetPieceAtIndex(moveTo) != None && pc == bb.GetColorAtIndex(moveTo) && !move.Castle)
if (bb.GetPieceAtIndex(moveTo) != None && pc == bb.GetColorAtIndex(moveTo) && !move.GetCastle())
{
// There is a piece on the square we are moving to, and it is ours, so we can't capture it.
// The one exception is castling, which is encoded as king captures rook.
Expand All @@ -698,7 +701,7 @@ public bool IsPseudoLegal(in Move move)

if (pt == Pawn)
{
if (move.EnPassant)
if (move.GetEnPassant())
{
return State->EPSquare != EPNone && (SquareBB[moveTo - ShiftUpDir(ToMove)] & bb.Pieces[Pawn] & bb.Colors[Not(ToMove)]) != 0;
}
Expand All @@ -722,7 +725,7 @@ public bool IsPseudoLegal(in Move move)

// This move is only pseudo-legal if the piece that is moving is actually able to get there.
// Pieces can only move to squares that they attack, with the one exception of queenside castling
return (bb.AttackMask(moveFrom, pc, pt, bb.Occupancy) & SquareBB[moveTo]) != 0 || move.Castle;
return (bb.AttackMask(moveFrom, pc, pt, bb.Occupancy) & SquareBB[moveTo]) != 0 || move.GetCastle();
}


Expand All @@ -736,8 +739,8 @@ public bool IsPseudoLegal(in Move move)
/// </summary>
public bool IsLegal(Move move, int ourKing, int theirKing, ulong pinnedPieces)
{
int moveFrom = move.From;
int moveTo = move.To;
int moveFrom = move.GetFrom();
int moveTo = move.GetTo();

int pt = bb.GetPieceAtIndex(moveFrom);

Expand Down Expand Up @@ -769,7 +772,7 @@ public bool IsLegal(Move move, int ourKing, int theirKing, ulong pinnedPieces)

int checker = idxChecker;
if (((LineBB[ourKing][checker] & SquareBB[moveTo]) != 0)
|| (move.EnPassant && GetIndexFile(moveTo) == GetIndexFile(checker)))
|| (move.GetEnPassant() && GetIndexFile(moveTo) == GetIndexFile(checker)))
{
// This move is another piece which has moved into the LineBB between our king and the checking piece.
// This will be legal as long as it isn't pinned.
Expand All @@ -783,7 +786,7 @@ public bool IsLegal(Move move, int ourKing, int theirKing, ulong pinnedPieces)

if (pt == Piece.King)
{
if (move.Castle)
if (move.GetCastle())
{
var thisCr = move.RelevantCastlingRight;
int rookSq = CastlingRookSquares[(int)thisCr];
Expand Down Expand Up @@ -824,7 +827,7 @@ public bool IsLegal(Move move, int ourKing, int theirKing, ulong pinnedPieces)
return ((bb.AttackersTo(moveTo, bb.Occupancy ^ SquareBB[ourKing]) & bb.Colors[theirColor])
| (NeighborsMask[moveTo] & SquareBB[theirKing])) == 0;
}
else if (move.EnPassant)
else if (move.GetEnPassant())
{
// En passant will remove both our pawn and the opponents pawn from the rank so this needs a special check
// to make sure it is still legal
Expand Down
Loading

0 comments on commit 3581897

Please sign in to comment.