From 1ae8c59c0bf7cc87d69673de95363bd222ef6642 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 7 Nov 2009 12:40:48 +0100 Subject: [PATCH] Convert Position to use Score struct No functional change. Signed-off-by: Marco Costalba --- src/evaluate.cpp | 5 ++--- src/position.cpp | 50 ++++++++++++++++-------------------------------- src/position.h | 13 ++++--------- src/uci.cpp | 4 ++-- 4 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 5ae816ed..0b4ffd5d 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -352,7 +352,7 @@ Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) { // Initialize by reading the incrementally updated scores included in the // position object (material + piece square tables) - ei.value = Score(pos.mg_value(), pos.eg_value()); + ei.value = pos.value(); // Probe the material hash table ei.mi = MaterialTable[threadID]->get_material_info(pos); @@ -489,11 +489,10 @@ Value quick_evaluate(const Position &pos) { static const ScaleFactor sf[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL}; - Score v = Score(pos.mg_value(), pos.eg_value()); Phase ph = pos.game_phase(); Color stm = pos.side_to_move(); - return Sign[stm] * scale_by_game_phase(v, ph, sf); + return Sign[stm] * scale_by_game_phase(pos.value(), ph, sf); } diff --git a/src/position.cpp b/src/position.cpp index 10c9d679..e7a0017c 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -213,8 +213,7 @@ void Position::from_fen(const string& fen) { st->key = compute_key(); st->pawnKey = compute_pawn_key(); st->materialKey = compute_material_key(); - st->mgValue = compute_value(); - st->egValue = compute_value(); + st->value = Score(compute_value(), compute_value()); st->npMaterial[WHITE] = compute_non_pawn_material(WHITE); st->npMaterial[BLACK] = compute_non_pawn_material(BLACK); } @@ -830,8 +829,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) { } // Update incremental scores - st->mgValue += pst_delta(piece, from, to); - st->egValue += pst_delta(piece, from, to); + st->value += Score(pst_delta(piece, from, to), pst_delta(piece, from, to)); if (pm) // promotion ? { @@ -866,10 +864,8 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) { st->pawnKey ^= zobrist[us][PAWN][to]; // Partially revert and update incremental scores - st->mgValue -= pst(us, PAWN, to); - st->mgValue += pst(us, promotion, to); - st->egValue -= pst(us, PAWN, to); - st->egValue += pst(us, promotion, to); + st->value -= Score(pst(us, PAWN, to), pst(us, PAWN, to)); + st->value += Score(pst(us, promotion, to), pst(us, promotion, to)); // Update material st->npMaterial[us] += piece_value_midgame(promotion); @@ -899,9 +895,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) { // Finish sideToMove = opposite_color(sideToMove); - - st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame; - st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame; + st->value += (sideToMove == WHITE) ? TempoValue : -TempoValue; assert(is_ok()); } @@ -937,8 +931,7 @@ void Position::do_capture_move(Bitboard& key, PieceType capture, Color them, Squ key ^= zobrist[them][capture][capsq]; // Update incremental scores - st->mgValue -= pst(them, capture, capsq); - st->egValue -= pst(them, capture, capsq); + st->value -= Score(pst(them, capture, capsq), pst(them, capture, capsq)); // If the captured piece was a pawn, update pawn hash key, // otherwise update non-pawn material. @@ -1035,10 +1028,8 @@ void Position::do_castle_move(Move m) { index[rto] = tmp; // Update incremental scores - st->mgValue += pst_delta(king, kfrom, kto); - st->egValue += pst_delta(king, kfrom, kto); - st->mgValue += pst_delta(rook, rfrom, rto); - st->egValue += pst_delta(rook, rfrom, rto); + st->value += Score(pst_delta(king, kfrom, kto), pst_delta(king, kfrom, kto)); + st->value += Score(pst_delta(rook, rfrom, rto), pst_delta(rook, rfrom, rto)); // Update hash key st->key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto]; @@ -1064,9 +1055,7 @@ void Position::do_castle_move(Move m) { // Finish sideToMove = opposite_color(sideToMove); - - st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame; - st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame; + st->value += (sideToMove == WHITE) ? TempoValue : -TempoValue; assert(is_ok()); } @@ -1258,8 +1247,7 @@ void Position::do_null_move(StateInfo& backupSt) { // a backup storage not as a new state to be used. backupSt.key = st->key; backupSt.epSquare = st->epSquare; - backupSt.mgValue = st->mgValue; - backupSt.egValue = st->egValue; + backupSt.value = st->value; backupSt.previous = st->previous; backupSt.pliesFromNull = st->pliesFromNull; st->previous = &backupSt; @@ -1279,10 +1267,8 @@ void Position::do_null_move(StateInfo& backupSt) { st->epSquare = SQ_NONE; st->rule50++; st->pliesFromNull = 0; + st->value += (sideToMove == WHITE) ? TempoValue : -TempoValue; gamePly++; - - st->mgValue += (sideToMove == WHITE)? TempoValueMidgame : -TempoValueMidgame; - st->egValue += (sideToMove == WHITE)? TempoValueEndgame : -TempoValueEndgame; } @@ -1297,8 +1283,7 @@ void Position::undo_null_move() { StateInfo* backupSt = st->previous; st->key = backupSt->key; st->epSquare = backupSt->epSquare; - st->mgValue = backupSt->mgValue; - st->egValue = backupSt->egValue; + st->value = backupSt->value; st->previous = backupSt->previous; st->pliesFromNull = backupSt->pliesFromNull; @@ -1664,8 +1649,8 @@ Value Position::compute_value() const { } } - const Value TempoValue = (Phase == MidGame ? TempoValueMidgame : TempoValueEndgame); - result += (side_to_move() == WHITE)? TempoValue / 2 : -TempoValue / 2; + const Value tv = (Phase == MidGame ? TempoValue.mg() : TempoValue.eg()); + result += (side_to_move() == WHITE)? tv / 2 : -tv / 2; return result; } @@ -1878,8 +1863,7 @@ void Position::flipped_copy(const Position& pos) { st->materialKey = compute_material_key(); // Incremental scores - st->mgValue = compute_value(); - st->egValue = compute_value(); + st->value = Score(compute_value(), compute_value()); // Material st->npMaterial[WHITE] = compute_non_pawn_material(WHITE); @@ -2008,10 +1992,10 @@ bool Position::is_ok(int* failedStep) const { if (failedStep) (*failedStep)++; if (debugIncrementalEval) { - if (st->mgValue != compute_value()) + if (st->value.mg() != compute_value()) return false; - if (st->egValue != compute_value()) + if (st->value.eg() != compute_value()) return false; } diff --git a/src/position.h b/src/position.h index 0e3c16c7..fafb55f7 100644 --- a/src/position.h +++ b/src/position.h @@ -90,7 +90,7 @@ struct StateInfo { Key key, pawnKey, materialKey; int castleRights, rule50, pliesFromNull; Square epSquare; - Value mgValue, egValue; + Score value; Value npMaterial[2]; PieceType capture; @@ -240,8 +240,7 @@ public: Key get_material_key() const; // Incremental evaluation - Value mg_value() const; - Value eg_value() const; + Score value() const; Value non_pawn_material(Color c) const; Phase game_phase() const; template Value pst_delta(Piece piece, Square from, Square to) const; @@ -514,12 +513,8 @@ inline Value Position::pst_delta(Piece piece, Square from, Square to) const { : EgPieceSquareTable[piece][to] - EgPieceSquareTable[piece][from]); } -inline Value Position::mg_value() const { - return st->mgValue; -} - -inline Value Position::eg_value() const { - return st->egValue; +inline Score Position::value() const { + return st->value; } inline Value Position::non_pawn_material(Color c) const { diff --git a/src/uci.cpp b/src/uci.cpp index b9024096..a0ac045b 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -148,8 +148,8 @@ namespace { else if (token == "eval") { EvalInfo ei; - cout << "Incremental mg: " << RootPosition.mg_value() - << "\nIncremental eg: " << RootPosition.eg_value() + cout << "Incremental mg: " << RootPosition.value().mg() + << "\nIncremental eg: " << RootPosition.value().eg() << "\nFull eval: " << evaluate(RootPosition, ei, 0) << endl; } else if (token == "key") -- 2.39.2