Code style massage evaluate()
authorMarco Costalba <mcostalba@gmail.com>
Thu, 25 Sep 2008 08:31:41 +0000 (10:31 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 26 Sep 2008 03:54:49 +0000 (05:54 +0200)
No functional changes.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/evaluate.cpp

index f44173445bac916afc72090d68d5a217b0d4cc2d..049a40deacd3de78f18023a0df22c45e338048b4 100644 (file)
@@ -290,141 +290,122 @@ namespace {
 /// between them based on the remaining material.
 
 Value evaluate(const Position &pos, EvalInfo &ei, int threadID) {
-  Color stm;
-  Square s;
-  ScaleFactor factor[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL};
-  Phase phase;
-
-  memset(&ei, 0, sizeof(EvalInfo));
 
   assert(pos.is_ok());
   assert(threadID >= 0 && threadID < THREAD_MAX);
 
-  stm = pos.side_to_move();
+  memset(&ei, 0, sizeof(EvalInfo));
 
   // Initialize by reading the incrementally updated scores included in the
-  // position object (material + piece square tables):
+  // position object (material + piece square tables)
   ei.mgValue = pos.mg_value();
   ei.egValue = pos.eg_value();
 
-  // Probe the material hash table:
+  // Probe the material hash table
   ei.mi = MaterialTable[threadID]->get_material_info(pos);
   ei.mgValue += ei.mi->mg_value();
   ei.egValue += ei.mi->eg_value();
 
-  factor[WHITE] = ei.mi->scale_factor(pos, WHITE);
-  factor[BLACK] = ei.mi->scale_factor(pos, BLACK);
-
   // If we have a specialized evaluation function for the current material
-  // configuration, call it and return:
-  if(ei.mi->specialized_eval_exists())
-    return ei.mi->evaluate(pos);
+  // configuration, call it and return
+  if (ei.mi->specialized_eval_exists())
+      return ei.mi->evaluate(pos);
 
-  phase = pos.game_phase();
+  // After get_material_info() call that modifies them
+  ScaleFactor factor[2];
+  factor[WHITE] = ei.mi->scale_factor(pos, WHITE);
+  factor[BLACK] = ei.mi->scale_factor(pos, BLACK);
 
-  // Probe the pawn hash table:
+  // Probe the pawn hash table
   ei.pi = PawnTable[threadID]->get_pawn_info(pos);
   ei.mgValue += apply_weight(ei.pi->mg_value(), WeightPawnStructureMidgame);
   ei.egValue += apply_weight(ei.pi->eg_value(), WeightPawnStructureEndgame);
 
-  // Initialize king attack bitboards and king attack zones for both sides:
+  // Initialize king attack bitboards and king attack zones for both sides
   ei.attackedBy[WHITE][KING] = pos.king_attacks(pos.king_square(WHITE));
   ei.attackedBy[BLACK][KING] = pos.king_attacks(pos.king_square(BLACK));
-  ei.kingZone[WHITE] =
-    ei.attackedBy[BLACK][KING] | (ei.attackedBy[BLACK][KING] >> 8);
-  ei.kingZone[BLACK] =
-    ei.attackedBy[WHITE][KING] | (ei.attackedBy[WHITE][KING] << 8);
-
-  // Initialize pawn attack bitboards for both sides:
-  ei.attackedBy[WHITE][PAWN] =
-    ((pos.pawns(WHITE) << 9) & ~FileABB) | ((pos.pawns(WHITE) << 7) & ~FileHBB);
-  ei.kingAttackersCount[WHITE] +=
-    count_1s_max_15(ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING])/2;
-  ei.attackedBy[BLACK][PAWN] =
-    ((pos.pawns(BLACK) >> 7) & ~FileABB) | ((pos.pawns(BLACK) >> 9) & ~FileHBB);
-  ei.kingAttackersCount[BLACK] +=
-    count_1s_max_15(ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING])/2;
-
-  // Evaluate pieces:
-  for(Color c = WHITE; c <= BLACK; c++) {
-    Bitboard b;
+  ei.kingZone[WHITE] = ei.attackedBy[BLACK][KING] | (ei.attackedBy[BLACK][KING] >> 8);
+  ei.kingZone[BLACK] = ei.attackedBy[WHITE][KING] | (ei.attackedBy[WHITE][KING] << 8);
 
+  // Initialize pawn attack bitboards for both sides
+  ei.attackedBy[WHITE][PAWN] = ((pos.pawns(WHITE) << 9) & ~FileABB) | ((pos.pawns(WHITE) << 7) & ~FileHBB);
+  ei.attackedBy[BLACK][PAWN] = ((pos.pawns(BLACK) >> 7) & ~FileABB) | ((pos.pawns(BLACK) >> 9) & ~FileHBB);
+  ei.kingAttackersCount[WHITE] = count_1s_max_15(ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING])/2;
+  ei.kingAttackersCount[BLACK] = count_1s_max_15(ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING])/2;
+
+  // Evaluate pieces
+  for (Color c = WHITE; c <= BLACK; c++)
+  {
     // Knights
-    for(int i = 0; i < pos.knight_count(c); i++) {
-      s = pos.knight_list(c, i);
-      evaluate_knight(pos, s, c, ei);
-    }
+    for (int i = 0; i < pos.knight_count(c); i++)
+        evaluate_knight(pos, pos.knight_list(c, i), c, ei);
 
     // Bishops
-    for(int i = 0; i < pos.bishop_count(c); i++) {
-      s = pos.bishop_list(c, i);
-      evaluate_bishop(pos, s, c, ei);
-    }
+    for (int i = 0; i < pos.bishop_count(c); i++)
+        evaluate_bishop(pos, pos.bishop_list(c, i), c, ei);
 
     // Rooks
-    for(int i = 0; i < pos.rook_count(c); i++) {
-      s = pos.rook_list(c, i);
-      evaluate_rook(pos, s, c, ei);
-    }
+    for (int i = 0; i < pos.rook_count(c); i++)
+        evaluate_rook(pos, pos.rook_list(c, i), c, ei);
 
     // Queens
-    for(int i = 0; i < pos.queen_count(c); i++) {
-      s = pos.queen_list(c, i);
-      evaluate_queen(pos, s, c, ei);
-    }
-
-    // Some special patterns:
+    for(int i = 0; i < pos.queen_count(c); i++)
+        evaluate_queen(pos, pos.queen_list(c, i), c, ei);
 
-    // Trapped bishops on a7/h7/a2/h2
-    b = pos.bishops(c) & MaskA7H7[c];
-    while(b) {
-      s = pop_1st_bit(&b);
-      evaluate_trapped_bishop_a7h7(pos, s, c, ei);
+    // Special pattern: trapped bishops on a7/h7/a2/h2
+    Bitboard b = pos.bishops(c) & MaskA7H7[c];
+    while (b)
+    {
+        Square s = pop_1st_bit(&b);
+        evaluate_trapped_bishop_a7h7(pos, s, c, ei);
     }
 
-    // Trapped bishops on a1/h1/a8/h8 in Chess960:
-    if(Chess960) {
-      b = pos.bishops(c) & MaskA1H1[c];
-      while(b) {
-        s = pop_1st_bit(&b);
-        evaluate_trapped_bishop_a1h1(pos, s, c, ei);
-      }
+    // Special pattern: trapped bishops on a1/h1/a8/h8 in Chess960:
+    if (Chess960)
+    {
+        b = pos.bishops(c) & MaskA1H1[c];
+        while (b)
+        {
+            Square s = pop_1st_bit(&b);
+            evaluate_trapped_bishop_a1h1(pos, s, c, ei);
+        }
     }
 
-    ei.attackedBy[c][0] =
-      ei.attackedBy[c][PAWN] | ei.attackedBy[c][KNIGHT]
-      | ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK]
-      | ei.attackedBy[c][QUEEN] | ei.attackedBy[c][KING];
+    // Sum up all attacked squares
+    ei.attackedBy[c][0] =   ei.attackedBy[c][PAWN]   | ei.attackedBy[c][KNIGHT]
+                          | ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK]
+                          | ei.attackedBy[c][QUEEN]  | ei.attackedBy[c][KING];
   }
 
   // Kings.  Kings are evaluated after all other pieces for both sides,
   // because we need complete attack information for all pieces when computing
   // the king safety evaluation.
