Skip to content

Commit

Permalink
Added input buckets, released v 1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmoth committed Sep 25, 2024
1 parent b67533d commit d1aa0f4
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
working-directory: Sapling
id: get_version
run: |
VERSION=1.1.1
VERSION=1.1.2
echo "Application version: $VERSION"
echo "::set-output name=version::$VERSION"
Expand Down
23 changes: 12 additions & 11 deletions Sapling.Engine/BoardState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Sapling.Engine;

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Explicit, Size = 142)] // Explicit layout with size control
[StructLayout(LayoutKind.Explicit, Size = 144)] // Explicit layout with size control
public unsafe struct BoardStateData
{
// 8-byte fields (grouped together for optimal alignment)
Expand All @@ -18,22 +18,23 @@ public unsafe struct BoardStateData
// Grouped bools (using 1 byte each)
[FieldOffset(131)] public bool WhiteToMove; // 1 byte
[FieldOffset(132)] public bool InCheck; // 1 byte
[FieldOffset(133)] public bool ShouldWhiteMirrored; // 1 byte
[FieldOffset(134)] public bool ShouldBlackMirrored; // 1 byte
[FieldOffset(135)] public bool WhiteMirrored; // 1 byte
[FieldOffset(136)] public bool BlackMirrored; // 1 byte
[FieldOffset(133)] public bool WhiteMirrored; // 1 byte
[FieldOffset(134)] public bool BlackMirrored; // 1 byte

// 4-byte field (for proper alignment)

// CastleRights (assuming this is a byte-sized enum)
[FieldOffset(137)] public CastleRights CastleRights; // 1 byte
[FieldOffset(135)] public CastleRights CastleRights; // 1 byte

// Smaller fields (grouped together for minimal padding)
[FieldOffset(138)] public byte WhiteKingSquare; // 1 byte
[FieldOffset(139)] public byte BlackKingSquare; // 1 byte
[FieldOffset(140)] public byte EnPassantFile; // 1 byte
[FieldOffset(141)] public byte PieceCount; // 1 byte

[FieldOffset(136)] public byte WhiteKingSquare; // 1 byte
[FieldOffset(137)] public byte BlackKingSquare; // 1 byte
[FieldOffset(138)] public byte EnPassantFile; // 1 byte
[FieldOffset(139)] public byte PieceCount; // 1 byte
[FieldOffset(140)] public byte WhiteInputBucket; // 1 byte
[FieldOffset(141)] public byte BlackInputBucket; // 1 byte
[FieldOffset(142)] public bool WhiteNeedsRefresh; // 1 byte
[FieldOffset(143)] public bool BlackNeedsRefresh; // 1 byte
public void CloneTo(ref BoardStateData copy)
{
fixed (BoardStateData* sourcePtr = &this)
Expand Down
32 changes: 11 additions & 21 deletions Sapling.Engine/BoardStateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,24 +376,6 @@ public static void UpdateCastleStatus(this ref BoardStateData board, CastleRight
board.Hash ^= Zobrist.BlackQueenSideCastlingRights;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMirrorStatus(this ref BoardStateData board,byte piece, bool fromMirroredSide, bool toMirroredSide)
{
if (fromMirroredSide == toMirroredSide)
{
return;
}

if (piece == Constants.WhiteKing)
{
board.ShouldWhiteMirrored = toMirroredSide;
}

if (piece == Constants.BlackKing)
{
board.ShouldBlackMirrored = toMirroredSide;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void FinishApply(this ref BoardStateData board, VectorShort* whiteAcc, VectorShort* blackAcc, ulong* hashHistory, uint m,
Expand All @@ -404,6 +386,15 @@ public static unsafe void FinishApply(this ref BoardStateData board, VectorShort

var (movedPiece, fromSquare, toSquare, capturedPiece, moveType) = m.Deconstruct();

if (movedPiece == Constants.WhiteKing)
{
board.WhiteNeedsRefresh |= board.WhiteMirrored != toSquare.IsMirroredSide() || board.WhiteInputBucket != NnueEvaluator.GetKingBucket(toSquare);
}
else if (movedPiece == Constants.BlackKing)
{
board.BlackNeedsRefresh |= board.BlackMirrored != toSquare.IsMirroredSide() || board.BlackInputBucket != NnueEvaluator.GetKingBucket((byte)(toSquare ^ 0x38));
}

if (moveType == 0)
{
board.Replace(whiteAcc, blackAcc, movedPiece, fromSquare, toSquare);
Expand All @@ -427,8 +418,8 @@ public static unsafe void FinishApply(this ref BoardStateData board, VectorShort
// Castle move
if (toSquare == 62)
{
board.Replace(whiteAcc, blackAcc, Constants.BlackRook, 63, 61);
board.Replace(whiteAcc, blackAcc, Constants.BlackKing, fromSquare, toSquare);
board.Replace( whiteAcc, blackAcc, Constants.BlackRook, 63, 61);
board.Replace( whiteAcc, blackAcc, Constants.BlackKing, fromSquare, toSquare);
board.Hash ^= Zobrist.PiecesArray[Constants.BlackKing * 64 + fromSquare] ^
Zobrist.PiecesArray[Constants.BlackKing * 64 + toSquare] ^
Zobrist.PiecesArray[Constants.BlackRook * 64 + 63] ^
Expand Down Expand Up @@ -514,7 +505,6 @@ public static unsafe void FinishApply(this ref BoardStateData board, VectorShort
}

board.UpdateCastleStatus(prevCastle);
board.UpdateMirrorStatus(movedPiece, fromSquare.IsMirroredSide(), toSquare.IsMirroredSide());

hashHistory[board.TurnCount - 1] = board.Hash;
}
Expand Down
Loading

0 comments on commit d1aa0f4

Please sign in to comment.