Skip to content

Commit

Permalink
Coding style in TT code
Browse files Browse the repository at this point in the history
In particular seems more natural to return
bool and TTEntry on the same line, actually
we should pass and return them as a pair,
but due to limitations of C++ and not wanting
to use std::pair this can be an acceptable
compromise.

No functional change.

Resolves #157
  • Loading branch information
mcostalba authored and zamar committed Dec 14, 2014
1 parent 0edb634 commit 413b243
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
20 changes: 9 additions & 11 deletions src/search.cpp
Expand Up @@ -458,13 +458,12 @@ namespace {
Move pv[MAX_PLY+1], quietsSearched[64];
StateInfo st;
TTEntry* tte;
bool ttHit;
SplitPoint* splitPoint;
Key posKey;
Move ttMove, move, excludedMove, bestMove;
Depth extension, newDepth, predictedDepth;
Value bestValue, value, ttValue, eval, nullValue, futilityValue;
bool inCheck, givesCheck, singularExtensionNode, improving;
bool ttHit, inCheck, givesCheck, singularExtensionNode, improving;
bool captureOrPromotion, dangerous, doFullDepthSearch;
int moveCount, quietCount;

Expand Down Expand Up @@ -568,7 +567,7 @@ namespace {

tte->save(posKey, value_to_tt(value, ss->ply), BOUND_EXACT,
std::min(DEPTH_MAX - ONE_PLY, depth + 6 * ONE_PLY),
MOVE_NONE, VALUE_NONE, TT.get_generation());
MOVE_NONE, VALUE_NONE, TT.generation());

return value;
}
Expand Down Expand Up @@ -598,7 +597,7 @@ namespace {
eval = ss->staticEval =
(ss-1)->currentMove != MOVE_NULL ? evaluate(pos) : -(ss-1)->staticEval + 2 * Eval::Tempo;

tte->save(posKey, VALUE_NONE, BOUND_NONE, DEPTH_NONE, MOVE_NONE, ss->staticEval, TT.get_generation());
tte->save(posKey, VALUE_NONE, BOUND_NONE, DEPTH_NONE, MOVE_NONE, ss->staticEval, TT.generation());
}

if (ss->skipEarlyPruning)
Expand Down Expand Up @@ -1084,7 +1083,7 @@ namespace {
tte->save(posKey, value_to_tt(bestValue, ss->ply),
bestValue >= beta ? BOUND_LOWER :
PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER,
depth, bestMove, ss->staticEval, TT.get_generation());
depth, bestMove, ss->staticEval, TT.generation());

assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);

Expand All @@ -1110,11 +1109,10 @@ namespace {
Move pv[MAX_PLY+1];
StateInfo st;
TTEntry* tte;
bool ttHit;
Key posKey;
Move ttMove, move, bestMove;
Value bestValue, value, ttValue, futilityValue, futilityBase, oldAlpha;
bool givesCheck, evasionPrunable;
bool ttHit, givesCheck, evasionPrunable;
Depth ttDepth;

if (PvNode)
Expand Down Expand Up @@ -1184,7 +1182,7 @@ namespace {
{
if (!ttHit)
tte->save(pos.key(), value_to_tt(bestValue, ss->ply), BOUND_LOWER,
DEPTH_NONE, MOVE_NONE, ss->staticEval, TT.get_generation());
DEPTH_NONE, MOVE_NONE, ss->staticEval, TT.generation());

return bestValue;
}
Expand Down Expand Up @@ -1283,7 +1281,7 @@ namespace {
else // Fail high
{
tte->save(posKey, value_to_tt(value, ss->ply), BOUND_LOWER,
ttDepth, move, ss->staticEval, TT.get_generation());
ttDepth, move, ss->staticEval, TT.generation());

return value;
}
Expand All @@ -1298,7 +1296,7 @@ namespace {

tte->save(posKey, value_to_tt(bestValue, ss->ply),
PvNode && bestValue > oldAlpha ? BOUND_EXACT : BOUND_UPPER,
ttDepth, bestMove, ss->staticEval, TT.get_generation());
ttDepth, bestMove, ss->staticEval, TT.generation());

assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);

