Filter invalid threat pairs early

Passed STC:
LLR: 2.94 (-2.94,2.94) <0.00,2.00>
Total: 246400 W: 63981 L: 63391 D: 119028
Ptnml(0-2): 612, 26055, 69299, 26599, 635
https://tests.stockfishchess.org/tests/view/6a3fafbf3036e45021aebaa0

Measured 1% speedup locally on x86-64-avx512icl build (bench 512 1 16, 50 runs):

PASSED: speedup = +0.0104, P(speedup > 0) = 1.0000

closes https://github.com/official-stockfish/Stockfish/pull/6936

No functional change
This commit is contained in:
Syine Mineta
2026-07-03 20:22:16 +02:00
committed by Joost VandeVondele
parent d91c7f6eac
commit 83514e4970
2 changed files with 47 additions and 19 deletions
+37 -14
View File
@@ -1167,6 +1167,10 @@ void write_multiple_dirties(const Position& p,
}
#endif
constexpr bool can_slider_threat(Piece pc, Piece slider) {
return type_of(pc) != QUEEN || type_of(slider) == QUEEN;
}
template<bool ComputeRay>
void Position::update_piece_threats(Piece pc,
bool putPiece,
@@ -1179,11 +1183,12 @@ void Position::update_piece_threats(Piece pc,
const Bitboard bishopQueens = pieces(BISHOP, QUEEN);
const Bitboard rAttacks = attacks_bb<ROOK>(s, occupied);
const Bitboard bAttacks = attacks_bb<BISHOP>(s, occupied);
const Bitboard kings = pieces(KING);
Bitboard occupiedNoK = occupied ^ kings;
const Bitboard occupiedNoK = occupied ^ pieces(KING);
Bitboard sliders = (rookQueens & rAttacks) | (bishopQueens & bAttacks);
auto process_sliders = [&](bool addDirectAttacks) {
Bitboard sliders = (rookQueens & rAttacks) | (bishopQueens & bAttacks);
Bitboard directSliders = type_of(pc) == QUEEN ? sliders & pieces(QUEEN) : sliders;
auto process_sliders = [&](bool addDirectAttacks) {
while (sliders)
{
Square sliderSq = pop_lsb(sliders);
@@ -1197,10 +1202,11 @@ void Position::update_piece_threats(Piece pc,
{
const Square threatenedSq = lsb(discovered);
const Piece threatenedPc = piece_on(threatenedSq);
add_dirty_threat(dts, !putPiece, slider, threatenedPc, sliderSq, threatenedSq);
if (can_slider_threat(threatenedPc, slider))
add_dirty_threat(dts, !putPiece, slider, threatenedPc, sliderSq, threatenedSq);
}
if (addDirectAttacks)
if (addDirectAttacks && can_slider_threat(pc, slider))
add_dirty_threat(dts, putPiece, slider, pc, sliderSq, s);
}
};
@@ -1218,12 +1224,12 @@ void Position::update_piece_threats(Piece pc,
const Bitboard blackPawns = pieces(BLACK, PAWN);
Bitboard threatened = attacks_bb(pc, s, occupied) & occupiedNoK;
Bitboard incoming_threats =
(PseudoAttacks[KNIGHT][s] & knights) | (PseudoAttacks[KING][s] & kings);
Bitboard threatened = attacks_bb(pc, s, occupied) & occupiedNoK;
Bitboard incoming_threats = PseudoAttacks[KNIGHT][s] & knights;
// Compute both incoming and outgoing pawn threats. Incoming pawn pushers are only
// added if 'pc' is a pawn.
Bitboard pawnThreats = 0;
if (type_of(pc) == PAWN)
{
Bitboard whiteAttacks = PawnPushOrAttacks[WHITE][s];
@@ -1231,21 +1237,38 @@ void Position::update_piece_threats(Piece pc,
threatened |= (color_of(pc) == WHITE ? whiteAttacks : blackAttacks) & pieces(PAWN);
incoming_threats |= whiteAttacks & blackPawns;
incoming_threats |= blackAttacks & whitePawns;
pawnThreats = whiteAttacks & blackPawns;
pawnThreats |= blackAttacks & whitePawns;
}
else
{
incoming_threats |=
pawnThreats =
(attacks_bb<PAWN>(s, WHITE) & blackPawns) | (attacks_bb<PAWN>(s, BLACK) & whitePawns);
}
if (type_of(pc) == PAWN || type_of(pc) == KNIGHT || type_of(pc) == ROOK)
incoming_threats |= pawnThreats;
switch (type_of(pc))
{
case PAWN :
threatened &= pieces(PAWN, KNIGHT, ROOK);
break;
case BISHOP :
case ROOK :
threatened &= pieces(PAWN, KNIGHT, BISHOP, ROOK);
break;
default :
threatened &= occupiedNoK;
break;
}
#ifdef USE_AVX512ICL
DirtyThreat dt_template{pc, NO_PIECE, s, Square(0), putPiece};
write_multiple_dirties<DirtyThreat::ThreatenedSqOffset, DirtyThreat::ThreatenedPcOffset>(
*this, threatened, dt_template, dts);
Bitboard all_attackers = sliders | incoming_threats;
Bitboard all_attackers = directSliders | incoming_threats;
dt_template = {NO_PIECE, pc, Square(0), s, putPiece};
write_multiple_dirties<DirtyThreat::PcSqOffset, DirtyThreat::PcOffset>(*this, all_attackers,
@@ -1273,7 +1296,7 @@ void Position::update_piece_threats(Piece pc,
}
else
{
incoming_threats |= sliders;
incoming_threats |= directSliders;
}
#ifndef USE_AVX512ICL