X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Ftt.cpp;h=46d891cd04f1558e76b0f2b9161d872c1928694a;hp=24e94c8008bd691c4045719f065359dd1ff3ee5a;hb=15e21911110f9d459c4fef2bb17903d97345d0b9;hpb=40c863d41af5058a02f1de3423a000d801dcb08a diff --git a/src/tt.cpp b/src/tt.cpp index 24e94c80..46d891cd 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -26,22 +26,21 @@ TranspositionTable TT; // Our global transposition table -/// TranspositionTable::set_size() sets the size of the transposition table, +/// TranspositionTable::resize() sets the size of the transposition table, /// measured in megabytes. Transposition table consists of a power of 2 number -/// of clusters and each cluster consists of ClusterSize number of TTEntry. +/// of clusters and each cluster consists of TTClusterSize number of TTEntry. -void TranspositionTable::set_size(uint64_t mbSize) { +void TranspositionTable::resize(size_t mbSize) { - assert(msb((mbSize << 20) / sizeof(TTEntry)) < 32); + size_t newClusterCount = size_t(1) << msb((mbSize * 1024 * 1024) / sizeof(TTCluster)); - uint32_t size = ClusterSize << msb((mbSize << 20) / sizeof(TTEntry[ClusterSize])); - - if (hashMask == size - ClusterSize) + if (newClusterCount == clusterCount) return; - hashMask = size - ClusterSize; + clusterCount = newClusterCount; + free(mem); - mem = calloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1, 1); + mem = calloc(clusterCount * sizeof(TTCluster) + CACHE_LINE_SIZE - 1, 1); if (!mem) { @@ -50,7 +49,7 @@ void TranspositionTable::set_size(uint64_t mbSize) { exit(EXIT_FAILURE); } - table = (TTEntry*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)); + table = (TTCluster*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)); } @@ -60,7 +59,7 @@ void TranspositionTable::set_size(uint64_t mbSize) { void TranspositionTable::clear() { - std::memset(table, 0, (hashMask + ClusterSize) * sizeof(TTEntry)); + std::memset(table, 0, clusterCount * sizeof(TTCluster)); } @@ -70,12 +69,15 @@ void TranspositionTable::clear() { const TTEntry* TranspositionTable::probe(const Key key) const { - const TTEntry* tte = first_entry(key); - uint32_t key32 = key >> 32; + TTEntry* tte = first_entry(key); + uint16_t key16 = key >> 48; - for (unsigned i = 0; i < ClusterSize; ++i, ++tte) - if (tte->key() == key32) + for (unsigned i = 0; i < TTClusterSize; ++i, ++tte) + if (tte->key16 == key16) + { + tte->genBound8 = generation | tte->bound(); // Refresh return tte; + } return NULL; } @@ -91,15 +93,14 @@ const TTEntry* TranspositionTable::probe(const Key key) const { void TranspositionTable::store(const Key key, Value v, Bound b, Depth d, Move m, Value statV) { - int c1, c2, c3; TTEntry *tte, *replace; - uint32_t key32 = key >> 32; // Use the high 32 bits as key inside the cluster + uint16_t key16 = key >> 48; // Use the high 16 bits as key inside the cluster tte = replace = first_entry(key); - for (unsigned i = 0; i < ClusterSize; ++i, ++tte) + for (unsigned i = 0; i < TTClusterSize; ++i, ++tte) { - if (!tte->key() || tte->key() == key32) // Empty or overwrite old + if (!tte->key16 || tte->key16 == key16) // Empty or overwrite old { if (!m) m = tte->move(); // Preserve any existing ttMove @@ -109,13 +110,11 @@ void TranspositionTable::store(const Key key, Value v, Bound b, Depth d, Move m, } // Implement replace strategy - c1 = (replace->generation() == generation ? 2 : 0); - c2 = (tte->generation() == generation || tte->bound() == BOUND_EXACT ? -2 : 0); - c3 = (tte->depth() < replace->depth() ? 1 : 0); - - if (c1 + c2 + c3 > 0) + if ( (( tte->genBound8 & 0xFC) == generation || tte->bound() == BOUND_EXACT) + - ((replace->genBound8 & 0xFC) == generation) + - (tte->depth8 < replace->depth8) < 0) replace = tte; } - replace->save(key32, v, b, d, m, generation, statV); + replace->save(key16, v, b, d, m, generation, statV); }