Skip to content

Commit c3d2e6a

Browse files
snicoletzamar
authored andcommitted
Helper functions to count material for both sides
Syntactic sugar: helper functions to count material or pieces for both sides. No functional change Closes #1025
1 parent d490bb9 commit c3d2e6a

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/evaluate.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ namespace {
179179
S( 9, 10), S( 2, 10), S( 1, -8), S(-20,-12),
180180
S(-20,-12), S( 1, -8), S( 2, 10), S( 9, 10)
181181
};
182-
182+
183183
// Protector[PieceType-2][distance] contains a protecting bonus for our king,
184184
// indexed by piece type and distance between the piece and the king.
185185
const Score Protector[4][8] = {
@@ -302,7 +302,7 @@ namespace {
302302
int mob = popcount(b & ei.mobilityArea[Us]);
303303

304304
mobility[Us] += MobilityBonus[Pt-2][mob];
305-
305+
306306
// Bonus for this piece as a king protector
307307
score += Protector[Pt-2][distance(s, pos.square<KING>(Us))];
308308

@@ -740,7 +740,7 @@ namespace {
740740

741741
int kingDistance = distance<File>(pos.square<KING>(WHITE), pos.square<KING>(BLACK))
742742
- distance<Rank>(pos.square<KING>(WHITE), pos.square<KING>(BLACK));
743-
int pawns = pos.count<PAWN>(WHITE) + pos.count<PAWN>(BLACK);
743+
int pawns = pos.count<PAWN>();
744744
bool bothFlanks = (pos.pieces(PAWN) & QueenSide) && (pos.pieces(PAWN) & KingSide);
745745

746746
// Compute the initiative bonus for the attacking side
@@ -847,7 +847,7 @@ Value Eval::evaluate(const Position& pos) {
847847
- evaluate_passer_pawns<BLACK, DoTrace>(pos, ei);
848848

849849
// Evaluate space for both sides, only during opening
850-
if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 12222)
850+
if (pos.non_pawn_material() >= 12222)
851851
score += evaluate_space<WHITE>(pos, ei)
852852
- evaluate_space<BLACK>(pos, ei);
853853

@@ -870,7 +870,7 @@ Value Eval::evaluate(const Position& pos) {
870870
Trace::add(IMBALANCE, ei.me->imbalance());
871871
Trace::add(PAWN, ei.pe->pawns_score());
872872
Trace::add(MOBILITY, mobility[WHITE], mobility[BLACK]);
873-
if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 12222)
873+
if (pos.non_pawn_material() >= 12222)
874874
Trace::add(SPACE, evaluate_space<WHITE>(pos, ei)
875875
, evaluate_space<BLACK>(pos, ei));
876876
Trace::add(TOTAL, score);

src/position.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class Position {
9090
Square ep_square() const;
9191
bool empty(Square s) const;
9292
template<PieceType Pt> int count(Color c) const;
93+
template<PieceType Pt> int count() const;
9394
template<PieceType Pt> const Square* squares(Color c) const;
9495
template<PieceType Pt> Square square(Color c) const;
9596

@@ -154,6 +155,7 @@ class Position {
154155
int rule50_count() const;
155156
Score psq_score() const;
156157
Value non_pawn_material(Color c) const;
158+
Value non_pawn_material() const;
157159

158160
// Position consistency check, for debugging
159161
bool pos_is_ok(int* failedStep = nullptr) const;
@@ -236,6 +238,10 @@ template<PieceType Pt> inline int Position::count(Color c) const {
236238
return pieceCount[make_piece(c, Pt)];
237239
}
238240

241+
template<PieceType Pt> inline int Position::count() const {
242+
return pieceCount[make_piece(WHITE, Pt)] + pieceCount[make_piece(BLACK, Pt)];
243+
}
244+
239245
template<PieceType Pt> inline const Square* Position::squares(Color c) const {
240246
return pieceList[make_piece(c, Pt)];
241247
}
@@ -330,6 +336,10 @@ inline Value Position::non_pawn_material(Color c) const {
330336
return st->nonPawnMaterial[c];
331337
}
332338

339+
inline Value Position::non_pawn_material() const {
340+
return st->nonPawnMaterial[WHITE] + st->nonPawnMaterial[BLACK];
341+
}
342+
333343
inline int Position::game_ply() const {
334344
return gamePly;
335345
}

src/search.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ namespace {
643643
// Step 4a. Tablebase probe
644644
if (!rootNode && TB::Cardinality)
645645
{
646-
int piecesCount = pos.count<ALL_PIECES>(WHITE) + pos.count<ALL_PIECES>(BLACK);
646+
int piecesCount = pos.count<ALL_PIECES>();
647647

648648
if ( piecesCount <= TB::Cardinality
649649
&& (piecesCount < TB::Cardinality || depth >= TB::ProbeDepth)
@@ -900,7 +900,7 @@ namespace {
900900
{
901901
if ( !captureOrPromotion
902902
&& !givesCheck
903-
&& (!pos.advanced_pawn_push(move) || pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 5000))
903+
&& (!pos.advanced_pawn_push(move) || pos.non_pawn_material() >= 5000))
904904
{
905905
// Move count based pruning
906906
if (moveCountPruning)

0 commit comments

Comments
 (0)