|
5 | 5 |
|
6 | 6 | namespace Sapling.Engine;
|
7 | 7 |
|
8 |
| -[StructLayout(LayoutKind.Explicit, Size = 164)] // Updated size to fit alignment |
| 8 | +[StructLayout(LayoutKind.Explicit, Size = 168)] // Adjusted size to fit more efficiently |
9 | 9 | public unsafe struct BoardStateData
|
10 | 10 | {
|
11 |
| - public const uint BoardStateSize = 164; |
| 11 | + public const uint BoardStateSize = 168; |
12 | 12 |
|
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. |
15 | 15 |
|
16 | 16 | // 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. |
18 | 18 |
|
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) |
20 | 21 | [FieldOffset(128)] public ushort TurnCount; // 16-bit
|
21 |
| - |
22 |
| - // 1 byte, follows TurnCount (16-bit aligned, so no padding needed) |
23 | 22 | [FieldOffset(130)] public byte HalfMoveClock; // 8-bit
|
24 |
| - |
25 |
| - // 1 byte, no special alignment |
26 | 23 | [FieldOffset(131)] public bool WhiteToMove; // 8-bit
|
27 |
| - |
28 |
| - // 1 byte, no special alignment |
29 | 24 | [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. |
46 | 50 | }
|
47 | 51 |
|
| 52 | + |
48 | 53 | public static unsafe class AccumulatorStateExtensions
|
49 | 54 | {
|
50 | 55 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
0 commit comments