Skip to content

Commit

Permalink
Add support printsysteminfo UCI command.
Browse files Browse the repository at this point in the history
Add PEXT Bitboards
  • Loading branch information
eduherminio committed Oct 20, 2023
1 parent aa54d88 commit e622a5b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ If you're a Linux user and are new to .NET ecosystem, the conversation in [this

- Razoring [[1](https://www.chessprogramming.org/Razoring)]

- PEXT Bitboards [[1](https://www.chessprogramming.org/BMI2#PEXTBitboards)] [[2](https://analog-hors.github.io/site/magic-bitboards/)]

## Credits

Lynx development wouldn't have been possible without:
Expand Down
44 changes: 44 additions & 0 deletions src/Lynx.Benchmark/PEXT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using BenchmarkDotNet.Attributes;
using Lynx.Model;

namespace Lynx.Benchmark;
public class PEXTBenchmark : BaseBenchmark
{
private readonly Position _position = new Position(Constants.TrickyTestPositionFEN);

[Benchmark(Baseline = true)]
public ulong MagicNumbers()
{
ulong result = default;

for (int i = 0; i < 64; ++i)
{
result |= MagicNumbersRookAttacks(i, _position.OccupancyBitBoards[0]);
result |= MagicNumbersBishopAttacks(i, _position.OccupancyBitBoards[0]);
}

return result;
}

[Benchmark]
public ulong PEXT()
{
ulong result = default;

for (int i = 0; i < 64; ++i)
{
result |= PEXTRookAttacks(i, _position.OccupancyBitBoards[0]);
result |= PEXTBishopAttacks(i, _position.OccupancyBitBoards[0]);
}

return result;
}

private static BitBoard MagicNumbersRookAttacks(int squareIndex, BitBoard occupancy) => Attacks.MagicNumbersRookAttacks(squareIndex, occupancy);

private static BitBoard PEXTRookAttacks(int squareIndex, BitBoard occupancy) => Attacks.RookAttacks(squareIndex, occupancy);

private static BitBoard MagicNumbersBishopAttacks(int squareIndex, BitBoard occupancy) => Attacks.MagicNumbersBishopAttacks(squareIndex, occupancy);

private static BitBoard PEXTBishopAttacks(int squareIndex, BitBoard occupancy) => Attacks.BishopAttacks(squareIndex, occupancy);
}
20 changes: 20 additions & 0 deletions src/Lynx/LynxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Lynx.UCI.Commands.Engine;
using Lynx.UCI.Commands.GUI;
using NLog;
using System.Runtime.Intrinsics.X86;
using System.Text.Json;
using System.Threading.Channels;

Expand Down Expand Up @@ -100,6 +101,9 @@ static ReadOnlySpan<char> ExtractCommandItems(string rawCommand)
case "printsettings":
await HandleSettings();
break;
case "printsysteminfo":
await HandleSystemInfo();
break;
case "staticeval":
await HandleStaticEval(rawCommand, cancellationToken);
HandleQuit();
Expand Down Expand Up @@ -342,6 +346,22 @@ private async ValueTask HandleSettings()
await _engineWriter.Writer.WriteAsync(message);
}

private async ValueTask HandleSystemInfo()
{
try
{
var simd = Bmi2.X64.IsSupported
? "Bmi2.X64 supported, PEXT BitBoards will be used"
: "Bmi2.X64 not supported";

await _engineWriter.Writer.WriteAsync(simd);
}
catch (Exception e)
{
_logger.Error(e);
}
}

private async ValueTask HandleStaticEval(string rawCommand, CancellationToken cancellationToken)
{
try
Expand Down

0 comments on commit e622a5b

Please sign in to comment.