Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get rid of PieceLists
  • Loading branch information
razvanfilea committed Apr 27, 2021
commit eaf3c88e2bb1caab18a18dec1c790b76366baf6d
40 changes: 10 additions & 30 deletions ChessAndroid/app/src/main/cpp/chess/Board.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Board.h"

#include "Psqt.h"
#include "Zobrist.h"
#include "algorithm/Evaluation.h"
#include "persistence/FenParser.h"
Expand Down Expand Up @@ -404,7 +405,8 @@ void Board::addPiece(const Square square, const Piece piece) noexcept
if (piece.type() != PAWN)
npm += Evaluation::getPieceValue(piece.type());

pieceList[piece][pieceCount[piece]++] = square;
++pieceCount[piece];
psq += PSQT[piece.type()][square];
}

void Board::movePiece(const Square from, const Square to) noexcept
Expand All @@ -424,17 +426,8 @@ void Board::movePiece(const Square from, const Square to) noexcept
getPiece(to) = piece;
getPieces(piece).addSquare(to);

[[maybe_unused]] bool pieceMoved{};
for (u8 &sq : pieceList[piece])
{
if (sq == from)
{
sq = to;
pieceMoved = true;
break;
}
}
assert(pieceMoved);
psq -= PSQT[piece.type()][from];
psq += PSQT[piece.type()][to];
}

void Board::removePiece(const Square square) noexcept
Expand All @@ -451,22 +444,8 @@ void Board::removePiece(const Square square) noexcept
if (piece.type() != PAWN)
npm -= Evaluation::getPieceValue(piece.type());

auto &piecesSquares = pieceList[piece];

u8 pieceIndex = -1;
for (u8 i = 0; i < pieceCount[piece]; ++i)
{
if (piecesSquares[i] == square)
{
pieceIndex = i;
break;
}
}

assert(pieceIndex < SQUARE_NB);

// Move the last square in this unused index
piecesSquares[pieceIndex] = piecesSquares[--pieceCount[piece]];
--pieceCount[piece];
psq -= PSQT[piece.type()][square];
}

Bitboard Board::findBlockers(const Bitboard sliders, const Square sq, Bitboard &pinners) const noexcept
Expand Down Expand Up @@ -520,12 +499,13 @@ void Board::updatePieceList() noexcept
const Piece piece = getPiece(sq);
if (piece)
{
pieceList[piece][pieceCount[piece]++] = sq;

getPieces(piece).addSquare(sq);

if (piece.type() != PAWN)
npm += Evaluation::getPieceValue(piece.type());

++pieceCount[piece];
psq += PSQT[piece.type()][sq];
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions ChessAndroid/app/src/main/cpp/chess/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BoardState final

u32 moveContents{};
u8 castlingRights{};
Square enPassantSq{};
Square enPassantSq = SQ_NONE;
u8 fiftyMoveRule{};

Move getMove() const noexcept { return Move{ moveContents }; }
Expand Down Expand Up @@ -108,15 +108,15 @@ class Board final
public:
Bitboard occupied{};
std::array<std::array<Bitboard, PIECE_TYPE_NB>, COLOR_NB> pieces{};
std::array<std::array<u8, 16>, 15> pieceList{};
std::array<u8, 15> pieceCount{};

std::array<Piece, SQUARE_NB> data{};

BoardState state{};

short ply{};
short npm{};
i16 ply{};
i16 npm{};
Score psq{};
Color colorToMove{};

private:
Expand Down Expand Up @@ -190,7 +190,7 @@ inline Bitboard Board::getPieces(const Color color) const noexcept

inline Square Board::getKingSq(const Color color) const noexcept
{
return toSquare(pieceList[Piece{ KING, color }][0]);
return getPieces(KING, color).bitScanForward();
}

inline u64 Board::zKey() const noexcept
Expand Down
67 changes: 67 additions & 0 deletions ChessAndroid/app/src/main/cpp/chess/Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,70 @@ constexpr u8 distanceToRankEdge(const Square square) noexcept
const auto rank = rankOf(square);
return std::min<u8>(rank, 7u - rank);
}


class Score final
{
public:
Score() = default;

constexpr Score(const i16 mg, const i16 eg) noexcept
: mg(mg), eg(eg) {}

constexpr Score &operator=(const i16 rhs) noexcept
{
mg = rhs;
eg = rhs;
return *this;
}

constexpr void operator+=(const Score &rhs) noexcept
{
mg += rhs.mg;
eg += rhs.eg;
}

constexpr void operator-=(const Score &rhs) noexcept
{
mg -= rhs.mg;
eg -= rhs.eg;
}

constexpr void operator+=(const i16 rhs) noexcept
{
mg += rhs;
eg += rhs;
}

constexpr void operator-=(const i16 rhs) noexcept
{
mg -= rhs;
eg -= rhs;
}

constexpr Score operator+(const Score &rhs) const noexcept
{
Score lhs = *this;
lhs.mg += rhs.mg;
lhs.eg += rhs.eg;
return lhs;
}

constexpr Score operator-(const Score &rhs) const noexcept
{
Score lhs = *this;
lhs.mg -= rhs.mg;
lhs.eg -= rhs.eg;
return lhs;
}

constexpr Score operator*(const i16 rhs) const noexcept
{
Score lhs = *this;
lhs.mg *= rhs;
lhs.eg *= rhs;
return lhs;
}

i16 mg{}, eg{};
};
1 change: 0 additions & 1 deletion ChessAndroid/app/src/main/cpp/chess/PawnStructureTable.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "Bitboard.h"
#include "Score.h"

