33//
44
55#include < random>
6+ #include < memory>
67#include < cstring>
78#include " hash.h"
89
@@ -53,14 +54,14 @@ bool tt::hash_t::probe(U64 hash, tt::entry_t &entry) {
5354 const size_t index = (hash & num_entries) * bucket_size;
5455 tt::entry_t *bucket = table + index;
5556
56- if (bucket->hash == hash) {
57+ if (( bucket->coded_hash ^ bucket-> data ) == hash) {
5758 bucket->refresh (generation);
5859 entry = *bucket;
5960 return true ;
6061 }
6162
6263 for (size_t i = 1 ; i < bucket_size; i++) {
63- if ((bucket + i)->hash == hash) {
64+ if ((( bucket + i)->coded_hash ^ (bucket + i)-> data ) == hash) {
6465 (bucket + i)->refresh (generation);
6566 entry = *(bucket + i);
6667 return true ;
@@ -87,34 +88,40 @@ void tt::hash_t::save(Bound bound, U64 hash, int depth, int ply, int static_eval
8788 if (score >= MINCHECKMATE) score += ply;
8889 if (score <= -MINCHECKMATE) score -= ply;
8990
90- if (bucket->hash == hash) {
91+ tt::entry_t updated = {};
92+ updated.info .move = compress (move);
93+ updated.info .static_eval = static_cast <int16_t >(static_eval);
94+ updated.info .internal_value = static_cast <int16_t >(score);
95+ updated.info .about = uint16_t (bound) | (uint16_t (depth) << 2u ) | (generation << 10u );
96+ updated.coded_hash = hash ^ updated.data ;
97+
98+ if ((bucket->coded_hash ^ bucket->data ) == hash) {
9199 if (bound == EXACT || depth >= bucket->depth () - 2 ) {
92- bucket-> update (bound, depth, generation, move, static_eval, score) ;
100+ * bucket = updated ;
93101 }
94102 return ;
95103 }
96104
97105 tt::entry_t *replace = bucket;
98106 for (size_t i = 1 ; i < bucket_size; i++) {
99107 // Always replace if same position
100- if ((bucket + i)->hash == hash) {
108+ if ((( bucket + i)->coded_hash ^ (bucket + i)-> data ) == hash) {
101109 if (bound == EXACT || depth >= bucket->depth () - 2 ) {
102- (bucket + i)-> update (bound, depth, generation, move, static_eval, score) ;
110+ * (bucket + i) = updated ;
103111 }
104112 return ;
105- } else if ((bucket + i)->about < replace->about ) {
113+ } else if ((bucket + i)->info . about < replace->info . about ) {
106114 replace = (bucket + i);
107115 }
108116 }
109117
110118 // Replace best candidate
111- replace->hash = hash;
112- replace->update (bound, depth, generation, move, static_eval, score);
119+ *replace = updated;
113120}
114121
115122void tt::hash_t::age () {
116123 generation++;
117- if (generation >= 127 ) {
124+ if (generation >= 63 ) {
118125 generation = 1 ;
119126
120127 // Clear up hash
@@ -125,12 +132,17 @@ void tt::hash_t::age() {
125132}
126133
127134size_t tt::hash_t::hash_full () {
135+ constexpr size_t sample_size = 4000 ;
136+ constexpr size_t divisor = sample_size / 1000 ;
137+
138+ assert (num_entries * bucket_size > sample_size);
139+
128140 size_t cnt = 0 ;
129- for (size_t i = 0 ; i < num_entries * bucket_size ; i++) {
141+ for (size_t i = 0 ; i < sample_size ; i++) {
130142 if (table[i].generation () == generation) {
131143 cnt++;
132144 }
133145 }
134146
135- return ( cnt * 1000 ) / (num_entries * bucket_size) ;
147+ return cnt / divisor ;
136148}
0 commit comments