Skip to content

Commit 43f6b33

Browse files
Rocky640mcostalba
authored andcommitted
Small eval cleanup and renaming
Non-functional changes a) splitting the threat array to avoid using an enum b) reorder the scores according to functions where they are used. c) declarations in evaluate_pieces after the const(s) like elsewhere d) more compact definitions of KingFlank, now that we need it also for the PanwLessFlank penalty. e) reuse CenterFiles in evaluate_space f) move one line inside next popcount No functional change.
1 parent ab4f498 commit 43f6b33

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

src/evaluate.cpp

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,18 @@ namespace {
159159
S(0, 0), S(0, 0), S(176, 139), S(131, 127), S(217, 218), S(203, 215)
160160
};
161161

162-
// Threat[by minor/by rook][attacked PieceType] contains
163-
// bonuses according to which piece type attacks which one.
164-
// Attacks on lesser pieces which are pawn-defended are not considered.
165-
const Score Threat[][PIECE_TYPE_NB] = {
166-
{ S(0, 0), S(0, 33), S(45, 43), S(46, 47), S(72,107), S(48,118) }, // by Minor
167-
{ S(0, 0), S(0, 25), S(40, 62), S(40, 59), S( 0, 34), S(35, 48) } // by Rook
162+
// ThreatByMinor/ByRook[attacked PieceType] contains bonuses according to
163+
// which piece type attacks which one. Attacks on lesser pieces which are
164+
// pawn-defended are not considered.
165+
const Score ThreatByMinor[PIECE_TYPE_NB] = {
166+
S(0, 0), S(0, 33), S(45, 43), S(46, 47), S(72, 107), S(48, 118)
168167
};
169168

170-
// ThreatByKing[on one/on many] contains bonuses for King attacks on
169+
const Score ThreatByRook[PIECE_TYPE_NB] = {
170+
S(0, 0), S(0, 25), S(40, 62), S(40, 59), S( 0, 34), S(35, 48)
171+
};
172+
173+
// ThreatByKing[on one/on many] contains bonuses for king attacks on
171174
// pawns or pieces which are not pawn-defended.
172175
const Score ThreatByKing[2] = { S(3, 62), S(9, 138) };
173176

@@ -181,24 +184,24 @@ namespace {
181184
// PassedFile[File] contains a bonus according to the file of a passed pawn
182185
const Score PassedFile[FILE_NB] = {
183186
S( 9, 10), S( 2, 10), S( 1, -8), S(-20,-12),
184-
S(-20,-12), S( 1, -8), S( 2, 10), S( 9, 10)
187+
S(-20,-12), S( 1, -8), S( 2, 10), S( 9, 10)
185188
};
186189

187190
// Assorted bonuses and penalties used by evaluation
188191
const Score MinorBehindPawn = S(16, 0);
189192
const Score BishopPawns = S( 8, 12);
190193
const Score RookOnPawn = S( 8, 24);
191194
const Score TrappedRook = S(92, 0);
192-
const Score CloseEnemies = S( 7, 0);
195+
const Score WeakQueen = S(50, 10);
193196
const Score OtherCheck = S(10, 10);
194-
const Score ThreatByHangingPawn = S(71, 61);
197+
const Score CloseEnemies = S( 7, 0);
198+
const Score PawnlessFlank = S(20, 80);
195199
const Score LooseEnemies = S( 0, 25);
196-
const Score WeakQueen = S(50, 10);
200+
const Score ThreatByHangingPawn = S(71, 61);
201+
const Score ThreatByRank = S(16, 3);
197202
const Score Hanging = S(48, 27);
198203
const Score ThreatByPawnPush = S(38, 22);
199-
const Score PawnlessFlank = S(20, 80);
200204
const Score HinderPassedPawn = S( 7, 0);
201-
const Score ThreatByRank = S(16, 3);
202205

203206
// Penalty for a bishop on a1/h1 (a8/h8 for black) which is trapped by
204207
// a friendly pawn on b2/g2 (b7/g7 for black). This can obviously only
@@ -238,8 +241,7 @@ namespace {
238241
if (pos.non_pawn_material(Us) >= QueenValueMg)
239242
{
240243
ei.kingRing[Them] = b | shift<Down>(b);
241-
b &= ei.attackedBy[Us][PAWN];
242-
ei.kingAttackersCount[Us] = popcount(b);
244+
ei.kingAttackersCount[Us] = popcount(b & ei.attackedBy[Us][PAWN]);
243245
ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0;
244246
}
245247
else
@@ -253,16 +255,16 @@ namespace {
253255
template<bool DoTrace, Color Us = WHITE, PieceType Pt = KNIGHT>
254256
Score evaluate_pieces(const Position& pos, EvalInfo& ei, Score* mobility,
255257
const Bitboard* mobilityArea) {
256-
Bitboard b, bb;
257-
Square s;
258-
Score score = SCORE_ZERO;
259-
260258
const PieceType NextPt = (Us == WHITE ? Pt : PieceType(Pt + 1));
261259
const Color Them = (Us == WHITE ? BLACK : WHITE);
262260
const Bitboard OutpostRanks = (Us == WHITE ? Rank4BB | Rank5BB | Rank6BB
263261
: Rank5BB | Rank4BB | Rank3BB);
264262
const Square* pl = pos.squares<Pt>(Us);
265263

264+
Bitboard b, bb;
265+
Square s;
266+
Score score = SCORE_ZERO;
267+
266268
ei.attackedBy[Us][Pt] = 0;
267269

268270
while ((s = *pl++) != SQ_NONE)
@@ -376,28 +378,24 @@ namespace {
376378

377379
// evaluate_king() assigns bonuses and penalties to a king of a given color
378380

379-
const Bitboard WhiteCamp = Rank1BB | Rank2BB | Rank3BB | Rank4BB | Rank5BB;
380-
const Bitboard BlackCamp = Rank8BB | Rank7BB | Rank6BB | Rank5BB | Rank4BB;
381-
const Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB;
382381
const Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
383-
const Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB;
384382

385-
const Bitboard KingFlank[COLOR_NB][FILE_NB] = {
386-
{ QueenSide & WhiteCamp, QueenSide & WhiteCamp, QueenSide & WhiteCamp, CenterFiles & WhiteCamp,
387-
CenterFiles & WhiteCamp, KingSide & WhiteCamp, KingSide & WhiteCamp, KingSide & WhiteCamp },
388-
{ QueenSide & BlackCamp, QueenSide & BlackCamp, QueenSide & BlackCamp, CenterFiles & BlackCamp,
389-
CenterFiles & BlackCamp, KingSide & BlackCamp, KingSide & BlackCamp, KingSide & BlackCamp },
383+
const Bitboard KingFlank[FILE_NB] = {
384+
CenterFiles >> 2, CenterFiles >> 2, CenterFiles >> 2, CenterFiles, CenterFiles,
385+
CenterFiles << 2, CenterFiles << 2, CenterFiles << 2
390386
};
391387

392388
template<Color Us, bool DoTrace>
393389
Score evaluate_king(const Position& pos, const EvalInfo& ei) {
394390

395-
const Color Them = (Us == WHITE ? BLACK : WHITE);
396-
const Square Up = (Us == WHITE ? NORTH : SOUTH);
391+
const Color Them = (Us == WHITE ? BLACK : WHITE);
392+
const Square Up = (Us == WHITE ? NORTH : SOUTH);
393+
const Bitboard Camp = (Us == WHITE ? ~Bitboard(0) ^ Rank6BB ^ Rank7BB ^ Rank8BB
394+
: ~Bitboard(0) ^ Rank1BB ^ Rank2BB ^ Rank3BB);
397395

396+
const Square ksq = pos.square<KING>(Us);
398397
Bitboard undefended, b, b1, b2, safe, other;
399398
int kingDanger;
400-
const Square ksq = pos.square<KING>(Us);
401399

402400
// King shelter and enemy pawns storm
403401
Score score = ei.pi->king_safety<Us>(pos, ksq);
@@ -483,7 +481,7 @@ namespace {
483481

484482
// King tropism: firstly, find squares that opponent attacks in our king flank
485483
File kf = file_of(ksq);
486-
b = ei.attackedBy[Them][ALL_PIECES] & KingFlank[Us][kf];
484+
b = ei.attackedBy[Them][ALL_PIECES] & KingFlank[kf] & Camp;
487485

488486
assert(((Us == WHITE ? b << 4 : b >> 4) & b) == 0);
489487
assert(popcount(Us == WHITE ? b << 4 : b >> 4) == popcount(b));
@@ -496,7 +494,7 @@ namespace {
496494
score -= CloseEnemies * popcount(b);
497495

498496
// Penalty when our king is on a pawnless flank
499-
if (!(pos.pieces(PAWN) & (KingFlank[WHITE][kf] | KingFlank[BLACK][kf])))
497+
if (!(pos.pieces(PAWN) & KingFlank[kf]))
500498
score -= PawnlessFlank;
501499

502500
if (DoTrace)
@@ -519,8 +517,6 @@ namespace {
519517
const Bitboard TRank2BB = (Us == WHITE ? Rank2BB : Rank7BB);
520518
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
521519

522-
enum { Minor, Rook };
523-
524520
Bitboard b, weak, defended, safeThreats;
525521
Score score = SCORE_ZERO;
526522

@@ -561,7 +557,7 @@ namespace {
561557
while (b)
562558
{
563559
Square s = pop_lsb(&b);
564-
score += Threat[Minor][type_of(pos.piece_on(s))];
560+
score += ThreatByMinor[type_of(pos.piece_on(s))];
565561
if (type_of(pos.piece_on(s)) != PAWN)
566562
score += ThreatByRank * (int)relative_rank(Them, s);
567563
}
@@ -570,7 +566,7 @@ namespace {
570566
while (b)
571567
{
572568
Square s = pop_lsb(&b);
573-
score += Threat[Rook][type_of(pos.piece_on(s))];
569+
score += ThreatByRook[type_of(pos.piece_on(s))];
574570
if (type_of(pos.piece_on(s)) != PAWN)
575571
score += ThreatByRank * (int)relative_rank(Them, s);
576572
}
@@ -702,8 +698,8 @@ namespace {
702698

703699
const Color Them = (Us == WHITE ? BLACK : WHITE);
704700
const Bitboard SpaceMask =
705-
Us == WHITE ? (FileCBB | FileDBB | FileEBB | FileFBB) & (Rank2BB | Rank3BB | Rank4BB)
706-
: (FileCBB | FileDBB | FileEBB | FileFBB) & (Rank7BB | Rank6BB | Rank5BB);
701+
Us == WHITE ? CenterFiles & (Rank2BB | Rank3BB | Rank4BB)
702+
: CenterFiles & (Rank7BB | Rank6BB | Rank5BB);
707703

708704
// Find the safe squares for our pieces inside the area defined by
709705
// SpaceMask. A square is unsafe if it is attacked by an enemy

0 commit comments

Comments
 (0)