Skip to content

Commit f5e872a

Browse files
committed
Some evaluation code reshuffle
No functional change.
1 parent 25cb851 commit f5e872a

File tree

1 file changed

+74
-76
lines changed

1 file changed

+74
-76
lines changed

src/evaluate.cpp

Lines changed: 74 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ namespace {
232232
void init_eval_info(const Position& pos, EvalInfo& ei);
233233

234234
template<Color Us, bool Trace>
235-
Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility);
235+
Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score* mobility);
236236

237237
template<Color Us, bool Trace>
238238
Score evaluate_king(const Position& pos, const EvalInfo& ei, Value margins[]);
@@ -310,7 +310,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
310310

311311
EvalInfo ei;
312312
Value margins[COLOR_NB];
313-
Score score, mobilityWhite, mobilityBlack;
313+
Score score, mobility[2] = { SCORE_ZERO, SCORE_ZERO };
314314
Thread* th = pos.this_thread();
315315

316316
// margins[] store the uncertainty estimation of position's evaluation
@@ -343,10 +343,10 @@ Value do_evaluate(const Position& pos, Value& margin) {
343343
init_eval_info<BLACK>(pos, ei);
344344

345345
// Evaluate pieces and mobility
346-
score += evaluate_pieces_of_color<WHITE, Trace>(pos, ei, mobilityWhite)
347-
- evaluate_pieces_of_color<BLACK, Trace>(pos, ei, mobilityBlack);
346+
score += evaluate_pieces_of_color<WHITE, Trace>(pos, ei, mobility)
347+
- evaluate_pieces_of_color<BLACK, Trace>(pos, ei, mobility);
348348

349-
score += apply_weight(mobilityWhite - mobilityBlack, Weights[Mobility]);
349+
score += apply_weight(mobility[WHITE] - mobility[BLACK], Weights[Mobility]);
350350

351351
// Evaluate kings after all other pieces because we need complete attack
352352
// information when computing the king safety evaluation.
@@ -443,7 +443,8 @@ Value do_evaluate(const Position& pos, Value& margin) {
443443
b &= ei.attackedBy[Us][PAWN];
444444
ei.kingAttackersCount[Us] = b ? popcount<Max15>(b) / 2 : 0;
445445
ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0;
446-
} else
446+
}
447+
else
447448
ei.kingRing[Them] = ei.kingAttackersCount[Us] = 0;
448449
}
449450

@@ -470,14 +471,15 @@ Value do_evaluate(const Position& pos, Value& margin) {
470471
else
471472
bonus += bonus / 2;
472473
}
474+
473475
return make_score(bonus, bonus);
474476
}
475477

476478

477-
// evaluate_pieces<>() assigns bonuses and penalties to the pieces of a given color
479+
// evaluate_pieces() assigns bonuses and penalties to the pieces of a given color
478480

