Group common evaluate code
authorMarco Costalba <mcostalba@gmail.com>
Wed, 24 Sep 2008 07:49:18 +0000 (09:49 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Wed, 24 Sep 2008 19:02:14 +0000 (21:02 +0200)
This removes code redundancy but perhaps
impact performance due to uninlining.

Testing for regression is needed. For now
aim to best code readibility.

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

index 67d5be2435224286330a052b8b9f500394bfb3ff..6f4de561fd42aa68156a4df42e25ec54f1b7790a 100644 (file)
@@ -571,27 +571,41 @@ void read_weights(Color sideToMove) {
 
 namespace {
 
-  // evaluate_knight() assigns bonuses and penalties to a knight of a given
-  // color on a given square.
+  // evaluate_common() computes terms common to all pieces attack
 
-  void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) {
+  int evaluate_common(const Position&p, const Bitboard& b, Color us, EvalInfo& ei,
+                       int AttackWeight, const Value* mgBonus, const Value* egBonus) {
 
     Color them = opposite_color(us);
-    Bitboard b = p.knight_attacks(s);
-    ei.attackedBy[us][KNIGHT] |= b;
 
     // King attack
     if(b & ei.attackZone[us]) {
       ei.attackCount[us]++;
-      ei.attackWeight[us] += KnightAttackWeight;
+      ei.attackWeight[us] += AttackWeight;
       Bitboard bb = (b & ei.attackedBy[them][KING]);
       if(bb) ei.attacked[us] += count_1s_max_15(bb);
     }
 
     // Mobility
     int mob = count_1s_max_15(b & ~p.pieces_of_color(us));
-    ei.mgMobility += Sign[us] * MidgameKnightMobilityBonus[mob];
-    ei.egMobility += Sign[us] * EndgameKnightMobilityBonus[mob];
+    ei.mgMobility += Sign[us] * mgBonus[mob];
+    ei.egMobility += Sign[us] * egBonus[mob];
+
+    return mob;
+  }
+
+  // evaluate_knight() assigns bonuses and penalties to a knight of a given
+  // color on a given square.
+
+  void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) {
+
+    Color them = opposite_color(us);
+    Bitboard b = p.knight_attacks(s);
+    ei.attackedBy[us][KNIGHT] |= b;
+
+    // King attack and mobility
+    evaluate_common(p, b, us, ei, KnightAttackWeight,
+                    MidgameKnightMobilityBonus, EndgameKnightMobilityBonus);
 
     // Knight outposts:
     if(p.square_is_weak(s, them)) {
@@ -628,18 +642,9 @@ namespace {
 
     ei.attackedBy[us][BISHOP] |= b;
 
-    // King attack
-    if(b & ei.attackZone[us]) {
-      ei.attackCount[us]++;
-      ei.attackWeight[us] += BishopAttackWeight;
-      Bitboard bb = (b & ei.attackedBy[them][KING]);
-      if(bb) ei.attacked[us] += count_1s_max_15(bb);
-    }
-
-    // Mobility:
-    int mob = count_1s_max_15(b & ~p.pieces_of_color(us));
-    ei.mgMobility += Sign[us] * MidgameBishopMobilityBonus[mob];
-    ei.egMobility += Sign[us] * EndgameBishopMobilityBonus[mob];
+    // King attack and mobility
+    evaluate_common(p, b, us, ei, BishopAttackWeight,
+                    MidgameBishopMobilityBonus, EndgameBishopMobilityBonus);
 
     // Bishop outposts:
     if(p.square_is_weak(s, them)) {
@@ -697,18 +702,9 @@ namespace {
       rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
     ei.attackedBy[us][ROOK] |= b;
 
-    // King attack
-    if(b & ei.attackZone[us]) {
-      ei.attackCount[us]++;
-      ei.attackWeight[us] += RookAttackWeight;
-      Bitboard bb = (b & ei.attackedBy[them][KING]);
-      if(bb) ei.attacked[us] += count_1s_max_15(bb);
-    }
-
-    // Mobility
-    int mob = count_1s_max_15(b & ~p.pieces_of_color(us));
-    ei.mgMobility += Sign[us] * MidgameRookMobilityBonus[mob];
-    ei.egMobility += Sign[us] * EndgameRookMobilityBonus[mob];
+    // King attack and mobility
+    int mob = evaluate_common(p, b, us, ei, RookAttackWeight,
+                              MidgameRookMobilityBonus, EndgameRookMobilityBonus);
 
     // Penalize rooks which are trapped inside a king which has lost the
     // right to castle:
@@ -756,18 +752,9 @@ namespace {
     Bitboard b = p.queen_attacks(s);
     ei.attackedBy[us][QUEEN] |= b;
 
-    // King attack
-    if(b & ei.attackZone[us]) {
-      ei.attackCount[us]++;
-      ei.attackWeight[us] += QueenAttackWeight;
-      Bitboard bb = (b & ei.attackedBy[them][KING]);
-      if(bb) ei.attacked[us] += count_1s_max_15(bb);
-    }
-
-    // Mobility
-    int mob = count_1s(b & ~p.pieces_of_color(us));
-    ei.mgMobility += Sign[us] * MidgameQueenMobilityBonus[mob];
-    ei.egMobility += Sign[us] * EndgameQueenMobilityBonus[mob];
+    // King attack and mobility
+    evaluate_common(p, b, us, ei, QueenAttackWeight,
+                    MidgameQueenMobilityBonus, EndgameQueenMobilityBonus);
   }