Skip to content

Commit aecdbfc

Browse files
glinscottmcostalba
authored andcommitted
Add lsb() overload
Helper to find least significant bit relative to the given color. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
1 parent e6482b7 commit aecdbfc

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

src/bitboard.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,9 @@ extern Square pop_lsb(Bitboard* b);
319319

320320
#endif
321321

322+
/// lsb() overload finds least significant bit relative to the given color
323+
inline Square lsb(Color c, Bitboard b) {
324+
return c == WHITE ? lsb(b) : msb(b);
325+
}
326+
322327
#endif // #ifndef BITBOARD_H_INCLUDED

src/endgame.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -443,18 +443,7 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
443443
// The bishop has the wrong color, and the defending king is on the
444444
// file of the pawn(s) or the adjacent file. Find the rank of the
445445
// frontmost pawn.
446-
Rank rank;
447-
if (strongerSide == WHITE)
448-
{
449-
for (rank = RANK_7; !(rank_bb(rank) & pawns); rank--) {}
450-
assert(rank >= RANK_2 && rank <= RANK_7);
451-
}
452-
else
453-
{
454-
for (rank = RANK_2; !(rank_bb(rank) & pawns); rank++) {}
455-
rank = Rank(rank ^ 7); // HACK to get the relative rank
456-
assert(rank >= RANK_2 && rank <= RANK_7);
457-
}
446+
Rank rank = relative_rank(strongerSide, lsb(weakerSide, pawns));
458447
// If the defending king has distance 1 to the promotion square or
459448
// is placed somewhere in front of the pawn, it's a draw.
460449
if ( square_distance(kingSq, queeningSq) <= 1
@@ -469,9 +458,8 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
469458
&& pos.non_pawn_material(weakerSide) == 0
470459
&& pos.count<PAWN>(weakerSide) >= 1)
471460
{
472-
// Get weaker pawn closest to opponent's queening square
473-
Bitboard wkPawns = pos.pieces(weakerSide, PAWN);
474-
Square weakerPawnSq = strongerSide == WHITE ? msb(wkPawns) : lsb(wkPawns);
461+
// Get weakerSide pawn that is closest to home rank
462+
Square weakerPawnSq = lsb(weakerSide, pos.pieces(weakerSide, PAWN));
475463

476464
Square strongerKingSq = pos.king_square(strongerSide);
477465
Square weakerKingSq = pos.king_square(weakerSide);

src/pawns.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) {
229229
for (int f = kf - 1; f <= kf + 1; f++)
230230
{
231231
b = ourPawns & FileBB[f];
232-
rkUs = b ? relative_rank(Us, Us == WHITE ? lsb(b) : msb(b)) : RANK_1;
232+
rkUs = b ? relative_rank(Us, lsb(Us, b)) : RANK_1;
233233
safety -= ShelterWeakness[rkUs];
234234

235235
b = theirPawns & FileBB[f];
236-
rkThem = b ? relative_rank(Us, Us == WHITE ? lsb(b) : msb(b)) : RANK_1;
236+
rkThem = b ? relative_rank(Us, lsb(Us, b)) : RANK_1;
237237
safety -= StormDanger[rkUs == RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem];
238238
}
239239

0 commit comments

Comments
 (0)