struct PawnStructureEntry
{
Expand Down
1 change: 0 additions & 1 deletion ChessAndroid/app/src/main/cpp/chess/Psqt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <array>

#include "Defs.h"
#include "Score.h"

constexpr auto PSQT = []
{
Expand Down
67 changes: 0 additions & 67 deletions ChessAndroid/app/src/main/cpp/chess/Score.h

This file was deleted.

40 changes: 23 additions & 17 deletions ChessAndroid/app/src/main/cpp/chess/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,21 +266,23 @@ namespace Tests

static void perftWrapper(std::string_view tag, std::string_view fen, const std::initializer_list<u64> perftResults)
{
using std::setw;

Board board;
board.setToFen(std::string(fen));

std::vector<u64> perftVector(perftResults);
const std::vector<u64> perftVector(perftResults);

constexpr auto DepthW = 5;
constexpr auto ColumnW = 12;
constexpr std::string_view Pipe = " | ";

const auto displayRow = [&](const i32 depth, const double time, const PerftInfo &info)
{
using std::setw;

const auto expectedNodes = perftVector[depth];
const auto wrongResult = expectedNodes != info.nodes;
const auto nodes = (wrongResult ? (std::string("!!!") + std::to_string(expectedNodes)) : std::to_string(expectedNodes));
const auto nodes = (wrongResult ? (std::string("!!!") + std::to_string(info.nodes)) : std::to_string(info.nodes));

std::cout << "| " << std::setfill(' ') << std::fixed << std::setprecision(1)
<< setw(DepthW) << depth << Pipe
Expand All @@ -297,16 +299,16 @@ namespace Tests

std::cout << tag << ':' << '\n';
std::cout << "| " << std::setfill(' ')
<< std::setw(DepthW) << "Depth" << Pipe
<< std::setw(ColumnW) << "Seconds" << Pipe
<< std::setw(ColumnW) << "Expected" << Pipe
<< std::setw(ColumnW) << "Nodes" << Pipe
<< std::setw(ColumnW) << "E.P." << Pipe
<< std::setw(ColumnW) << "Captures" << Pipe
<< std::setw(ColumnW) << "Castles" << Pipe
<< std::setw(ColumnW) << "Promotions" << Pipe
<< std::setw(ColumnW) << "Checks" << Pipe
<< std::setw(ColumnW) << "DoubleChecks" << Pipe << '\n';
<< setw(DepthW) << "Depth" << Pipe
<< setw(ColumnW) << "Seconds" << Pipe
<< setw(ColumnW) << "Expected" << Pipe
<< setw(ColumnW) << "Nodes" << Pipe
<< setw(ColumnW) << "E.P." << Pipe
<< setw(ColumnW) << "Captures" << Pipe
<< setw(ColumnW) << "Castles" << Pipe
<< setw(ColumnW) << "Promotions" << Pipe
<< setw(ColumnW) << "Checks" << Pipe
<< setw(ColumnW) << "DoubleChecks" << Pipe << '\n';

for (unsigned depth = 1; depth < perftVector.size(); ++depth)
{
Expand All @@ -320,11 +322,11 @@ namespace Tests

displayRow(depth, timeNeeded, info);

/*if (depth == perftVector.size() - 1 || info.nodes != expectedNodeCount)
if (depth == perftVector.size() - 1 || info.nodes != perftVector[depth])
{
std::cout << '\n' << out.str();
break;
}*/
// break;
}

std::cout << std::endl;
}
Expand All @@ -346,8 +348,12 @@ namespace Tests
1, 14, 191, 2812, 43238, 674624, 11030083, 178633661
});

/*perftWrapper("Position 4", "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/P2P2PP/r2Q1R1K w kq - 0 2", {
1, 6, 264, 9467, 422333, 15833292, 706045033
});*/

perftWrapper("Position 4", "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1", {
1, 6, 264, 9467, 422333, 15833292
1, 6, 264, 9467, 422333, 15833292, 706045033
});

perftWrapper("Position 5", "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8", {
Expand Down
4 changes: 4 additions & 0 deletions ChessAndroid/app/src/main/cpp/chess/TranspositionTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <cstring>
#include <iostream>

#ifdef _MSC_VER
#include <xmmintrin.h>
#endif

static constexpr u64 MB = 1ull << 20;

TranspositionTable::TranspositionTable(const usize sizeMb)
Expand Down
5 changes: 2 additions & 3 deletions ChessAndroid/app/src/main/cpp/chess/Uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ void Uci::loop()
else if (token == "stop")
{
Search::stopSearch();
_searchThread.join();
if (_searchThread.joinable())
_searchThread.join();
std::cout << "Joined Thread\n";
} else if (token == "quit")
quit = true;
Expand Down Expand Up @@ -111,8 +112,6 @@ void Uci::loop()
std::cout << results;
} else if (token == "perft")
Tests::runPerftTests();
else
std::cout << "Unknown command\n";

std::cout.flush();

Expand Down
Loading