479481
template<PieceType Piece, Color Us, bool Trace>
480-
Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score& mobility, Bitboard mobilityArea) {
482+
Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score* mobility, Bitboard mobilityArea) {
481483

482484
Bitboard b;
483485
Square s;
@@ -499,17 +501,15 @@ Value do_evaluate(const Position& pos, Value& margin) {
499501

500502
if (b & ei.kingRing[Them])
501503
{
502-
++ei.kingAttackersCount[Us];
504+
ei.kingAttackersCount[Us]++;
503505
ei.kingAttackersWeight[Us] += KingAttackWeights[Piece];
504-
Bitboard bb = (b & ei.attackedBy[Them][KING]);
506+
Bitboard bb = b & ei.attackedBy[Them][KING];
505507
if (bb)
506508
ei.kingAdjacentZoneAttacksCount[Us] += popcount<Max15>(bb);
507509
}
508510

509-
int mob = Piece != QUEEN ? popcount<Max15>(b & mobilityArea)
510-
: popcount<Full >(b & mobilityArea);
511-
512-
mobility += MobilityBonus[Piece][mob];
511+
int mob = popcount<Piece == QUEEN ? Full : Max15>(b & mobilityArea);
512+
mobility[Us] += MobilityBonus[Piece][mob];
513513

514514
// Decrease score if we are attacked by an enemy pawn. Remaining part
515515
// of threat evaluation must be done later when we have full attack info.
@@ -596,79 +596,34 @@ Value do_evaluate(const Position& pos, Value& margin) {
596596
}
597597

598598

599-
// evaluate_threats<>() assigns bonuses according to the type of attacking piece
600-
// and the type of attacked one.
601-
602-
template<Color Us, bool Trace>
603-
Score evaluate_threats(const Position& pos, const EvalInfo& ei) {
604-
605-
const Color Them = (Us == WHITE ? BLACK : WHITE);
606-
607-
Bitboard b, undefendedMinors, weakEnemies;
608-
Score score = SCORE_ZERO;
609-
610-
// Undefended minors get penalized even if not under attack
611-
undefendedMinors = pos.pieces(Them, BISHOP, KNIGHT)
612-
& ~ei.attackedBy[Them][ALL_PIECES];
613-
614-
if (undefendedMinors)
615-
score += UndefendedMinor;
616-
617-
// Enemy pieces not defended by a pawn and under our attack
618-
weakEnemies = pos.pieces(Them)
619-
& ~ei.attackedBy[Them][PAWN]
620-
& ei.attackedBy[Us][ALL_PIECES];
621-
622-
// Add bonus according to type of attacked enemy piece and to the
623-
// type of attacking piece, from knights to queens. Kings are not
624-
// considered because are already handled in king evaluation.
625-
if (weakEnemies)
626-
for (PieceType pt1 = KNIGHT; pt1 < KING; ++pt1)
627-
{
628-
b = ei.attackedBy[Us][pt1] & weakEnemies;
629-
if (b)
630-
for (PieceType pt2 = PAWN; pt2 < KING; ++pt2)
631-
if (b & pos.pieces(pt2))
632-
score += Threat[pt1][pt2];
633-
}
634-
635-
if (Trace)
636-
Tracing::scores[Us][THREAT] = score;
637-
638-
return score;
639-
}
640-
641-
642-
// evaluate_pieces_of_color<>() assigns bonuses and penalties to all the
599+
// evaluate_pieces_of_color() assigns bonuses and penalties to all the
643600
// pieces of a given color.
644601

645602
template<Color Us, bool Trace>
646-
Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility) {
603+
Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score* mobility) {
647604

648605
const Color Them = (Us == WHITE ? BLACK : WHITE);
649606

650-
Score score = mobility = SCORE_ZERO;
651-
652607
// Do not include in mobility squares protected by enemy pawns or occupied by our pieces
653608
const Bitboard mobilityArea = ~(ei.attackedBy[Them][PAWN] | pos.pieces(Us, PAWN, KING));
654609

655-
score += evaluate_pieces<KNIGHT, Us, Trace>(pos, ei, mobility, mobilityArea);
656-
score += evaluate_pieces<BISHOP, Us, Trace>(pos, ei, mobility, mobilityArea);
657-
score += evaluate_pieces<ROOK, Us, Trace>(pos, ei, mobility, mobilityArea);
658-
score += evaluate_pieces<QUEEN, Us, Trace>(pos, ei, mobility, mobilityArea);
610+
Score score = evaluate_pieces<KNIGHT, Us, Trace>(pos, ei, mobility, mobilityArea)
611+
+ evaluate_pieces<BISHOP, Us, Trace>(pos, ei, mobility, mobilityArea)
612+
+ evaluate_pieces<ROOK, Us, Trace>(pos, ei, mobility, mobilityArea)
613+
+ evaluate_pieces<QUEEN, Us, Trace>(pos, ei, mobility, mobilityArea);
659614

660-
// Sum up all attacked squares
661-
ei.attackedBy[Us][ALL_PIECES] = ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
662-
| ei.attackedBy[Us][BISHOP] | ei.attackedBy[Us][ROOK]
663-
| ei.attackedBy[Us][QUEEN] | ei.attackedBy[Us][KING];
615+
// Sum up all attacked squares (updated in evaluate_pieces)
616+
ei.attackedBy[Us][ALL_PIECES] = ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
617+
| ei.attackedBy[Us][BISHOP] | ei.attackedBy[Us][ROOK]
618+
| ei.attackedBy[Us][QUEEN] | ei.attackedBy[Us][KING];
664619
if (Trace)
665-
Tracing::scores[Us][MOBILITY] = apply_weight(mobility, Weights[Mobility]);
620+
Tracing::scores[Us][MOBILITY] = apply_weight(mobility[Us], Weights[Mobility]);
666621

667622
return score;
668623
}
669624

670625

671-
// evaluate_king<>() assigns bonuses and penalties to a king of a given color
626+
// evaluate_king() assigns bonuses and penalties to a king of a given color
672627

673628
template<Color Us, bool Trace>
674629
Score evaluate_king(const Position& pos, const EvalInfo& ei, Value margins[]) {
@@ -682,15 +637,15 @@ Value do_evaluate(const Position& pos, Value& margin) {
682637
// King shelter and enemy pawns storm
683638
Score score = ei.pi->king_safety<Us>(pos, ksq);
684639

685-
// King safety. This is quite complicated, and is almost certainly far
686-
// from optimally tuned.
640+
// Main king safety evaluation
687641
if ( ei.kingAttackersCount[Them] >= 2
688642
&& ei.kingAdjacentZoneAttacksCount[Them])
689643
{
690644
// Find the attacked squares around the king which has no defenders
691645
// apart from the king itself
692-
undefended = ei.attackedBy[Them][ALL_PIECES] & ei.attackedBy[Us][KING];
693-
undefended &= ~( ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
646+
undefended = ei.attackedBy[Them][ALL_PIECES]
647+
& ei.attackedBy[Us][KING]
648+
& ~( ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
694649
| ei.attackedBy[Us][BISHOP] | ei.attackedBy[Us][ROOK]
695650
| ei.attackedBy[Us][QUEEN]);
696651

@@ -781,7 +736,50 @@ Value do_evaluate(const Position& pos, Value& margin) {
781736
}
782737

783738

784-
// evaluate_passed_pawns<>() evaluates the passed pawns of the given color
739+
// evaluate_threats() assigns bonuses according to the type of attacking piece
740+
// and the type of attacked one.
741+
742+
template<Color Us, bool Trace>
743+
Score evaluate_threats(const Position& pos, const EvalInfo& ei) {
744+
745+
const Color Them = (Us == WHITE ? BLACK : WHITE);
746+
747+
Bitboard b, undefendedMinors, weakEnemies;
748+
Score score = SCORE_ZERO;
749+
750+
// Undefended minors get penalized even if not under attack
751+
undefendedMinors = pos.pieces(Them, BISHOP, KNIGHT)
752+
& ~ei.attackedBy[Them][ALL_PIECES];
753+
754+
if (undefendedMinors)
755+
score += UndefendedMinor;
756+
757+
// Enemy pieces not defended by a pawn and under our attack
758+
weakEnemies = pos.pieces(Them)
759+
& ~ei.attackedBy[Them][PAWN]
760+
& ei.attackedBy[Us][ALL_PIECES];
761+
762+
// Add bonus according to type of attacked enemy piece and to the
763+
// type of attacking piece, from knights to queens. Kings are not
764+
// considered because are already handled in king evaluation.
765+
if (weakEnemies)
766+
for (PieceType pt1 = KNIGHT; pt1 < KING; ++pt1)
767+
{
768+
b = ei.attackedBy[Us][pt1] & weakEnemies;
769+
if (b)
770+
for (PieceType pt2 = PAWN; pt2 < KING; ++pt2)
771+
if (b & pos.pieces(pt2))
772+
score += Threat[pt1][pt2];
773+
}
774+
775+
if (Trace)
776+
Tracing::scores[Us][THREAT] = score;
777+
778+
return score;
779+
}
780+
781+
782+
// evaluate_passed_pawns() evaluates the passed pawns of the given color
785783

786784
template<Color Us, bool Trace>
787785
Score evaluate_passed_pawns(const Position& pos, const EvalInfo& ei) {

0 commit comments

Comments
 (0)