Skip to content

Commit

Permalink
Pre-calculate SquareBits and place them in a static array
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Jan 10, 2024
1 parent 828139f commit 25e9813
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Lynx.Benchmark/PieceAtSquare_Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private static int PieceAt_KnowingColor(Position position, int targetSquare)
/// <returns></returns>
private static int PieceAt(Position position, int targetSquare)
{
var bit = BitBoardExtensions.SquareBit(targetSquare);
var bit = Constants.SquareBits[targetSquare];

Side color;

Expand Down
71 changes: 71 additions & 0 deletions src/Lynx.Benchmark/SquareBit_Benchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
*
*
*/

using BenchmarkDotNet.Attributes;
using Lynx.Model;

namespace Lynx.Benchmark;

file static class BitBoardHelpers
{
public static readonly ulong[] SquareBitsArray =
Enum.GetValuesAsUnderlyingType<BoardSquare>()
.OfType<int>()
.Select(square => 1UL << square)
.ToArray();

public static ulong CaculateSquareBit(int boardSquare)
{
return 1UL << boardSquare;
}
}

public class SquareBit_Benchmark : BaseBenchmark
{
[Params(1, 10, 100, 1_000, 10_000)]
public int Size { get; set; }

private BoardSquare[] _squares = null!;

[GlobalSetup]
public void Setup()
{
_squares = Enumerable.Range((int)BoardSquare.a1, (int)BoardSquare.h8).
Select(_ => (BoardSquare)Random.Shared.Next((int)BoardSquare.a1, (int)BoardSquare.h8))
.ToArray();
}

[Benchmark(Baseline = true)]
public ulong Calculated()
{
ulong count = 0;

for (int i = 0; i < Size; ++i)
{
foreach (var square in _squares)
{
count += BitBoardHelpers.CaculateSquareBit((int)square);
}
}

return count;
}

[Benchmark]
public ulong Array()
{
ulong count = 0;

for (int i = 0; i < Size; ++i)
{
foreach (var square in _squares)
{
count += BitBoardHelpers.SquareBitsArray[(int)square];
}
}

return count;
}
}
9 changes: 8 additions & 1 deletion src/Lynx/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public static class Constants
public const string WhiteLongCastle = "e1c1";
public const string BlackLongCastle = "e8c8";


public static readonly int[] EnPassantCaptureSquares =
[
0, 0, 0, 0, 0, 0, 0, 0, // 0-7
Expand Down Expand Up @@ -301,6 +300,14 @@ public static class Constants
];

public const int AbsoluteMaxDepth = 255;

/// <summary>
/// Bits that represents each square on a bitboard
/// </summary>
public static readonly ulong[] SquareBits = Enum.GetValuesAsUnderlyingType<BoardSquare>()
.OfType<int>()
.Select(square => 1UL << square)
.ToArray();
}

#pragma warning restore IDE0055
14 changes: 2 additions & 12 deletions src/Lynx/Model/BitBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,26 +167,16 @@ public static int CountBits(this BitBoard board)
return BitOperations.PopCount(board);
}

/// <summary>
/// Extracts the bit that represents each square on a bitboard
/// </summary>
/// <param name="boardSquare"></param>
/// <returns></returns>
public static ulong SquareBit(int boardSquare)
{
return 1UL << boardSquare;
}

public static bool Contains(this BitBoard board, int boardSquare)
{
var bit = SquareBit(boardSquare);
var bit = Constants.SquareBits[boardSquare];

return (board & bit) != default;
}

public static bool DoesNotContain(this BitBoard board, int boardSquare)
{
var bit = SquareBit(boardSquare);
var bit = Constants.SquareBits[boardSquare];

return (board & bit) == default;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lynx/Model/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public string FEN(int halfMovesWithoutCaptureOrPawnMove = 0, int fullMoveClock =
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int PieceAt(int square)
{
var bit = BitBoardExtensions.SquareBit(square);
var bit = Constants.SquareBits[square];

Side color;

Expand Down
8 changes: 4 additions & 4 deletions src/Lynx/SEE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static bool IsGoodCapture(Position position, Move move, int threshold = 0
var targetSquare = move.TargetSquare();

var occupancy = position.OccupancyBitBoards[(int)Side.Both]
^ BitBoardExtensions.SquareBit(move.SourceSquare())
^ BitBoardExtensions.SquareBit(targetSquare);
^ Constants.SquareBits[move.SourceSquare()]
^ Constants.SquareBits[targetSquare];

var queens = position.Queens;
var bishops = queens | position.Bishops;
Expand Down Expand Up @@ -145,8 +145,8 @@ public static bool HasPositiveScore(Position position, Move move, int threshold
var targetSquare = move.TargetSquare();

var occupancy = position.OccupancyBitBoards[(int)Side.Both]
^ BitBoardExtensions.SquareBit(move.SourceSquare())
^ BitBoardExtensions.SquareBit(targetSquare);
^ Constants.SquareBits[move.SourceSquare()]
^ Constants.SquareBits[targetSquare];

var queens = position.Queens;
var bishops = queens | position.Bishops;
Expand Down

0 comments on commit 25e9813

Please sign in to comment.