Skip to content

Commit

Permalink
Assorted cleanups
Browse files Browse the repository at this point in the history
Assorted cleanups

closes #5046

No functional change

Co-Authored-By: Shahin M. Shahin <41402573+peregrineshahin@users.noreply.github.com>
Co-Authored-By: cj5716 <125858804+cj5716@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 11, 2024
1 parent 3d5b16d commit 9068fdc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 39 deletions.
3 changes: 0 additions & 3 deletions src/bitboard.h
Expand Up @@ -163,7 +163,6 @@ inline Bitboard pawn_attacks_bb(Color c, Square s) {
inline Bitboard line_bb(Square s1, Square s2) {

assert(is_ok(s1) && is_ok(s2));

return LineBB[s1][s2];
}

Expand All @@ -178,7 +177,6 @@ inline Bitboard line_bb(Square s1, Square s2) {
inline Bitboard between_bb(Square s1, Square s2) {

assert(is_ok(s1) && is_ok(s2));

return BetweenBB[s1][s2];
}

Expand Down Expand Up @@ -216,7 +214,6 @@ template<PieceType Pt>
inline Bitboard attacks_bb(Square s) {

assert((Pt != PAWN) && (is_ok(s)));

return PseudoAttacks[Pt][s];
}

Expand Down
2 changes: 1 addition & 1 deletion src/evaluate.cpp
Expand Up @@ -219,7 +219,7 @@ Value Eval::evaluate(const Position& pos, int optimism) {
v = v * (200 - shuffling) / 214;

// Guarantee evaluation does not hit the tablebase range
v = std::clamp(int(v), VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
v = std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);

return v;
}
Expand Down
2 changes: 1 addition & 1 deletion src/movepick.cpp
Expand Up @@ -173,7 +173,7 @@ void MovePicker::score() {
else if constexpr (Type == QUIETS)
{
Piece pc = pos.moved_piece(m);
PieceType pt = type_of(pos.moved_piece(m));
PieceType pt = type_of(pc);
Square from = m.from_sq();
Square to = m.to_sq();

Expand Down
2 changes: 0 additions & 2 deletions src/position.h
Expand Up @@ -252,13 +252,11 @@ inline CastlingRights Position::castling_rights(Color c) const {

inline bool Position::castling_impeded(CastlingRights cr) const {
assert(cr == WHITE_OO || cr == WHITE_OOO || cr == BLACK_OO || cr == BLACK_OOO);

return pieces() & castlingPath[cr];
}

inline Square Position::castling_rook_square(CastlingRights cr) const {
assert(cr == WHITE_OO || cr == WHITE_OOO || cr == BLACK_OO || cr == BLACK_OOO);

return castlingRookSquare[cr];
}

Expand Down
37 changes: 16 additions & 21 deletions src/search.cpp
Expand Up @@ -67,7 +67,7 @@ constexpr int futility_move_count(bool improving, Depth depth) {
Value to_corrected_static_eval(Value v, const Worker& w, const Position& pos) {
auto cv = w.correctionHistory[pos.side_to_move()][pawn_structure_index<Correction>(pos)];
v += cv * std::abs(cv) / 12890;
return std::clamp(int(v), VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
return std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
}

// History and stats update bonus, based on depth
Expand Down Expand Up @@ -297,9 +297,9 @@ void Search::Worker::iterative_deepening() {

// Reset aspiration window starting size
Value avg = rootMoves[pvIdx].averageScore;
delta = Value(9) + int(avg) * avg / 12480;
delta = 9 + avg * avg / 12480;
alpha = std::max(avg - delta, -VALUE_INFINITE);
beta = std::min(avg + delta, int(VALUE_INFINITE));
beta = std::min(avg + delta, VALUE_INFINITE);

// Adjust optimism based on root move's averageScore (~4 Elo)
optimism[us] = 131 * avg / (std::abs(avg) + 95);
Expand Down Expand Up @@ -350,7 +350,7 @@ void Search::Worker::iterative_deepening() {
}
else if (bestValue >= beta)
{
beta = std::min(bestValue + delta, int(VALUE_INFINITE));
beta = std::min(bestValue + delta, VALUE_INFINITE);
++failedHighCnt;
}
else
Expand Down Expand Up @@ -481,7 +481,6 @@ void Search::Worker::clear() {
for (auto& h : to)
h->fill(-71);


for (size_t i = 1; i < reductions.size(); ++i)
reductions[i] = int((20.37 + std::log(size_t(options["Threads"])) / 2) * std::log(i));
}
Expand Down Expand Up @@ -538,7 +537,7 @@ Value Search::Worker::search(

// Check for the available remaining time
if (is_mainthread())
main_manager()->check_time(*this);
main_manager()->check_time(*thisThread);

// Used to send selDepth info to GUI (selDepth counts from 1, ply from 0)
if (PvNode && thisThread->selDepth < ss->ply + 1)
Expand Down Expand Up @@ -680,10 +679,8 @@ Value Search::Worker::search(
}
}


Value unadjustedStaticEval = VALUE_NONE;

// Step 6. Static evaluation of the position
Value unadjustedStaticEval = VALUE_NONE;
if (ss->inCheck)
{
// Skip early pruning when in check
Expand Down Expand Up @@ -820,11 +817,10 @@ Value Search::Worker::search(
if (cutNode && depth >= 8 && !ttMove)
depth -= 2;

probCutBeta = beta + 182 - 68 * improving;

// Step 11. ProbCut (~10 Elo)
// If we have a good enough capture (or queen promotion) and a reduced search returns a value
// much above beta, we can (almost) safely prune the previous move.
probCutBeta = beta + 182 - 68 * improving;
if (
!PvNode && depth > 3
&& std::abs(beta) < VALUE_TB_WIN_IN_MAX_PLY
Expand Down Expand Up @@ -1285,7 +1281,6 @@ Value Search::Worker::search(
{
if (capture)
capturesSearched[captureCount++] = move;

else
quietsSearched[quietCount++] = move;
}
Expand Down Expand Up @@ -1424,9 +1419,8 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
&& (tte->bound() & (ttValue >= beta ? BOUND_LOWER : BOUND_UPPER)))
return ttValue;

Value unadjustedStaticEval = VALUE_NONE;

// Step 4. Static evaluation of the position
Value unadjustedStaticEval = VALUE_NONE;
if (ss->inCheck)
bestValue = futilityBase = -VALUE_INFINITE;
else
Expand Down Expand Up @@ -1521,7 +1515,7 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,

// If static eval is much lower than alpha and move is not winning material
// we can prune this move. (~2 Elo)
if (futilityBase <= alpha && !pos.see_ge(move, VALUE_ZERO + 1))
if (futilityBase <= alpha && !pos.see_ge(move, 1))
{
bestValue = std::max(bestValue, futilityBase);
continue;
Expand Down Expand Up @@ -1597,7 +1591,6 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
if (ss->inCheck && bestValue == -VALUE_INFINITE)
{
assert(!MoveList<LEGAL>(pos).size());

return mated_in(ss->ply); // Plies to mate from the root
}

Expand All @@ -1615,6 +1608,10 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
return bestValue;
}

Depth Search::Worker::reduction(bool i, Depth d, int mn, int delta) {
int reductionScale = reductions[d] * reductions[mn];
return (reductionScale + 1177 - delta * 776 / rootDelta) / 1024 + (!i && reductionScale > 842);
}

namespace {
// Adjusts a mate or TB score from "plies to mate from the root"
Expand All @@ -1623,7 +1620,6 @@ namespace {
Value value_to_tt(Value v, int ply) {

assert(v != VALUE_NONE);

return v >= VALUE_TB_WIN_IN_MAX_PLY ? v + ply : v <= VALUE_TB_LOSS_IN_MAX_PLY ? v - ply : v;
}

Expand Down Expand Up @@ -1805,9 +1801,9 @@ Move Skill::pick_best(const RootMoves& rootMoves, size_t multiPV) {
for (size_t i = 0; i < multiPV; ++i)
{
// This is our magic formula
int push = int((weakness * int(topScore - rootMoves[i].score)
+ delta * (rng.rand<unsigned>() % int(weakness)))
/ 128);
int push = (weakness * int(topScore - rootMoves[i].score)
+ delta * (rng.rand<unsigned>() % int(weakness)))
/ 128;

if (rootMoves[i].score + push >= maxScore)
{
Expand Down Expand Up @@ -1921,7 +1917,6 @@ bool RootMove::extract_ponder_from_tt(const TranspositionTable& tt, Position& po
bool ttHit;

assert(pv.size() == 1);

if (pv[0] == Move::none())
return false;

Expand Down
18 changes: 7 additions & 11 deletions src/search.h
Expand Up @@ -47,7 +47,6 @@ enum NodeType {
class TranspositionTable;
class ThreadPool;
class OptionsMap;
class UCI;

namespace Search {

Expand Down Expand Up @@ -124,10 +123,12 @@ struct LimitsType {
// The UCI stores the uci options, thread pool, and transposition table.
// This struct is used to easily forward data to the Search::Worker class.
struct SharedState {
SharedState(const OptionsMap& o, ThreadPool& tp, TranspositionTable& t) :
options(o),
threads(tp),
tt(t) {}
SharedState(const OptionsMap& optionsMap,
ThreadPool& threadPool,
TranspositionTable& transpositionTable) :
options(optionsMap),
threads(threadPool),
tt(transpositionTable) {}

const OptionsMap& options;
ThreadPool& threads;
Expand Down Expand Up @@ -209,11 +210,7 @@ class Worker {
template<NodeType nodeType>
Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth = 0);

Depth reduction(bool i, Depth d, int mn, int delta) {
int reductionScale = reductions[d] * reductions[mn];
return (reductionScale + 1177 - int(delta) * 776 / int(rootDelta)) / 1024
+ (!i && reductionScale > 842);
}
Depth reduction(bool i, Depth d, int mn, int delta);

// Get a pointer to the search manager, only allowed to be called by the
// main thread.
Expand Down Expand Up @@ -251,7 +248,6 @@ class Worker {
TranspositionTable& tt;

friend class Stockfish::ThreadPool;
friend class Stockfish::UCI;
friend class SearchManager;
};

Expand Down

0 comments on commit 9068fdc

Please sign in to comment.