X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fevaluate.cpp;h=132b8c1b4b7a5e6c3f081c94acef00b814f3a82a;hp=cb03ba25ced51d02b0b9b8ca7914d59ff486ac9e;hb=f00c976bb22bc4dbcb2dbb0801f93fc8b6553a90;hpb=045beac156a97669d3d4ddbc53eceae4be1e5186 diff --git a/src/evaluate.cpp b/src/evaluate.cpp index cb03ba25..132b8c1b 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -45,11 +45,6 @@ namespace { // Pointer to pawn hash table entry PawnInfo* pi; - // updateKingTables[color] is set to true if we have enough material - // to trigger the opponent's king safety calculation. When is false we - // skip the time consuming update of the king attackers tables. - bool updateKingTables[2]; - // attackedBy[color][piece type] is a bitboard representing all squares // attacked by a given color and piece type, attackedBy[color][0] contains // all squares attacked by the given color. @@ -133,18 +128,14 @@ namespace { V(0), V(0), V(4), V(8), V(8), V(4), V(0), V(0), V(0), V(4),V(17),V(26),V(26),V(17), V(4), V(0), V(0), V(8),V(26),V(35),V(35),V(26), V(8), V(0), - V(0), V(4),V(17),V(17),V(17),V(17), V(4), V(0), - V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), - V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, + V(0), V(4),V(17),V(17),V(17),V(17), V(4), V(0) }, { V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // Bishops V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(5), V(5), V(5), V(5), V(0), V(0), V(0), V(5),V(10),V(10),V(10),V(10), V(5), V(0), V(0),V(10),V(21),V(21),V(21),V(21),V(10), V(0), - V(0), V(5), V(8), V(8), V(8), V(8), V(5), V(0), - V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), - V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) } + V(0), V(5), V(8), V(8), V(8), V(8), V(5), V(0) } }; // ThreatBonus[attacking][attacked] contains threat bonuses according to @@ -335,10 +326,8 @@ Value do_evaluate(const Position& pos, Value& margin) { bonus += evaluate_passed_pawns(pos, ei) - evaluate_passed_pawns(pos, ei); - Phase phase = mi->game_phase(); - // Evaluate space for both sides, only in middle-game. - if (phase > PHASE_ENDGAME && mi->space_weight() > 0) + if (mi->space_weight()) { int s = evaluate_space(pos, ei) - evaluate_space(pos, ei); bonus += apply_weight(make_score(s * mi->space_weight(), 0), Weights[Space]); @@ -347,6 +336,7 @@ Value do_evaluate(const Position& pos, Value& margin) { // Scale winning side if position is more drawish that what it appears ScaleFactor sf = eg_value(bonus) > VALUE_ZERO ? mi->scale_factor(pos, WHITE) : mi->scale_factor(pos, BLACK); + Phase phase = mi->game_phase(); // If we don't already have an unusual scale factor, check for opposite // colored bishop endgames, and use a lower scale for those. @@ -443,18 +433,22 @@ namespace { template void init_eval_info(const Position& pos, EvalInfo& ei) { + const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15; const Color Them = (Us == WHITE ? BLACK : WHITE); Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from(pos.king_square(Them)); - ei.kingZone[Us] = (b | (Us == WHITE ? b >> 8 : b << 8)); ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us); - ei.updateKingTables[Us] = pos.piece_count(Us, QUEEN) && pos.non_pawn_material(Us) >= QueenValueMidgame + RookValueMidgame; - if (ei.updateKingTables[Us]) + + // Init king safety tables only if we are going to use them + if ( pos.piece_count(Us, QUEEN) + && pos.non_pawn_material(Us) >= QueenValueMidgame + RookValueMidgame) { + ei.kingZone[Us] = (b | (Us == WHITE ? b >> 8 : b << 8)); b &= ei.attackedBy[Us][PAWN]; - ei.kingAttackersCount[Us] = b ? count_1s_max_15(b) / 2 : EmptyBoardBB; - ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = EmptyBoardBB; - } + ei.kingAttackersCount[Us] = b ? count_1s(b) / 2 : 0; + ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0; + } else + ei.kingZone[Us] = ei.kingAttackersCount[Us] = 0; } @@ -495,6 +489,8 @@ namespace { File f; Score bonus = SCORE_ZERO; + const BitCountType Full = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64 : CNT32; + const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15; const Color Them = (Us == WHITE ? BLACK : WHITE); const Square* ptr = pos.piece_list_begin(Us, Piece); @@ -516,18 +512,18 @@ namespace { ei.attackedBy[Us][Piece] |= b; // King attacks - if (ei.updateKingTables[Us] && (b & ei.kingZone[Us])) + if (b & ei.kingZone[Us]) { ei.kingAttackersCount[Us]++; ei.kingAttackersWeight[Us] += KingAttackWeights[Piece]; Bitboard bb = (b & ei.attackedBy[Them][KING]); if (bb) - ei.kingAdjacentZoneAttacksCount[Us] += count_1s_max_15(bb); + ei.kingAdjacentZoneAttacksCount[Us] += count_1s(bb); } // Mobility - mob = (Piece != QUEEN ? count_1s_max_15(b & mobilityArea) - : count_1s(b & mobilityArea)); + mob = (Piece != QUEEN ? count_1s(b & mobilityArea) + : count_1s(b & mobilityArea)); mobility += MobilityBonus[Piece][mob]; @@ -656,6 +652,7 @@ namespace { template Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin) { + const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15; const Color Them = (Us == WHITE ? BLACK : WHITE); Bitboard undefended, b, b1, b2, safe; @@ -667,8 +664,7 @@ namespace { // King safety. This is quite complicated, and is almost certainly far // from optimally tuned. - if ( ei.updateKingTables[Them] - && ei.kingAttackersCount[Them] >= 2 + if ( ei.kingAttackersCount[Them] >= 2 && ei.kingAdjacentZoneAttacksCount[Them]) { // Find the attacked squares around the king which has no defenders @@ -684,7 +680,7 @@ namespace { // attacked and undefended squares around our king, the square of the // king, and the quality of the pawn shelter. attackUnits = Min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2) - + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s_max_15(undefended)) + + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s(undefended)) + InitKingDanger[relative_square(Us, ksq)] - mg_value(ei.pi->king_shelter(pos, ksq)) / 32; @@ -698,7 +694,7 @@ namespace { | ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK]); if (b) attackUnits += QueenContactCheckBonus - * count_1s_max_15(b) + * count_1s(b) * (Them == pos.side_to_move() ? 2 : 1); } @@ -716,7 +712,7 @@ namespace { | ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][QUEEN]); if (b) attackUnits += RookContactCheckBonus - * count_1s_max_15(b) + * count_1s(b) * (Them == pos.side_to_move() ? 2 : 1); } @@ -729,22 +725,22 @@ namespace { // Enemy queen safe checks b = (b1 | b2) & ei.attackedBy[Them][QUEEN]; if (b) - attackUnits += QueenCheckBonus * count_1s_max_15(b); + attackUnits += QueenCheckBonus * count_1s(b); // Enemy rooks safe checks b = b1 & ei.attackedBy[Them][ROOK]; if (b) - attackUnits += RookCheckBonus * count_1s_max_15(b); + attackUnits += RookCheckBonus * count_1s(b); // Enemy bishops safe checks b = b2 & ei.attackedBy[Them][BISHOP]; if (b) - attackUnits += BishopCheckBonus * count_1s_max_15(b); + attackUnits += BishopCheckBonus * count_1s(b); // Enemy knights safe checks b = pos.attacks_from(ksq) & ei.attackedBy[Them][KNIGHT] & safe; if (b) - attackUnits += KnightCheckBonus * count_1s_max_15(b); + attackUnits += KnightCheckBonus * count_1s(b); // To index KingDangerTable[] attackUnits must be in [0, 99] range attackUnits = Min(99, Max(0, attackUnits)); @@ -869,6 +865,7 @@ namespace { template int evaluate_space(const Position& pos, EvalInfo& ei) { + const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15; const Color Them = (Us == WHITE ? BLACK : WHITE); // Find the safe squares for our pieces inside the area defined by @@ -884,7 +881,7 @@ namespace { behind |= (Us == WHITE ? behind >> 8 : behind << 8); behind |= (Us == WHITE ? behind >> 16 : behind << 16); - return count_1s_max_15(safe) + count_1s_max_15(behind & safe); + return count_1s(safe) + count_1s(behind & safe); }