Skip to content

Commit 75dfdea

Browse files
protonspringsnicolet
authored andcommitted
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 #2485 No functional change
1 parent 7a7bcd6 commit 75dfdea

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

src/bitbase.cpp

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ namespace {
4343
// bit 12: side to move (WHITE or BLACK)
4444
// bit 13-14: white pawn file (from FILE_A to FILE_D)
4545
// bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
46-
unsigned index(Color us, Square bksq, Square wksq, Square psq) {
47-
return int(wksq) | (bksq << 6) | (us << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15);
46+
unsigned index(Color stm, Square bksq, Square wksq, Square psq) {
47+
return int(wksq) | (bksq << 6) | (stm << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15);
4848
}
4949

5050
enum Result {
@@ -60,24 +60,21 @@ namespace {
6060
KPKPosition() = default;
6161
explicit KPKPosition(unsigned idx);
6262
operator Result() const { return result; }
63-
Result classify(const std::vector<KPKPosition>& db)
64-
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
63+
Result classify(const std::vector<KPKPosition>& db);
6564

66-
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
67-
68-
Color us;
65+
Color stm;
6966
Square ksq[COLOR_NB], psq;
7067
Result result;
7168
};
7269

7370
} // namespace
7471

7572

76-
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) {
73+
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color stm) {
7774

7875
assert(file_of(wpsq) <= FILE_D);
7976

80-
unsigned idx = index(us, bksq, wksq, wpsq);
77+
unsigned idx = index(stm, bksq, wksq, wpsq);
8178
return KPKBitbase[idx / 32] & (1 << (idx & 0x1F));
8279
}
8380

@@ -110,36 +107,35 @@ namespace {
110107

111108
ksq[WHITE] = Square((idx >> 0) & 0x3F);
112109
ksq[BLACK] = Square((idx >> 6) & 0x3F);
113-
us = Color ((idx >> 12) & 0x01);
110+
stm = Color ((idx >> 12) & 0x01);
114111
psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
115112

116113
// Check if two pieces are on the same square or if a king can be captured
117114
if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
118115
|| ksq[WHITE] == psq
119116
|| ksq[BLACK] == psq
120-
|| (us == WHITE && (PawnAttacks[WHITE][psq] & ksq[BLACK])))
117+
|| (stm == WHITE && (PawnAttacks[WHITE][psq] & ksq[BLACK])))
121118
result = INVALID;
122119

123120
// Immediate win if a pawn can be promoted without getting captured
124-
else if ( us == WHITE
121+
else if ( stm == WHITE
125122
&& rank_of(psq) == RANK_7
126-
&& ksq[us] != psq + NORTH
127-
&& ( distance(ksq[~us], psq + NORTH) > 1
128-
|| (PseudoAttacks[KING][ksq[us]] & (psq + NORTH))))
123+
&& ksq[stm] != psq + NORTH
124+
&& ( distance(ksq[~stm], psq + NORTH) > 1
125+
|| (PseudoAttacks[KING][ksq[stm]] & (psq + NORTH))))
129126
result = WIN;
130127

131128
// Immediate draw if it is a stalemate or a king captures undefended pawn
132-
else if ( us == BLACK
133-
&& ( !(PseudoAttacks[KING][ksq[us]] & ~(PseudoAttacks[KING][ksq[~us]] | PawnAttacks[~us][psq]))
134-
|| (PseudoAttacks[KING][ksq[us]] & psq & ~PseudoAttacks[KING][ksq[~us]])))
129+
else if ( stm == BLACK
130+
&& ( !(PseudoAttacks[KING][ksq[stm]] & ~(PseudoAttacks[KING][ksq[~stm]] | PawnAttacks[~stm][psq]))
131+
|| (PseudoAttacks[KING][ksq[stm]] & psq & ~PseudoAttacks[KING][ksq[~stm]])))
135132
result = DRAW;
136133

137134
// Position will be classified later
138135
else
139136
result = UNKNOWN;
140137
}
141138

142-
template<Color Us>
143139
Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
144140

145141
// White to move: If one move leads to a position classified as WIN, the result
@@ -151,27 +147,25 @@ namespace {
151147
// of the current position is DRAW. If all moves lead to positions classified
152148
// as WIN, the position is classified as WIN, otherwise the current position is
153149
// classified as UNKNOWN.
154-
155-
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
156-
constexpr Result Good = (Us == WHITE ? WIN : DRAW);
157-
constexpr Result Bad = (Us == WHITE ? DRAW : WIN);
150+
const Result Good = (stm == WHITE ? WIN : DRAW);
151+
const Result Bad = (stm == WHITE ? DRAW : WIN);
158152

159153
Result r = INVALID;
160-
Bitboard b = PseudoAttacks[KING][ksq[Us]];
154+
Bitboard b = PseudoAttacks[KING][ksq[stm]];
161155

162156
while (b)
163-
r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)]
164-
: db[index(Them, pop_lsb(&b), ksq[Them] , psq)];
157+
r |= stm == WHITE ? db[index(BLACK, ksq[BLACK] , pop_lsb(&b), psq)]
158+
: db[index(WHITE, pop_lsb(&b), ksq[WHITE], psq)];
165159

166-
if (Us == WHITE)
160+
if (stm == WHITE)
167161
{
168162
if (rank_of(psq) < RANK_7) // Single push
169-
r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH)];
163+
r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH)];
170164

171165
if ( rank_of(psq) == RANK_2 // Double push
172-
&& psq + NORTH != ksq[Us]
173-
&& psq + NORTH != ksq[Them])
174-
r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH + NORTH)];
166+
&& psq + NORTH != ksq[WHITE]
167+
&& psq + NORTH != ksq[BLACK])
168+
r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH + NORTH)];
175169
}
176170

177171
return result = r & Good ? Good : r & UNKNOWN ? UNKNOWN : Bad;

0 commit comments

Comments
 (0)