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

Zobrist::init() should be Position::init()

No functional change.
This commit is contained in:
Marco Costalba 2013-06-09 13:43:30 +02:00
parent 81e242a96d
commit a6e0f62a4f
4 changed files with 65 additions and 70 deletions

View file

@ -34,7 +34,7 @@ int main(int argc, char* argv[]) {
UCI::init(Options); UCI::init(Options);
Bitboards::init(); Bitboards::init();
Zobrist::init(); Position::init();
Bitbases::init_kpk(); Bitbases::init_kpk();
Search::init(); Search::init();
Eval::init(); Eval::init();

View file

@ -41,67 +41,16 @@ static const string PieceToChar(" PNBRQK pnbrqk");
CACHE_LINE_ALIGNMENT CACHE_LINE_ALIGNMENT
Score pieceSquareTable[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB]; Score psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
Value PieceValue[PHASE_NB][PIECE_NB] = { Value PieceValue[PHASE_NB][PIECE_NB] = {
{ VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg }, { VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg },
{ VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg } }; { VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg } };
namespace Zobrist { Key Zobrist::psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
Key Zobrist::enpassant[FILE_NB];
Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB]; Key Zobrist::castle[CASTLE_RIGHT_NB];
Key enpassant[FILE_NB]; Key Zobrist::side;
Key castle[CASTLE_RIGHT_NB]; Key Zobrist::exclusion;
Key side;
Key exclusion;
/// init() initializes at startup the various arrays used to compute hash keys
/// and the piece square tables. The latter is a two-step operation: First, the
/// white halves of the tables are copied from PSQT[] tables. Second, the black
/// halves of the tables are initialized by flipping and changing the sign of
/// the white scores.
void init() {
RKISS rk;
for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= KING; pt++)
for (Square s = SQ_A1; s <= SQ_H8; s++)
psq[c][pt][s] = rk.rand<Key>();
for (File f = FILE_A; f <= FILE_H; f++)
enpassant[f] = rk.rand<Key>();
for (int cr = CASTLES_NONE; cr <= ALL_CASTLES; cr++)
{
Bitboard b = cr;
while (b)
{
Key k = castle[1ULL << pop_lsb(&b)];
castle[cr] ^= k ? k : rk.rand<Key>();
}
}
side = rk.rand<Key>();
exclusion = rk.rand<Key>();
for (PieceType pt = PAWN; pt <= KING; pt++)
{
PieceValue[MG][make_piece(BLACK, pt)] = PieceValue[MG][pt];
PieceValue[EG][make_piece(BLACK, pt)] = PieceValue[EG][pt];
Score v = make_score(PieceValue[MG][pt], PieceValue[EG][pt]);
for (Square s = SQ_A1; s <= SQ_H8; s++)
{
pieceSquareTable[WHITE][pt][ s] = (v + PSQT[pt][s]);
pieceSquareTable[BLACK][pt][~s] = -(v + PSQT[pt][s]);
}
}
}
} // namespace Zobrist
namespace { namespace {
@ -156,6 +105,53 @@ CheckInfo::CheckInfo(const Position& pos) {
} }
/// Position::init() initializes at startup the various arrays used to compute
/// hash keys and the piece square tables. The latter is a two-step operation:
/// First, the white halves of the tables are copied from PSQT[] tables. Second,
/// the black halves of the tables are initialized by flipping and changing the
/// sign of the white scores.
void Position::init() {
RKISS rk;
for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= KING; pt++)
for (Square s = SQ_A1; s <= SQ_H8; s++)
Zobrist::psq[c][pt][s] = rk.rand<Key>();
for (File f = FILE_A; f <= FILE_H; f++)
Zobrist::enpassant[f] = rk.rand<Key>();
for (int cr = CASTLES_NONE; cr <= ALL_CASTLES; cr++)
{
Bitboard b = cr;
while (b)
{
Key k = Zobrist::castle[1ULL << pop_lsb(&b)];
Zobrist::castle[cr] ^= k ? k : rk.rand<Key>();
}
}
Zobrist::side = rk.rand<Key>();
Zobrist::exclusion = rk.rand<Key>();
for (PieceType pt = PAWN; pt <= KING; pt++)
{
PieceValue[MG][make_piece(BLACK, pt)] = PieceValue[MG][pt];
PieceValue[EG][make_piece(BLACK, pt)] = PieceValue[EG][pt];
Score v = make_score(PieceValue[MG][pt], PieceValue[EG][pt]);
for (Square s = SQ_A1; s <= SQ_H8; s++)
{
psq[WHITE][pt][ s] = (v + PSQT[pt][s]);
psq[BLACK][pt][~s] = -(v + PSQT[pt][s]);
}
}
}
/// Position::operator=() creates a copy of 'pos'. We want the new born Position /// Position::operator=() creates a copy of 'pos'. We want the new born Position
/// object do not depend on any external data so we detach state pointer from /// object do not depend on any external data so we detach state pointer from
/// the source one. /// the source one.
@ -288,7 +284,7 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) {
st->key = compute_key(); st->key = compute_key();
st->pawnKey = compute_pawn_key(); st->pawnKey = compute_pawn_key();
st->materialKey = compute_material_key(); st->materialKey = compute_material_key();
st->psqScore = compute_psq_score(); st->psq = compute_psq_score();
st->npMaterial[WHITE] = compute_non_pawn_material(WHITE); st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
st->npMaterial[BLACK] = compute_non_pawn_material(BLACK); st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(~sideToMove); st->checkersBB = attackers_to(king_square(sideToMove)) & pieces(~sideToMove);
@ -767,7 +763,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
do_castle(from, to, rfrom, rto); do_castle(from, to, rfrom, rto);
st->psqScore += pieceSquareTable[us][ROOK][rto] - pieceSquareTable[us][ROOK][rfrom]; st->psq += psq[us][ROOK][rto] - psq[us][ROOK][rfrom];
k ^= Zobrist::psq[us][ROOK][rfrom] ^ Zobrist::psq[us][ROOK][rto]; k ^= Zobrist::psq[us][ROOK][rfrom] ^ Zobrist::psq[us][ROOK][rto];
} }
@ -820,7 +816,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
prefetch((char*)thisThread->materialTable[st->materialKey]); prefetch((char*)thisThread->materialTable[st->materialKey]);
// Update incremental scores // Update incremental scores
st->psqScore -= pieceSquareTable[them][capture][capsq]; st->psq -= psq[them][capture][capsq];
// Reset rule 50 counter // Reset rule 50 counter
st->rule50 = 0; st->rule50 = 0;
@ -903,7 +899,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
^ Zobrist::psq[us][PAWN][pieceCount[us][PAWN]]; ^ Zobrist::psq[us][PAWN][pieceCount[us][PAWN]];
// Update incremental score // Update incremental score
st->psqScore += pieceSquareTable[us][promotion][to] - pieceSquareTable[us][PAWN][to]; st->psq += psq[us][promotion][to] - psq[us][PAWN][to];
// Update material // Update material
st->npMaterial[us] += PieceValue[MG][promotion]; st->npMaterial[us] += PieceValue[MG][promotion];
@ -918,7 +914,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
} }
// Update incremental scores // Update incremental scores
st->psqScore += pieceSquareTable[us][pt][to] - pieceSquareTable[us][pt][from]; st->psq += psq[us][pt][to] - psq[us][pt][from];
// Set capture piece // Set capture piece
st->capturedType = capture; st->capturedType = capture;
@ -1350,7 +1346,7 @@ Score Position::compute_psq_score() const {
{ {
Square s = pop_lsb(&b); Square s = pop_lsb(&b);
Piece pc = piece_on(s); Piece pc = piece_on(s);
score += pieceSquareTable[color_of(pc)][type_of(pc)][s]; score += psq[color_of(pc)][type_of(pc)][s];
} }
return score; return score;
@ -1445,7 +1441,7 @@ void Position::flip() {
st->key = compute_key(); st->key = compute_key();
st->pawnKey = compute_pawn_key(); st->pawnKey = compute_pawn_key();
st->materialKey = compute_material_key(); st->materialKey = compute_material_key();
st->psqScore = compute_psq_score(); st->psq = compute_psq_score();
st->npMaterial[WHITE] = compute_non_pawn_material(WHITE); st->npMaterial[WHITE] = compute_non_pawn_material(WHITE);
st->npMaterial[BLACK] = compute_non_pawn_material(BLACK); st->npMaterial[BLACK] = compute_non_pawn_material(BLACK);
@ -1536,7 +1532,7 @@ bool Position::pos_is_ok(int* failedStep) const {
if ((*step)++, debugMaterialKey && st->materialKey != compute_material_key()) if ((*step)++, debugMaterialKey && st->materialKey != compute_material_key())
return false; return false;
if ((*step)++, debugIncrementalEval && st->psqScore != compute_psq_score()) if ((*step)++, debugIncrementalEval && st->psq != compute_psq_score())
return false; return false;
if ((*step)++, debugNonPawnMaterial) if ((*step)++, debugNonPawnMaterial)

View file

@ -52,7 +52,7 @@ struct StateInfo {
Key pawnKey, materialKey; Key pawnKey, materialKey;
Value npMaterial[COLOR_NB]; Value npMaterial[COLOR_NB];
int castleRights, rule50, pliesFromNull; int castleRights, rule50, pliesFromNull;
Score psqScore; Score psq;
Square epSquare; Square epSquare;
Key key; Key key;
@ -95,6 +95,7 @@ public:
Position(const Position& p, Thread* t) { *this = p; thisThread = t; } Position(const Position& p, Thread* t) { *this = p; thisThread = t; }
Position(const std::string& f, bool c960, Thread* t) { set(f, c960, t); } Position(const std::string& f, bool c960, Thread* t) { set(f, c960, t); }
Position& operator=(const Position&); Position& operator=(const Position&);
static void init();
// Text input/output // Text input/output
void set(const std::string& fen, bool isChess960, Thread* th); void set(const std::string& fen, bool isChess960, Thread* th);
@ -358,7 +359,7 @@ inline Key Position::material_key() const {
} }
inline Score Position::psq_score() const { inline Score Position::psq_score() const {
return st->psqScore; return st->psq;
} }
inline Value Position::non_pawn_material(Color c) const { inline Value Position::non_pawn_material(Color c) const {

View file

@ -335,8 +335,6 @@ namespace Zobrist {
extern Key castle[CASTLE_RIGHT_NB]; extern Key castle[CASTLE_RIGHT_NB];
extern Key side; extern Key side;
extern Key exclusion; extern Key exclusion;
void init();
} }
extern Value PieceValue[PHASE_NB][PIECE_NB]; extern Value PieceValue[PHASE_NB][PIECE_NB];