Skip to content

Commit fba7bd7

Browse files
committed
resolved issues
1 parent 3faf5fa commit fba7bd7

12 files changed

Lines changed: 75 additions & 31 deletions

File tree

‎Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ deps:
3131
test -d deps/chesslib || git clone https://github.com/winapiadmin/chesslib deps/chesslib
3232
test -d deps/tbprobe || git clone https://github.com/winapiadmin/tb_probing_tool deps/tbprobe
3333
# Tuning is not required on Makefile, use CMake.
34-
SRCS = $(filter-out tuning_cmd.cpp, $(wildcard *.cpp)) $(wildcard deps/chesslib/*.cpp) $(wildcard deps/tbprobe/*.cpp)
34+
SRCS = $(filter-out tune_cmd.cpp, $(wildcard *.cpp)) $(wildcard deps/chesslib/*.cpp) $(wildcard deps/tbprobe/*.cpp)
3535
OBJS = $(SRCS:.cpp=.o)
3636

37+
.PHONY: all clean deps
3738
all: deps $(TARGET)
3839

3940
$(TARGET): $(OBJS)

‎eval.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,14 @@ EvalComponents eval_components(const chess::Position &board) {
712712
board.count<ROOK>() == 0 && board.count<QUEEN>() == 0)
713713
return { 0, 0, 0 };
714714

715-
phase = (phase * 256 + TotalPhase / 2) / TotalPhase;
715+
phase = std::min((phase * 256 + TotalPhase / 2) / TotalPhase, 256);
716716
return { mgScore, egScore, phase };
717717
}
718718
Value eval(const chess::Position &board) {
719719
const int sign = board.side_to_move() == WHITE ? 1 : -1;
720720
auto [mg, eg, phase] = eval_components(board);
721+
if (mg == 0 && eg == 0)
722+
return 0;
721723
return (((mg * phase) + (eg * (256 - phase))) * sign) / 256 + engine::eval::tempo;
722724
}
723725
Value piece_value(PieceType pt) {

‎main.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "uci.h"
55
#include "ucioption.h"
66
#include <iostream>
7+
#include <new>
78
using namespace engine;
89
#define STR_HELPER(x) #x
910
#define STR(x) STR_HELPER(x)
@@ -13,7 +14,11 @@ int main() {
1314
std::cout << "cppchess_engine version " << BUILD_VERSION << '\n';
1415
options.add("Move Overhead", Option(10, 0, 1000));
1516
options.add("Hash", Option(16, 1, 1 << 25, [](const Option &o) {
16-
search::tt.resize(int(o));
17+
try {
18+
search::tt.resize(int(o));
19+
} catch (std::bad_alloc &) {
20+
std::cerr << "info string Hash resize failed: bad_alloc\n";
21+
}
1722
return std::nullopt;
1823
}));
1924

@@ -22,11 +27,11 @@ int main() {
2227

2328
return std::nullopt;
2429
}));
25-
// work in progress
2630
options.add("SyzygyPath", Option("", [](const Option &o) {
2731
tb::init(std::string(o));
2832
return std::nullopt;
2933
}));
3034
Tune::init(options);
35+
std::cout.flush();
3136
loop();
3237
}

‎movepick.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Value see(Position &board, Move move) {
5858

5959
Bitboard occ = board.occ();
6060
occ ^= 1ULL << from;
61+
if (move.type_of() == EN_PASSANT)
62+
occ ^= 1ULL << Square(int(to) + (board.side_to_move() == WHITE ? -8 : 8));
6163

6264
Bitboard attackers = board.attackers(WHITE, to, occ) | board.attackers(BLACK, to, occ);
6365

@@ -77,9 +79,7 @@ Value see(Position &board, Move move) {
7779
if (gain[d] < 0)
7880
break;
7981

80-
occ &= attackers;
81-
82-
Bitboard stmAttackers = attackers & occ & board.occ(stm);
82+
Bitboard stmAttackers = attackers & board.occ(stm);
8383
if (!stmAttackers)
8484
break;
8585

‎search.cpp‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Value qsearch(Position &board, Value alpha, Value beta, search::Session &session
7272
session.qnodes++;
7373
session.seldepth = std::max(session.seldepth, ply);
7474
if (((session.nodes & 2047) == 0 && session.tm.elapsed() >= session.tm.optimum()) ||
75+
(session.tc.nodes > 0 && session.nodes >= session.tc.nodes) ||
7576
stopSearch.load(std::memory_order_relaxed))
7677
return VALUE_NONE;
7778

@@ -187,6 +188,7 @@ Value doSearch(
187188
if (depth <= 0)
188189
return qsearch(board, alpha, beta, session, ply);
189190
if (((session.nodes & 2047) == 0 && session.tm.elapsed() >= session.tm.optimum()) ||
191+
(session.tc.nodes > 0 && session.nodes >= session.tc.nodes) ||
190192
stopSearch.load(std::memory_order_relaxed))
191193
return VALUE_NONE;
192194

@@ -417,7 +419,8 @@ Value doSearch(
417419
} else if (movesSearched >= 6) {
418420
reduction = 1 + movesSearched / 8;
419421
}
420-
reduction = std::clamp(reduction, 1, depth - 2);
422+
if (reduction > 0)
423+
reduction = std::clamp(reduction, 1, depth - 2);
421424
}
422425

423426
int ext = 0;
@@ -569,7 +572,10 @@ void search(const chess::Position &board, const timeman::LimitsType timecontrol)
569572
Position board_ = board;
570573
Move best = Move::none();
571574
Value bestScore = -VALUE_INFINITE;
572-
Session tmpSession{};
575+
double unusedTimeAdjust = -1;
576+
Session tmpSession;
577+
tmpSession.tc = session.tc;
578+
tmpSession.tm.init(session.tc, board.side_to_move(), board.ply(), unusedTimeAdjust);
573579
for (Move move : moves) {
574580
if (!session.tc.searchmoves.empty() &&
575581
std::find(session.tc.searchmoves.begin(),
@@ -669,7 +675,10 @@ void search(const chess::Position &board, const timeman::LimitsType timecontrol)
669675
Position board_ = board;
670676
Move best = Move::none();
671677
Value bestScore = -VALUE_INFINITE;
672-
Session tmpSession{};
678+
double unusedTimeAdjust = -1;
679+
Session tmpSession;
680+
tmpSession.tc = session.tc;
681+
tmpSession.tm.init(session.tc, board.side_to_move(), board.ply(), unusedTimeAdjust);
673682
for (Move move : moves) {
674683
if (!session.tc.searchmoves.empty() &&
675684
std::find(session.tc.searchmoves.begin(),

‎search.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ struct Session {
1010
int seldepth = 0;
1111
uint64_t nodes = 0, qnodes = 0, lmrResearches = 0;
1212
uint64_t tbHits = 0, ttHits = 0, ttCutoffs = 0, nullCutoffs = 0;
13-
chess::Move pv[MAX_PLY][MAX_PLY];
13+
chess::Move pv[MAX_PLY][MAX_PLY]{};
1414
Value historyHeuristic[chess::SQUARE_NB][chess::SQUARE_NB]{};
15-
chess::Move killerMoves[MAX_PLY][2];
16-
chess::Move counterMoves[4096];
15+
chess::Move killerMoves[MAX_PLY][2]{};
16+
chess::Move counterMoves[4096]{};
1717
timeman::TimePoint lastLogTime;
1818
int depth = 0;
1919
chess::Color ogcolor;

‎tb.cpp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ void init(const std::string &path) {
2828
return;
2929
}
3030

31-
tbprobe::syzygy::initialize();
32-
tablebase.add_directory(path, true, true);
33-
std::cout << "info string Found " << wdl_count() << " WDL and " << dtz_count() << " DTZ tablebase files\n";
31+
try {
32+
tbprobe::syzygy::initialize();
33+
tablebase.add_directory(path, true, true);
34+
std::cout << "info string Found " << wdl_count() << " WDL and " << dtz_count() << " DTZ tablebase files\n";
35+
} catch (const std::exception &e) {
36+
std::cout << "info string Syzygy init failed: " << e.what() << '\n';
37+
}
3438
}
3539

3640
std::size_t wdl_count() { return unique_table_count(tablebase.wdl); }

‎tt.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ static inline uint64_t index_for_hash(uint64_t hash, uint64_t buckets) {
1919
__uint128_t prod = (__uint128_t)hash * (__uint128_t)buckets;
2020
return (uint64_t)(prod >> 64);
2121
#else
22-
uint64_t aL = uint32_t(hash), aH = a >> 32;
23-
uint64_t bL = uint32_t(buckets), bH = b >> 32;
22+
uint64_t aL = uint32_t(hash), aH = hash >> 32;
23+
uint64_t bL = uint32_t(buckets), bH = buckets >> 32;
2424
uint64_t c1 = (aL * bL) >> 32;
2525
uint64_t c2 = aH * bL + c1;
2626
uint64_t c3 = aL * bH + uint32_t(c2);
@@ -56,7 +56,7 @@ void TranspositionTable::store(uint64_t hash, chess::Move best, int16_t score, i
5656
}
5757

5858
TTEntry *TranspositionTable::lookup(uint64_t hash) {
59-
if (buckets == 0)
59+
if (buckets == 0 || hash == 0)
6060
return nullptr;
6161

6262
uint64_t bucket = index_for_hash(hash, buckets);

‎tt.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct TTEntry {
8383
};
8484
class TranspositionTable {
8585
TTEntry *table;
86-
int buckets; // number of buckets (pairs)
86+
size_t buckets; // number of buckets (pairs)
8787
uint32_t time;
8888

8989
public:

‎tune.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <algorithm>
2222
#include <cctype>
2323
#include <climits>
24+
#include <cstdlib>
2425
#include <iostream>
2526
#include <map>
2627
#include <optional>
@@ -76,7 +77,7 @@ void Tune::make_option(OptionsMap *opts, const string &n, int v, const SetRange
7677
<< a << "," //
7778
<< b << "," //
7879
<< (b - a) / 20.0 << "," //
79-
<< "0.0020" << std::endl;
80+
<< "0.0020" << '\n';
8081
}
8182

8283
string Tune::next(string &names, bool pop) {

0 commit comments

Comments
 (0)