Skip to content

Commit

Permalink
Add bitboard shifts (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed May 16, 2024
1 parent 25cb24f commit deac4f4
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Lynx/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public static class Constants
/// </summary>
public const BitBoard FullBoard = ulong.MaxValue;

/// <summary>
/// 8 1 0 0 0 0 0 0 0
/// 7 1 0 0 0 0 0 0 0
/// 6 1 0 0 0 0 0 0 0
/// 5 1 0 0 0 0 0 0 0
/// 4 1 0 0 0 0 0 0 0
/// 3 1 0 0 0 0 0 0 0
/// 2 1 0 0 0 0 0 0 0
/// 1 1 0 0 0 0 0 0 0
/// a b c d e f g h
/// </summary>
public const BitBoard AFile = 0x101010101010101;

/// <summary>
/// 8 0 1 1 1 1 1 1 1
/// 7 0 1 1 1 1 1 1 1
Expand All @@ -34,6 +47,19 @@ public static class Constants
/// </summary>
public const BitBoard NotAFile = 0xFEFEFEFEFEFEFEFE;

/// <summary>
/// 8 0 0 0 0 0 0 0 1
/// 7 0 0 0 0 0 0 0 1
/// 6 0 0 0 0 0 0 0 1
/// 5 0 0 0 0 0 0 0 1
/// 4 0 0 0 0 0 0 0 1
/// 3 0 0 0 0 0 0 0 1
/// 2 0 0 0 0 0 0 0 1
/// 1 0 0 0 0 0 0 0 1
/// a b c d e f g h
/// </summary>
public const BitBoard HFile = 0x8080808080808080;

/// <summary>
/// 8 1 1 1 1 1 1 1 0
/// 7 1 1 1 1 1 1 1 0
Expand Down
48 changes: 48 additions & 0 deletions src/Lynx/Model/BitBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,54 @@ public static ulong LSB(this BitBoard board)
return board & (~board + 1);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitBoard ShiftUp(this BitBoard board)
{
return board >> 8;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitBoard ShiftDown(this BitBoard board)
{
return board << 8;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitBoard ShiftLeft(this BitBoard board)
{
return (board >> 1) & Constants.NotHFile;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitBoard ShiftRight(this BitBoard board)
{
return (board << 1) & Constants.NotAFile;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitBoard ShiftUpRight(this BitBoard board)
{
return board.ShiftUp().ShiftRight();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitBoard ShiftUpLeft(this BitBoard board)
{
return board.ShiftUp().ShiftLeft();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitBoard ShiftDownRight(this BitBoard board)
{
return board.ShiftDown().ShiftRight();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitBoard ShiftDownLeft(this BitBoard board)
{
return board.ShiftDown().ShiftLeft();
}

#region Static methods

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
26 changes: 26 additions & 0 deletions tests/Lynx.Test/ConstantsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,30 @@ public void EnPassantCaptureSquares()
[(int)BoardSquare.g3] = (int)BoardSquare.g3 - 8,
[(int)BoardSquare.h3] = (int)BoardSquare.h3 - 8,
}.ToFrozenDictionary();

[Test]
public void HFile()
{
BitBoard bb = 0;
bb.SetBit(BoardSquare.h1);

Assert.NotZero(bb & Constants.HFile);
Assert.Zero(bb & Constants.NotHFile);
Assert.Zero(bb & Constants.AFile);

Assert.AreEqual(Constants.NotHFile, ~Constants.HFile);
}

[Test]
public void AFile()
{
BitBoard bb = 0;
bb.SetBit(BoardSquare.a1);

Assert.NotZero(bb & Constants.AFile);
Assert.Zero(bb & Constants.NotAFile);
Assert.Zero(bb & Constants.HFile);

Assert.AreEqual(Constants.NotAFile, ~Constants.AFile);
}
}
226 changes: 226 additions & 0 deletions tests/Lynx.Test/Model/BitBoardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,230 @@ public void GetLS1BIndex(BoardSquare[] occupiedSquares, int expectedLS1B)
Assert.AreEqual(((BoardSquare)expectedLS1B).ToString(), Constants.Coordinates[expectedLS1B]);
}
}

[Test]
public void ShiftUp()
{
BitBoard bb = 0;
bb.SetBit(BoardSquare.a8);
bb.SetBit(BoardSquare.b8);
bb.SetBit(BoardSquare.c8);
bb.SetBit(BoardSquare.d8);
bb.SetBit(BoardSquare.e8);
bb.SetBit(BoardSquare.f8);
bb.SetBit(BoardSquare.g8);
bb.SetBit(BoardSquare.h8);

Assert.Zero(bb.ShiftUp());

BitBoard another = 0;
another.SetBit(BoardSquare.a7);
another.SetBit(BoardSquare.a7);
another.SetBit(BoardSquare.b7);
another.SetBit(BoardSquare.c7);
another.SetBit(BoardSquare.d7);
another.SetBit(BoardSquare.e7);
another.SetBit(BoardSquare.f7);
another.SetBit(BoardSquare.g7);
another.SetBit(BoardSquare.h7);

Assert.AreEqual(bb, another.ShiftUp());
Assert.AreEqual(another, bb.ShiftDown());
}

[Test]
public void ShiftDown()
{
BitBoard bb = 0;
bb.SetBit(BoardSquare.a1);
bb.SetBit(BoardSquare.b1);
bb.SetBit(BoardSquare.c1);
bb.SetBit(BoardSquare.d1);
bb.SetBit(BoardSquare.e1);
bb.SetBit(BoardSquare.f1);
bb.SetBit(BoardSquare.g1);
bb.SetBit(BoardSquare.h1);

Assert.Zero(bb.ShiftDown());

BitBoard another = 0;
another.SetBit(BoardSquare.a2);
another.SetBit(BoardSquare.a2);
another.SetBit(BoardSquare.b2);
another.SetBit(BoardSquare.c2);
another.SetBit(BoardSquare.d2);
another.SetBit(BoardSquare.e2);
another.SetBit(BoardSquare.f2);
another.SetBit(BoardSquare.g2);
another.SetBit(BoardSquare.h2);

Assert.AreEqual(bb, another.ShiftDown());
Assert.AreEqual(another, bb.ShiftUp());
}

[Test]
public void ShiftLeft()
{
BitBoard bb = 0;
bb.SetBit(BoardSquare.a8);
bb.SetBit(BoardSquare.a7);
bb.SetBit(BoardSquare.a6);
bb.SetBit(BoardSquare.a5);
bb.SetBit(BoardSquare.a4);
bb.SetBit(BoardSquare.a3);
bb.SetBit(BoardSquare.a2);
bb.SetBit(BoardSquare.a1);

Assert.Zero(bb.ShiftLeft());

BitBoard another = 0;
another.SetBit(BoardSquare.b8);
another.SetBit(BoardSquare.b7);
another.SetBit(BoardSquare.b6);
another.SetBit(BoardSquare.b5);
another.SetBit(BoardSquare.b4);
another.SetBit(BoardSquare.b3);
another.SetBit(BoardSquare.b2);
another.SetBit(BoardSquare.b1);

Assert.AreEqual(bb, another.ShiftLeft());
Assert.AreEqual(another, bb.ShiftRight());
}

[Test]
public void ShiftRight()
{
BitBoard bb = 0;
bb.SetBit(BoardSquare.h8);
bb.SetBit(BoardSquare.h7);
bb.SetBit(BoardSquare.h6);
bb.SetBit(BoardSquare.h5);
bb.SetBit(BoardSquare.h4);
bb.SetBit(BoardSquare.h3);
bb.SetBit(BoardSquare.h2);
bb.SetBit(BoardSquare.h1);

Assert.Zero(bb.ShiftRight());

BitBoard another = 0;
another.SetBit(BoardSquare.g8);
another.SetBit(BoardSquare.g7);
another.SetBit(BoardSquare.g6);
another.SetBit(BoardSquare.g5);
another.SetBit(BoardSquare.g4);
another.SetBit(BoardSquare.g3);
another.SetBit(BoardSquare.g2);
another.SetBit(BoardSquare.g1);

Assert.AreEqual(bb, another.ShiftRight());
Assert.AreEqual(another, bb.ShiftLeft());
}

[Test]
public void ShiftUpRight()
{
BitBoard defaultbb = 0;

BitBoard bb = 0;
bb.SetBit(BoardSquare.a1);

Assert.AreEqual(defaultbb.SetBit(BoardSquare.b2), bb.ShiftUpRight());

bb = 0;
bb.SetBit(BoardSquare.h8);
Assert.Zero(bb.ShiftUpRight());

bb = 0;
bb.SetBit(BoardSquare.a8);
Assert.Zero(bb.ShiftUpRight());

bb = 0;
bb.SetBit(BoardSquare.h5);
Assert.Zero(bb.ShiftUpRight());

bb = 0;
bb.SetBit(BoardSquare.h1);
Assert.Zero(bb.ShiftUpRight());
}

[Test]
public void ShiftUpLeft()
{
BitBoard defaultbb = 0;

BitBoard bb = 0;
bb.SetBit(BoardSquare.h1);

Assert.AreEqual(defaultbb.SetBit(BoardSquare.g2), bb.ShiftUpLeft());

bb = 0;
bb.SetBit(BoardSquare.a8);
Assert.Zero(bb.ShiftUpLeft());

bb = 0;
bb.SetBit(BoardSquare.h8);
Assert.Zero(bb.ShiftUpLeft());

bb = 0;
bb.SetBit(BoardSquare.a5);
Assert.Zero(bb.ShiftUpLeft());

bb = 0;
bb.SetBit(BoardSquare.a1);
Assert.Zero(bb.ShiftUpLeft());
}

[Test]
public void ShiftDownLeft()
{
BitBoard defaultbb = 0;

BitBoard bb = 0;
bb.SetBit(BoardSquare.h8);

Assert.AreEqual(defaultbb.SetBit(BoardSquare.g7), bb.ShiftDownLeft());

bb = 0;
bb.SetBit(BoardSquare.a1);
Assert.Zero(bb.ShiftDownLeft());

bb = 0;
bb.SetBit(BoardSquare.h1);
Assert.Zero(bb.ShiftDownLeft());

bb = 0;
bb.SetBit(BoardSquare.a5);
Assert.Zero(bb.ShiftDownLeft());

bb = 0;
bb.SetBit(BoardSquare.a8);
Assert.Zero(bb.ShiftDownLeft());
}

[Test]
public void ShiftDownRight()
{
BitBoard defaultbb = 0;

BitBoard bb = 0;
bb.SetBit(BoardSquare.a8);

Assert.AreEqual(defaultbb.SetBit(BoardSquare.b7), bb.ShiftDownRight());

bb = 0;
bb.SetBit(BoardSquare.h1);
Assert.Zero(bb.ShiftDownRight());

bb = 0;
bb.SetBit(BoardSquare.a1);
Assert.Zero(bb.ShiftDownRight());

bb = 0;
bb.SetBit(BoardSquare.h5);
Assert.Zero(bb.ShiftDownRight());

bb = 0;
bb.SetBit(BoardSquare.h8);
Assert.Zero(bb.ShiftDownRight());
}
}

0 comments on commit deac4f4

Please sign in to comment.