1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-29 16:23:09 +00:00

Split transposition table lookup in a separate function

This slims down the code and is a prerequisite for
future patches.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2008-09-06 15:53:43 +02:00
parent c2c0ba875f
commit a230dc1404
3 changed files with 39 additions and 72 deletions

View file

@ -240,6 +240,7 @@ namespace {
bool singleReply, bool mateThreat); bool singleReply, bool mateThreat);
bool ok_to_do_nullmove(const Position &pos); bool ok_to_do_nullmove(const Position &pos);
bool ok_to_prune(const Position &pos, Move m, Move threat, Depth d); bool ok_to_prune(const Position &pos, Move m, Move threat, Depth d);
bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply);
bool fail_high_ply_1(); bool fail_high_ply_1();
int current_search_time(); int current_search_time();
@ -458,7 +459,7 @@ void think(const Position &pos, bool infinite, bool ponder, int time,
// We're ready to start thinking. Call the iterative deepening loop // We're ready to start thinking. Call the iterative deepening loop
// function: // function:
id_loop(pos, searchMoves); id_loop(pos, searchMoves);;
if(UseLogFile) if(UseLogFile)
LogFile.close(); LogFile.close();
@ -859,14 +860,11 @@ namespace {
// Transposition table lookup. At PV nodes, we don't use the TT for // Transposition table lookup. At PV nodes, we don't use the TT for
// pruning, but only for move ordering. // pruning, but only for move ordering.
Value ttValue; const TTEntry* tte = TT.retrieve(pos);
Depth ttDepth;
Move ttMove = MOVE_NONE;
ValueType ttValueType;
TT.retrieve(pos, &ttValue, &ttDepth, &ttMove, &ttValueType); Move ttMove = (tte ? tte->move() : MOVE_NONE);
// Internal iterative deepening. // Go with internal iterative deepening if we don't have a TT move.
if(UseIIDAtPVNodes && ttMove == MOVE_NONE && depth >= 5*OnePly) { if(UseIIDAtPVNodes && ttMove == MOVE_NONE && depth >= 5*OnePly) {
search_pv(pos, ss, alpha, beta, depth-2*OnePly, ply, threadID); search_pv(pos, ss, alpha, beta, depth-2*OnePly, ply, threadID);
ttMove = ss[ply].pv[ply]; ttMove = ss[ply].pv[ply];
@ -1045,24 +1043,14 @@ namespace {
return beta-1; return beta-1;
// Transposition table lookup // Transposition table lookup
bool ttFound; const TTEntry* tte = TT.retrieve(pos);
Value ttValue;
Depth ttDepth;
Move ttMove = MOVE_NONE;
ValueType ttValueType;
ttFound = TT.retrieve(pos, &ttValue, &ttDepth, &ttMove, &ttValueType); Move ttMove = (tte ? tte->move() : MOVE_NONE);
if(ttFound) {
ttValue = value_from_tt(ttValue, ply); if (tte && ok_to_use_TT(tte, depth, beta, ply))
if(ttDepth >= depth {
|| ttValue >= Max(value_mate_in(100), beta) ss[ply].currentMove = ttMove; // can be MOVE_NONE ?
|| ttValue < Min(value_mated_in(100), beta)) { return value_from_tt(tte->value(), ply);
if((is_lower_bound(ttValueType) && ttValue >= beta) ||
(is_upper_bound(ttValueType) && ttValue < beta)) {
ss[ply].currentMove = ttMove;
return ttValue;
}
}
} }
Value approximateEval = quick_evaluate(pos); Value approximateEval = quick_evaluate(pos);
@ -1590,6 +1578,20 @@ namespace {
lock_release(&(sp->lock)); lock_release(&(sp->lock));
} }
// ok_to_use_TT() returns true if a transposition table score
// can be used at a given point in search.
bool ok_to_use_TT(const TTEntry* tte, Depth depth, Value beta, int ply) {
Value v = value_from_tt(tte->value(), ply);
return ( tte->depth() >= depth
|| v >= Max(value_mate_in(100), beta)
|| v < Min(value_mated_in(100), beta))
&& ( (is_lower_bound(tte->type()) && v >= beta)
|| (is_upper_bound(tte->type()) && v < beta));
}
/// The RootMove class /// The RootMove class

View file

@ -136,29 +136,20 @@ void TranspositionTable::store(const Position &pos, Value v, Depth d,
/// TranspositionTable::retrieve looks up the current position in the /// TranspositionTable::retrieve looks up the current position in the
/// transposition table, and extracts the value, value type, depth and /// transposition table. Returns a pointer to the TTEntry or NULL
/// best move if the position is found. The return value is true if /// if position is not found.
/// the position is found, and false if it isn't.
const TTEntry* TranspositionTable::retrieve(const Position &pos) const {
bool TranspositionTable::retrieve(const Position &pos, Value *value,
Depth *d, Move *move,
ValueType *type) const {
TTEntry *tte = first_entry(pos); TTEntry *tte = first_entry(pos);
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
tte += i; tte += i;
if (tte->key() == pos.get_key()) if (tte->key() == pos.get_key())
{ return tte;
*value = tte->value();
*type = tte->type();
*d = tte->depth();
*move = tte->move();
return true;
}
} }
*move = MOVE_NONE; return NULL;
return false;
} }
@ -222,28 +213,4 @@ TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
value_(v), depth_(int16_t(d)) {} value_(v), depth_(int16_t(d)) {}
/// Functions for extracting data from TTEntry objects.
inline Key TTEntry::key() const {
return key_;
}
inline Depth TTEntry::depth() const {
return Depth(depth_);
}
inline Move TTEntry::move() const {
return Move(data & 0x7FFFF);
}
inline Value TTEntry::value() const {
return Value(value_);
}
inline ValueType TTEntry::type() const {
return ValueType((data >> 20) & 3);
}
inline int TTEntry::generation() const {
return (data >> 23);
}

View file

@ -40,12 +40,12 @@ class TTEntry {
public: public:
TTEntry(); TTEntry();
TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation); TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation);
Key key() const; Key key() const { return key_; }
Depth depth() const; Depth depth() const { return Depth(depth_); }
Move move() const; Move move() const { return Move(data & 0x7FFFF); }
Value value() const; Value value() const { return Value(value_); }
ValueType type() const; ValueType type() const { return ValueType((data >> 20) & 3); }
int generation() const; int generation() const { return (data >> 23); }
private: private:
Key key_; Key key_;
@ -54,7 +54,6 @@ private:
int16_t depth_; int16_t depth_;
}; };
/// The transposition table class. This is basically just a huge array /// The transposition table class. This is basically just a huge array
/// containing TTEntry objects, and a few methods for writing new entries /// containing TTEntry objects, and a few methods for writing new entries
/// and reading new ones. /// and reading new ones.
@ -67,8 +66,7 @@ public:
void set_size(unsigned mbSize); void set_size(unsigned mbSize);
void clear(); void clear();
void store(const Position &pos, Value v, Depth d, Move m, ValueType type); void store(const Position &pos, Value v, Depth d, Move m, ValueType type);
bool retrieve(const Position &pos, Value *value, Depth *d, Move *move, const TTEntry* retrieve(const Position &pos) const;
ValueType *type) const;
void new_search(); void new_search();
void insert_pv(const Position &pos, Move pv[]); void insert_pv(const Position &pos, Move pv[]);
int full(); int full();