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

Change TT interface to ask directly for a position key

Instead of a position because the key is all that we
need.

Interface is more clear and also very very little bit faster.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-05-02 14:57:52 +02:00
parent fdb2242d34
commit aabd526f7c
3 changed files with 25 additions and 25 deletions

View file

@ -1055,7 +1055,7 @@ 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.
const TTEntry* tte = TT.retrieve(pos); const TTEntry* tte = TT.retrieve(pos.get_key());
Move ttMove = (tte ? tte->move() : MOVE_NONE); Move ttMove = (tte ? tte->move() : MOVE_NONE);
// Go with internal iterative deepening if we don't have a TT move // Go with internal iterative deepening if we don't have a TT move
@ -1188,7 +1188,7 @@ namespace {
return bestValue; return bestValue;
if (bestValue <= oldAlpha) if (bestValue <= oldAlpha)
TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE); TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE);
else if (bestValue >= beta) else if (bestValue >= beta)
{ {
@ -1199,10 +1199,10 @@ namespace {
update_history(pos, m, depth, movesSearched, moveCount); update_history(pos, m, depth, movesSearched, moveCount);
update_killers(m, ss[ply]); update_killers(m, ss[ply]);
} }
TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, m); TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, m);
} }
else else
TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_EXACT, depth, ss[ply].pv[ply]); TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_EXACT, depth, ss[ply].pv[ply]);
return bestValue; return bestValue;
} }
@ -1244,7 +1244,7 @@ namespace {
return beta - 1; return beta - 1;
// Transposition table lookup // Transposition table lookup
const TTEntry* tte = TT.retrieve(pos); const TTEntry* tte = TT.retrieve(pos.get_key());
Move ttMove = (tte ? tte->move() : MOVE_NONE); Move ttMove = (tte ? tte->move() : MOVE_NONE);
if (tte && ok_to_use_TT(tte, depth, beta, ply)) if (tte && ok_to_use_TT(tte, depth, beta, ply))
@ -1449,7 +1449,7 @@ namespace {
return bestValue; return bestValue;
if (bestValue < beta) if (bestValue < beta)
TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE); TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE);
else else
{ {
BetaCounter.add(pos.side_to_move(), depth, threadID); BetaCounter.add(pos.side_to_move(), depth, threadID);
@ -1459,7 +1459,7 @@ namespace {
update_history(pos, m, depth, movesSearched, moveCount); update_history(pos, m, depth, movesSearched, moveCount);
update_killers(m, ss[ply]); update_killers(m, ss[ply]);
} }
TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, m); TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, m);
} }
assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE); assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
@ -1497,7 +1497,7 @@ namespace {
bool pvNode = (beta - alpha != 1); bool pvNode = (beta - alpha != 1);
if (!pvNode) if (!pvNode)
{ {
tte = TT.retrieve(pos); tte = TT.retrieve(pos.get_key());
if (tte && ok_to_use_TT(tte, depth, beta, ply)) if (tte && ok_to_use_TT(tte, depth, beta, ply))
{ {
assert(tte->type() != VALUE_TYPE_EVAL); assert(tte->type() != VALUE_TYPE_EVAL);
@ -1538,7 +1538,7 @@ namespace {
{ {
// Store the score to avoid a future costly evaluation() call // Store the score to avoid a future costly evaluation() call
if (!isCheck && !tte && ei.futilityMargin == 0) if (!isCheck && !tte && ei.futilityMargin == 0)
TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_EVAL, Depth(-127*OnePly), MOVE_NONE); TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_EVAL, Depth(-127*OnePly), MOVE_NONE);
return bestValue; return bestValue;
} }
@ -1631,9 +1631,9 @@ namespace {
{ {
Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1)); Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1));
if (bestValue < beta) if (bestValue < beta)
TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, d, MOVE_NONE); TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, d, MOVE_NONE);
else else
TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, d, m); TT.store(pos.get_key(), value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, d, m);
} }
// Update killers only for good check moves // Update killers only for good check moves

View file

