Skip to content

Commit 045728a

Browse files
syzygy1vondele
authored andcommitted
Remove piece lists
This patch removes the incrementally updated piece lists from the Position object. This has been tried before but always failed. My reasons for trying again are: * 32-bit systems (including phones) are now much less important than they were some years ago (and are absent from fishtest); * NNUE may have made SF less finely tuned to the order in which moves were generated. STC: LLR: 2.94 (-2.94,2.94) {-1.25,0.25} Total: 55272 W: 5260 L: 5216 D: 44796 Ptnml(0-2): 208, 4147, 18898, 4159, 224 https://tests.stockfishchess.org/tests/view/5fc2986a42a050a89f02c926 LTC: LLR: 2.96 (-2.94,2.94) {-0.75,0.25} Total: 16600 W: 673 L: 608 D: 15319 Ptnml(0-2): 14, 533, 7138, 604, 11 https://tests.stockfishchess.org/tests/view/5fc2f98342a050a89f02c95c closes official-stockfish/Stockfish#3247 Bench: 3940967
1 parent 2bc4ae1 commit 045728a

File tree

6 files changed

+20
-44
lines changed

6 files changed

+20
-44
lines changed

src/endgame.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ ScaleFactor Endgame<KRPPKRP>::operator()(const Position& pos) const {
553553
assert(verify_material(pos, strongSide, RookValueMg, 2));
554554
assert(verify_material(pos, weakSide, RookValueMg, 1));
555555

556-
Square strongPawn1 = pos.squares<PAWN>(strongSide)[0];
557-
Square strongPawn2 = pos.squares<PAWN>(strongSide)[1];
556+
Square strongPawn1 = lsb(pos.pieces(strongSide, PAWN));
557+
Square strongPawn2 = msb(pos.pieces(strongSide, PAWN));
558558
Square weakKing = pos.square<KING>(weakSide);
559559

560560
// Does the stronger side have a passed pawn?
@@ -638,8 +638,8 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
638638
return SCALE_FACTOR_NONE;
639639

640640
Square weakKing = pos.square<KING>(weakSide);
641-
Square strongPawn1 = pos.squares<PAWN>(strongSide)[0];
642-
Square strongPawn2 = pos.squares<PAWN>(strongSide)[1];
641+
Square strongPawn1 = lsb(pos.pieces(strongSide, PAWN));
642+
Square strongPawn2 = msb(pos.pieces(strongSide, PAWN));
643643
Square blockSq1, blockSq2;
644644

645645
if (relative_rank(strongSide, strongPawn1) > relative_rank(strongSide, strongPawn2))

src/evaluate.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,15 @@ namespace {
387387
constexpr Direction Down = -pawn_push(Us);
388388
constexpr Bitboard OutpostRanks = (Us == WHITE ? Rank4BB | Rank5BB | Rank6BB
389389
: Rank5BB | Rank4BB | Rank3BB);
390-
const Square* pl = pos.squares<Pt>(Us);
391-
390+
Bitboard b1 = pos.pieces(Us, Pt);
392391
Bitboard b, bb;
393392
Score score = SCORE_ZERO;
394393

395394
attackedBy[Us][Pt] = 0;
396395

397-
for (Square s = *pl; s != SQ_NONE; s = *++pl)
398-
{
396+
while (b1) {
397+
Square s = pop_lsb(&b1);
398+
399399
// Find attacked squares, including x-ray attacks for bishops and rooks
400400
b = Pt == BISHOP ? attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(QUEEN))
401401
: Pt == ROOK ? attacks_bb< ROOK>(s, pos.pieces() ^ pos.pieces(QUEEN) ^ pos.pieces(Us, ROOK))

src/movegen.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,11 @@ namespace {
180180

181181
static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
182182

183-
const Square* pl = pos.squares<Pt>(Us);
183+
Bitboard bb = pos.pieces(Us, Pt);
184+
185+
while (bb) {
186+
Square from = pop_lsb(&bb);
184187

185-
for (Square from = *pl; from != SQ_NONE; from = *++pl)
186-
{
187188
if (Checks)
188189
{
189190
if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)

src/pawns.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace {
9191
Square s;
9292
bool backward, passed, doubled;
9393
Score score = SCORE_ZERO;
94-
const Square* pl = pos.squares<PAWN>(Us);
94+
Bitboard b = pos.pieces(Us, PAWN);
9595

9696
Bitboard ourPawns = pos.pieces( Us, PAWN);
9797
Bitboard theirPawns = pos.pieces(Them, PAWN);
@@ -104,8 +104,9 @@ namespace {
104104
e->blockedCount += popcount(shift<Up>(ourPawns) & (theirPawns | doubleAttackThem));
105105

106106
// Loop through all pawns of the current color and score each pawn
107-
while ((s = *pl++) != SQ_NONE)
108-
{
107+
while (b) {
108+
s = pop_lsb(&b);
109+
109110
assert(pos.piece_on(s) == make_piece(Us, PAWN));
110111

111112
Rank r = relative_rank(Us, s);

src/position.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
197197

198198
std::memset(this, 0, sizeof(Position));
199199
std::memset(si, 0, sizeof(StateInfo));
200-
std::fill_n(&pieceList[0][0], sizeof(pieceList) / sizeof(Square), SQ_NONE);
201200
st = si;
202201

203202
ss >> std::noskipws;
@@ -1327,16 +1326,10 @@ bool Position::pos_is_ok() const {
13271326
assert(0 && "pos_is_ok: State");
13281327

13291328
for (Piece pc : Pieces)
1330-
{
13311329
if ( pieceCount[pc] != popcount(pieces(color_of(pc), type_of(pc)))
13321330
|| pieceCount[pc] != std::count(board, board + SQUARE_NB, pc))
13331331
assert(0 && "pos_is_ok: Pieces");
13341332

1335-
for (int i = 0; i < pieceCount[pc]; ++i)
1336-
if (board[pieceList[pc][i]] != pc || index[pieceList[pc][i]] != i)
1337-
assert(0 && "pos_is_ok: Index");
1338-
}
1339-
13401333
for (Color c : { WHITE, BLACK })
13411334
for (CastlingRights cr : {c & KING_SIDE, c & QUEEN_SIDE})
13421335
{

src/position.h

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class Position {
9999
bool empty(Square s) const;
100100
template<PieceType Pt> int count(Color c) const;
101101
template<PieceType Pt> int count() const;
102-
template<PieceType Pt> const Square* squares(Color c) const;
103102
template<PieceType Pt> Square square(Color c) const;
104103
bool is_on_semiopen_file(Color c, Square s) const;
105104

@@ -190,8 +189,6 @@ class Position {
190189
Bitboard byTypeBB[PIECE_TYPE_NB];
191190
Bitboard byColorBB[COLOR_NB];
192191
int pieceCount[PIECE_NB];
193-
Square pieceList[PIECE_NB][16];
194-
int index[SQUARE_NB];
195192
int castlingRightsMask[SQUARE_NB];
196193
Square castlingRookSquare[CASTLING_RIGHT_NB];
197194
Bitboard castlingPath[CASTLING_RIGHT_NB];
@@ -254,13 +251,9 @@ template<PieceType Pt> inline int Position::count() const {
254251
return count<Pt>(WHITE) + count<Pt>(BLACK);
255252
}
256253

257-
template<PieceType Pt> inline const Square* Position::squares(Color c) const {
258-
return pieceList[make_piece(c, Pt)];
259-
}
260-
261254
template<PieceType Pt> inline Square Position::square(Color c) const {
262-
assert(pieceCount[make_piece(c, Pt)] == 1);
263-
return squares<Pt>(c)[0];
255+
assert(count<Pt>(c) == 1);
256+
return lsb(pieces(c, Pt));
264257
}
265258

266259
inline Square Position::ep_square() const {
@@ -394,44 +387,32 @@ inline void Position::put_piece(Piece pc, Square s) {
394387
board[s] = pc;
395388
byTypeBB[ALL_PIECES] |= byTypeBB[type_of(pc)] |= s;
396389
byColorBB[color_of(pc)] |= s;
397-
index[s] = pieceCount[pc]++;
398-
pieceList[pc][index[s]] = s;
390+
pieceCount[pc]++;
399391
pieceCount[make_piece(color_of(pc), ALL_PIECES)]++;
400392
psq += PSQT::psq[pc][s];
401393
}
402394

403395
inline void Position::remove_piece(Square s) {
404396

405-
// WARNING: This is not a reversible operation. If we remove a piece in
406-
// do_move() and then replace it in undo_move() we will put it at the end of
407-
// the list and not in its original place, it means index[] and pieceList[]
408-
// are not invariant to a do_move() + undo_move() sequence.
409397
Piece pc = board[s];
410398
byTypeBB[ALL_PIECES] ^= s;
411399
byTypeBB[type_of(pc)] ^= s;
412400
byColorBB[color_of(pc)] ^= s;
413401
/* board[s] = NO_PIECE; Not needed, overwritten by the capturing one */
414-
Square lastSquare = pieceList[pc][--pieceCount[pc]];
415-
index[lastSquare] = index[s];
416-
pieceList[pc][index[lastSquare]] = lastSquare;
417-
pieceList[pc][pieceCount[pc]] = SQ_NONE;
402+
pieceCount[pc]--;
418403
pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
419404
psq -= PSQT::psq[pc][s];
420405
}
421406

422407
inline void Position::move_piece(Square from, Square to) {
423408

424-
// index[from] is not updated and becomes stale. This works as long as index[]
425-
// is accessed just by known occupied squares.
426409
Piece pc = board[from];
427410
Bitboard fromTo = from | to;
428411
byTypeBB[ALL_PIECES] ^= fromTo;
429412
byTypeBB[type_of(pc)] ^= fromTo;
430413
byColorBB[color_of(pc)] ^= fromTo;
431414
board[from] = NO_PIECE;
432415
board[to] = pc;
433-
index[to] = index[from];
434-
pieceList[pc][index[to]] = to;
435416
psq += PSQT::psq[pc][to] - PSQT::psq[pc][from];
436417
}
437418

0 commit comments

Comments
 (0)