-  for(Color c = WHITE; c <= BLACK; c++) {
-    s = pos.king_square(c);
-    evaluate_king(pos, s, c, ei);
-  }
+  for (Color c = WHITE; c <= BLACK; c++)
+      evaluate_king(pos, pos.king_square(c), c, ei);
 
   // Evaluate passed pawns.  We evaluate passed pawns for both sides at once,
   // because we need to know which side promotes first in positions where
   // both sides have an unstoppable passed pawn.
-  if(ei.pi->passed_pawns())
-    evaluate_passed_pawns(pos, ei);
+  if (ei.pi->passed_pawns())
+      evaluate_passed_pawns(pos, ei);
 
-  // Middle-game specific evaluation terms
-  if(phase > PHASE_ENDGAME) {
+  Phase phase = pos.game_phase();
 
+  // Middle-game specific evaluation terms
+  if (phase > PHASE_ENDGAME)
+  {
     // Pawn storms in positions with opposite castling.
-    if(square_file(pos.king_square(WHITE)) >= FILE_E &&
-       square_file(pos.king_square(BLACK)) <= FILE_D)
-      ei.mgValue +=
-        ei.pi->queenside_storm_value(WHITE) -
-        ei.pi->kingside_storm_value(BLACK);
-    else if(square_file(pos.king_square(WHITE)) <= FILE_D &&
-            square_file(pos.king_square(BLACK)) >= FILE_E)
-      ei.mgValue +=
-        ei.pi->kingside_storm_value(WHITE) -
-        ei.pi->queenside_storm_value(BLACK);
+    if (   square_file(pos.king_square(WHITE)) >= FILE_E
+        && square_file(pos.king_square(BLACK)) <= FILE_D)
+
+        ei.mgValue += ei.pi->queenside_storm_value(WHITE)
+                    - ei.pi->kingside_storm_value(BLACK);
+
+    else if (   square_file(pos.king_square(WHITE)) <= FILE_D
+             && square_file(pos.king_square(BLACK)) >= FILE_E)
+
+        ei.mgValue += ei.pi->kingside_storm_value(WHITE)
+                    - ei.pi->queenside_storm_value(BLACK);
   }
 
   // Mobility
@@ -433,46 +414,50 @@ Value evaluate(const Position &pos, EvalInfo &ei, int threadID) {
 
   // If we don't already have an unusual scale factor, check for opposite
   // colored bishop endgames, and use a lower scale for those:
-  if(phase < PHASE_MIDGAME && pos.opposite_colored_bishops()
-     && ((factor[WHITE] == SCALE_FACTOR_NORMAL && ei.egValue > Value(0)) ||
-         (factor[BLACK] == SCALE_FACTOR_NORMAL && ei.egValue < Value(0)))) {
-    if(pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) ==
-       2*BishopValueMidgame) {
+  if (   phase < PHASE_MIDGAME
+      && pos.opposite_colored_bishops()
+      && (   (factor[WHITE] == SCALE_FACTOR_NORMAL && ei.egValue > Value(0))
+          || (factor[BLACK] == SCALE_FACTOR_NORMAL && ei.egValue < Value(0))))
+  {
+    if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == 2*BishopValueMidgame)
+    {
       // Only the two bishops
-      if(pos.pawn_count(WHITE) + pos.pawn_count(BLACK) == 1) {
+      if (pos.pawn_count(WHITE) + pos.pawn_count(BLACK) == 1)
+      {
         // KBP vs KB with only a single pawn; almost certainly a draw.
-        if(factor[WHITE] == SCALE_FACTOR_NORMAL)
-          factor[WHITE] = ScaleFactor(8);
-        if(factor[BLACK] == SCALE_FACTOR_NORMAL)
-          factor[BLACK] = ScaleFactor(8);
+        if (factor[WHITE] == SCALE_FACTOR_NORMAL)
+            factor[WHITE] = ScaleFactor(8);
+        if (factor[BLACK] == SCALE_FACTOR_NORMAL)
+            factor[BLACK] = ScaleFactor(8);
       }
-      else {
+      else
+      {
         // At least two pawns
-        if(factor[WHITE] == SCALE_FACTOR_NORMAL)
-          factor[WHITE] = ScaleFactor(32);
-        if(factor[BLACK] == SCALE_FACTOR_NORMAL)
-          factor[BLACK] = ScaleFactor(32);
+        if (factor[WHITE] == SCALE_FACTOR_NORMAL)
+            factor[WHITE] = ScaleFactor(32);
+        if (factor[BLACK] == SCALE_FACTOR_NORMAL)
+            factor[BLACK] = ScaleFactor(32);
       }
     }
-    else {
+    else
+    {
       // Endgame with opposite-colored bishops, but also other pieces.
       // Still a bit drawish, but not as drawish as with only the two
       // bishops.
-      if(factor[WHITE] == SCALE_FACTOR_NORMAL)
-        factor[WHITE] = ScaleFactor(50);
-      if(factor[BLACK] == SCALE_FACTOR_NORMAL)
-        factor[BLACK] = ScaleFactor(50);
+      if (factor[WHITE] == SCALE_FACTOR_NORMAL)
+          factor[WHITE] = ScaleFactor(50);
+      if (factor[BLACK] == SCALE_FACTOR_NORMAL)
+          factor[BLACK] = ScaleFactor(50);
     }
   }
 
   // Interpolate between the middle game and the endgame score, and
-  // return:
-  Value value = scale_by_game_phase(ei.mgValue, ei.egValue, phase, factor);
+  // return
+  Color stm = pos.side_to_move();
+
+  Value v = Sign[stm] * scale_by_game_phase(ei.mgValue, ei.egValue, phase, factor);
 
-  if(ei.mateThreat[stm] != MOVE_NONE)
-    return 8 * QueenValueMidgame - Sign[stm] * value;
-  else
-    return Sign[stm] * value;
+  return (ei.mateThreat[stm] == MOVE_NONE ? v : 8 * QueenValueMidgame - v);
 }