@ -97,14 +97,14 @@ void TranspositionTable::clear() {
/// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL /// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL
/// never replaces another entry for the same position. /// never replaces another entry for the same position.
void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d, Move m) { void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m) {
TTEntry *tte, *replace; TTEntry *tte, *replace;
tte = replace = first_entry(p); tte = replace = first_entry(posKey);
for (int i = 0; i < 4; i++, tte++) for (int i = 0; i < 4; i++, tte++)
{ {
if (!tte->key() || tte->key() == p.get_key()) // empty or overwrite old if (!tte->key() || tte->key() == posKey) // empty or overwrite old
{ {
// Do not overwrite when new type is VALUE_TYPE_EVAL // Do not overwrite when new type is VALUE_TYPE_EVAL
if (tte->key() && t == VALUE_TYPE_EVAL) if (tte->key() && t == VALUE_TYPE_EVAL)
@ -113,7 +113,7 @@ void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d,
if (m == MOVE_NONE) if (m == MOVE_NONE)
m = tte->move(); m = tte->move();
*tte = TTEntry(p.get_key(), v, t, d, m, generation); *tte = TTEntry(posKey, v, t, d, m, generation);
return; return;
} }
else if (i == 0) // replace would be a no-op in this common case else if (i == 0) // replace would be a no-op in this common case
@ -126,7 +126,7 @@ void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d,
if (c1 + c2 + c3 > 0) if (c1 + c2 + c3 > 0)
replace = tte; replace = tte;
} }
*replace = TTEntry(p.get_key(), v, t, d, m, generation); *replace = TTEntry(posKey, v, t, d, m, generation);
writes++; writes++;
} }
@ -135,12 +135,12 @@ void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d,
/// transposition table. Returns a pointer to the TTEntry or NULL /// transposition table. Returns a pointer to the TTEntry or NULL
/// if position is not found. /// if position is not found.
TTEntry* TranspositionTable::retrieve(const Position& pos) const { TTEntry* TranspositionTable::retrieve(const Key posKey) const {
TTEntry *tte = first_entry(pos); TTEntry *tte = first_entry(posKey);
for (int i = 0; i < 4; i++, tte++) for (int i = 0; i < 4; i++, tte++)
if (tte->key() == pos.get_key()) if (tte->key() == posKey)
return tte; return tte;
return NULL; return NULL;
@ -150,9 +150,9 @@ TTEntry* TranspositionTable::retrieve(const Position& pos) const {
/// TranspositionTable::first_entry returns a pointer to the first /// TranspositionTable::first_entry returns a pointer to the first
/// entry of a cluster given a position. /// entry of a cluster given a position.
inline TTEntry* TranspositionTable::first_entry(const Position& pos) const { inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
return entries + (int(pos.get_key() & (size - 1)) << 2); return entries + (int(posKey & (size - 1)) << 2);
} }
/// TranspositionTable::new_search() is called at the beginning of every new /// TranspositionTable::new_search() is called at the beginning of every new
@ -179,7 +179,7 @@ void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
for (int i = 0; pv[i] != MOVE_NONE; i++) for (int i = 0; pv[i] != MOVE_NONE; i++)
{ {
store(p, VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]); store(p.get_key(), VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
p.do_move(pv[i], st); p.do_move(pv[i], st);
} }
} }

View file

@ -83,14 +83,14 @@ public:
~TranspositionTable(); ~TranspositionTable();
void set_size(unsigned mbSize); void set_size(unsigned mbSize);
void clear(); void clear();
void store(const Position& pos, Value v, ValueType type, Depth d, Move m); void store(const Key posKey, Value v, ValueType type, Depth d, Move m);
TTEntry* retrieve(const Position& pos) const; TTEntry* retrieve(const Key posKey) 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() const; int full() const;
private: private:
inline TTEntry* first_entry(const Position& pos) const; inline TTEntry* first_entry(const Key posKey) const;
unsigned size, writes; unsigned size, writes;
TTEntry* entries; TTEntry* entries;