From 9f42980dd2c6109aeb4a56b2f717c223e569e052 Mon Sep 17 00:00:00 2001 From: KazApps Date: Tue, 20 Jan 2026 11:23:49 +0900 Subject: [PATCH] refactor update_piece_threats to reduce branching Passed STC Non-Regression: https://tests.stockfishchess.org/tests/view/696f1a398b64097dacd231c3 LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 122272 W: 31587 L: 31466 D: 59219 Ptnml(0-2): 301, 13358, 33750, 13373, 354 slight speedup as well: 1 thread bench: sf_base = 2238429 +/- 1221 (95%) sf_test = 2248298 +/- 1371 (95%) diff = 9869 +/- 1571 (95%) speedup = 0.44090% +/- 0.070% (95%) 32 thread speedtest: sf_base = 41016996 +/- 83654 (95%) sf_test = 41185801 +/- 84269 (95%) diff = 168805 +/- 79986 (95%) speedup = 0.41155% +/- 0.195% (95%) closes https://github.com/official-stockfish/Stockfish/pull/6559 No functional change --- src/position.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index 3bbe60d6d..a8d3cc506 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1069,8 +1069,8 @@ inline void add_dirty_threat( DirtyThreats* const dts, Piece pc, Piece threatened, Square s, Square threatenedSq) { if (PutPiece) { - dts->threatenedSqs |= square_bb(threatenedSq); - dts->threateningSqs |= square_bb(s); + dts->threatenedSqs |= threatenedSq; + dts->threateningSqs |= s; } dts->list.push_back({pc, threatened, s, threatenedSq, PutPiece}); @@ -1139,30 +1139,25 @@ void Position::update_piece_threats(Piece pc, | (attacks_bb(s, BLACK) & whitePawns) | (PseudoAttacks[KING][s] & kings); #ifdef USE_AVX512ICL - if (threatened) + if constexpr (PutPiece) { - if constexpr (PutPiece) - { - dts->threatenedSqs |= threatened; - dts->threateningSqs |= square_bb(s); - } - - DirtyThreat dt_template{pc, NO_PIECE, s, Square(0), PutPiece}; - write_multiple_dirties( - *this, threatened, dt_template, dts); + dts->threatenedSqs |= threatened; + dts->threateningSqs |= s; } + DirtyThreat dt_template{pc, NO_PIECE, s, Square(0), PutPiece}; + write_multiple_dirties( + *this, threatened, dt_template, dts); + Bitboard all_attackers = sliders | incoming_threats; - if (!all_attackers) - return; // Square s is threatened iff there's at least one attacker if constexpr (PutPiece) { - dts->threatenedSqs |= square_bb(s); + dts->threatenedSqs |= s; dts->threateningSqs |= all_attackers; } - DirtyThreat dt_template{NO_PIECE, pc, Square(0), s, PutPiece}; + dt_template = {NO_PIECE, pc, Square(0), s, PutPiece}; write_multiple_dirties(*this, all_attackers, dt_template, dts); #else