diff --git a/source/search.cpp b/source/search.cpp index 02d709a..8bac924 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -300,7 +300,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear bool ttHit = false; entry = ctx->TT->GetEntry(board.hashKey); - if (entry.hashKey == board.hashKey) { + if (entry.hashKey == PackHashKey(board.hashKey)) { ttHit = true; switch (entry.nodeType) { @@ -423,7 +423,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (!ctx->excluded) entry = ctx->TT->GetEntry(board.hashKey); - const bool ttHit = entry.hashKey == board.hashKey; + const bool ttHit = entry.hashKey == PackHashKey(board.hashKey); const bool ttpv = isPV | entry.ttpv; if constexpr (!isPV) { diff --git a/source/tt.cpp b/source/tt.cpp index 8b6b645..d67feaf 100644 --- a/source/tt.cpp +++ b/source/tt.cpp @@ -4,11 +4,12 @@ TTable SharedTT; void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) { TTBucket *bucket = &table[hashKey % table.size()]; + const uint16_t packedKey = PackHashKey(hashKey); TTEntry *current = nullptr; - if (bucket->depthPreferred && bucket->depthPreferred.hashKey == hashKey) { + if (bucket->depthPreferred && bucket->depthPreferred.hashKey == packedKey) { current = &bucket->depthPreferred; - } else if (bucket->alwaysReplace && bucket->alwaysReplace.hashKey == hashKey) { + } else if (bucket->alwaysReplace && bucket->alwaysReplace.hashKey == packedKey) { current = &bucket->alwaysReplace; } else if (!bucket->alwaysReplace) { current = &bucket->alwaysReplace; @@ -27,7 +28,7 @@ void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move b } } - const bool samePosition = current->hashKey == hashKey; + const bool samePosition = current->hashKey == packedKey; const bool exactBound = nodeType == PV; const bool entryFromCurrentAge = current->age == age; const int writeDepth = depth + 4 + ttpv * 2; @@ -36,7 +37,7 @@ void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move b return; } - current->hashKey = hashKey; + current->hashKey = packedKey; current->nodeType = nodeType; current->score = score; current->depth = depth; diff --git a/source/tt.h b/source/tt.h index da85a77..6b80947 100644 --- a/source/tt.h +++ b/source/tt.h @@ -19,9 +19,16 @@ public: } }; +constexpr U64 hashKeyMask = 0xFFFF; + +// only the lowest bits of the hashkey are stored, the rest is only used for indexing +inline uint16_t PackHashKey(U64 &hashKey) { + return uint16_t(hashKey & hashKeyMask); +} + class TTEntry { public: - U64 hashKey = 0; + uint16_t hashKey = 0; int16_t score = 0; Move bestMove = Move(); uint8_t depth = 0; @@ -46,6 +53,8 @@ public: TTEntry alwaysReplace; }; +static_assert(sizeof(TTEntry) == 10); + // 1024 MB constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTBucket); // 8 MB @@ -84,9 +93,11 @@ public: TTEntry GetEntry(U64 &hashKey) { TTBucket *current = &table[hashKey % table.size()]; - if (current->depthPreferred.hashKey == hashKey) { + const uint16_t packedKey = PackHashKey(hashKey); + + if (current->depthPreferred.hashKey == packedKey) { return current->depthPreferred; - } else if (current->alwaysReplace.hashKey == hashKey) { + } else if (current->alwaysReplace.hashKey == packedKey) { return current->alwaysReplace; }