Skip to content

Commit 4413659

Browse files
committed
Major cleanup Part 1
bench: 4380745
2 parents 2895237 + 3cc1671 commit 4413659

20 files changed

+904
-572
lines changed

src_files/bitboard.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ using Diagonal = int8_t;
3535
using AntiDiagonal = int8_t;
3636
using Direction = int8_t;
3737

38+
using NodeType = uint8_t;
39+
using NodeAge = uint8_t;
40+
41+
3842
using File = int8_t;
3943
using Rank = int8_t;
4044
using Piece = int8_t;
@@ -118,6 +122,14 @@ enum Directions{
118122
N_DIRECTIONS = 8
119123
};
120124

125+
enum Nodes : NodeType {
126+
PV_NODE = 0,
127+
CUT_NODE = 1,
128+
ALL_NODE = 2,
129+
FORCED_ALL_NODE = 3,
130+
FORMER_CUT_NODE = 6,
131+
};
132+
121133
enum Ranks{
122134
RANK_1,
123135
RANK_2,

src_files/board.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Board::Board(const std::string& fen) {
6262
this->m_boardStatusHistory.push_back(boardStatus);
6363

6464
// using some string utilties defined in Util.h, we split the fen into parts.
65-
std::vector<std::string> split = split_input_fen(fen);
65+
std::vector<std::string> split = splitString(fen);
6666

6767
// first we parse the pieces on the board.
6868
File x {0};

src_files/board.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ class Board {
288288
template<bb::Color color, bb::PieceType piece_type>
289289
[[nodiscard]] inline bb::U64 getPieceBB() const {return m_piecesBB[color * 8 + piece_type];}
290290

291+
template<bb::PieceType piece_type>
292+
[[nodiscard]] inline bb::U64 getPieceTypeBB() const {return getPieceBB<bb::WHITE, piece_type>() | getPieceBB<bb::BLACK, piece_type>();}
293+
291294
[[nodiscard]] bb::Score evaluate();
292295
};
293296

src_files/history.cpp

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,23 @@
2121
using namespace bb;
2222
using namespace move;
2323

24-
int SearchData::getHistories(Move m, Color side, Move previous, Move followup, Square threatSquare) const {
24+
/**
25+
* getHistories function retrieves the history scores of a given move, side, previous move, followup
26+
* move, and threat square. The function checks if the move is a capture move, and returns the
27+
* corresponding capture history score. If not, it computes the FMH, CMH and THREAT_HISTORY scores and
28+
* returns their average.
29+
*
30+
* @param m The move to retrieve history scores for
31+
* @param side The side the move is being made by
32+
* @param previous The previous move that was made in the search tree
33+
* @param followup The next move after the given move in the search tree
34+
* @param threatSquare The square that is being threatened by the given move
35+
* @return The history score for the given move
36+
*/
37+
int SearchData::getHistories(Move m, Color side, Move previous, Move followup,
38+
Square threatSquare) const {
2539
if (isCapture(m)) {
26-
return CAPTURE_HISTORY(this,side,m);
40+
return CAPTURE_HISTORY(this, side, m);
2741
} else {
2842
auto fmh_value = (followup != 0 ? FMH(this, followup, side, m) : 0);
2943
auto cmh_value = CMH(this, previous, side, m);
@@ -32,8 +46,14 @@ int SearchData::getHistories(Move m, Color side, Move previous, Move followup, S
3246
}
3347
}
3448

35-
/*
36-
* Set killer
49+
/**
50+
* setKiller sets a given move as the killer move for a given ply and color. It checks if the move is
51+
* not the same as the current killer move, and updates the killer move accordingly.
52+
*
53+
* @param move The move to set as the killer move
54+
* @param ply The ply in the search tree
55+
* @param color The color making the move
56+
* @return None
3757
*/
3858
void SearchData::setKiller(Move move, Depth ply, Color color) {
3959
if (!sameMove(move, KILLER1(this, color, ply))) {
@@ -42,39 +62,61 @@ void SearchData::setKiller(Move move, Depth ply, Color color) {
4262
}
4363
}
4464

45-
46-
/*
47-
* Is killer?
65+
/**
66+
* isKiller checks if a given move is a killer move for a given ply and color.
67+
* It compares the given move to the first and second killer moves and returns 2 if it matches the
68+
* first, 1 if it matches the second, and 0 otherwise.
69+
*
70+
* @param move The move to check if it is a killer move
71+
* @param ply The ply in the search tree
72+
* @param color The color making the move
73+
* @return 2 if the move is the first killer move, 1 if the move is the second killer move, 0
74+
* otherwise
4875
*/
4976
int SearchData::isKiller(Move move, Depth ply, Color color) const {
5077
if (sameMove(move, KILLER1(this, color, ply)))
5178
return 2;
5279
return sameMove(move, KILLER2(this, color, ply));
5380
}
54-
/*
55-
* Set historic eval
81+
82+
/**
83+
* setHistoricEval sets the evaluation score for a given color and ply in the search tree.
84+
*
85+
* @param ev The evaluation score to set
86+
* @param color The color of the position being evaluated
87+
* @param ply The ply in the search tree
88+
* @return None
5689
*/
5790
void SearchData::setHistoricEval(Score ev, Color color, Depth ply) {
5891
EVAL_HISTORY(this, color, ply) = ev;
5992
}
6093

61-
/*
62-
* Is improving
94+
/**
95+
* isImproving checks if the evaluation score for a given color and ply in the search tree is
96+
* improving. It compares the given evaluation score to the previous evaluation score from 2 plies ago
97+
* and returns true if it is greater, false otherwise.
98+
*
99+
* @param ev The evaluation score to check for improvement
100+
* @param color The color of the position being evaluated
101+
* @param ply The ply in the search tree
102+
* @return true if the given evaluation score is greater than the previous evaluation score from 2
103+
* plies ago, false otherwise
63104
*/
64105
bool SearchData::isImproving(Score ev, Color color, Depth ply) const {
65106
if (ply >= 2) {
66-
return (ev > EVAL_HISTORY(this, color, ply-2));
107+
return (ev > EVAL_HISTORY(this, color, ply - 2));
67108
}
68109
return true;
69110
}
70111

112+
/**
113+
* clear function sets all elements in the `th`, `captureHistory`, `cmh`, `fmh`, `killer` and
114+
* `maxImprovement` arrays to 0. This function is used to reset the search data for a new search
115+
* iteration.
116+
*
117+
* @return None
118+
*/
71119
void SearchData::clear() {
72-
// std::memset(this->th, 0, 2*64*4096*4);
73-
// std::memset(this->captureHistory, 0, 2*4096*4);
74-
// std::memset(this->cmh, 0, 384*2*384*4);
75-
// std::memset(this->fmh, 0, 384*2*384*4);
76-
// std::memset(this->killer, 0, 2*257*2*4);
77-
// std::memset(this->maxImprovement, 0, 64*64*4);
78120
std::memset(this->th, 0, sizeof(this->th));
79121
std::memset(this->captureHistory, 0, sizeof(this->captureHistory));
80122
std::memset(this->cmh, 0, sizeof(this->cmh));

src_files/history.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,23 @@ struct SearchData {
9898
bool targetReached{1};
9999
// clang-format on
100100

101-
[[nodiscard]] int getHistories(move::Move m, bb::Color side, move::Move previous,
102-
move::Move followup, bb::Square threatSquare) const;
101+
// function to extract histories
102+
[[nodiscard]] int getHistories(move::Move m, bb::Color side, move::Move previous,
103+
move::Move followup, bb::Square threatSquare) const;
103104

104-
void setKiller(move::Move move, bb::Depth ply, bb::Color color);
105+
// function to set killers
106+
void setKiller(move::Move move, bb::Depth ply, bb::Color color);
105107

106-
[[nodiscard]] int isKiller(move::Move move, bb::Depth ply, bb::Color color) const;
108+
// checks if a given move is a killer move
109+
[[nodiscard]] int isKiller(move::Move move, bb::Depth ply, bb::Color color) const;
107110

108-
void setHistoricEval(bb::Score eval, bb::Color color, bb::Depth ply);
109-
111+
// sets historic evals across plies and saves them
112+
void setHistoricEval(bb::Score eval, bb::Color color, bb::Depth ply);
113+
114+
// clears the history. This involves resetting all history tables
110115
void clear();
111-
116+
117+
// checks if the evaluation is improving using the historic evals
112118
[[nodiscard]] bool isImproving(bb::Score eval, bb::Color color, bb::Depth ply) const;
113119
} __attribute__((aligned(64)));
114120

src_files/perft.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ bb::U64 perft(Board* b, int depth, bool print, bool d1, bool hash, int ply) {
8181
}
8282

8383
zob = b->zobrist();
84-
Entry en = perft_tt->get(zob);
85-
if (en.depth == depth && en.zobrist == zob) {
84+
TTEntry en = perft_tt->get(zob);
85+
if (en.depth == depth && en.key == zob) {
8686
return en.move;
8787
}
8888
}

src_files/pv.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/
18+
19+
//
20+
// Created by Luecx on 15.01.2023.
21+
//
22+
23+
#include "pv.h"
24+
move::Move& PVLine::operator()(bb::Depth depth) {
25+
// This operator overload allows the struct to be accessed like an array,
26+
// so we can use pvLine(depth) to access the move at a specific depth
27+
return pv[depth];
28+
}
29+
30+
move::Move PVLine::operator()(bb::Depth depth) const {
31+
// This is the const version of the above operator
32+
return pv[depth];
33+
}
34+
35+
PVLine& PVTable::operator()(bb::Depth depth) {
36+
// This operator overload allows the struct to be accessed like an array,
37+
// so we can use pvTable(depth) to access the PVLine at a specific depth
38+
return pvs[depth];
39+
}
40+
41+
PVLine PVTable::operator()(bb::Depth depth) const {
42+
// This is the const version of the above operator
43+
return pvs[depth];
44+
}
45+
46+
void PVTable::reset() {
47+
// reset all the lengths of PVLine structs to 0
48+
for (auto &pvLine : pvs) {
49+
pvLine.length = 0;
50+
}
51+
}
52+
53+
void PVTable::updatePV(bb::Depth ply, move::Move m) {
54+
// Add the move to the beginning of the PVLine at the current depth
55+
pvs[ply](0) = m;
56+
// Copy the PVLine at the next depth to the current depth
57+
memcpy(&pvs[ply](1), &pvs[ply + 1](0), sizeof(move::Move) * pvs[ply + 1].length);
58+
// Update the length of the PVLine at the current depth
59+
pvs[ply].length = pvs[ply + 1].length + 1;
60+
}

src_files/pv.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/****************************************************************************************************
2+
* *
3+
* Koivisto UCI Chess engine *
4+
* by. Kim Kahre and Finn Eggers *
5+
* *
6+
* Koivisto is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* Koivisto is distributed in the hope that it will be useful, *
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13+
* GNU General Public License for more details. *
14+
* You should have received a copy of the GNU General Public License *
15+
* along with Koivisto. If not, see <http://www.gnu.org/licenses/>. *
16+
* *
17+
****************************************************************************************************/
18+
19+
#ifndef KOIVISTO_PV_H
20+
#define KOIVISTO_PV_H
21+
22+
#include <cstring>
23+
#include "move.h"
24+
25+
/**
26+
* PVLine struct represents a principal variation line.
27+
* This struct is used to store a sequence of moves that are expected to be the best
28+
* move at each ply.
29+
*/
30+
struct PVLine {
31+
// pv is an array that stores the sequence of moves that form the principal variation
32+
move::Move pv[bb::MAX_INTERNAL_PLY + 1];
33+
34+
// length is the number of moves in the principal variation
35+
uint16_t length;
36+
37+
// operator () allows the struct to be accessed like an array,
38+
// so we can use pvLine(depth) to access the move at a specific depth
39+
move::Move& operator()(bb::Depth depth);
40+
41+
// const version of the above operator
42+
move::Move operator()(bb::Depth depth) const;
43+
44+
} __attribute__((aligned(128)));
45+
46+
/**
47+
* PVTable struct represents a table of principal variations.
48+
* This struct is used to store multiple PVLine structs, one for each depth.
49+
*/
50+
struct PVTable {
51+
// pvs is an array of PVLine structs, one for each depth
52+
PVLine pvs[bb::MAX_INTERNAL_PLY + 1];
53+
54+
// operator () allows the struct to be accessed like an array,
55+
// so we can use pvTable(depth) to access the PVLine at a specific depth
56+
PVLine& operator()(bb::Depth depth);
57+
58+
// const version of the above operator
59+
PVLine operator()(bb::Depth depth) const;
60+
61+
// reset all the lengths of PVLine structs to 0
62+
void reset();
63+
64+
// updatePV updates the PVLine at a specific ply with the given move.
65+
// The move is added to the beginning of the PVLine and the PVLine at the next depth is appended
66+
// to it.
67+
void updatePV(bb::Depth ply, move::Move m);
68+
};
69+
70+
#endif // KOIVISTO_PV_H

0 commit comments

Comments
 (0)