Introduce distance() and unify some API

Original work by Lucas.

No functional change.
This commit is contained in:
Marco Costalba
2014-11-08 14:04:14 +01:00
parent 8631b08d97
commit 6fb0a1bc40
7 changed files with 63 additions and 69 deletions

View File

@@ -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;
}

View File

@@ -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<File>(s1, s2), distance<Rank>(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;

View File

@@ -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];
}
template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
inline int file_distance(Square s1, Square s2) {
return dist(file_of(s1), file_of(s2));
}
inline int rank_distance(Square s1, Square s2) {
return dist(rank_of(s1), rank_of(s2));
}
template<typename T1, typename T2> inline int distance(T2 x, T2 y);
template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); }
template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
/// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.

View File

@@ -162,7 +162,7 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
Value result = pos.non_pawn_material(strongSide)
+ pos.count<PAWN>(strongSide) * PawnValueEg
+ PushToEdges[loserKSq]
+ PushClose[square_distance(winnerKSq, loserKSq)];
+ PushClose[distance(winnerKSq, loserKSq)];
if ( pos.count<QUEEN>(strongSide)
|| pos.count<ROOK>(strongSide)
@@ -196,7 +196,7 @@ Value Endgame<KBNK>::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<KRKP>::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<KRKN>::operator()(const Position& pos) const {
Square bksq = pos.king_square(weakSide);
Square bnsq = pos.list<KNIGHT>(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<KQKP>::operator()(const Position& pos) const {
Square loserKSq = pos.king_square(weakSide);
Square pawnSq = pos.list<PAWN>(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<KQKR>::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<KBPsK>::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<KBPsK>::operator()(const Position& pos) const {
&& (pos.pieces(strongSide, PAWN) & (weakPawnSq + pawn_push(weakSide)))
&& (opposite_colors(bishopSq, weakPawnSq) || pos.count<PAWN>(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<KRPKR>::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<KRPKR>::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<KRPKR>::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<KRPKR>::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<KRPKR>::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<File>(bksq, wpsq) == 1
&& distance(wksq, bksq) > 2)
return ScaleFactor(24 - 2 * distance(wksq, bksq));
}
return SCALE_FACTOR_NONE;
}
@@ -564,7 +564,7 @@ ScaleFactor Endgame<KRPKB>::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<KRPKB>::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<File>(bsq, psq) >= 2)
return ScaleFactor(8);
}
@@ -604,8 +604,8 @@ ScaleFactor Endgame<KRPPKRP>::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<File>(bksq, wpsq1) <= 1
&& distance<File>(bksq, wpsq2) <= 1
&& relative_rank(strongSide, bksq) > r)
{
switch (r) {
@@ -638,7 +638,7 @@ ScaleFactor Endgame<KPsK>::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<File>(ksq, psq) <= 1)
return SCALE_FACTOR_DRAW;
return SCALE_FACTOR_NONE;
@@ -690,7 +690,7 @@ ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
return SCALE_FACTOR_DRAW;
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
&& square_distance(weakBishopSq, pawnSq) >= 3)
&& distance(weakBishopSq, pawnSq) >= 3)
return SCALE_FACTOR_DRAW;
}
}
@@ -729,7 +729,7 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
blockSq2 = make_square(file_of(psq1), rank_of(psq2));
}
switch (file_distance(psq1, psq2))
switch (distance<File>(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<KBPPKB>::operator()(const Position& pos) const {
&& opposite_colors(ksq, wbsq)
&& ( bbsq == blockSq2
|| (pos.attacks_from<BISHOP>(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<KNPK>::operator()(const Position& pos) const {
Square pawnSq = normalize(pos, strongSide, pos.list<PAWN>(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<KNPKB>::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<BISHOP>(bishopSq))
return ScaleFactor(square_distance(weakKingSq, pawnSq));
return ScaleFactor(distance(weakKingSq, pawnSq));
return SCALE_FACTOR_NONE;
}

View File

@@ -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))

View File

@@ -165,7 +165,7 @@ namespace {
value -= UnsupportedPawnPenalty;
if (doubled)
value -= Doubled[f] / rank_distance(s, lsb(doubled));
value -= Doubled[f] / distance<Rank>(s, Square(lsb(doubled)));
if (backward)
value -= Backward[opposed][f];

View File

@@ -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 <typename T> 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