#include <cassert>
#include <cmath>
+#include <cstring>
#include "tt.h"
/// the least valuable of the four entries in a cluster. A TTEntry t1 is
/// considered to be more valuable than a TTEntry t2 if t1 is from the
/// current search and t2 is from a previous search, or if the depth of t1
-/// is bigger than the depth of t2.
+/// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL
+/// never replaces another entry for the same position.
void TranspositionTable::store(const Position &pos, Value v, Depth d,
Move m, ValueType type) {
TTEntry *tte, *replace;
tte = replace = first_entry(pos);
- for (int i = 0; i < 4; i++)
+ for (int i = 0; i < 4; i++, tte++)
{
- if (!(tte+i)->key()) // still empty
- {
- *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
- writes++;
- return;
- }
- if ((tte+i)->key() == pos.get_key()) // overwrite old
+ if (!tte->key() || tte->key() == pos.get_key()) // empty or overwrite old
{
+ // Do not overwrite position entry when VALUE_TYPE_EVAL
+ if ( tte->key()
+ && type == VALUE_TYPE_EVAL)
+ return;
+
if (m == MOVE_NONE)
- m = (tte+i)->move();
+ m = tte->move();
- *(tte+i) = TTEntry(pos.get_key(), v, type, d, m, generation);
+ *tte = TTEntry(pos.get_key(), v, type, d, m, generation);
return;
}
- if ( i == 0 // already is (replace == tte+i), common case
- || replace->generation() < (tte+i)->generation())
+ else if (i == 0) // replace would be a no-op in this common case
continue;
- if ( replace->generation() > (tte+i)->generation()
- || (tte+i)->depth() < replace->depth())
- replace = tte+i;
+ int c1 = (replace->generation() == generation ? 2 : 0);
+ int c2 = (tte->generation() == generation ? -2 : 0);
+ int c3 = (tte->depth() < replace->depth() ? 1 : 0);
+
+ if (c1 + c2 + c3 > 0)
+ replace = tte;
}
*replace = TTEntry(pos.get_key(), v, type, d, m, generation);
writes++;
/// transposition table. Returns a pointer to the TTEntry or NULL
/// if position is not found.
-const TTEntry* TranspositionTable::retrieve(const Position &pos) const {
+TTEntry* TranspositionTable::retrieve(const Position &pos) const {
TTEntry *tte = first_entry(pos);
- for (int i = 0; i < 4; i++)
+ for (int i = 0; i < 4; i++, tte++)
{
- tte += i;
if (tte->key() == pos.get_key())
return tte;
}
void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
- UndoInfo u;
+ StateInfo st;
Position p(pos);
for (int i = 0; pv[i] != MOVE_NONE; i++)
{
- store(p, VALUE_NONE, Depth(0), pv[i], VALUE_TYPE_NONE);
- p.do_move(pv[i], u);
+ store(p, VALUE_NONE, Depth(-127*OnePly), pv[i], VALUE_TYPE_NONE);
+ p.do_move(pv[i], st);
}
}
TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
int generation) :
- key_ (k), data((m & 0x7FFFF) | (t << 20) | (generation << 23)),
- value_(v), depth_(int16_t(d)) {}
-
-
-
+ key_ (k), data((m & 0x1FFFF) | (t << 20) | (generation << 23)),
+ value_(int16_t(v)), depth_(int16_t(d)) {}