// Assorted bonuses and penalties
constexpr Score BishopPawns = S( 3, 7);
- constexpr Score CloseEnemies = S( 8, 0);
constexpr Score CorneredBishop = S( 50, 50);
+ constexpr Score FlankAttacks = S( 8, 0);
constexpr Score Hanging = S( 69, 36);
constexpr Score KingProtector = S( 7, 8);
constexpr Score KnightOnQueen = S( 16, 12);
constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH);
constexpr Bitboard LowRanks = (Us == WHITE ? Rank2BB | Rank3BB: Rank7BB | Rank6BB);
+ const Square ksq = pos.square<KING>(Us);
+
// Find our pawns that are blocked or on the first two ranks
Bitboard b = pos.pieces(Us, PAWN) & (shift<Down>(pos.pieces()) | LowRanks);
- // Squares occupied by those pawns, by our king or queen, or controlled by enemy pawns
- // are excluded from the mobility area.
+ // Squares occupied by those pawns, by our king or queen or controlled by
+ // enemy pawns are excluded from the mobility area.
mobilityArea[Us] = ~(b | pos.pieces(Us, KING, QUEEN) | pe->pawn_attacks(Them));
- // Initialise attackedBy bitboards for kings and pawns
- attackedBy[Us][KING] = pos.attacks_from<KING>(pos.square<KING>(Us));
+ // Initialize attackedBy[] for king and pawns
+ attackedBy[Us][KING] = pos.attacks_from<KING>(ksq);
attackedBy[Us][PAWN] = pe->pawn_attacks(Us);
attackedBy[Us][ALL_PIECES] = attackedBy[Us][KING] | attackedBy[Us][PAWN];
attackedBy2[Us] = attackedBy[Us][KING] & attackedBy[Us][PAWN];
// Init our king safety tables
kingRing[Us] = attackedBy[Us][KING];
- if (relative_rank(Us, pos.square<KING>(Us)) == RANK_1)
+ if (relative_rank(Us, ksq) == RANK_1)
kingRing[Us] |= shift<Up>(kingRing[Us]);
- if (file_of(pos.square<KING>(Us)) == FILE_H)
+ if (file_of(ksq) == FILE_H)
kingRing[Us] |= shift<WEST>(kingRing[Us]);
- else if (file_of(pos.square<KING>(Us)) == FILE_A)
+ else if (file_of(ksq) == FILE_A)
kingRing[Us] |= shift<EAST>(kingRing[Us]);
kingAttackersCount[Them] = popcount(kingRing[Us] & pe->pawn_attacks(Them));
- kingRing[Us] &= ~double_pawn_attacks_bb<Us>(pos.pieces(Us, PAWN));
kingAttacksCount[Them] = kingAttackersWeight[Them] = 0;
+
+ // Remove from kingRing[] the squares defended by two pawns
+ kingRing[Us] &= ~pawn_double_attacks_bb<Us>(pos.pieces(Us, PAWN));
}
const Square* pl = pos.squares<Pt>(Us);
Bitboard b, bb;
- Square s;
Score score = SCORE_ZERO;
attackedBy[Us][Pt] = 0;
- while ((s = *pl++) != SQ_NONE)
+ for (Square s = *pl; s != SQ_NONE; s = *++pl)
{
// Find attacked squares, including x-ray attacks for bishops and rooks
b = Pt == BISHOP ? attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(QUEEN))
constexpr Bitboard Camp = (Us == WHITE ? AllSquares ^ Rank6BB ^ Rank7BB ^ Rank8BB
: AllSquares ^ Rank1BB ^ Rank2BB ^ Rank3BB);
+ Bitboard weak, b, b1, b2, safe, unsafeChecks = 0;
+ int kingDanger = 0;
const Square ksq = pos.square<KING>(Us);
- Bitboard kingFlank, weak, b, b1, b2, safe, unsafeChecks;
- // King shelter and enemy pawns storm
+ // Init the score with king shelter and enemy pawns storm
Score score = pe->king_safety<Us>(pos);
- // Find the squares that opponent attacks in our king flank, and the squares
- // which are attacked twice in that flank.
- kingFlank = KingFlank[file_of(ksq)];
- b1 = attackedBy[Them][ALL_PIECES] & kingFlank & Camp;
- b2 = b1 & attackedBy2[Them];
-
- int tropism = popcount(b1) + popcount(b2);
-
- // Main king safety evaluation
- int kingDanger = 0;
- unsafeChecks = 0;
-
// Attacked squares defended at most once by our queen or king
weak = attackedBy[Them][ALL_PIECES]
& ~attackedBy2[Us]
// Enemy bishops checks: we count them only if they are from squares from
// which we can't give a queen check, because queen checks are more valuable.
- Bitboard BishopCheck = b2
+ Bitboard BishopCheck = b2
& attackedBy[Them][BISHOP]
& safe
& ~QueenCheck;
// the square is in the attacker's mobility area.
unsafeChecks &= mobilityArea[Them];
+ // Find the squares that opponent attacks in our king flank, and the squares
+ // which are attacked twice in that flank.
+ b1 = attackedBy[Them][ALL_PIECES] & KingFlank[file_of(ksq)] & Camp;
+ b2 = b1 & attackedBy2[Them];
+
+ int kingFlankAttacks = popcount(b1) + popcount(b2);
+
kingDanger += kingAttackersCount[Them] * kingAttackersWeight[Them]
+ 69 * kingAttacksCount[Them]
+ 185 * popcount(kingRing[Us] & weak)
- 100 * bool(attackedBy[Us][KNIGHT] & attackedBy[Us][KING])
+ 150 * popcount(pos.blockers_for_king(Us) | unsafeChecks)
- + 5 * tropism * tropism / 16
- 873 * !pos.count<QUEEN>(Them)
- 6 * mg_value(score) / 8
+ mg_value(mobility[Them] - mobility[Us])
+ + 5 * kingFlankAttacks * kingFlankAttacks / 16
- 25;
// Transform the kingDanger units into a Score, and subtract it from the evaluation
score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16);
// Penalty when our king is on a pawnless flank
- if (!(pos.pieces(PAWN) & kingFlank))
+ if (!(pos.pieces(PAWN) & KingFlank[file_of(ksq)]))
score -= PawnlessFlank;
- // King tropism bonus, to anticipate slow motion attacks on our king
- score -= CloseEnemies * tropism;
+ // Penalty if king flank is under attack, potentially moving toward the king
+ score -= FlankAttacks * kingFlankAttacks;
if (T)
Trace::add(KING, Us, score);
v = mg_value(score) * int(me->game_phase())
+ eg_value(score) * int(PHASE_MIDGAME - me->game_phase()) * sf / SCALE_FACTOR_NORMAL;
- v /= int(PHASE_MIDGAME);
+ v /= PHASE_MIDGAME;
// In case of tracing add all remaining individual evaluation terms
if (T)
{ S(-48,-51), S( -3,-40), S(-12,-39), S(-25,-20) }
},
{ // Rook
- { S(-24, -2), S(-13,-6), S( -7, -3), S( 2,-2) },
- { S(-18,-10), S(-10,-7), S( -5, 1), S( 9, 0) },
- { S(-21, 10), S( -7,-4), S( 3, 2), S(-1,-2) },
- { S(-13, -5), S( -5, 2), S( -4, -8), S(-6, 8) },
- { S(-24, -8), S(-12, 5), S( -1, 4), S( 6,-9) },
- { S(-24, 3), S( -4,-2), S( 4,-10), S(10, 7) },
- { S( -8, 1), S( 6, 2), S( 10, 17), S(12,-8) },
- { S(-22, 12), S(-24,-6), S( -6, 13), S( 4, 7) }
+ { S(-24, -2), S(-13,-6), S(-7, -3), S( 2,-2) },
+ { S(-18,-10), S(-10,-7), S(-5, 1), S( 9, 0) },
+ { S(-21, 10), S( -7,-4), S( 3, 2), S(-1,-2) },
+ { S(-13, -5), S( -5, 2), S(-4, -8), S(-6, 8) },
+ { S(-24, -8), S(-12, 5), S(-1, 4), S( 6,-9) },
+ { S(-24, 3), S( -4,-2), S( 4,-10), S(10, 7) },
+ { S( -8, 1), S( 6, 2), S(10, 17), S(12,-8) },
+ { S(-22, 12), S(-24,-6), S(-6, 13), S( 4, 7) }
},
{ // Queen
- { S( 3,-69), S(-5,-57), S(-5,-47), S( 4,-26) },
- { S( -3,-55), S( 5,-31), S( 8,-22), S(12, -4) },
- { S( -3,-39), S( 6,-18), S(13, -9), S( 7, 3) },
- { S( 4,-23), S( 5, -3), S( 9, 13), S( 8, 24) },
- { S( 0,-29), S(14, -6), S(12, 9), S( 5, 21) },
- { S( -4,-38), S(10,-18), S( 6,-12), S( 8, 1) },
- { S( -5,-50), S( 6,-27), S(10,-24), S( 8, -8) },
- { S( -2,-75), S(-2,-52), S( 1,-43), S(-2,-36) }
+ { S( 3,-69), S(-5,-57), S(-5,-47), S( 4,-26) },
+ { S(-3,-55), S( 5,-31), S( 8,-22), S(12, -4) },
+ { S(-3,-39), S( 6,-18), S(13, -9), S( 7, 3) },
+ { S( 4,-23), S( 5, -3), S( 9, 13), S( 8, 24) },
+ { S( 0,-29), S(14, -6), S(12, 9), S( 5, 21) },
+ { S(-4,-38), S(10,-18), S( 6,-12), S( 8, 1) },
+ { S(-5,-50), S( 6,-27), S(10,-24), S( 8, -8) },
+ { S(-2,-75), S(-2,-52), S( 1,-43), S(-2,-36) }
},
{ // King
{ S(272, 0), S(325, 41), S(273, 80), S(190, 93) },
{ S(277, 57), S(305, 98), S(241,138), S(183,131) },
{ S(198, 86), S(253,138), S(168,165), S(120,173) },
{ S(169,103), S(191,152), S(136,168), S(108,169) },
- { S(145, 98), S(176,166), S(112,197), S(69, 194) },
- { S(122, 87), S(159,164), S(85, 174), S(36, 189) },
- { S(87, 40), S(120, 99), S(64, 128), S(25, 141) },
- { S(64, 5), S(87, 60), S(49, 75), S(0, 75) }
+ { S(145, 98), S(176,166), S(112,197), S( 69,194) },
+ { S(122, 87), S(159,164), S( 85,174), S( 36,189) },
+ { S( 87, 40), S(120, 99), S( 64,128), S( 25,141) },
+ { S( 64, 5), S( 87, 60), S( 49, 75), S( 0, 75) }
}
};
constexpr Score PBonus[RANK_NB][FILE_NB] =
{ // Pawn (asymmetric distribution)
{ },
- { S( 0,-10), S( -5,-3), S( 10, 7), S( 13, -1), S( 21, 7), S( 17, 6), S( 6, 1), S( -3,-20) },
- { S(-11, -6), S(-10,-6), S( 15, -1), S( 22, -1), S( 26, -1), S( 28, 2), S( 4, -2), S(-24, -5) },
- { S( -9, 4), S(-18,-5), S( 8, -4), S( 22, -5), S( 33, -6), S( 25,-13), S( -4, -3), S(-16, -7) },
- { S( 6, 18), S( -3, 2), S(-10, 2), S( 1, -9), S( 12,-13), S( 6, -8), S(-12, 11), S( 1, 9) },
- { S( -6, 25), S( -8,17), S( 5, 19), S( 11, 29), S(-14, 29), S( 0, 8), S(-12, 4), S(-14, 12) },
- { S(-10, -1), S( 6,-6), S( -5, 18), S(-11, 22), S( -2, 22), S(-14, 17), S( 12, 2), S( -1, 9) }
+ { S( 0,-10), S( -5,-3), S( 10, 7), S( 13,-1), S( 21, 7), S( 17, 6), S( 6, 1), S( -3,-20) },
+ { S(-11, -6), S(-10,-6), S( 15,-1), S( 22,-1), S( 26, -1), S( 28, 2), S( 4,-2), S(-24, -5) },
+ { S( -9, 4), S(-18,-5), S( 8,-4), S( 22,-5), S( 33, -6), S( 25,-13), S( -4,-3), S(-16, -7) },
+ { S( 6, 18), S( -3, 2), S(-10, 2), S( 1,-9), S( 12,-13), S( 6, -8), S(-12,11), S( 1, 9) },
+ { S( -6, 25), S( -8,17), S( 5,19), S( 11,29), S(-14, 29), S( 0, 8), S(-12, 4), S(-14, 12) },
+ { S(-10, -1), S( 6,-6), S( -5,18), S(-11,22), S( -2, 22), S(-14, 17), S( 12, 2), S( -1, 9) }
};
#undef S
// Find out minimum score and reset votes for moves which can be voted
for (Thread* th: Threads)
- {
minScore = std::min(minScore, th->rootMoves[0].score);
- votes[th->rootMoves[0].pv[0]] = 0;
- }
// Vote according to score and depth
- auto square = [](int64_t x) { return x * x; };
for (Thread* th : Threads)
- votes[th->rootMoves[0].pv[0]] += 200 + (square(th->rootMoves[0].score - minScore + 1)
- * int64_t(th->completedDepth));
+ {
+ int64_t s = th->rootMoves[0].score - minScore + 1;
+ votes[th->rootMoves[0].pv[0]] += 200 + s * s * int(th->completedDepth);
+ }
// Select best thread
- int64_t bestVote = votes[this->rootMoves[0].pv[0]];
+ auto bestVote = votes[this->rootMoves[0].pv[0]];
for (Thread* th : Threads)
- {
if (votes[th->rootMoves[0].pv[0]] > bestVote)
{
bestVote = votes[th->rootMoves[0].pv[0]];
bestThread = th;
}
- }
}
previousScore = bestThread->rootMoves[0].score;
Move ttMove, move, excludedMove, bestMove;
Depth extension, newDepth;
Value bestValue, value, ttValue, eval, maxValue, pureStaticEval;
- bool ttHit, pvHit, inCheck, givesCheck, improving;
+ bool ttHit, ttPv, inCheck, givesCheck, improving;
bool captureOrPromotion, doFullDepthSearch, moveCountPruning, skipQuiets, ttCapture;
Piece movedPiece;
int moveCount, captureCount, quietCount;
ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
ttMove = rootNode ? thisThread->rootMoves[thisThread->pvIdx].pv[0]
: ttHit ? tte->move() : MOVE_NONE;
- pvHit = (ttHit && tte->pv_hit()) || (PvNode && depth > 4 * ONE_PLY);
+ ttPv = (ttHit && tte->is_pv()) || (PvNode && depth > 4 * ONE_PLY);
// At non-PV nodes we check for an early TT cutoff
if ( !PvNode
if ( b == BOUND_EXACT
|| (b == BOUND_LOWER ? value >= beta : value <= alpha))
{
- tte->save(posKey, value_to_tt(value, ss->ply), pvHit, b,
+ tte->save(posKey, value_to_tt(value, ss->ply), ttPv, b,
std::min(DEPTH_MAX - ONE_PLY, depth + 6 * ONE_PLY),
MOVE_NONE, VALUE_NONE);
else
ss->staticEval = eval = pureStaticEval = -(ss-1)->staticEval + 2 * Eval::Tempo;
- tte->save(posKey, VALUE_NONE, pvHit, BOUND_NONE, DEPTH_NONE, MOVE_NONE, pureStaticEval);
+ tte->save(posKey, VALUE_NONE, ttPv, BOUND_NONE, DEPTH_NONE, MOVE_NONE, pureStaticEval);
}
// Step 7. Razoring (~2 Elo)
Depth r = reduction<PvNode>(improving, depth, moveCount);
// Decrease reduction if position is or has been on the PV
- if (pvHit)
+ if (ttPv)
r -= ONE_PLY;
// Decrease reduction if opponent's move count is high (~10 Elo)
bestValue = std::min(bestValue, maxValue);
if (!excludedMove)
- tte->save(posKey, value_to_tt(bestValue, ss->ply), pvHit,
+ tte->save(posKey, value_to_tt(bestValue, ss->ply), ttPv,
bestValue >= beta ? BOUND_LOWER :
PvNode && bestMove ? BOUND_EXACT : BOUND_UPPER,
depth, bestMove, pureStaticEval);
tte = TT.probe(posKey, ttHit);
ttValue = ttHit ? value_from_tt(tte->value(), ss->ply) : VALUE_NONE;
ttMove = ttHit ? tte->move() : MOVE_NONE;
- pvHit = ttHit && tte->pv_hit();
+ pvHit = ttHit && tte->is_pv();
if ( !PvNode
&& ttHit