// Pointer to pawn hash table entry
PawnInfo* pi;
- // updateKingTables[color] is set to true if we have enough material
- // to trigger the opponent's king safety calculation. When is false we
- // skip the time consuming update of the king attackers tables.
- bool updateKingTables[2];
-
// attackedBy[color][piece type] is a bitboard representing all squares
// attacked by a given color and piece type, attackedBy[color][0] contains
// all squares attacked by the given color.
Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility);
template<Color Us, bool HasPopCnt>
- Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin);
+ Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]);
template<Color Us>
Score evaluate_threats(const Position& pos, EvalInfo& ei);
template<Color Us>
Score evaluate_passed_pawns(const Position& pos, EvalInfo& ei);
- Score apply_weight(Score v, Score weight);
+ inline Score apply_weight(Score v, Score weight);
Value scale_by_game_phase(const Score& v, Phase ph, ScaleFactor sf);
Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
void init_safety();
Value do_evaluate(const Position& pos, Value& margin) {
EvalInfo ei;
+ Value margins[2];
Score mobilityWhite, mobilityBlack;
assert(pos.is_ok());
// in the position object (material + piece square tables).
Score bonus = pos.value();
- // margin is the uncertainty estimation of position's evaluation
- // and typically is used by the search for pruning decisions.
- margin = VALUE_ZERO;
+ // margins[] store the uncertainty estimation of position's evaluation
+ // that typically is used by the search for pruning decisions.
+ margins[WHITE] = margins[BLACK] = VALUE_ZERO;
// Probe the material hash table
MaterialInfo* mi = MaterialTable[pos.thread()]->get_material_info(pos);
// If we have a specialized evaluation function for the current material
// configuration, call it and return.
if (mi->specialized_eval_exists())
+ {
+ margin = VALUE_ZERO;
return mi->evaluate(pos);
+ }
// Probe the pawn hash table
ei.pi = PawnTable[pos.thread()]->get_pawn_info(pos);
// Evaluate kings after all other pieces because we need complete attack
// information when computing the king safety evaluation.
- bonus += evaluate_king<WHITE, HasPopCnt>(pos, ei, margin)
- - evaluate_king<BLACK, HasPopCnt>(pos, ei, margin);
+ bonus += evaluate_king<WHITE, HasPopCnt>(pos, ei, margins)
+ - evaluate_king<BLACK, HasPopCnt>(pos, ei, margins);
// Evaluate tactical threats, we need full attack information including king
bonus += evaluate_threats<WHITE>(pos, ei)
bonus += evaluate_passed_pawns<WHITE>(pos, ei)
- evaluate_passed_pawns<BLACK>(pos, ei);
- Phase phase = mi->game_phase();
-
// Evaluate space for both sides, only in middle-game.
- if (phase > PHASE_ENDGAME && mi->space_weight() > 0)
+ if (mi->space_weight())
{
int s = evaluate_space<WHITE, HasPopCnt>(pos, ei) - evaluate_space<BLACK, HasPopCnt>(pos, ei);
bonus += apply_weight(make_score(s * mi->space_weight(), 0), Weights[Space]);
}
// Scale winning side if position is more drawish that what it appears
- ScaleFactor sf = eg_value(bonus) > VALUE_ZERO ? mi->scale_factor(pos, WHITE)
+ ScaleFactor sf = eg_value(bonus) > VALUE_DRAW ? mi->scale_factor(pos, WHITE)
: mi->scale_factor(pos, BLACK);
+ Phase phase = mi->game_phase();
// If we don't already have an unusual scale factor, check for opposite
// colored bishop endgames, and use a lower scale for those.
}
// Interpolate between the middle game and the endgame score
+ margin = margins[pos.side_to_move()];
Value v = scale_by_game_phase(bonus, phase, sf);
return pos.side_to_move() == WHITE ? v : -v;
}
template<Color Us, bool HasPopCnt>
void init_eval_info(const Position& pos, EvalInfo& ei) {
+ const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from<KING>(pos.king_square(Them));
- ei.kingZone[Us] = (b | (Us == WHITE ? b >> 8 : b << 8));
ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us);
- ei.updateKingTables[Us] = pos.piece_count(Us, QUEEN) && pos.non_pawn_material(Us) >= QueenValueMidgame + RookValueMidgame;
- if (ei.updateKingTables[Us])
+
+ // Init king safety tables only if we are going to use them
+ if ( pos.piece_count(Us, QUEEN)
+ && pos.non_pawn_material(Us) >= QueenValueMidgame + RookValueMidgame)
{
+ ei.kingZone[Us] = (b | (Us == WHITE ? b >> 8 : b << 8));
b &= ei.attackedBy[Us][PAWN];
- ei.kingAttackersCount[Us] = b ? count_1s_max_15<HasPopCnt>(b) / 2 : EmptyBoardBB;
- ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = EmptyBoardBB;
- }
+ ei.kingAttackersCount[Us] = b ? count_1s<Max15>(b) / 2 : 0;
+ ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0;
+ } else
+ ei.kingZone[Us] = ei.kingAttackersCount[Us] = 0;
}
File f;
Score bonus = SCORE_ZERO;
+ const BitCountType Full = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64 : CNT32;
+ const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
const Color Them = (Us == WHITE ? BLACK : WHITE);
const Square* ptr = pos.piece_list_begin(Us, Piece);
ei.attackedBy[Us][Piece] |= b;
// King attacks
- if (ei.updateKingTables[Us] && (b & ei.kingZone[Us]))
+ if (b & ei.kingZone[Us])
{
ei.kingAttackersCount[Us]++;
ei.kingAttackersWeight[Us] += KingAttackWeights[Piece];
Bitboard bb = (b & ei.attackedBy[Them][KING]);
if (bb)
- ei.kingAdjacentZoneAttacksCount[Us] += count_1s_max_15<HasPopCnt>(bb);
+ ei.kingAdjacentZoneAttacksCount[Us] += count_1s<Max15>(bb);
}
// Mobility
- mob = (Piece != QUEEN ? count_1s_max_15<HasPopCnt>(b & mobilityArea)
- : count_1s<HasPopCnt>(b & mobilityArea));
+ mob = (Piece != QUEEN ? count_1s<Max15>(b & mobilityArea)
+ : count_1s<Full >(b & mobilityArea));
mobility += MobilityBonus[Piece][mob];
// evaluate_king<>() assigns bonuses and penalties to a king of a given color
template<Color Us, bool HasPopCnt>
- Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin) {
+ Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]) {
+ const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard undefended, b, b1, b2, safe;
// King safety. This is quite complicated, and is almost certainly far
// from optimally tuned.
- if ( ei.updateKingTables[Them]
- && ei.kingAttackersCount[Them] >= 2
+ if ( ei.kingAttackersCount[Them] >= 2
&& ei.kingAdjacentZoneAttacksCount[Them])
{
// Find the attacked squares around the king which has no defenders
// attacked and undefended squares around our king, the square of the
// king, and the quality of the pawn shelter.
attackUnits = Min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2)
- + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s_max_15<HasPopCnt>(undefended))
+ + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s<Max15>(undefended))
+ InitKingDanger[relative_square(Us, ksq)]
- mg_value(ei.pi->king_shelter<Us>(pos, ksq)) / 32;
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK]);
if (b)
attackUnits += QueenContactCheckBonus
- * count_1s_max_15<HasPopCnt>(b)
+ * count_1s<Max15>(b)
* (Them == pos.side_to_move() ? 2 : 1);
}
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][QUEEN]);
if (b)
attackUnits += RookContactCheckBonus
- * count_1s_max_15<HasPopCnt>(b)
+ * count_1s<Max15>(b)
* (Them == pos.side_to_move() ? 2 : 1);
}
// Enemy queen safe checks
b = (b1 | b2) & ei.attackedBy[Them][QUEEN];
if (b)
- attackUnits += QueenCheckBonus * count_1s_max_15<HasPopCnt>(b);
+ attackUnits += QueenCheckBonus * count_1s<Max15>(b);
// Enemy rooks safe checks
b = b1 & ei.attackedBy[Them][ROOK];
if (b)
- attackUnits += RookCheckBonus * count_1s_max_15<HasPopCnt>(b);
+ attackUnits += RookCheckBonus * count_1s<Max15>(b);
// Enemy bishops safe checks
b = b2 & ei.attackedBy[Them][BISHOP];
if (b)
- attackUnits += BishopCheckBonus * count_1s_max_15<HasPopCnt>(b);
+ attackUnits += BishopCheckBonus * count_1s<Max15>(b);
// Enemy knights safe checks
b = pos.attacks_from<KNIGHT>(ksq) & ei.attackedBy[Them][KNIGHT] & safe;
if (b)
- attackUnits += KnightCheckBonus * count_1s_max_15<HasPopCnt>(b);
+ attackUnits += KnightCheckBonus * count_1s<Max15>(b);
// To index KingDangerTable[] attackUnits must be in [0, 99] range
attackUnits = Min(99, Max(0, attackUnits));
// be very big, and so capturing a single attacking piece can therefore
// result in a score change far bigger than the value of the captured piece.
bonus -= KingDangerTable[Us][attackUnits];
- if (pos.side_to_move() == Us)
- margin += mg_value(KingDangerTable[Us][attackUnits]);
+ margins[Us] += mg_value(KingDangerTable[Us][attackUnits]);
}
return bonus;
}
template<Color Us, bool HasPopCnt>
int evaluate_space(const Position& pos, EvalInfo& ei) {
+ const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
const Color Them = (Us == WHITE ? BLACK : WHITE);
// Find the safe squares for our pieces inside the area defined by
behind |= (Us == WHITE ? behind >> 8 : behind << 8);
behind |= (Us == WHITE ? behind >> 16 : behind << 16);
- return count_1s_max_15<HasPopCnt>(safe) + count_1s_max_15<HasPopCnt>(behind & safe);
+ return count_1s<Max15>(safe) + count_1s<Max15>(behind & safe);
}