From dda7e4639aa19b91f2bafd2e8fb5b55c57eda97a Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 7 Nov 2009 13:12:58 +0100 Subject: [PATCH] Introduce PieceSquareTable[16][64] Instead of MgPieceSquareTable[16][64] and EgPieceSquareTable[16][64] This allows to fetch mg and eg values from adjacent words in memory. No functional change. Signed-off-by: Marco Costalba --- src/movepick.cpp | 2 +- src/position.cpp | 47 +++++++++++++++++------------------------------ src/position.h | 21 ++++++++------------- src/value.h | 5 +++++ 4 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/movepick.cpp b/src/movepick.cpp index b96b7995..ca99e461 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -221,7 +221,7 @@ void MovePicker::score_noncaptures() { hs += 1000; // pst based scoring - cur->score = hs + pos.pst_delta(piece, from, to); + cur->score = hs + pos.pst_delta(piece, from, to).mg(); } } diff --git a/src/position.cpp b/src/position.cpp index e7a0017c..c43cde79 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -52,8 +52,7 @@ Key Position::zobCastle[16]; Key Position::zobMaterial[2][8][16]; Key Position::zobSideToMove; -Value Position::MgPieceSquareTable[16][64]; -Value Position::EgPieceSquareTable[16][64]; +Score Position::PieceSquareTable[16][64]; static bool RequestPending = false; @@ -213,7 +212,7 @@ void Position::from_fen(const string& fen) { st->key = compute_key(); st->pawnKey = compute_pawn_key(); st->materialKey = compute_material_key(); - st->value = Score(compute_value(), compute_value()); + st->value = compute_value(); st->npMaterial[WHITE] = compute_non_pawn_material(WHITE); st->npMaterial[BLACK] = compute_non_pawn_material(BLACK); } @@ -829,7 +828,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) { } // Update incremental scores - st->value += Score(pst_delta(piece, from, to), pst_delta(piece, from, to)); + st->value += pst_delta(piece, from, to); if (pm) // promotion ? { @@ -864,8 +863,8 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) { st->pawnKey ^= zobrist[us][PAWN][to]; // Partially revert and update incremental scores - st->value -= Score(pst(us, PAWN, to), pst(us, PAWN, to)); - st->value += Score(pst(us, promotion, to), pst(us, promotion, to)); + st->value -= pst(us, PAWN, to); + st->value += pst(us, promotion, to); // Update material st->npMaterial[us] += piece_value_midgame(promotion); @@ -931,7 +930,7 @@ void Position::do_capture_move(Bitboard& key, PieceType capture, Color them, Squ key ^= zobrist[them][capture][capsq]; // Update incremental scores - st->value -= Score(pst(them, capture, capsq), pst(them, capture, capsq)); + st->value -= pst(them, capture, capsq); // If the captured piece was a pawn, update pawn hash key, // otherwise update non-pawn material. @@ -1028,8 +1027,8 @@ void Position::do_castle_move(Move m) { index[rto] = tmp; // Update incremental scores - 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)); + st->value += pst_delta(king, kfrom, kto); + st->value += pst_delta(rook, rfrom, rto); // Update hash key st->key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto]; @@ -1630,10 +1629,9 @@ Key Position::compute_material_key() const { /// game and the endgame. These functions are used to initialize the incremental /// scores when a new position is set up, and to verify that the scores are correctly /// updated by do_move and undo_move when the program is running in debug mode. -template -Value Position::compute_value() const { +Score Position::compute_value() const { - Value result = Value(0); + Score result(0, 0); Bitboard b; Square s; @@ -1645,12 +1643,11 @@ Value Position::compute_value() const { { s = pop_1st_bit(&b); assert(piece_on(s) == piece_of_color_and_type(c, pt)); - result += pst(c, pt, s); + result += pst(c, pt, s); } } - const Value tv = (Phase == MidGame ? TempoValue.mg() : TempoValue.eg()); - result += (side_to_move() == WHITE)? tv / 2 : -tv / 2; + result += (side_to_move() == WHITE)? TempoValue / 2 : -TempoValue / 2; return result; } @@ -1799,16 +1796,12 @@ void Position::init_piece_square_tables() { for (Piece p = WP; p <= WK; p++) { i = (r == 0)? 0 : (genrand_int32() % (r*2) - r); - MgPieceSquareTable[p][s] = Value(MgPST[p][s] + i); - EgPieceSquareTable[p][s] = Value(EgPST[p][s] + i); + PieceSquareTable[p][s] = Score(MgPST[p][s] + i, EgPST[p][s] + i); } for (Square s = SQ_A1; s <= SQ_H8; s++) for (Piece p = BP; p <= BK; p++) - { - MgPieceSquareTable[p][s] = -MgPieceSquareTable[p-8][flip_square(s)]; - EgPieceSquareTable[p][s] = -EgPieceSquareTable[p-8][flip_square(s)]; - } + PieceSquareTable[p][s] = -PieceSquareTable[p-8][flip_square(s)]; } @@ -1863,7 +1856,7 @@ void Position::flipped_copy(const Position& pos) { st->materialKey = compute_material_key(); // Incremental scores - st->value = Score(compute_value(), compute_value()); + st->value = compute_value(); // Material st->npMaterial[WHITE] = compute_non_pawn_material(WHITE); @@ -1990,14 +1983,8 @@ bool Position::is_ok(int* failedStep) const { // Incremental eval OK? if (failedStep) (*failedStep)++; - if (debugIncrementalEval) - { - if (st->value.mg() != compute_value()) - return false; - - if (st->value.eg() != compute_value()) - return false; - } + if (debugIncrementalEval && st->value != compute_value()) + return false; // Non-pawn material OK? if (failedStep) (*failedStep)++; diff --git a/src/position.h b/src/position.h index fafb55f7..e2365a68 100644 --- a/src/position.h +++ b/src/position.h @@ -243,7 +243,7 @@ public: 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; + Score pst_delta(Piece piece, Square from, Square to) const; // Game termination checks bool is_mate() const; @@ -295,8 +295,8 @@ private: Key compute_material_key() const; // Computing incremental evaluation scores and material counts - template Value pst(Color c, PieceType pt, Square s) const; - template Value compute_value() const; + Score pst(Color c, PieceType pt, Square s) const; + Score compute_value() const; Value compute_non_pawn_material(Color c) const; // Board @@ -327,8 +327,7 @@ private: static Key zobCastle[16]; static Key zobMaterial[2][8][16]; static Key zobSideToMove; - static Value MgPieceSquareTable[16][64]; - static Value EgPieceSquareTable[16][64]; + static Score PieceSquareTable[16][64]; }; @@ -501,16 +500,12 @@ inline Key Position::get_material_key() const { return st->materialKey; } -template -inline Value Position::pst(Color c, PieceType pt, Square s) const { - return (Ph == MidGame ? MgPieceSquareTable[piece_of_color_and_type(c, pt)][s] - : EgPieceSquareTable[piece_of_color_and_type(c, pt)][s]); +inline Score Position::pst(Color c, PieceType pt, Square s) const { + return PieceSquareTable[piece_of_color_and_type(c, pt)][s]; } -template -inline Value Position::pst_delta(Piece piece, Square from, Square to) const { - return (Ph == MidGame ? MgPieceSquareTable[piece][to] - MgPieceSquareTable[piece][from] - : EgPieceSquareTable[piece][to] - EgPieceSquareTable[piece][from]); +inline Score Position::pst_delta(Piece piece, Square from, Square to) const { + return PieceSquareTable[piece][to] - PieceSquareTable[piece][from]; } inline Score Position::value() const { diff --git a/src/value.h b/src/value.h index ee093a6e..c86430c6 100644 --- a/src/value.h +++ b/src/value.h @@ -73,6 +73,9 @@ struct Score { Score& operator+=(const Score& s) { v.v32.mgv += s.v.v32.mgv; v.v32.egv += s.v.v32.egv; return *this; } Score& operator-=(const Score& s) { v.v32.mgv -= s.v.v32.mgv; v.v32.egv -= s.v.v32.egv; return *this; } + bool operator==(const Score& s) { return v.v64 == s.v.v64; } + bool operator!=(const Score& s) { return v.v64 != s.v.v64; } + Value mg() const { return Value(v.v32.mgv); } Value eg() const { return Value(v.v32.egv); } @@ -80,8 +83,10 @@ private: ScoreValue v; }; +inline Score operator-(Score s1, Score s2) { return Score(s1.mg() - s2.mg(), s1.eg() - s2.eg()); } inline Score operator*(int i, Score s) { return Score(i * s.mg(), i * s.eg()); } inline Score operator*(Score s, int i) { return s * i; } +inline Score operator/(Score s, int i) { return Score(s.mg() / i, s.eg() / i); } inline Score operator-(Score s) { return Score(-s.mg(), -s.eg()); } extern std::ostream& operator<<(std::ostream& os, Score s); -- 2.39.2