Skip to content

Commit 06e0134

Browse files
committed
Correct a bug introduced by Stéphane in the previous patch.
When we are using the "Bitboard + Square" overloaded operators, the compiler uses the interpediate SquareBB[s] to transform the square into a Bitboard, and then calculate the result. For instance, the following code: ``` b = pos.pieces(Us, PAWN) & s ``` generates in fact the code: ``` b = pos.pieces(Us, PAWN) & SquareBB[s]` ``` The bug introduced by Stéphane in the previous patch was the use of `b = pos.pieces(Us, PAWN) & (s + Up)` which can result in out-of-bounds errors for the SquareBB[] array if s in the last rank of the board. We coorect the bug, and also add some asserts in bitboard.h to make the code more robust for this particular bug in the future. Bug report by Joost VandeVondele. Thanks! Bench: 5512000
1 parent 12ef8f7 commit 06e0134

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/bitboard.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,27 @@ extern Magic BishopMagics[SQUARE_NB];
107107
/// whether a given bit is set in a bitboard, and for setting and clearing bits.
108108

109109
inline Bitboard operator&(Bitboard b, Square s) {
110+
assert(s >= SQ_A1 && s <= SQ_H8);
110111
return b & SquareBB[s];
111112
}
112113

113114
inline Bitboard operator|(Bitboard b, Square s) {
115+
assert(s >= SQ_A1 && s <= SQ_H8);
114116
return b | SquareBB[s];
115117
}
116118

117119
inline Bitboard operator^(Bitboard b, Square s) {
120+
assert(s >= SQ_A1 && s <= SQ_H8);
118121
return b ^ SquareBB[s];
119122
}
120123

121124
inline Bitboard& operator|=(Bitboard& b, Square s) {
125+
assert(s >= SQ_A1 && s <= SQ_H8);
122126
return b |= SquareBB[s];
123127
}
124128

125129
inline Bitboard& operator^=(Bitboard& b, Square s) {
130+
assert(s >= SQ_A1 && s <= SQ_H8);
126131
return b ^= SquareBB[s];
127132
}
128133

src/pawns.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,16 @@ Value Entry::evaluate_shelter(const Position& pos, Square ksq) {
226226

227227
enum { Unopposed, BlockedByPawn, Unblocked };
228228
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
229-
constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH);
230-
constexpr Bitboard BlockRanks = (Us == WHITE ? Rank2BB | Rank3BB : Rank7BB | Rank6BB);
229+
constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH);
230+
constexpr Bitboard BlockRanks = (Us == WHITE ? Rank1BB | Rank2BB : Rank8BB | Rank7BB);
231231

232232
Bitboard b = pos.pieces(PAWN) & (forward_ranks_bb(Us, ksq) | rank_bb(ksq));
233233
Bitboard ourPawns = b & pos.pieces(Us);
234234
Bitboard theirPawns = b & pos.pieces(Them);
235235

236236
Value safety = (ourPawns & file_bb(ksq)) ? Value(5) : Value(-5);
237237

238-
if ((theirPawns & (FileABB | FileHBB) & BlockRanks) & (ksq + Up))
238+
if ((shift<Down>(theirPawns) & (FileABB | FileHBB) & BlockRanks) & ksq)
239239
safety += 374;
240240

241241
File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));

0 commit comments

Comments
 (0)