mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 08:43:09 +00:00
Position: Unify and templetize mg_pst() and eg_pst()
Also templetize compute_value() can be simpler now that the above is templetized too. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
f30aa83f8a
commit
b7cb6180cf
2 changed files with 44 additions and 48 deletions
|
@ -210,8 +210,8 @@ void Position::from_fen(const std::string& fen) {
|
||||||
key = compute_key();
|
key = compute_key();
|
||||||
pawnKey = compute_pawn_key();
|
pawnKey = compute_pawn_key();
|
||||||
materialKey = compute_material_key();
|
materialKey = compute_material_key();
|
||||||
mgValue = compute_value(MidGame);
|
mgValue = compute_value<MidGame>();
|
||||||
egValue = compute_value(EndGame);
|
egValue = compute_value<EndGame>();
|
||||||
npMaterial[WHITE] = compute_non_pawn_material(WHITE);
|
npMaterial[WHITE] = compute_non_pawn_material(WHITE);
|
||||||
npMaterial[BLACK] = compute_non_pawn_material(BLACK);
|
npMaterial[BLACK] = compute_non_pawn_material(BLACK);
|
||||||
}
|
}
|
||||||
|
@ -758,10 +758,10 @@ void Position::do_move(Move m, UndoInfo& u) {
|
||||||
key ^= zobrist[us][piece][from] ^ zobrist[us][piece][to];
|
key ^= zobrist[us][piece][from] ^ zobrist[us][piece][to];
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
mgValue -= mg_pst(us, piece, from);
|
mgValue -= pst<MidGame>(us, piece, from);
|
||||||
mgValue += mg_pst(us, piece, to);
|
mgValue += pst<MidGame>(us, piece, to);
|
||||||
egValue -= eg_pst(us, piece, from);
|
egValue -= pst<EndGame>(us, piece, from);
|
||||||
egValue += eg_pst(us, piece, to);
|
egValue += pst<EndGame>(us, piece, to);
|
||||||
|
|
||||||
// If the moving piece was a king, update the king square
|
// If the moving piece was a king, update the king square
|
||||||
if (piece == KING)
|
if (piece == KING)
|
||||||
|
@ -851,8 +851,8 @@ void Position::do_capture_move(Move m, PieceType capture, Color them, Square to)
|
||||||
pawnKey ^= zobrist[them][PAWN][to];
|
pawnKey ^= zobrist[them][PAWN][to];
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
mgValue -= mg_pst(them, capture, to);
|
mgValue -= pst<MidGame>(them, capture, to);
|
||||||
egValue -= eg_pst(them, capture, to);
|
egValue -= pst<EndGame>(them, capture, to);
|
||||||
|
|
||||||
assert(!move_promotion(m) || capture != PAWN);
|
assert(!move_promotion(m) || capture != PAWN);
|
||||||
|
|
||||||
|
@ -939,14 +939,14 @@ void Position::do_castle_move(Move m) {
|
||||||
index[rto] = tmp;
|
index[rto] = tmp;
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
mgValue -= mg_pst(us, KING, kfrom);
|
mgValue -= pst<MidGame>(us, KING, kfrom);
|
||||||
mgValue += mg_pst(us, KING, kto);
|
mgValue += pst<MidGame>(us, KING, kto);
|
||||||
egValue -= eg_pst(us, KING, kfrom);
|
egValue -= pst<EndGame>(us, KING, kfrom);
|
||||||
egValue += eg_pst(us, KING, kto);
|
egValue += pst<EndGame>(us, KING, kto);
|
||||||
mgValue -= mg_pst(us, ROOK, rfrom);
|
mgValue -= pst<MidGame>(us, ROOK, rfrom);
|
||||||
mgValue += mg_pst(us, ROOK, rto);
|
mgValue += pst<MidGame>(us, ROOK, rto);
|
||||||
egValue -= eg_pst(us, ROOK, rfrom);
|
egValue -= pst<EndGame>(us, ROOK, rfrom);
|
||||||
egValue += eg_pst(us, ROOK, rto);
|
egValue += pst<EndGame>(us, ROOK, rto);
|
||||||
|
|
||||||
// Update hash key
|
// Update hash key
|
||||||
key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
|
key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
|
||||||
|
@ -1039,10 +1039,10 @@ void Position::do_promotion_move(Move m, UndoInfo &u) {
|
||||||
index[to] = pieceCount[us][promotion] - 1;
|
index[to] = pieceCount[us][promotion] - 1;
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
mgValue -= mg_pst(us, PAWN, from);
|
mgValue -= pst<MidGame>(us, PAWN, from);
|
||||||
mgValue += mg_pst(us, promotion, to);
|
mgValue += pst<MidGame>(us, promotion, to);
|
||||||
egValue -= eg_pst(us, PAWN, from);
|
egValue -= pst<EndGame>(us, PAWN, from);
|
||||||
egValue += eg_pst(us, promotion, to);
|
egValue += pst<EndGame>(us, promotion, to);
|
||||||
|
|
||||||
// Update material
|
// Update material
|
||||||
npMaterial[us] += piece_value_midgame(promotion);
|
npMaterial[us] += piece_value_midgame(promotion);
|
||||||
|
@ -1133,12 +1133,12 @@ void Position::do_ep_move(Move m) {
|
||||||
pawnKey ^= zobrist[them][PAWN][capsq];
|
pawnKey ^= zobrist[them][PAWN][capsq];
|
||||||
|
|
||||||
// Update incremental scores
|
// Update incremental scores
|
||||||
mgValue -= mg_pst(them, PAWN, capsq);
|
mgValue -= pst<MidGame>(them, PAWN, capsq);
|
||||||
mgValue -= mg_pst(us, PAWN, from);
|
mgValue -= pst<MidGame>(us, PAWN, from);
|
||||||
mgValue += mg_pst(us, PAWN, to);
|
mgValue += pst<MidGame>(us, PAWN, to);
|
||||||
egValue -= eg_pst(them, PAWN, capsq);
|
egValue -= pst<EndGame>(them, PAWN, capsq);
|
||||||
egValue -= eg_pst(us, PAWN, from);
|
egValue -= pst<EndGame>(us, PAWN, from);
|
||||||
egValue += eg_pst(us, PAWN, to);
|
egValue += pst<EndGame>(us, PAWN, to);
|
||||||
|
|
||||||
// Reset en passant square
|
// Reset en passant square
|
||||||
epSquare = SQ_NONE;
|
epSquare = SQ_NONE;
|
||||||
|
@ -1811,13 +1811,12 @@ Key Position::compute_material_key() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::compute_mg_value() and Position::compute_eg_value() compute the
|
/// Position::compute_value() compute the incremental scores for the middle
|
||||||
/// incremental scores for the middle game and the endgame. These functions
|
/// game and the endgame. These functions are used to initialize the incremental
|
||||||
/// are used to initialize the incremental scores when a new position is set
|
/// scores when a new position is set up, and to verify that the scores are correctly
|
||||||
/// up, and to verify that the scores are correctly updated by do_move
|
/// updated by do_move and undo_move when the program is running in debug mode.
|
||||||
/// and undo_move when the program is running in debug mode.
|
template<Position::GamePhase Phase>
|
||||||
|
Value Position::compute_value() const {
|
||||||
Value Position::compute_value(GamePhase p) const {
|
|
||||||
|
|
||||||
Value result = Value(0);
|
Value result = Value(0);
|
||||||
Bitboard b;
|
Bitboard b;
|
||||||
|
@ -1831,11 +1830,11 @@ Value Position::compute_value(GamePhase p) const {
|
||||||
{
|
{
|
||||||
s = pop_1st_bit(&b);
|
s = pop_1st_bit(&b);
|
||||||
assert(piece_on(s) == piece_of_color_and_type(c, pt));
|
assert(piece_on(s) == piece_of_color_and_type(c, pt));
|
||||||
result += (p == MidGame ? mg_pst(c, pt, s) : eg_pst(c, pt, s));
|
result += pst<Phase>(c, pt, s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Value TempoValue = (p == MidGame ? TempoValueMidgame : TempoValueEndgame);
|
const Value TempoValue = (Phase == MidGame ? TempoValueMidgame : TempoValueEndgame);
|
||||||
result += (side_to_move() == WHITE)? TempoValue / 2 : -TempoValue / 2;
|
result += (side_to_move() == WHITE)? TempoValue / 2 : -TempoValue / 2;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -2057,8 +2056,8 @@ void Position::flipped_copy(const Position &pos) {
|
||||||
materialKey = compute_material_key();
|
materialKey = compute_material_key();
|
||||||
|
|
||||||
// Incremental scores
|
// Incremental scores
|
||||||
mgValue = compute_value(MidGame);
|
mgValue = compute_value<MidGame>();
|
||||||
egValue = compute_value(EndGame);
|
egValue = compute_value<EndGame>();
|
||||||
|
|
||||||
// Material
|
// Material
|
||||||
npMaterial[WHITE] = compute_non_pawn_material(WHITE);
|
npMaterial[WHITE] = compute_non_pawn_material(WHITE);
|
||||||
|
@ -2187,10 +2186,10 @@ bool Position::is_ok(int* failedStep) const {
|
||||||
if (failedStep) (*failedStep)++;
|
if (failedStep) (*failedStep)++;
|
||||||
if (debugIncrementalEval)
|
if (debugIncrementalEval)
|
||||||
{
|
{
|
||||||
if (mgValue != compute_value(MidGame))
|
if (mgValue != compute_value<MidGame>())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (egValue != compute_value(EndGame))
|
if (egValue != compute_value<EndGame>())
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -319,9 +319,8 @@ private:
|
||||||
MidGame,
|
MidGame,
|
||||||
EndGame
|
EndGame
|
||||||
};
|
};
|
||||||
Value mg_pst(Color c, PieceType pt, Square s) const;
|
template<GamePhase> Value pst(Color c, PieceType pt, Square s) const;
|
||||||
Value eg_pst(Color c, PieceType pt, Square s) const;
|
template<GamePhase> Value compute_value() const;
|
||||||
Value compute_value(GamePhase p) const;
|
|
||||||
Value compute_non_pawn_material(Color c) const;
|
Value compute_non_pawn_material(Color c) const;
|
||||||
|
|
||||||
// Bitboards
|
// Bitboards
|
||||||
|
@ -633,8 +632,10 @@ inline Key Position::get_material_key() const {
|
||||||
return materialKey;
|
return materialKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value Position::mg_pst(Color c, PieceType pt, Square s) const {
|
template<Position::GamePhase Phase>
|
||||||
return MgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
|
inline Value Position::pst(Color c, PieceType pt, Square s) const {
|
||||||
|
return (Phase == MidGame ? MgPieceSquareTable[piece_of_color_and_type(c, pt)][s]
|
||||||
|
: EgPieceSquareTable[piece_of_color_and_type(c, pt)][s]);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value Position::mg_pst_delta(Move m) const {
|
inline Value Position::mg_pst_delta(Move m) const {
|
||||||
|
@ -642,10 +643,6 @@ inline Value Position::mg_pst_delta(Move m) const {
|
||||||
-MgPieceSquareTable[piece_on(move_from(m))][move_from(m)];
|
-MgPieceSquareTable[piece_on(move_from(m))][move_from(m)];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value Position::eg_pst(Color c, PieceType pt, Square s) const {
|
|
||||||
return EgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Value Position::mg_value() const {
|
inline Value Position::mg_value() const {
|
||||||
return mgValue;
|
return mgValue;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue