X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fendgame.cpp;h=167a9ed4f5df1f8b479c6b5064de0cba2630bc44;hb=8d4caebabe91a473bd052d2f771e79a184902c31;hp=e0efd0e665271b75f2fc6bab5b824b97f843de89;hpb=f08a6eed0d3938e451b6da384ae39ffb58f25dd4;p=stockfish
diff --git a/src/endgame.cpp b/src/endgame.cpp
index e0efd0e6..167a9ed4 100644
--- a/src/endgame.cpp
+++ b/src/endgame.cpp
@@ -17,20 +17,13 @@
along with this program. If not, see .
*/
-
-////
-//// Includes
-////
-
#include
#include "bitcount.h"
#include "endgame.h"
+#include "pawns.h"
-
-////
-//// Local definitions
-////
+extern uint32_t probe_kpk_bitbase(Square wksq, Square wpsq, Square bksq, Color stm);
namespace {
@@ -68,9 +61,6 @@ namespace {
// and knight in KR vs KN endgames.
const int KRKNKingKnightDistancePenalty[8] = { 0, 0, 4, 10, 20, 32, 48, 70 };
- // Bitbase for KP vs K
- uint8_t KPKBitbase[24576];
-
// Various inline functions for accessing the above arrays
inline Value mate_table(Square s) {
return Value(MateTable[s]);
@@ -88,23 +78,6 @@ namespace {
return Value(KRKNKingKnightDistancePenalty[d]);
}
- // Function for probing the KP vs K bitbase
- int probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm);
-
-}
-
-
-////
-//// Functions
-////
-
-/// 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);
}
@@ -155,7 +128,7 @@ Value EvaluationFunction::apply(const Position& pos) const {
// 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 (!same_color_squares(bishopSquare, SQ_A1))
+ if (opposite_color_squares(bishopSquare, SQ_A1))
{
winnerKSq = flop_square(winnerKSq);
loserKSq = flop_square(loserKSq);
@@ -203,7 +176,7 @@ Value EvaluationFunction::apply(const Position& pos) const {
wpsq = flop_square(wpsq);
}
- if (!probe_kpk(wksq, wpsq, bksq, stm))
+ if (!probe_kpk_bitbase(wksq, wpsq, bksq, stm))
return VALUE_DRAW;
Value result = VALUE_KNOWN_WIN
@@ -401,8 +374,8 @@ ScaleFactor ScalingFunction::apply(const Position& pos) const {
Square queeningSq = relative_square(strongerSide, make_square(pawnFile, RANK_8));
Square kingSq = pos.king_square(weakerSide);
- if ( !same_color_squares(queeningSq, bishopSq)
- && file_distance(square_file(kingSq), pawnFile) <= 1)
+ if ( opposite_color_squares(queeningSq, bishopSq)
+ && abs(square_file(kingSq) - pawnFile) <= 1)
{
// The bishop has the wrong color, and the defending king is on the
// file of the pawn(s) or the neighboring file. Find the rank of the
@@ -446,8 +419,8 @@ ScaleFactor ScalingFunction::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(kingSq) & pos.pieces(PAWN, weakerSide)))
{
Square rsq = pos.piece_list(weakerSide, ROOK, 0);
@@ -678,12 +651,12 @@ ScaleFactor ScalingFunction::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)
- && ( !same_color_squares(weakerKingSq, strongerBishopSq)
+ && ( opposite_color_squares(weakerKingSq, strongerBishopSq)
|| relative_rank(strongerSide, weakerKingSq) <= RANK_6))
return SCALE_FACTOR_ZERO;
// Case 2: Opposite colored bishops
- if (!same_color_squares(strongerBishopSq, weakerBishopSq))
+ if (opposite_color_squares(strongerBishopSq, weakerBishopSq))
{
// We assume that the position is drawn in the following three situations:
//
@@ -728,8 +701,7 @@ ScaleFactor ScalingFunction::apply(const Position& pos) const {
Square wbsq = pos.piece_list(strongerSide, BISHOP, 0);
Square bbsq = pos.piece_list(weakerSide, BISHOP, 0);
- if (same_color_squares(wbsq, bbsq))
- // Not opposite-colored bishops, no scaling
+ if (!opposite_color_squares(wbsq, bbsq))
return SCALE_FACTOR_NONE;
Square ksq = pos.king_square(weakerSide);
@@ -757,7 +729,7 @@ ScaleFactor ScalingFunction::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)
- && !same_color_squares(ksq, wbsq))
+ && opposite_color_squares(ksq, wbsq))
return SCALE_FACTOR_ZERO;
else
return SCALE_FACTOR_NONE;
@@ -767,14 +739,14 @@ ScaleFactor ScalingFunction::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
- && !same_color_squares(ksq, wbsq)
+ && opposite_color_squares(ksq, wbsq)
&& ( bbsq == blockSq2
|| (pos.attacks_from(blockSq2) & pos.pieces(BISHOP, weakerSide))
- || rank_distance(r1, r2) >= 2))
+ || abs(r1 - r2) >= 2))
return SCALE_FACTOR_ZERO;
else if ( ksq == blockSq2
- && !same_color_squares(ksq, wbsq)
+ && opposite_color_squares(ksq, wbsq)
&& ( bbsq == blockSq1
|| (pos.attacks_from(blockSq1) & pos.pieces(BISHOP, weakerSide))))
return SCALE_FACTOR_ZERO;
@@ -808,7 +780,7 @@ ScaleFactor ScalingFunction::apply(const Position& pos) const {
if ( square_file(weakerKingSq) == square_file(pawnSq)
&& relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq)
- && ( !same_color_squares(weakerKingSq, strongerBishopSq)
+ && ( opposite_color_squares(weakerKingSq, strongerBishopSq)
|| relative_rank(strongerSide, weakerKingSq) <= RANK_6))
return SCALE_FACTOR_ZERO;
@@ -890,21 +862,5 @@ ScaleFactor ScalingFunction::apply(const Position& pos) const {
// Probe the KPK bitbase with the weakest side's pawn removed. If it's a
// draw, it's probably at least a draw even with the pawn.
- return probe_kpk(wksq, wpsq, bksq, stm) ? SCALE_FACTOR_NONE : SCALE_FACTOR_ZERO;
-}
-
-
-namespace {
-
- // Probe the KP vs K bitbase
-
- int probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm) {
-
- int wp = square_file(wpsq) + 4 * (square_rank(wpsq) - 1);
- int index = int(stm) + 2 * bksq + 128 * wksq + 8192 * wp;
-
- assert(index >= 0 && index < 24576 * 8);
-
- return KPKBitbase[index / 8] & (1 << (index & 7));
- }
+ return probe_kpk_bitbase(wksq, wpsq, bksq, stm) ? SCALE_FACTOR_NONE : SCALE_FACTOR_ZERO;
}