]> git.sesse.net Git - stockfish/blobdiff - src/evaluate.cpp
Allow using Intel SDE for PGO builds.
[stockfish] / src / evaluate.cpp
index d55ef695b75f45e51f91eeed22990adecbd2d669..ca86a435fbfe9bd98318b1fe4d4bed468ca0f408 100644 (file)
@@ -54,7 +54,8 @@
 
 
 using namespace std;
-using namespace Eval::NNUE;
+
+namespace Stockfish {
 
 namespace Eval {
 
@@ -394,8 +395,9 @@ namespace {
 
     attackedBy[Us][Pt] = 0;
 
-    while (b1) {
-        Square s = pop_lsb(&b1);
+    while (b1)
+    {
+        Square s = pop_lsb(b1);
 
         // Find attacked squares, including x-ray attacks for bishops and rooks
         b = Pt == BISHOP ? attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(QUEEN))
@@ -656,11 +658,11 @@ namespace {
     {
         b = (defended | weak) & (attackedBy[Us][KNIGHT] | attackedBy[Us][BISHOP]);
         while (b)
-            score += ThreatByMinor[type_of(pos.piece_on(pop_lsb(&b)))];
+            score += ThreatByMinor[type_of(pos.piece_on(pop_lsb(b)))];
 
         b = weak & attackedBy[Us][ROOK];
         while (b)
-            score += ThreatByRook[type_of(pos.piece_on(pop_lsb(&b)))];
+            score += ThreatByRook[type_of(pos.piece_on(pop_lsb(b)))];
 
         if (weak & attackedBy[Us][KING])
             score += ThreatByKing;
@@ -758,7 +760,7 @@ namespace {
 
     while (b)
     {
-        Square s = pop_lsb(&b);
+        Square s = pop_lsb(b);
 
         assert(!(pos.pieces(Them, PAWN) & forward_file_bb(Us, s + Up)));
 
@@ -1036,7 +1038,51 @@ make_v:
     return v;
   }
 
-} // namespace
+
+  /// Fisher Random Chess: correction for cornered bishops, to fix chess960 play with NNUE
+
+  Value fix_FRC(const Position& pos) {
+
+    constexpr Bitboard Corners =  1ULL << SQ_A1 | 1ULL << SQ_H1 | 1ULL << SQ_A8 | 1ULL << SQ_H8;
+
+    if (!(pos.pieces(BISHOP) & Corners))
+        return VALUE_ZERO;
+
+    constexpr int penalty1 = -209;
+    constexpr int penalty2 = -136;
+    constexpr int penalty3 = -148;
+
+    int correction = 0;
+
+    if (   pos.piece_on(SQ_A1) == W_BISHOP
+        && pos.piece_on(SQ_B2) == W_PAWN)
+        correction += !pos.empty(SQ_B3)              ? penalty1
+                     : pos.piece_on(SQ_C3) == W_PAWN ? penalty2
+                                                     : penalty3;
+
+    if (   pos.piece_on(SQ_H1) == W_BISHOP
+        && pos.piece_on(SQ_G2) == W_PAWN)
+        correction += !pos.empty(SQ_G3)              ? penalty1
+                     : pos.piece_on(SQ_F3) == W_PAWN ? penalty2
+                                                     : penalty3;
+
+    if (   pos.piece_on(SQ_A8) == B_BISHOP
+        && pos.piece_on(SQ_B7) == B_PAWN)
+        correction += !pos.empty(SQ_B6)              ? -penalty1
+                     : pos.piece_on(SQ_C6) == B_PAWN ? -penalty2
+                                                     : -penalty3;
+
+    if (   pos.piece_on(SQ_H8) == B_BISHOP
+        && pos.piece_on(SQ_G7) == B_PAWN)
+        correction += !pos.empty(SQ_G6)              ? -penalty1
+                     : pos.piece_on(SQ_F6) == B_PAWN ? -penalty2
+                                                     : -penalty3;
+
+    return pos.side_to_move() == WHITE ?  Value(correction)
+                                       : -Value(correction);
+  }
+
+} // namespace Eval
 
 
 /// evaluate() is the evaluator for the outer world. It returns a static
@@ -1051,9 +1097,19 @@ Value Eval::evaluate(const Position& pos) {
   else
   {
       // Scale and shift NNUE for compatibility with search and classical evaluation
-      auto  adjusted_NNUE = [&](){
-         int mat = pos.non_pawn_material() + 2 * PawnValueMg * pos.count<PAWN>();
-         return NNUE::evaluate(pos) * (641 + mat / 32 - 4 * pos.rule50_count()) / 1024 + Tempo;
+      auto  adjusted_NNUE = [&]()
+      {
+         int material = pos.non_pawn_material() + 2 * PawnValueMg * pos.count<PAWN>();
+         int scale =  641
+                    + material / 32
+                    - 4 * pos.rule50_count();
+
+         Value nnue = NNUE::evaluate(pos) * scale / 1024 + Tempo;
+
+         if (pos.is_chess960())
+             nnue += fix_FRC(pos);
+
+         return nnue;
       };
 
       // If there is PSQ imbalance use classical eval, with small probability if it is small
@@ -1146,3 +1202,5 @@ std::string Eval::trace(const Position& pos) {
 
   return ss.str();
 }
+
+} // namespace Stockfish