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

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.
This commit is contained in:
Marco Costalba 2013-06-29 10:23:43 +02:00
parent 17d41b3861
commit 203fdc9ac1
3 changed files with 42 additions and 44 deletions

View file

@ -570,9 +570,9 @@ namespace {
&& tte && tte
&& tte->depth() >= depth && tte->depth() >= depth
&& ttValue != VALUE_NONE // Only in case of TT access race && ttValue != VALUE_NONE // Only in case of TT access race
&& ( PvNode ? tte->type() == BOUND_EXACT && ( PvNode ? tte->bound() == BOUND_EXACT
: ttValue >= beta ? (tte->type() & BOUND_LOWER) : ttValue >= beta ? (tte->bound() & BOUND_LOWER)
: (tte->type() & BOUND_UPPER))) : (tte->bound() & BOUND_UPPER)))
{ {
TT.refresh(tte); TT.refresh(tte);
ss->currentMove = ttMove; // Can be MOVE_NONE ss->currentMove = ttMove; // Can be MOVE_NONE
@ -601,8 +601,8 @@ namespace {
// Can ttValue be used as a better position evaluation? // Can ttValue be used as a better position evaluation?
if (ttValue != VALUE_NONE) if (ttValue != VALUE_NONE)
if ( ((tte->type() & BOUND_LOWER) && ttValue > eval) if ( ((tte->bound() & BOUND_LOWER) && ttValue > eval)
|| ((tte->type() & BOUND_UPPER) && ttValue < eval)) || ((tte->bound() & BOUND_UPPER) && ttValue < eval))
eval = ttValue; eval = ttValue;
} }
else else
@ -775,7 +775,7 @@ split_point_start: // At split points actual search starts from here
&& depth >= (PvNode ? 6 * ONE_PLY : 8 * ONE_PLY) && depth >= (PvNode ? 6 * ONE_PLY : 8 * ONE_PLY)
&& ttMove != MOVE_NONE && ttMove != MOVE_NONE
&& !excludedMove // Recursive singular search is not allowed && !excludedMove // Recursive singular search is not allowed
&& (tte->type() & BOUND_LOWER) && (tte->bound() & BOUND_LOWER)
&& tte->depth() >= depth - 3 * ONE_PLY; && tte->depth() >= depth - 3 * ONE_PLY;
// Step 11. Loop through moves // Step 11. Loop through moves
@ -1172,9 +1172,9 @@ split_point_start: // At split points actual search starts from here
if ( tte if ( tte
&& tte->depth() >= ttDepth && tte->depth() >= ttDepth
&& ttValue != VALUE_NONE // Only in case of TT access race && ttValue != VALUE_NONE // Only in case of TT access race
&& ( PvNode ? tte->type() == BOUND_EXACT && ( PvNode ? tte->bound() == BOUND_EXACT
: ttValue >= beta ? (tte->type() & BOUND_LOWER) : ttValue >= beta ? (tte->bound() & BOUND_LOWER)
: (tte->type() & BOUND_UPPER))) : (tte->bound() & BOUND_UPPER)))
{ {
ss->currentMove = ttMove; // Can be MOVE_NONE ss->currentMove = ttMove; // Can be MOVE_NONE
return ttValue; 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) { void RootMove::extract_pv_from_tt(Position& pos) {
StateInfo state[MAX_PLY_PLUS_2], *st = state; StateInfo state[MAX_PLY_PLUS_2], *st = state;
TTEntry* tte; const TTEntry* tte;
int ply = 0; int ply = 0;
Move m = pv[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) { void RootMove::insert_pv_in_tt(Position& pos) {
StateInfo state[MAX_PLY_PLUS_2], *st = state; StateInfo state[MAX_PLY_PLUS_2], *st = state;
TTEntry* tte; const TTEntry* tte;
int ply = 0; int ply = 0;
do { do {

View file

@ -41,7 +41,7 @@ void TranspositionTable::set_size(size_t mbSize) {
hashMask = size - ClusterSize; hashMask = size - ClusterSize;
free(mem); free(mem);
mem = malloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1); mem = calloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1, 1);
if (!mem) 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)); 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 /// TranspositionTable::store() writes a new entry containing position key and
/// valuable information of current position. The lowest order bits of position /// valuable information of current position. The lowest order bits of position
/// key are used to decide on which cluster the position will be placed. /// 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 if (!tte->key() || tte->key() == key32) // Empty or overwrite old
{ {
// Preserve any existing ttMove if (!m)
if (m == MOVE_NONE) m = tte->move(); // Preserve any existing ttMove
m = tte->move();
tte->save(key32, v, t, d, m, generation, statV, kingD); replace = tte;
return; break;
} }
// Implement replace strategy // Implement replace strategy
c1 = (replace->generation() == generation ? 2 : 0); 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); c3 = (tte->depth() < replace->depth() ? 1 : 0);
if (c1 + c2 + c3 > 0) if (c1 + c2 + c3 > 0)
replace = tte; replace = tte;
} }
replace->save(key32, v, t, d, m, generation, statV, kingD); 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;
}

View file

@ -34,27 +34,26 @@
/// static value: 16 bit /// static value: 16 bit
/// static margin: 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) { void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value ev, Value em) {
key32 = (uint32_t)k; key32 = (uint32_t)k;
move16 = (uint16_t)m; move16 = (uint16_t)m;
bound = (uint8_t)b; bound8 = (uint8_t)b;
generation8 = (uint8_t)g; generation8 = (uint8_t)g;
value16 = (int16_t)v; value16 = (int16_t)v;
depth16 = (int16_t)d; depth16 = (int16_t)d;
evalValue = (int16_t)ev; evalValue = (int16_t)ev;
evalMargin = (int16_t)em; 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; } uint32_t key() const { return key32; }
Depth depth() const { return (Depth)depth16; } Depth depth() const { return (Depth)depth16; }
Move move() const { return (Move)move16; } Move move() const { return (Move)move16; }
Value value() const { return (Value)value16; } Value value() const { return (Value)value16; }
Bound type() const { return (Bound)bound; } Bound bound() const { return (Bound)bound8; }
int generation() const { return (int)generation8; } int generation() const { return (int)generation8; }
Value eval_value() const { return (Value)evalValue; } Value eval_value() const { return (Value)evalValue; }
Value eval_margin() const { return (Value)evalMargin; } Value eval_margin() const { return (Value)evalMargin; }
@ -62,7 +61,7 @@ public:
private: private:
uint32_t key32; uint32_t key32;
uint16_t move16; uint16_t move16;
uint8_t bound, generation8; uint8_t bound8, generation8;
int16_t value16, depth16, evalValue, evalMargin; int16_t value16, depth16, evalValue, evalMargin;
}; };
@ -81,7 +80,7 @@ public:
~TranspositionTable() { free(mem); } ~TranspositionTable() { free(mem); }
void new_search() { generation++; } void new_search() { generation++; }
TTEntry* probe(const Key key) const; const TTEntry* probe(const Key key) const;
TTEntry* first_entry(const Key key) const; TTEntry* first_entry(const Key key) const;
void refresh(const TTEntry* tte) const; void refresh(const TTEntry* tte) const;
void set_size(size_t mbSize); void set_size(size_t mbSize);
@ -92,7 +91,7 @@ private:
uint32_t hashMask; uint32_t hashMask;
TTEntry* table; TTEntry* table;
void* mem; 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; extern TranspositionTable TT;