From 6fb0a1bc4050dd9b15e9c163c46c60f25c48137d Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 8 Nov 2014 14:04:14 +0100 Subject: [PATCH 1/1] Introduce distance() and unify some API Original work by Lucas. No functional change. --- src/bitbase.cpp | 4 +- src/bitboard.cpp | 6 +-- src/bitboard.h | 15 +++----- src/endgame.cpp | 98 ++++++++++++++++++++++++------------------------ src/evaluate.cpp | 6 +-- src/pawns.cpp | 2 +- src/types.h | 1 - 7 files changed, 63 insertions(+), 69 deletions(-) diff --git a/src/bitbase.cpp b/src/bitbase.cpp index 0f274c8a..000ed7a5 100644 --- a/src/bitbase.cpp +++ b/src/bitbase.cpp @@ -114,7 +114,7 @@ namespace { result = UNKNOWN; // Check if two pieces are on the same square or if a king can be captured - if ( square_distance(wksq, bksq) <= 1 + if ( distance(wksq, bksq) <= 1 || wksq == psq || bksq == psq || (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq))) @@ -125,7 +125,7 @@ namespace { // Immediate win if a pawn can be promoted without getting captured if ( rank_of(psq) == RANK_7 && wksq != psq + DELTA_N - && ( square_distance(bksq, psq + DELTA_N) > 1 + && ( distance(bksq, psq + DELTA_N) > 1 ||(StepAttacksBB[KING][wksq] & (psq + DELTA_N)))) result = WIN; } diff --git a/src/bitboard.cpp b/src/bitboard.cpp index aa65d7f8..5a4c8b6b 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -177,7 +177,7 @@ void Bitboards::init() { for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2) if (s1 != s2) { - SquareDistance[s1][s2] = std::max(file_distance(s1, s2), rank_distance(s1, s2)); + SquareDistance[s1][s2] = std::max(distance(s1, s2), distance(s1, s2)); DistanceRingsBB[s1][SquareDistance[s1][s2] - 1] |= s2; } @@ -191,7 +191,7 @@ void Bitboards::init() { { Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]); - if (is_ok(to) && square_distance(s, to) < 3) + if (is_ok(to) && distance(s, to) < 3) StepAttacksBB[make_piece(c, pt)][s] |= to; } @@ -229,7 +229,7 @@ namespace { for (int i = 0; i < 4; ++i) for (Square s = sq + deltas[i]; - is_ok(s) && square_distance(s, s - deltas[i]) == 1; + is_ok(s) && distance(s, s - deltas[i]) == 1; s += deltas[i]) { attack |= s; diff --git a/src/bitboard.h b/src/bitboard.h index a8c0afbc..7c4a55f0 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -112,17 +112,12 @@ inline bool more_than_one(Bitboard b) { return b & (b - 1); } -inline int square_distance(Square s1, Square s2) { - return SquareDistance[s1][s2]; -} - -inline int file_distance(Square s1, Square s2) { - return dist(file_of(s1), file_of(s2)); -} +template inline int distance(T x, T y) { return x < y ? y - x : x - y; } +template<> inline int distance(Square x, Square y) { return SquareDistance[x][y]; } -inline int rank_distance(Square s1, Square s2) { - return dist(rank_of(s1), rank_of(s2)); -} +template inline int distance(T2 x, T2 y); +template<> inline int distance(Square x, Square y) { return distance(file_of(x), file_of(y)); } +template<> inline int distance(Square x, Square y) { return distance(rank_of(x), rank_of(y)); } /// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns. diff --git a/src/endgame.cpp b/src/endgame.cpp index 2a1614af..f683edc1 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -162,7 +162,7 @@ Value Endgame::operator()(const Position& pos) const { Value result = pos.non_pawn_material(strongSide) + pos.count(strongSide) * PawnValueEg + PushToEdges[loserKSq] - + PushClose[square_distance(winnerKSq, loserKSq)]; + + PushClose[distance(winnerKSq, loserKSq)]; if ( pos.count(strongSide) || pos.count(strongSide) @@ -196,7 +196,7 @@ Value Endgame::operator()(const Position& pos) const { } Value result = VALUE_KNOWN_WIN - + PushClose[square_distance(winnerKSq, loserKSq)] + + PushClose[distance(winnerKSq, loserKSq)] + PushToCorners[loserKSq]; return strongSide == pos.side_to_move() ? result : -result; @@ -246,26 +246,26 @@ Value Endgame::operator()(const Position& pos) const { // If the stronger side's king is in front of the pawn, it's a win if (wksq < psq && file_of(wksq) == file_of(psq)) - result = RookValueEg - square_distance(wksq, psq); + result = RookValueEg - distance(wksq, psq); // If the weaker side's king is too far from the pawn and the rook, // it's a win. - else if ( square_distance(bksq, psq) >= 3 + (pos.side_to_move() == weakSide) - && square_distance(bksq, rsq) >= 3) - result = RookValueEg - square_distance(wksq, psq); + else if ( distance(bksq, psq) >= 3 + (pos.side_to_move() == weakSide) + && distance(bksq, rsq) >= 3) + result = RookValueEg - distance(wksq, psq); // If the pawn is far advanced and supported by the defending king, // the position is drawish else if ( rank_of(bksq) <= RANK_3 - && square_distance(bksq, psq) == 1 + && distance(bksq, psq) == 1 && rank_of(wksq) >= RANK_4 - && square_distance(wksq, psq) > 2 + (pos.side_to_move() == strongSide)) - result = Value(80) - 8 * square_distance(wksq, psq); + && distance(wksq, psq) > 2 + (pos.side_to_move() == strongSide)) + result = Value(80) - 8 * distance(wksq, psq); else - result = Value(200) - 8 * ( square_distance(wksq, psq + DELTA_S) - - square_distance(bksq, psq + DELTA_S) - - square_distance(psq, queeningSq)); + result = Value(200) - 8 * ( distance(wksq, psq + DELTA_S) + - distance(bksq, psq + DELTA_S) + - distance(psq, queeningSq)); return strongSide == pos.side_to_move() ? result : -result; } @@ -294,7 +294,7 @@ Value Endgame::operator()(const Position& pos) const { Square bksq = pos.king_square(weakSide); Square bnsq = pos.list(weakSide)[0]; - Value result = Value(PushToEdges[bksq] + PushAway[square_distance(bksq, bnsq)]); + Value result = Value(PushToEdges[bksq] + PushAway[distance(bksq, bnsq)]); return strongSide == pos.side_to_move() ? result : -result; } @@ -313,10 +313,10 @@ Value Endgame::operator()(const Position& pos) const { Square loserKSq = pos.king_square(weakSide); Square pawnSq = pos.list(weakSide)[0]; - Value result = Value(PushClose[square_distance(winnerKSq, loserKSq)]); + Value result = Value(PushClose[distance(winnerKSq, loserKSq)]); if ( relative_rank(weakSide, pawnSq) != RANK_7 - || square_distance(loserKSq, pawnSq) != 1 + || distance(loserKSq, pawnSq) != 1 || !((FileABB | FileCBB | FileFBB | FileHBB) & pawnSq)) result += QueenValueEg - PawnValueEg; @@ -340,7 +340,7 @@ Value Endgame::operator()(const Position& pos) const { Value result = QueenValueEg - RookValueEg + PushToEdges[loserKSq] - + PushClose[square_distance(winnerKSq, loserKSq)]; + + PushClose[distance(winnerKSq, loserKSq)]; return strongSide == pos.side_to_move() ? result : -result; } @@ -375,7 +375,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { Square kingSq = pos.king_square(weakSide); if ( opposite_colors(queeningSq, bishopSq) - && square_distance(queeningSq, kingSq) <= 1) + && distance(queeningSq, kingSq) <= 1) return SCALE_FACTOR_DRAW; } @@ -398,8 +398,8 @@ ScaleFactor Endgame::operator()(const Position& pos) const { && (pos.pieces(strongSide, PAWN) & (weakPawnSq + pawn_push(weakSide))) && (opposite_colors(bishopSq, weakPawnSq) || pos.count(strongSide) == 1)) { - int strongKingDist = square_distance(weakPawnSq, strongKingSq); - int weakKingDist = square_distance(weakPawnSq, weakKingSq); + int strongKingDist = distance(weakPawnSq, strongKingSq); + int weakKingDist = distance(weakPawnSq, weakKingSq); // It's a draw if the weak king is on its back two ranks, within 2 // squares of the blocking pawn and the strong king is not @@ -469,7 +469,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // If the pawn is not too far advanced and the defending king defends the // queening square, use the third-rank defence. if ( r <= RANK_5 - && square_distance(bksq, queeningSq) <= 1 + && distance(bksq, queeningSq) <= 1 && wksq <= SQ_H5 && (rank_of(brsq) == RANK_6 || (r <= RANK_3 && rank_of(wrsq) != RANK_6))) return SCALE_FACTOR_DRAW; @@ -477,15 +477,15 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // The defending side saves a draw by checking from behind in case the pawn // has advanced to the 6th rank with the king behind. if ( r == RANK_6 - && square_distance(bksq, queeningSq) <= 1 + && distance(bksq, queeningSq) <= 1 && rank_of(wksq) + tempo <= RANK_6 - && (rank_of(brsq) == RANK_1 || (!tempo && dist(file_of(brsq), f) >= 3))) + && (rank_of(brsq) == RANK_1 || (!tempo && distance(file_of(brsq), f) >= 3))) return SCALE_FACTOR_DRAW; if ( r >= RANK_6 && bksq == queeningSq && rank_of(brsq) == RANK_1 - && (!tempo || square_distance(wksq, wpsq) >= 2)) + && (!tempo || distance(wksq, wpsq) >= 2)) return SCALE_FACTOR_DRAW; // White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7 @@ -501,8 +501,8 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // away, it's a draw. if ( r <= RANK_5 && bksq == wpsq + DELTA_N - && square_distance(wksq, wpsq) - tempo >= 2 - && square_distance(wksq, brsq) - tempo >= 2) + && distance(wksq, wpsq) - tempo >= 2 + && distance(wksq, brsq) - tempo >= 2) return SCALE_FACTOR_DRAW; // Pawn on the 7th rank supported by the rook from behind usually wins if the @@ -512,22 +512,22 @@ ScaleFactor Endgame::operator()(const Position& pos) const { && f != FILE_A && file_of(wrsq) == f && wrsq != queeningSq - && (square_distance(wksq, queeningSq) < square_distance(bksq, queeningSq) - 2 + tempo) - && (square_distance(wksq, queeningSq) < square_distance(bksq, wrsq) + tempo)) - return ScaleFactor(SCALE_FACTOR_MAX - 2 * square_distance(wksq, queeningSq)); + && (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo) + && (distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo)) + return ScaleFactor(SCALE_FACTOR_MAX - 2 * distance(wksq, queeningSq)); // Similar to the above, but with the pawn further back if ( f != FILE_A && file_of(wrsq) == f && wrsq < wpsq - && (square_distance(wksq, queeningSq) < square_distance(bksq, queeningSq) - 2 + tempo) - && (square_distance(wksq, wpsq + DELTA_N) < square_distance(bksq, wpsq + DELTA_N) - 2 + tempo) - && ( square_distance(bksq, wrsq) + tempo >= 3 - || ( square_distance(wksq, queeningSq) < square_distance(bksq, wrsq) + tempo - && (square_distance(wksq, wpsq + DELTA_N) < square_distance(bksq, wrsq) + tempo)))) + && (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo) + && (distance(wksq, wpsq + DELTA_N) < distance(bksq, wpsq + DELTA_N) - 2 + tempo) + && ( distance(bksq, wrsq) + tempo >= 3 + || ( distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo + && (distance(wksq, wpsq + DELTA_N) < distance(bksq, wrsq) + tempo)))) return ScaleFactor( SCALE_FACTOR_MAX - - 8 * square_distance(wpsq, queeningSq) - - 2 * square_distance(wksq, queeningSq)); + - 8 * distance(wpsq, queeningSq) + - 2 * distance(wksq, queeningSq)); // If the pawn is not far advanced and the defending king is somewhere in // the pawn's path, it's probably a draw. @@ -535,9 +535,9 @@ ScaleFactor Endgame::operator()(const Position& pos) const { { if (file_of(bksq) == file_of(wpsq)) return ScaleFactor(10); - if ( dist(file_of(bksq), file_of(wpsq)) == 1 - && square_distance(wksq, bksq) > 2) - return ScaleFactor(24 - 2 * square_distance(wksq, bksq)); + if ( distance(bksq, wpsq) == 1 + && distance(wksq, bksq) > 2) + return ScaleFactor(24 - 2 * distance(wksq, bksq)); } return SCALE_FACTOR_NONE; } @@ -564,7 +564,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // corner but not trapped there. if (rk == RANK_5 && !opposite_colors(bsq, psq)) { - int d = square_distance(psq + 3 * push, ksq); + int d = distance(psq + 3 * push, ksq); if (d <= 2 && !(d == 0 && ksq == pos.king_square(strongSide) + 2 * push)) return ScaleFactor(24); @@ -577,9 +577,9 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // pawn from a reasonable distance and the defending king is near // the corner if ( rk == RANK_6 - && square_distance(psq + 2 * push, ksq) <= 1 + && distance(psq + 2 * push, ksq) <= 1 && (PseudoAttacks[BISHOP][bsq] & (psq + push)) - && file_distance(bsq, psq) >= 2) + && distance(bsq, psq) >= 2) return ScaleFactor(8); } @@ -604,8 +604,8 @@ ScaleFactor Endgame::operator()(const Position& pos) const { Rank r = std::max(relative_rank(strongSide, wpsq1), relative_rank(strongSide, wpsq2)); - if ( file_distance(bksq, wpsq1) <= 1 - && file_distance(bksq, wpsq2) <= 1 + if ( distance(bksq, wpsq1) <= 1 + && distance(bksq, wpsq2) <= 1 && relative_rank(strongSide, bksq) > r) { switch (r) { @@ -638,7 +638,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // the king is within one file of the pawns, it's a draw. if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq))) && !((pawns & ~FileABB) && (pawns & ~FileHBB)) - && file_distance(ksq, psq) <= 1) + && distance(ksq, psq) <= 1) return SCALE_FACTOR_DRAW; return SCALE_FACTOR_NONE; @@ -690,7 +690,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { return SCALE_FACTOR_DRAW; if ( (pos.attacks_from(weakBishopSq) & path) - && square_distance(weakBishopSq, pawnSq) >= 3) + && distance(weakBishopSq, pawnSq) >= 3) return SCALE_FACTOR_DRAW; } } @@ -729,7 +729,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { blockSq2 = make_square(file_of(psq1), rank_of(psq2)); } - switch (file_distance(psq1, psq2)) + switch (distance(psq1, psq2)) { case 0: // Both pawns are on the same file. It's an easy draw if the defender firmly @@ -749,7 +749,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { && opposite_colors(ksq, wbsq) && ( bbsq == blockSq2 || (pos.attacks_from(blockSq2) & pos.pieces(weakSide, BISHOP)) - || dist(r1, r2) >= 2)) + || distance(r1, r2) >= 2)) return SCALE_FACTOR_DRAW; else if ( ksq == blockSq2 @@ -802,7 +802,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { Square pawnSq = normalize(pos, strongSide, pos.list(strongSide)[0]); Square weakKingSq = normalize(pos, strongSide, pos.king_square(weakSide)); - if (pawnSq == SQ_A7 && square_distance(SQ_A8, weakKingSq) <= 1) + if (pawnSq == SQ_A7 && distance(SQ_A8, weakKingSq) <= 1) return SCALE_FACTOR_DRAW; return SCALE_FACTOR_NONE; @@ -821,7 +821,7 @@ ScaleFactor Endgame::operator()(const Position& pos) const { // King needs to get close to promoting pawn to prevent knight from blocking. // Rules for this are very tricky, so just approximate. if (forward_bb(strongSide, pawnSq) & pos.attacks_from(bishopSq)) - return ScaleFactor(square_distance(weakKingSq, pawnSq)); + return ScaleFactor(distance(weakKingSq, pawnSq)); return SCALE_FACTOR_NONE; } diff --git a/src/evaluate.cpp b/src/evaluate.cpp index d3665ea1..fe02ca29 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -583,12 +583,12 @@ namespace { Square blockSq = s + pawn_push(Us); // Adjust bonus based on the king's proximity - ebonus += square_distance(pos.king_square(Them), blockSq) * 5 * rr - - square_distance(pos.king_square(Us ), blockSq) * 2 * rr; + ebonus += distance(pos.king_square(Them), blockSq) * 5 * rr + - distance(pos.king_square(Us ), blockSq) * 2 * rr; // If blockSq is not the queening square then consider also a second push if (relative_rank(Us, blockSq) != RANK_8) - ebonus -= square_distance(pos.king_square(Us), blockSq + pawn_push(Us)) * rr; + ebonus -= distance(pos.king_square(Us), blockSq + pawn_push(Us)) * rr; // If the pawn is free to advance, then increase the bonus if (pos.empty(blockSq)) diff --git a/src/pawns.cpp b/src/pawns.cpp index 3634f957..1099c5a4 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -165,7 +165,7 @@ namespace { value -= UnsupportedPawnPenalty; if (doubled) - value -= Doubled[f] / rank_distance(s, lsb(doubled)); + value -= Doubled[f] / distance(s, Square(lsb(doubled))); if (backward) value -= Backward[opposed][f]; diff --git a/src/types.h b/src/types.h index a0552fd2..4b7c7cec 100644 --- a/src/types.h +++ b/src/types.h @@ -249,7 +249,6 @@ enum Rank { RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB }; -template inline T dist(T x, T y) { return x < y ? y - x : x - y; } /// The Score enum stores a middlegame and an endgame value in a single integer /// (enum). The least significant 16 bits are used to store the endgame value -- 2.39.2