@@ -575,17 +575,31 @@ namespace {
575575 // Step 5. Evaluate the position statically and update parent's gain statistics
576576 if (inCheck)
577577 ss->staticEval = ss->evalMargin = eval = VALUE_NONE;
578- else
578+
579+ else if (tte)
579580 {
580- eval = ss->staticEval = evaluate (pos, ss->evalMargin );
581+ // Following asserts are valid only in single thread condition because
582+ // TT access is always racy and its contents cannot be trusted.
583+ assert (tte->static_value () != VALUE_NONE || Threads.size () > 1 );
584+ assert (ttValue != VALUE_NONE || tte->type () == BOUND_NONE || Threads.size () > 1 );
585+
586+ ss->staticEval = eval = tte->static_value ();
587+ ss->evalMargin = tte->static_value_margin ();
588+
589+ if (eval == VALUE_NONE || ss->evalMargin == VALUE_NONE) // Due to a race
590+ eval = ss->staticEval = evaluate (pos, ss->evalMargin );
581591
582592 // Can ttValue be used as a better position evaluation?
583- if (tte && ttValue != VALUE_NONE)
584- {
593+ if (ttValue != VALUE_NONE)
585594 if ( ((tte->type () & BOUND_LOWER) && ttValue > eval)
586595 || ((tte->type () & BOUND_UPPER) && ttValue < eval))
587596 eval = ttValue;
588- }
597+ }
598+ else
599+ {
600+ eval = ss->staticEval = evaluate (pos, ss->evalMargin );
601+ TT.store (posKey, VALUE_NONE, BOUND_NONE, DEPTH_NONE, MOVE_NONE,
602+ ss->staticEval , ss->evalMargin );
589603 }
590604
591605 // Update gain for the parent non-capture move given the static position
@@ -1041,7 +1055,8 @@ namespace {
10411055
10421056 if (bestValue >= beta) // Failed high
10431057 {
1044- TT.store (posKey, value_to_tt (bestValue, ss->ply ), BOUND_LOWER, depth, bestMove);
1058+ TT.store (posKey, value_to_tt (bestValue, ss->ply ), BOUND_LOWER, depth,
1059+ bestMove, ss->staticEval , ss->evalMargin );
10451060
10461061 if (!pos.is_capture_or_promotion (bestMove) && !inCheck)
10471062 {
@@ -1066,7 +1081,7 @@ namespace {
10661081 else // Failed low or PV search
10671082 TT.store (posKey, value_to_tt (bestValue, ss->ply ),
10681083 PvNode && bestMove != MOVE_NONE ? BOUND_EXACT : BOUND_UPPER,
1069- depth, bestMove);
1084+ depth, bestMove, ss-> staticEval , ss-> evalMargin );
10701085
10711086 assert (bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
10721087
@@ -1147,14 +1162,25 @@ namespace {
11471162 ss->staticEval = bestValue = -(ss-1 )->staticEval ;
11481163 ss->evalMargin = VALUE_ZERO;
11491164 }
1165+ else if (tte)
1166+ {
1167+ assert (tte->static_value () != VALUE_NONE || Threads.size () > 1 );
1168+
1169+ ss->staticEval = bestValue = tte->static_value ();
1170+ ss->evalMargin = tte->static_value_margin ();
1171+
1172+ if (ss->staticEval == VALUE_NONE || ss->evalMargin == VALUE_NONE) // Due to a race
1173+ ss->staticEval = bestValue = evaluate (pos, ss->evalMargin );
1174+ }
11501175 else
11511176 ss->staticEval = bestValue = evaluate (pos, ss->evalMargin );
11521177
11531178 // Stand pat. Return immediately if static value is at least beta
11541179 if (bestValue >= beta)
11551180 {
11561181 if (!tte)
1157- TT.store (pos.key (), value_to_tt (bestValue, ss->ply ), BOUND_LOWER, DEPTH_NONE, MOVE_NONE);
1182+ TT.store (pos.key (), value_to_tt (bestValue, ss->ply ), BOUND_LOWER,
1183+ DEPTH_NONE, MOVE_NONE, ss->staticEval , ss->evalMargin );
11581184
11591185 return bestValue;
11601186 }
@@ -1263,7 +1289,9 @@ namespace {
12631289 }
12641290 else // Fail high
12651291 {
1266- TT.store (posKey, value_to_tt (value, ss->ply ), BOUND_LOWER, ttDepth, move);
1292+ TT.store (posKey, value_to_tt (value, ss->ply ), BOUND_LOWER,
1293+ ttDepth, move, ss->staticEval , ss->evalMargin );
1294+
12671295 return value;
12681296 }
12691297 }
@@ -1277,7 +1305,7 @@ namespace {
12771305
12781306 TT.store (posKey, value_to_tt (bestValue, ss->ply ),
12791307 PvNode && bestValue > oldAlpha ? BOUND_EXACT : BOUND_UPPER,
1280- ttDepth, bestMove);
1308+ ttDepth, bestMove, ss-> staticEval , ss-> evalMargin );
12811309
12821310 assert (bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
12831311
@@ -1574,7 +1602,7 @@ void RootMove::insert_pv_in_tt(Position& pos) {
15741602 tte = TT.probe (pos.key ());
15751603
15761604 if (!tte || tte->move () != pv[ply]) // Don't overwrite correct entries
1577- TT.store (pos.key (), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[ply]);
1605+ TT.store (pos.key (), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[ply], VALUE_NONE, VALUE_NONE );
15781606
15791607 assert (MoveList<LEGAL>(pos).contains (pv[ply]));
15801608
0 commit comments