Skip to content

Commit

Permalink
Use calloc() in TranspositionTable::set_size()
Browse files Browse the repository at this point in the history
Function calloc() already initializes memory to
zero, so avoid calling clear() afterwards.

Also some renaming while there (inspired by DiscoCheck).

No functional change.
  • Loading branch information
mcostalba committed Jun 29, 2013
1 parent 17d41b3 commit 203fdc9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 44 deletions.
22 changes: 11 additions & 11 deletions src/search.cpp
Expand Up @@ -570,9 +570,9 @@ namespace {
&& tte
&& tte->depth() >= depth
&& ttValue != VALUE_NONE // Only in case of TT access race
&& ( PvNode ? tte->type() == BOUND_EXACT
: ttValue >= beta ? (tte->type() & BOUND_LOWER)
: (tte->type() & BOUND_UPPER)))
&& ( PvNode ? tte->bound() == BOUND_EXACT
: ttValue >= beta ? (tte->bound() & BOUND_LOWER)
: (tte->bound() & BOUND_UPPER)))
{
TT.refresh(tte);
ss->currentMove = ttMove; // Can be MOVE_NONE
Expand Down Expand Up @@ -601,8 +601,8 @@ namespace {

// Can ttValue be used as a better position evaluation?
if (ttValue != VALUE_NONE)
if ( ((tte->type() & BOUND_LOWER) && ttValue > eval)
|| ((tte->type() & BOUND_UPPER) && ttValue < eval))
if ( ((tte->bound() & BOUND_LOWER) && ttValue > eval)
|| ((tte->bound() & BOUND_UPPER) && ttValue < eval))
eval = ttValue;
}
else
Expand Down Expand Up @@ -775,7 +775,7 @@ namespace {
&& depth >= (PvNode ? 6 * ONE_PLY : 8 * ONE_PLY)
&& ttMove != MOVE_NONE
&& !excludedMove // Recursive singular search is not allowed
&& (tte->type() & BOUND_LOWER)
&& (tte->bound() & BOUND_LOWER)
&& tte->depth() >= depth - 3 * ONE_PLY;

// Step 11. Loop through moves
Expand Down Expand Up @@ -1172,9 +1172,9 @@ namespace {
if ( tte
&& tte->depth() >= ttDepth
&& ttValue != VALUE_NONE // Only in case of TT access race
&& ( PvNode ? tte->type() == BOUND_EXACT
: ttValue >= beta ? (tte->type() & BOUND_LOWER)
: (tte->type() & BOUND_UPPER)))
&& ( PvNode ? tte->bound() == BOUND_EXACT
: ttValue >= beta ? (tte->bound() & BOUND_LOWER)
: (tte->bound() & BOUND_UPPER)))
{
ss->currentMove = ttMove; // Can be MOVE_NONE
return ttValue;
Expand Down Expand Up @@ -1584,7 +1584,7 @@ namespace {
void RootMove::extract_pv_from_tt(Position& pos) {

StateInfo state[MAX_PLY_PLUS_2], *st = state;
TTEntry* tte;
const TTEntry* tte;
int ply = 0;
Move m = pv[0];

Expand Down Expand Up @@ -1617,7 +1617,7 @@ void RootMove::extract_pv_from_tt(Position& pos) {
void RootMove::insert_pv_in_tt(Position& pos) {

StateInfo state[MAX_PLY_PLUS_2], *st = state;
TTEntry* tte;
const TTEntry* tte;
int ply = 0;

do {
Expand Down
49 changes: 24 additions & 25 deletions src/tt.cpp
Expand Up @@ -41,7 +41,7 @@ void TranspositionTable::set_size(size_t mbSize) {

hashMask = size - ClusterSize;
free(mem);
mem = malloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1);
mem = calloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1, 1);

if (!mem)
{
Expand All @@ -51,7 +51,6 @@ void TranspositionTable::set_size(size_t mbSize) {
}

table = (TTEntry*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1));
clear(); // Operator new is not guaranteed to initialize memory to zero
}


Expand All @@ -65,6 +64,23 @@ void TranspositionTable::clear() {
}


/// TranspositionTable::probe() looks up the current position in the
/// transposition table. Returns a pointer to the TTEntry or NULL if
/// position is not found.

const TTEntry* TranspositionTable::probe(const Key key) const {

const TTEntry* tte = first_entry(key);
uint32_t key32 = key >> 32;

for (unsigned i = 0; i < ClusterSize; i++, tte++)
if (tte->key() == key32)
return tte;

return NULL;
}


/// TranspositionTable::store() writes a new entry containing position key and
/// valuable information of current position. The lowest order bits of position
/// key are used to decide on which cluster the position will be placed.
Expand All @@ -85,38 +101,21 @@ void TranspositionTable::store(const Key key, Value v, Bound t, Depth d, Move m,
{
if (!tte->key() || tte->key() == key32) // Empty or overwrite old
{
// Preserve any existing ttMove
if (m == MOVE_NONE)
m = tte->move();
if (!m)
m = tte->move(); // Preserve any existing ttMove

tte->save(key32, v, t, d, m, generation, statV, kingD);
return;
replace = tte;
break;
}

// Implement replace strategy
c1 = (replace->generation() == generation ? 2 : 0);
c2 = (tte->generation() == generation || tte->type() == BOUND_EXACT ? -2 : 0);
c2 = (tte->generation() == generation || tte->bound() == BOUND_EXACT ? -2 : 0);
c3 = (tte->depth() < replace->depth() ? 1 : 0);

if (c1 + c2 + c3 > 0)
replace = tte;
}
replace->save(key32, v, t, d, m, generation, statV, kingD);
}


/// TranspositionTable::probe() looks up the current position in the
/// transposition table. Returns a pointer to the TTEntry or NULL if
/// position is not found.

TTEntry* TranspositionTable::probe(const Key key) const {

TTEntry* tte = first_entry(key);
uint32_t key32 = key >> 32;

for (unsigned i = 0; i < ClusterSize; i++, tte++)
if (tte->key() == key32)
return tte;

return NULL;
replace->save(key32, v, t, d, m, generation, statV, kingD);
}
15 changes: 7 additions & 8 deletions src/tt.h
Expand Up @@ -34,35 +34,34 @@
/// static value: 16 bit
/// static margin: 16 bit

class TTEntry {
struct TTEntry {

public:
void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value ev, Value em) {

key32 = (uint32_t)k;
move16 = (uint16_t)m;
bound = (uint8_t)b;
bound8 = (uint8_t)b;
generation8 = (uint8_t)g;
value16 = (int16_t)v;
depth16 = (int16_t)d;
evalValue = (int16_t)ev;
evalMargin = (int16_t)em;
}
void set_generation(int g) { generation8 = (uint8_t)g; }
void set_generation(uint8_t g) { generation8 = g; }

uint32_t key() const { return key32; }
Depth depth() const { return (Depth)depth16; }
Move move() const { return (Move)move16; }
Value value() const { return (Value)value16; }
Bound type() const { return (Bound)bound; }
Bound bound() const { return (Bound)bound8; }
int generation() const { return (int)generation8; }
Value eval_value() const { return (Value)evalValue; }
Value eval_margin() const { return (Value)evalMargin; }

private:
uint32_t key32;
uint16_t move16;
uint8_t bound, generation8;
uint8_t bound8, generation8;
int16_t value16, depth16, evalValue, evalMargin;
};

Expand All @@ -81,7 +80,7 @@ class TranspositionTable {
~TranspositionTable() { free(mem); }
void new_search() { generation++; }

TTEntry* probe(const Key key) const;
const TTEntry* probe(const Key key) const;
TTEntry* first_entry(const Key key) const;
void refresh(const TTEntry* tte) const;
void set_size(size_t mbSize);
Expand All @@ -92,7 +91,7 @@ class TranspositionTable {
uint32_t hashMask;
TTEntry* table;
void* mem;
uint8_t generation; // Size must be not bigger then TTEntry::generation8
uint8_t generation; // Size must be not bigger than TTEntry::generation8
};

extern TranspositionTable TT;
Expand Down

0 comments on commit 203fdc9

Please sign in to comment.