From 203fdc9ac1b0ad5e5820b450a0cffdffb917e069 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 29 Jun 2013 10:23:43 +0200 Subject: [PATCH] Use calloc() in TranspositionTable::set_size() Function calloc() already initializes memory to zero, so avoid calling clear() afterwards. Also some renaming while there (inspired by DiscoCheck). No functional change. --- src/search.cpp | 22 +++++++++++----------- src/tt.cpp | 49 ++++++++++++++++++++++++------------------------- src/tt.h | 15 +++++++-------- 3 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index f4f411c5..15981755 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -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 @@ -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 @@ -775,7 +775,7 @@ split_point_start: // At split points actual search starts from here && 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 @@ -1172,9 +1172,9 @@ split_point_start: // At split points actual search starts from here 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; @@ -1584,7 +1584,7 @@ split_point_start: // At split points actual search starts from here 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]; @@ -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 { diff --git a/src/tt.cpp b/src/tt.cpp index c6b294a0..0d85d030 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -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) { @@ -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 } @@ -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. @@ -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); } diff --git a/src/tt.h b/src/tt.h index 5f34957c..aee530b0 100644 --- a/src/tt.h +++ b/src/tt.h @@ -34,27 +34,26 @@ /// 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; } @@ -62,7 +61,7 @@ public: private: uint32_t key32; uint16_t move16; - uint8_t bound, generation8; + uint8_t bound8, generation8; int16_t value16, depth16, evalValue, evalMargin; }; @@ -81,7 +80,7 @@ public: ~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); @@ -92,7 +91,7 @@ private: 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; -- 2.39.2