Skip to content

Commit 34530be

Browse files
committed
Added FRC support (bench: 3200702)
1 parent 2938413 commit 34530be

File tree

6 files changed

+558
-236
lines changed

6 files changed

+558
-236
lines changed

src/Sapling.Engine/BoardState.cs

+33-28
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,51 @@
55

66
namespace Sapling.Engine;
77

8-
[StructLayout(LayoutKind.Explicit, Size = 164)] // Updated size to fit alignment
8+
[StructLayout(LayoutKind.Explicit, Size = 168)] // Adjusted size to fit more efficiently
99
public unsafe struct BoardStateData
1010
{
11-
public const uint BoardStateSize = 164;
11+
public const uint BoardStateSize = 168;
1212

13-
// 15 * 8 = 120 bytes (no padding needed)
14-
[FieldOffset(0)] public fixed ulong Occupancy[15]; // 15 ulong values
13+
// 15 * 8 = 120 bytes (occupancy array)
14+
[FieldOffset(0)] public fixed ulong Occupancy[15]; // 15 ulong values, no padding needed.
1515

1616
// 8 bytes, aligned at 120
17-
[FieldOffset(120)] public ulong Hash; // 64-bit, aligned at 120
17+
[FieldOffset(120)] public ulong Hash; // 64-bit aligned naturally.
1818

19-
// 2 bytes, aligned at 128 (needs no padding since Hash is 64-bit)
19+
// Combine all 1-byte fields (byte and bool) together to avoid unnecessary padding.
20+
// 8 bytes in total (TurnCount + HalfMoveClock + WhiteToMove + InCheck + CastleRights + WhiteKingSquare + BlackKingSquare + EnPassantFile)
2021
[FieldOffset(128)] public ushort TurnCount; // 16-bit
21-
22-
// 1 byte, follows TurnCount (16-bit aligned, so no padding needed)
2322
[FieldOffset(130)] public byte HalfMoveClock; // 8-bit
24-
25-
// 1 byte, no special alignment
2623
[FieldOffset(131)] public bool WhiteToMove; // 8-bit
27-
28-
// 1 byte, no special alignment
2924
[FieldOffset(132)] public bool InCheck; // 8-bit
30-
31-
// 1 byte, no special alignment, still on byte boundary
32-
[FieldOffset(133)] public CastleRights CastleRights; // Enum type, typically 1 byte
33-
34-
// Now grouping the remaining 1-byte fields together
35-
[FieldOffset(134)] public byte WhiteKingSquare; // 1 byte
36-
[FieldOffset(135)] public byte BlackKingSquare; // 1 byte
37-
[FieldOffset(136)] public byte EnPassantFile; // 1 byte
38-
[FieldOffset(137)] public byte PieceCount; // 1 byte
39-
40-
// Add padding for alignment (2 bytes of padding at the end to make the size a multiple of 8)
41-
[FieldOffset(138)] private fixed byte _padding[2]; // Padding to align total size to 8-byte boundary
42-
[FieldOffset(140)] public ulong PawnHash;
43-
[FieldOffset(148)] public ulong WhiteMaterialHash;
44-
[FieldOffset(156)] public ulong BlackMaterialHash;
45-
25+
[FieldOffset(133)] public CastleRights CastleRights; // 8-bit enum
26+
[FieldOffset(134)] public byte WhiteKingSquare; // 8-bit
27+
[FieldOffset(135)] public byte BlackKingSquare; // 8-bit
28+
[FieldOffset(136)] public byte EnPassantFile; // 8-bit
29+
[FieldOffset(137)] public byte PieceCount; // 8-bit
30+
31+
// Group additional fields that fit in the next available space:
32+
// 8 bytes for PawnHash, aligned at 138.
33+
[FieldOffset(138)] public ulong PawnHash; // 64-bit aligned naturally.
34+
35+
// 8 bytes for WhiteMaterialHash, aligned at 146.
36+
[FieldOffset(146)] public ulong WhiteMaterialHash; // 64-bit
37+
38+
// 8 bytes for BlackMaterialHash, aligned at 154.
39+
[FieldOffset(154)] public ulong BlackMaterialHash; // 64-bit
40+
41+
// Group remaining small fields together (total 4 bytes):
42+
[FieldOffset(162)] public byte WhiteKingSideTargetSquare; // 1 byte
43+
[FieldOffset(163)] public byte WhiteQueenSideTargetSquare; // 1 byte
44+
[FieldOffset(164)] public byte BlackKingSideTargetSquare; // 1 byte
45+
[FieldOffset(165)] public byte BlackQueenSideTargetSquare; // 1 byte
46+
[FieldOffset(166)] public bool Is960; // 1 byte
47+
48+
// Add padding if necessary to make the total size a multiple of 8 bytes.
49+
[FieldOffset(167)] private fixed byte _padding[1]; // Padding to align total size to 168 bytes.
4650
}
4751

52+
4853
public static unsafe class AccumulatorStateExtensions
4954
{
5055
[MethodImpl(MethodImplOptions.AggressiveInlining)]

0 commit comments

Comments
 (0)