1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Rewrite evaluate_common() as a template

Seems to speed up this very hot path and code is
cleaner too.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-01-07 15:37:28 +01:00
parent 539051b1e0
commit bdbbc4e06b

View file

@ -559,17 +559,24 @@ namespace {
// evaluate_common() computes terms common to all pieces attack // evaluate_common() computes terms common to all pieces attack
int evaluate_common(const Position&p, const Bitboard& b, Color us, EvalInfo& ei, template<PieceType Piece>
int AttackWeight, const Value* mgBonus, const Value* egBonus, int evaluate_common(const Position& p, const Bitboard& b, Color us, EvalInfo& ei, Square s = SQ_NONE) {
Square s = SQ_NONE, const Value* OutpostBonus = NULL) {
static const int AttackWeight[] = { 0, 0, KnightAttackWeight, BishopAttackWeight, RookAttackWeight, QueenAttackWeight };
static const Value* MgBonus[] = { 0, 0, MidgameKnightMobilityBonus, MidgameBishopMobilityBonus, MidgameRookMobilityBonus, MidgameQueenMobilityBonus };
static const Value* EgBonus[] = { 0, 0, EndgameKnightMobilityBonus, EndgameBishopMobilityBonus, EndgameRookMobilityBonus, EndgameQueenMobilityBonus };
static const Value* OutpostBonus[] = { 0, 0, KnightOutpostBonus, BishopOutpostBonus, 0, 0 };
Color them = opposite_color(us); Color them = opposite_color(us);
// Update attack info
ei.attackedBy[us][Piece] |= b;
// King attack // King attack
if (b & ei.kingZone[us]) if (b & ei.kingZone[us])
{ {
ei.kingAttackersCount[us]++; ei.kingAttackersCount[us]++;
ei.kingAttackersWeight[us] += AttackWeight; ei.kingAttackersWeight[us] += AttackWeight[Piece];
Bitboard bb = (b & ei.attackedBy[them][KING]); Bitboard bb = (b & ei.attackedBy[them][KING]);
if (bb) if (bb)
ei.kingAdjacentZoneAttacksCount[us] += count_1s_max_15(bb); ei.kingAdjacentZoneAttacksCount[us] += count_1s_max_15(bb);
@ -580,16 +587,17 @@ namespace {
// Mobility // Mobility
int mob = count_1s_max_15(bb & ~p.pieces_of_color(us)); int mob = count_1s_max_15(bb & ~p.pieces_of_color(us));
ei.mgMobility += Sign[us] * mgBonus[mob]; ei.mgMobility += Sign[us] * MgBonus[Piece][mob];
ei.egMobility += Sign[us] * egBonus[mob]; ei.egMobility += Sign[us] * EgBonus[Piece][mob];
// Bishop and Knight outposts // Bishop and Knight outposts
if (!OutpostBonus || !p.square_is_weak(s, them)) if ( (Piece != BISHOP && Piece != KNIGHT) // compile time condition
|| !p.square_is_weak(s, them))
return mob; return mob;
// Initial bonus based on square // Initial bonus based on square
Value v, bonus; Value v, bonus;
v = bonus = OutpostBonus[relative_square(us, s)]; v = bonus = OutpostBonus[Piece][relative_square(us, s)];
// Increase bonus if supported by pawn, especially if the opponent has // Increase bonus if supported by pawn, especially if the opponent has
// no minor piece which can exchange the outpost piece // no minor piece which can exchange the outpost piece
@ -609,42 +617,34 @@ namespace {
// evaluate_knight() assigns bonuses and penalties to a knight of a given // evaluate_knight() assigns bonuses and penalties to a knight of a given
// color on a given square. // color on a given square.
void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) { void evaluate_knight(const Position& p, Square s, Color us, EvalInfo& ei) {
Bitboard b = p.piece_attacks<KNIGHT>(s); // Attacks, mobility and outposts
ei.attackedBy[us][KNIGHT] |= b; evaluate_common<KNIGHT>(p, p.piece_attacks<KNIGHT>(s), us, ei, s);
// King attack, mobility and outposts
evaluate_common(p, b, us, ei, KnightAttackWeight, MidgameKnightMobilityBonus,
EndgameKnightMobilityBonus, s, KnightOutpostBonus);
} }
// evaluate_bishop() assigns bonuses and penalties to a bishop of a given // evaluate_bishop() assigns bonuses and penalties to a bishop of a given
// color on a given square. // color on a given square.
void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei) { void evaluate_bishop(const Position& p, Square s, Color us, EvalInfo& ei) {
Bitboard b = bishop_attacks_bb(s, p.occupied_squares() & ~p.queens(us)); Bitboard b = bishop_attacks_bb(s, p.occupied_squares() & ~p.queens(us));
ei.attackedBy[us][BISHOP] |= b;
// King attack, mobility and outposts // Attacks, mobility and outposts
evaluate_common(p, b, us, ei, BishopAttackWeight, MidgameBishopMobilityBonus, evaluate_common<BISHOP>(p, b, us, ei, s);
EndgameBishopMobilityBonus, s, BishopOutpostBonus);
} }
// evaluate_rook() assigns bonuses and penalties to a rook of a given // evaluate_rook() assigns bonuses and penalties to a rook of a given
// color on a given square. // color on a given square.
void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei) { void evaluate_rook(const Position& p, Square s, Color us, EvalInfo& ei) {
Bitboard b = rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us)); Bitboard b = rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
ei.attackedBy[us][ROOK] |= b;
// King attack and mobility // Attacks and mobility
int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus, int mob = evaluate_common<ROOK>(p, b, us, ei);
EndgameRookMobilityBonus);
// Rook on 7th rank // Rook on 7th rank
Color them = opposite_color(us); Color them = opposite_color(us);
@ -703,14 +703,10 @@ namespace {
// evaluate_queen() assigns bonuses and penalties to a queen of a given // evaluate_queen() assigns bonuses and penalties to a queen of a given
// color on a given square. // color on a given square.
void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei) { void evaluate_queen(const Position& p, Square s, Color us, EvalInfo& ei) {
Bitboard b = p.piece_attacks<QUEEN>(s); // Attacks and mobility
ei.attackedBy[us][QUEEN] |= b; evaluate_common<QUEEN>(p, p.piece_attacks<QUEEN>(s), us, ei);
// King attack and mobility
evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus,
EndgameQueenMobilityBonus);
// Queen on 7th rank // Queen on 7th rank
Color them = opposite_color(us); Color them = opposite_color(us);
@ -731,7 +727,7 @@ namespace {
// evaluate_king() assigns bonuses and penalties to a king of a given // evaluate_king() assigns bonuses and penalties to a king of a given
// color on a given square. // color on a given square.
void evaluate_king(const Position &p, Square s, Color us, EvalInfo &ei) { void evaluate_king(const Position& p, Square s, Color us, EvalInfo& ei) {
int shelter = 0, sign = Sign[us]; int shelter = 0, sign = Sign[us];