]> git.sesse.net Git - stockfish/blobdiff - src/endgame.cpp
Use opposite_color_squares() instead of same_color_squares()
[stockfish] / src / endgame.cpp
index e3cd20076e4aa9b83dfa9aef62375c6e02a8853e..348feecf70bf5e170211460a23145a55749610dd 100644 (file)
@@ -24,7 +24,6 @@
 
 #include <cassert>
 
-#include "bitbase.h"
 #include "bitcount.h"
 #include "endgame.h"
 
@@ -102,6 +101,7 @@ namespace {
 /// init_bitbases() is called during program initialization, and simply loads
 /// bitbases from disk into memory.  At the moment, there is only the bitbase
 /// for KP vs K, but we may decide to add other bitbases later.
+extern void generate_kpk_bitbase(uint8_t bitbase[]);
 
 void init_bitbases() {
   generate_kpk_bitbase(KPKBitbase);
@@ -115,8 +115,8 @@ void init_bitbases() {
 template<>
 Value EvaluationFunction<KXK>::apply(const Position& pos) const {
 
-  assert(pos.non_pawn_material(weakerSide) == Value(0));
-  assert(pos.piece_count(weakerSide, PAWN) == Value(0));
+  assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
+  assert(pos.piece_count(weakerSide, PAWN) == VALUE_ZERO);
 
   Square winnerKSq = pos.king_square(strongerSide);
   Square loserKSq = pos.king_square(weakerSide);
@@ -141,8 +141,8 @@ Value EvaluationFunction<KXK>::apply(const Position& pos) const {
 template<>
 Value EvaluationFunction<KBNK>::apply(const Position& pos) const {
 
-  assert(pos.non_pawn_material(weakerSide) == Value(0));
-  assert(pos.piece_count(weakerSide, PAWN) == Value(0));
+  assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
+  assert(pos.piece_count(weakerSide, PAWN) == VALUE_ZERO);
   assert(pos.non_pawn_material(strongerSide) == KnightValueMidgame + BishopValueMidgame);
   assert(pos.piece_count(strongerSide, BISHOP) == 1);
   assert(pos.piece_count(strongerSide, KNIGHT) == 1);
@@ -152,7 +152,10 @@ Value EvaluationFunction<KBNK>::apply(const Position& pos) const {
   Square loserKSq = pos.king_square(weakerSide);
   Square bishopSquare = pos.piece_list(strongerSide, BISHOP, 0);
 
-  if (square_color(bishopSquare) == BLACK)
+  // kbnk_mate_table() tries to drive toward corners A1 or H8,
+  // if we have a bishop that cannot reach the above squares we
+  // mirror the kings so to drive enemy toward corners A8 or H1.
+  if (opposite_color_squares(bishopSquare, SQ_A1))
   {
       winnerKSq = flop_square(winnerKSq);
       loserKSq = flop_square(loserKSq);
@@ -170,8 +173,8 @@ Value EvaluationFunction<KBNK>::apply(const Position& pos) const {
 template<>
 Value EvaluationFunction<KPK>::apply(const Position& pos) const {
 
-  assert(pos.non_pawn_material(strongerSide) == Value(0));
-  assert(pos.non_pawn_material(weakerSide) == Value(0));
+  assert(pos.non_pawn_material(strongerSide) == VALUE_ZERO);
+  assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
   assert(pos.piece_count(strongerSide, PAWN) == 1);
   assert(pos.piece_count(weakerSide, PAWN) == 0);
 
@@ -354,7 +357,7 @@ Value EvaluationFunction<KBBKN>::apply(const Position& pos) const {
   result += Value(square_distance(bksq, nsq) * 32);
 
   // Bonus for restricting the knight's mobility
-  result += Value((8 - count_1s_max_15(pos.attacks_from<KNIGHT>(nsq))) * 8);
+  result += Value((8 - count_1s<CNT32_MAX15>(pos.attacks_from<KNIGHT>(nsq))) * 8);
 
   return strongerSide == pos.side_to_move() ? result : -result;
 }
@@ -364,12 +367,12 @@ Value EvaluationFunction<KBBKN>::apply(const Position& pos) const {
 /// king alone are always draw.
 template<>
 Value EvaluationFunction<KmmKm>::apply(const Position&) const {
-  return Value(0);
+  return VALUE_DRAW;
 }
 
 template<>
 Value EvaluationFunction<KNNK>::apply(const Position&) const {
-  return Value(0);
+  return VALUE_DRAW;
 }
 
 /// KBPKScalingFunction scales endgames where the stronger side has king,
@@ -398,7 +401,7 @@ ScaleFactor ScalingFunction<KBPsK>::apply(const Position& pos) const {
       Square queeningSq = relative_square(strongerSide, make_square(pawnFile, RANK_8));
       Square kingSq = pos.king_square(weakerSide);
 
-      if (   square_color(queeningSq) != square_color(bishopSq)
+      if (   opposite_color_squares(queeningSq, bishopSq)
           && file_distance(square_file(kingSq), pawnFile) <= 1)
       {
           // The bishop has the wrong color, and the defending king is on the
@@ -443,8 +446,8 @@ ScaleFactor ScalingFunction<KQKRPs>::apply(const Position& pos) const {
   Square kingSq = pos.king_square(weakerSide);
   if (   relative_rank(weakerSide, kingSq) <= RANK_2
       && relative_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4
-      && (pos.pieces(ROOK, weakerSide) & relative_rank_bb(weakerSide, RANK_3))
-      && (pos.pieces(PAWN, weakerSide) & relative_rank_bb(weakerSide, RANK_2))
+      && (pos.pieces(ROOK, weakerSide) & rank_bb(relative_rank(weakerSide, RANK_3)))
+      && (pos.pieces(PAWN, weakerSide) & rank_bb(relative_rank(weakerSide, RANK_2)))
       && (pos.attacks_from<KING>(kingSq) & pos.pieces(PAWN, weakerSide)))
   {
       Square rsq = pos.piece_list(weakerSide, ROOK, 0);
@@ -622,9 +625,9 @@ ScaleFactor ScalingFunction<KRPPKRP>::apply(const Position& pos) const {
 template<>
 ScaleFactor ScalingFunction<KPsK>::apply(const Position& pos) const {
 
-  assert(pos.non_pawn_material(strongerSide) == Value(0));
+  assert(pos.non_pawn_material(strongerSide) == VALUE_ZERO);
   assert(pos.piece_count(strongerSide, PAWN) >= 2);
-  assert(pos.non_pawn_material(weakerSide) == Value(0));
+  assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
   assert(pos.piece_count(weakerSide, PAWN) == 0);
 
   Square ksq = pos.king_square(weakerSide);
@@ -675,12 +678,12 @@ ScaleFactor ScalingFunction<KBPKB>::apply(const Position& pos) const {
   // Case 1: Defending king blocks the pawn, and cannot be driven away
   if (   square_file(weakerKingSq) == square_file(pawnSq)
       && relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq)
-      && (   square_color(weakerKingSq) != square_color(strongerBishopSq)
+      && (   opposite_color_squares(weakerKingSq, strongerBishopSq)
           || relative_rank(strongerSide, weakerKingSq) <= RANK_6))
       return SCALE_FACTOR_ZERO;
 
   // Case 2: Opposite colored bishops
-  if (square_color(strongerBishopSq) != square_color(weakerBishopSq))
+  if (opposite_color_squares(strongerBishopSq, weakerBishopSq))
   {
       // We assume that the position is drawn in the following three situations:
       //
@@ -696,11 +699,12 @@ ScaleFactor ScalingFunction<KBPKB>::apply(const Position& pos) const {
           return SCALE_FACTOR_ZERO;
       else
       {
-          Bitboard ray = ray_bb(pawnSq, (strongerSide == WHITE)? SIGNED_DIR_N : SIGNED_DIR_S);
-          if (ray & pos.pieces(KING, weakerSide))
+          Bitboard path = squares_in_front_of(strongerSide, pawnSq);
+
+          if (path & pos.pieces(KING, weakerSide))
               return SCALE_FACTOR_ZERO;
 
-          if (  (pos.attacks_from<BISHOP>(weakerBishopSq) & ray)
+          if (  (pos.attacks_from<BISHOP>(weakerBishopSq) & path)
               && square_distance(weakerBishopSq, pawnSq) >= 3)
               return SCALE_FACTOR_ZERO;
       }
@@ -724,8 +728,7 @@ ScaleFactor ScalingFunction<KBPPKB>::apply(const Position& pos) const {
   Square wbsq = pos.piece_list(strongerSide, BISHOP, 0);
   Square bbsq = pos.piece_list(weakerSide, BISHOP, 0);
 
-  if (square_color(wbsq) == square_color(bbsq))
-      // Not opposite-colored bishops, no scaling
+  if (!opposite_color_squares(wbsq, bbsq))
       return SCALE_FACTOR_NONE;
 
   Square ksq = pos.king_square(weakerSide);
@@ -753,7 +756,7 @@ ScaleFactor ScalingFunction<KBPPKB>::apply(const Position& pos) const {
     // some square in the frontmost pawn's path.
     if (   square_file(ksq) == square_file(blockSq1)
         && relative_rank(strongerSide, ksq) >= relative_rank(strongerSide, blockSq1)
-        && square_color(ksq) != square_color(wbsq))
+        && opposite_color_squares(ksq, wbsq))
         return SCALE_FACTOR_ZERO;
     else
         return SCALE_FACTOR_NONE;
@@ -763,14 +766,14 @@ ScaleFactor ScalingFunction<KBPPKB>::apply(const Position& pos) const {
     // in front of the frontmost pawn's path, and the square diagonally behind
     // this square on the file of the other pawn.
     if (   ksq == blockSq1
-        && square_color(ksq) != square_color(wbsq)
+        && opposite_color_squares(ksq, wbsq)
         && (   bbsq == blockSq2
             || (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(BISHOP, weakerSide))
             || rank_distance(r1, r2) >= 2))
         return SCALE_FACTOR_ZERO;
 
     else if (   ksq == blockSq2
-             && square_color(ksq) != square_color(wbsq)
+             && opposite_color_squares(ksq, wbsq)
              && (   bbsq == blockSq1
                  || (pos.attacks_from<BISHOP>(blockSq1) & pos.pieces(BISHOP, weakerSide))))
         return SCALE_FACTOR_ZERO;
@@ -804,7 +807,7 @@ ScaleFactor ScalingFunction<KBPKN>::apply(const Position& pos) const {
 
   if (   square_file(weakerKingSq) == square_file(pawnSq)
       && relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq)
-      && (   square_color(weakerKingSq) != square_color(strongerBishopSq)
+      && (   opposite_color_squares(weakerKingSq, strongerBishopSq)
           || relative_rank(strongerSide, weakerKingSq) <= RANK_6))
       return SCALE_FACTOR_ZERO;
 
@@ -821,7 +824,7 @@ ScaleFactor ScalingFunction<KNPK>::apply(const Position& pos) const {
   assert(pos.non_pawn_material(strongerSide) == KnightValueMidgame);
   assert(pos.piece_count(strongerSide, KNIGHT) == 1);
   assert(pos.piece_count(strongerSide, PAWN) == 1);
-  assert(pos.non_pawn_material(weakerSide) == Value(0));
+  assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
   assert(pos.piece_count(weakerSide, PAWN) == 0);
 
   Square pawnSq = pos.piece_list(strongerSide, PAWN, 0);
@@ -848,8 +851,8 @@ ScaleFactor ScalingFunction<KNPK>::apply(const Position& pos) const {
 template<>
 ScaleFactor ScalingFunction<KPKP>::apply(const Position& pos) const {
 
-  assert(pos.non_pawn_material(strongerSide) == Value(0));
-  assert(pos.non_pawn_material(weakerSide) == Value(0));
+  assert(pos.non_pawn_material(strongerSide) == VALUE_ZERO);
+  assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
   assert(pos.piece_count(WHITE, PAWN) == 1);
   assert(pos.piece_count(BLACK, PAWN) == 1);