From: Marco Costalba Date: Wed, 24 Sep 2008 07:49:18 +0000 (+0200) Subject: Group common evaluate code X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=853ce65f178204f0c88098f937d759dd40c4c3c9 Group common evaluate code This removes code redundancy but perhaps impact performance due to uninlining. Testing for regression is needed. For now aim to best code readibility. Signed-off-by: Marco Costalba --- diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 67d5be24..6f4de561 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -571,27 +571,41 @@ void read_weights(Color sideToMove) { namespace { - // evaluate_knight() assigns bonuses and penalties to a knight of a given - // color on a given square. + // evaluate_common() computes terms common to all pieces attack - void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) { + int evaluate_common(const Position&p, const Bitboard& b, Color us, EvalInfo& ei, + int AttackWeight, const Value* mgBonus, const Value* egBonus) { Color them = opposite_color(us); - Bitboard b = p.knight_attacks(s); - ei.attackedBy[us][KNIGHT] |= b; // King attack if(b & ei.attackZone[us]) { ei.attackCount[us]++; - ei.attackWeight[us] += KnightAttackWeight; + ei.attackWeight[us] += AttackWeight; Bitboard bb = (b & ei.attackedBy[them][KING]); if(bb) ei.attacked[us] += count_1s_max_15(bb); } // Mobility int mob = count_1s_max_15(b & ~p.pieces_of_color(us)); - ei.mgMobility += Sign[us] * MidgameKnightMobilityBonus[mob]; - ei.egMobility += Sign[us] * EndgameKnightMobilityBonus[mob]; + ei.mgMobility += Sign[us] * mgBonus[mob]; + ei.egMobility += Sign[us] * egBonus[mob]; + + return mob; + } + + // evaluate_knight() assigns bonuses and penalties to a knight of a given + // color on a given square. + + void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) { + + Color them = opposite_color(us); + Bitboard b = p.knight_attacks(s); + ei.attackedBy[us][KNIGHT] |= b; + + // King attack and mobility + evaluate_common(p, b, us, ei, KnightAttackWeight, + MidgameKnightMobilityBonus, EndgameKnightMobilityBonus); // Knight outposts: if(p.square_is_weak(s, them)) { @@ -628,18 +642,9 @@ namespace { ei.attackedBy[us][BISHOP] |= b; - // King attack - if(b & ei.attackZone[us]) { - ei.attackCount[us]++; - ei.attackWeight[us] += BishopAttackWeight; - Bitboard bb = (b & ei.attackedBy[them][KING]); - if(bb) ei.attacked[us] += count_1s_max_15(bb); - } - - // Mobility: - int mob = count_1s_max_15(b & ~p.pieces_of_color(us)); - ei.mgMobility += Sign[us] * MidgameBishopMobilityBonus[mob]; - ei.egMobility += Sign[us] * EndgameBishopMobilityBonus[mob]; + // King attack and mobility + evaluate_common(p, b, us, ei, BishopAttackWeight, + MidgameBishopMobilityBonus, EndgameBishopMobilityBonus); // Bishop outposts: if(p.square_is_weak(s, them)) { @@ -697,18 +702,9 @@ namespace { rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us)); ei.attackedBy[us][ROOK] |= b; - // King attack - if(b & ei.attackZone[us]) { - ei.attackCount[us]++; - ei.attackWeight[us] += RookAttackWeight; - Bitboard bb = (b & ei.attackedBy[them][KING]); - if(bb) ei.attacked[us] += count_1s_max_15(bb); - } - - // Mobility - int mob = count_1s_max_15(b & ~p.pieces_of_color(us)); - ei.mgMobility += Sign[us] * MidgameRookMobilityBonus[mob]; - ei.egMobility += Sign[us] * EndgameRookMobilityBonus[mob]; + // King attack and mobility + int mob = evaluate_common(p, b, us, ei, RookAttackWeight, + MidgameRookMobilityBonus, EndgameRookMobilityBonus); // Penalize rooks which are trapped inside a king which has lost the // right to castle: @@ -756,18 +752,9 @@ namespace { Bitboard b = p.queen_attacks(s); ei.attackedBy[us][QUEEN] |= b; - // King attack - if(b & ei.attackZone[us]) { - ei.attackCount[us]++; - ei.attackWeight[us] += QueenAttackWeight; - Bitboard bb = (b & ei.attackedBy[them][KING]); - if(bb) ei.attacked[us] += count_1s_max_15(bb); - } - - // Mobility - int mob = count_1s(b & ~p.pieces_of_color(us)); - ei.mgMobility += Sign[us] * MidgameQueenMobilityBonus[mob]; - ei.egMobility += Sign[us] * EndgameQueenMobilityBonus[mob]; + // King attack and mobility + evaluate_common(p, b, us, ei, QueenAttackWeight, + MidgameQueenMobilityBonus, EndgameQueenMobilityBonus); }