Skip to content

Commit

Permalink
This is stable implementation from eleeye
Browse files Browse the repository at this point in the history
  • Loading branch information
PikaCat-OuO committed Nov 10, 2024
1 parent 384ae4a commit 56a8f62
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class Position {
bool rule_judge(Value& result, int ply = 0);
int rule60_count() const;
uint16_t chased(Color c);
int possible_repetition() const;
Value major_material(Color c) const;
Value major_material() const;

Expand Down Expand Up @@ -284,6 +285,8 @@ inline Key Position::minor_piece_key() const { return st->minorPieceKey; }

inline Key Position::non_pawn_key(Color c) const { return st->nonPawnKey[c]; }

inline int Position::possible_repetition() const { return filter[st->key]; }

inline Value Position::major_material(Color c) const { return st->majorMaterial[c]; }

inline Value Position::major_material() const {
Expand Down
2 changes: 2 additions & 0 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ Value Search::Worker::search(
excludedMove = ss->excludedMove;
posKey = pos.key();
auto [ttHit, ttData, ttWriter] = tt.probe(posKey);
ttHit &= tt.stable(pos);
// Need further processing of the saved data
ss->ttHit = ttHit;
ttData.move = rootNode ? thisThread->rootMoves[thisThread->pvIdx].pv[0]
Expand Down Expand Up @@ -1422,6 +1423,7 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta)
// Step 3. Transposition table lookup
posKey = pos.key();
auto [ttHit, ttData, ttWriter] = tt.probe(posKey);
ttHit &= tt.stable(pos);
// Need further processing of the saved data
ss->ttHit = ttHit;
ttData.move = ttHit ? ttData.move : Move::none();
Expand Down
15 changes: 14 additions & 1 deletion src/tt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ uint8_t TranspositionTable::generation() const { return generation8; }
// minus 8 times its relative age. TTEntry t1 is considered more valuable than
// TTEntry t2 if its replace value is greater than that of t2.
std::tuple<bool, TTData, TTWriter> TranspositionTable::probe(const Key key) const {

TTEntry* const tte = first_entry(key);
const uint16_t key16 = uint16_t(key); // Use the low 16 bits as key inside the cluster

Expand All @@ -248,6 +247,20 @@ std::tuple<bool, TTData, TTWriter> TranspositionTable::probe(const Key key) cons
return {false, TTData(), TTWriter(replace)};
}

bool TranspositionTable::stable(Position& pos) const {
auto [ttHit, ttData, ttWriter] = probe(pos.key());

if (ttData.move == Move::none() || !pos.pseudo_legal(ttData.move) || !pos.legal(ttData.move)
|| pos.capture(ttData.move))
return true;

StateInfo st;
pos.do_move(ttData.move, st);
bool result = pos.possible_repetition() ? false : stable(pos);
pos.undo_move(ttData.move);
return result;
}


TTEntry* TranspositionTable::first_entry(const Key key) const {
return &table[mul_hi64(key, clusterCount)].entry[0];
Expand Down
2 changes: 2 additions & 0 deletions src/tt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "memory.h"
#include "types.h"
#include "position.h"

namespace Stockfish {

Expand Down Expand Up @@ -81,6 +82,7 @@ class TranspositionTable {
uint8_t generation() const; // The current age, used when writing new data to the TT
std::tuple<bool, TTData, TTWriter>
probe(const Key key) const; // The main method, whose retvals separate local vs global objects
bool stable(Position& pos) const;
TTEntry* first_entry(const Key key)
const; // This is the hash function; its only external use is memory prefetching.

Expand Down

0 comments on commit 56a8f62

Please sign in to comment.