1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Simplify KPK classify

This is a non-functional simplification. If we use the "side to move" of the entry
instead of the template, one of the classify methods goes away. Furthermore, I've
resolved  the colors in some of the statements (we're already assuming direction
using NORTH), and used stm (side to move) instead of "us," since this is much clearer
to me.

This is not tested because it is non-functional, only applies building the bitbase
and there are no changes to the binary (on my machine).

Closes https://github.com/official-stockfish/Stockfish/pull/2485

No functional change
This commit is contained in:
protonspring 2020-01-10 15:08:47 -07:00 committed by Stéphane Nicolet
parent 7a7bcd6359
commit 75dfdeac11

View file

@ -43,8 +43,8 @@ namespace {
// bit 12: side to move (WHITE or BLACK) // bit 12: side to move (WHITE or BLACK)
// bit 13-14: white pawn file (from FILE_A to FILE_D) // bit 13-14: white pawn file (from FILE_A to FILE_D)
// bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2) // bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
unsigned index(Color us, Square bksq, Square wksq, Square psq) { unsigned index(Color stm, Square bksq, Square wksq, Square psq) {
return int(wksq) | (bksq << 6) | (us << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15); return int(wksq) | (bksq << 6) | (stm << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15);
} }
enum Result { enum Result {
@ -60,12 +60,9 @@ namespace {
KPKPosition() = default; KPKPosition() = default;
explicit KPKPosition(unsigned idx); explicit KPKPosition(unsigned idx);
operator Result() const { return result; } operator Result() const { return result; }
Result classify(const std::vector<KPKPosition>& db) Result classify(const std::vector<KPKPosition>& db);
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
template<Color Us> Result classify(const std::vector<KPKPosition>& db); Color stm;
Color us;
Square ksq[COLOR_NB], psq; Square ksq[COLOR_NB], psq;
Result result; Result result;
}; };
@ -73,11 +70,11 @@ namespace {
} // namespace } // namespace
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) { bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color stm) {
assert(file_of(wpsq) <= FILE_D); assert(file_of(wpsq) <= FILE_D);
unsigned idx = index(us, bksq, wksq, wpsq); unsigned idx = index(stm, bksq, wksq, wpsq);
return KPKBitbase[idx / 32] & (1 << (idx & 0x1F)); return KPKBitbase[idx / 32] & (1 << (idx & 0x1F));
} }
@ -110,28 +107,28 @@ namespace {
ksq[WHITE] = Square((idx >> 0) & 0x3F); ksq[WHITE] = Square((idx >> 0) & 0x3F);
ksq[BLACK] = Square((idx >> 6) & 0x3F); ksq[BLACK] = Square((idx >> 6) & 0x3F);
us = Color ((idx >> 12) & 0x01); stm = Color ((idx >> 12) & 0x01);
psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7))); psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
// Check if two pieces are on the same square or if a king can be captured // Check if two pieces are on the same square or if a king can be captured
if ( distance(ksq[WHITE], ksq[BLACK]) <= 1 if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
|| ksq[WHITE] == psq || ksq[WHITE] == psq
|| ksq[BLACK] == psq || ksq[BLACK] == psq
|| (us == WHITE && (PawnAttacks[WHITE][psq] & ksq[BLACK]))) || (stm == WHITE && (PawnAttacks[WHITE][psq] & ksq[BLACK])))
result = INVALID; result = INVALID;
// Immediate win if a pawn can be promoted without getting captured // Immediate win if a pawn can be promoted without getting captured
else if ( us == WHITE else if ( stm == WHITE
&& rank_of(psq) == RANK_7 && rank_of(psq) == RANK_7
&& ksq[us] != psq + NORTH && ksq[stm] != psq + NORTH
&& ( distance(ksq[~us], psq + NORTH) > 1 && ( distance(ksq[~stm], psq + NORTH) > 1
|| (PseudoAttacks[KING][ksq[us]] & (psq + NORTH)))) || (PseudoAttacks[KING][ksq[stm]] & (psq + NORTH))))
result = WIN; result = WIN;
// Immediate draw if it is a stalemate or a king captures undefended pawn // Immediate draw if it is a stalemate or a king captures undefended pawn
else if ( us == BLACK else if ( stm == BLACK
&& ( !(PseudoAttacks[KING][ksq[us]] & ~(PseudoAttacks[KING][ksq[~us]] | PawnAttacks[~us][psq])) && ( !(PseudoAttacks[KING][ksq[stm]] & ~(PseudoAttacks[KING][ksq[~stm]] | PawnAttacks[~stm][psq]))
|| (PseudoAttacks[KING][ksq[us]] & psq & ~PseudoAttacks[KING][ksq[~us]]))) || (PseudoAttacks[KING][ksq[stm]] & psq & ~PseudoAttacks[KING][ksq[~stm]])))
result = DRAW; result = DRAW;
// Position will be classified later // Position will be classified later
@ -139,7 +136,6 @@ namespace {
result = UNKNOWN; result = UNKNOWN;
} }
template<Color Us>
Result KPKPosition::classify(const std::vector<KPKPosition>& db) { Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
// White to move: If one move leads to a position classified as WIN, the result // White to move: If one move leads to a position classified as WIN, the result
@ -151,27 +147,25 @@ namespace {
// of the current position is DRAW. If all moves lead to positions classified // of the current position is DRAW. If all moves lead to positions classified
// as WIN, the position is classified as WIN, otherwise the current position is // as WIN, the position is classified as WIN, otherwise the current position is
// classified as UNKNOWN. // classified as UNKNOWN.
const Result Good = (stm == WHITE ? WIN : DRAW);
constexpr Color Them = (Us == WHITE ? BLACK : WHITE); const Result Bad = (stm == WHITE ? DRAW : WIN);
constexpr Result Good = (Us == WHITE ? WIN : DRAW);
constexpr Result Bad = (Us == WHITE ? DRAW : WIN);
Result r = INVALID; Result r = INVALID;
Bitboard b = PseudoAttacks[KING][ksq[Us]]; Bitboard b = PseudoAttacks[KING][ksq[stm]];
while (b) while (b)
r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)] r |= stm == WHITE ? db[index(BLACK, ksq[BLACK] , pop_lsb(&b), psq)]
: db[index(Them, pop_lsb(&b), ksq[Them] , psq)]; : db[index(WHITE, pop_lsb(&b), ksq[WHITE], psq)];
if (Us == WHITE) if (stm == WHITE)
{ {
if (rank_of(psq) < RANK_7) // Single push if (rank_of(psq) < RANK_7) // Single push
r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH)]; r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH)];
if ( rank_of(psq) == RANK_2 // Double push if ( rank_of(psq) == RANK_2 // Double push
&& psq + NORTH != ksq[Us] && psq + NORTH != ksq[WHITE]
&& psq + NORTH != ksq[Them]) && psq + NORTH != ksq[BLACK])
r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH + NORTH)]; r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH + NORTH)];
} }
return result = r & Good ? Good : r & UNKNOWN ? UNKNOWN : Bad; return result = r & Good ? Good : r & UNKNOWN ? UNKNOWN : Bad;