Expand Down Expand Up @@ -1480,7 +1478,7 @@ void RootMove::insert_pv_in_tt(Position& pos) {
TTEntry* tte = TT.probe(pos.key(), ttHit);

if (!ttHit || tte->move() != pv[idx]) // Don't overwrite correct entries
tte->save(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[idx], VALUE_NONE, TT.get_generation());
tte->save(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[idx], VALUE_NONE, TT.generation());

assert(MoveList<LEGAL>(pos).contains(pv[idx]));

Expand Down
24 changes: 11 additions & 13 deletions src/tt.cpp
Expand Up @@ -63,12 +63,12 @@ void TranspositionTable::clear() {
}


/// TranspositionTable::probe() looks up the current position in the
/// transposition table. It returns true and a pointer to the TTEntry if
/// the position is found. Otherwise, it returns false and a pointer to an empty or
/// least valuable TTEntry to be replaced later. 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.
/// TranspositionTable::probe() looks up the current position in the transposition
/// table. It returns true and a pointer to the TTEntry if the position is found.
/// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
/// to be replaced later. 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.

TTEntry* TranspositionTable::probe(const Key key, bool& found) const {

Expand All @@ -79,20 +79,18 @@ TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
if (!tte[i].key16 || tte[i].key16 == key16)
{
if (tte[i].key16)
tte[i].genBound8 = uint8_t(generation | tte[i].bound()); // Refresh
tte[i].genBound8 = uint8_t(generation8 | tte[i].bound()); // Refresh

found = (bool)tte[i].key16;
return &tte[i];
return found = (bool)tte[i].key16, &tte[i];
}

// Find an entry to be replaced according to the replacement strategy
TTEntry* replace = tte;
for (unsigned i = 1; i < TTClusterSize; ++i)
if ( (( tte[i].genBound8 & 0xFC) == generation || tte[i].bound() == BOUND_EXACT)
- ((replace->genBound8 & 0xFC) == generation)
if ( (( tte[i].genBound8 & 0xFC) == generation8 || tte[i].bound() == BOUND_EXACT)
- ((replace->genBound8 & 0xFC) == generation8)
- (tte[i].depth8 < replace->depth8) < 0)
replace = &tte[i];

found = false;
return replace;
return found = false, replace;
}
12 changes: 6 additions & 6 deletions src/tt.h
Expand Up @@ -43,10 +43,10 @@ struct TTEntry {

void save(Key k, Value v, Bound b, Depth d, Move m, Value ev, uint8_t g) {

k >>= 48;
if (m || k != key16) // preserve any existing ttMove
if (m || (k >> 48) != key16) // Preserve any existing move for the same position
move16 = (uint16_t)m;
key16 = (uint16_t)k;

key16 = (uint16_t)(k >> 48);
value16 = (int16_t)v;
evalValue = (int16_t)ev;
genBound8 = (uint8_t)(g | b);
Expand Down Expand Up @@ -86,8 +86,8 @@ class TranspositionTable {

public:
~TranspositionTable() { free(mem); }
void new_search() { generation += 4; } // Lower 2 bits are used by Bound
uint8_t get_generation() const { return generation; }
void new_search() { generation8 += 4; } // Lower 2 bits are used by Bound
uint8_t generation() const { return generation8; }
TTEntry* probe(const Key key, bool& found) const;
TTEntry* first_entry(const Key key) const;
void resize(size_t mbSize);
Expand All @@ -97,7 +97,7 @@ class TranspositionTable {
size_t clusterCount;
TTCluster* table;
void* mem;
uint8_t generation; // Size must be not bigger than TTEntry::genBound8
uint8_t generation8; // Size must be not bigger than TTEntry::genBound8
};

extern TranspositionTable TT;
Expand Down

0 comments on commit 413b243

Please sign in to comment.