From ee0371f86e319aa24bc1d32f02d9495eea79aa72 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 9 May 2015 11:09:06 +0200 Subject: [PATCH] Cleanup work in misc.cpp Also some code style tidy up of latest patches. Also renamed checkSq -> checkSquares because it is a bitboard and not a square. No functional change. --- src/bitboard.cpp | 18 ++++++++---------- src/misc.cpp | 23 ++++++++++------------- src/movegen.cpp | 4 ++-- src/position.cpp | 14 +++++++------- src/position.h | 2 +- src/psqt.cpp | 2 +- src/search.cpp | 16 ++++++++-------- src/tt.h | 44 ++++++++++++++++---------------------------- 8 files changed, 53 insertions(+), 70 deletions(-) diff --git a/src/bitboard.cpp b/src/bitboard.cpp index f76ed31c..7ab798a3 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -201,17 +201,15 @@ void Bitboards::init() { PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb(s1, 0); PseudoAttacks[QUEEN][s1] |= PseudoAttacks[ ROOK][s1] = attacks_bb< ROOK>(s1, 0); - for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2) - { - Piece pc = (PseudoAttacks[BISHOP][s1] & s2) ? W_BISHOP : - (PseudoAttacks[ROOK][s1] & s2) ? W_ROOK : NO_PIECE; - - if (pc == NO_PIECE) - continue; + for (Piece pc = W_BISHOP; pc <= W_ROOK; ++pc) + for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2) + { + if (!(PseudoAttacks[pc][s1] & s2)) + continue; - LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2; - BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]); - } + LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2; + BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]); + } } } diff --git a/src/misc.cpp b/src/misc.cpp index 7df4287a..c75f2ecf 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -33,41 +33,37 @@ namespace { /// DD-MM-YY and show in engine_info. const string Version = ""; -/// Debug counters -int64_t hits[2], means[2]; - /// Our fancy logging facility. The trick here is to replace cin.rdbuf() and /// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We /// can toggle the logging of std::cout and std:cin at runtime whilst preserving -/// usual i/o functionality, all without changing a single line of code! +/// usual I/O functionality, all without changing a single line of code! /// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81 struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout - Tie(streambuf* b, ofstream* f) : buf(b), file(f) {} + Tie(streambuf* b, streambuf* l) : buf(b), logBuf(l) {} - int sync() { return file->rdbuf()->pubsync(), buf->pubsync(); } + int sync() { return logBuf->pubsync(), buf->pubsync(); } int overflow(int c) { return log(buf->sputc((char)c), "<< "); } int underflow() { return buf->sgetc(); } int uflow() { return log(buf->sbumpc(), ">> "); } - streambuf* buf; - ofstream* file; + streambuf *buf, *logBuf; int log(int c, const char* prefix) { - static int last = '\n'; + static int last = '\n'; // Single log file if (last == '\n') - file->rdbuf()->sputn(prefix, 3); + logBuf->sputn(prefix, 3); - return last = file->rdbuf()->sputc((char)c); + return last = logBuf->sputc((char)c); } }; class Logger { - Logger() : in(cin.rdbuf(), &file), out(cout.rdbuf(), &file) {} + Logger() : in(cin.rdbuf(), file.rdbuf()), out(cout.rdbuf(), file.rdbuf()) {} ~Logger() { start(false); } ofstream file; @@ -80,7 +76,7 @@ public: if (b && !l.file.is_open()) { - l.file.open("io_log.txt", ifstream::out | ifstream::app); + l.file.open("io_log.txt", ifstream::out); cin.rdbuf(&l.in); cout.rdbuf(&l.out); } @@ -124,6 +120,7 @@ const string engine_info(bool to_uci) { /// Debug functions used mainly to collect run-time statistics +static int64_t hits[2], means[2]; void dbg_hit_on(bool b) { ++hits[0]; if (b) ++hits[1]; } void dbg_hit_on(bool c, bool b) { if (c) dbg_hit_on(b); } diff --git a/src/movegen.cpp b/src/movegen.cpp index 4a0a34ef..51adc5b4 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -232,7 +232,7 @@ namespace { if (Checks) { if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN) - && !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt])) + && !(PseudoAttacks[Pt][from] & target & ci->checkSquares[Pt])) continue; if (ci->dcCandidates && (ci->dcCandidates & from)) @@ -242,7 +242,7 @@ namespace { Bitboard b = pos.attacks_from(from) & target; if (Checks) - b &= ci->checkSq[Pt]; + b &= ci->checkSquares[Pt]; while (b) *moveList++ = make_move(from, pop_lsb(&b)); diff --git a/src/position.cpp b/src/position.cpp index 1aa476b6..87e65d02 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -94,12 +94,12 @@ CheckInfo::CheckInfo(const Position& pos) { pinned = pos.pinned_pieces(pos.side_to_move()); dcCandidates = pos.discovered_check_candidates(); - checkSq[PAWN] = pos.attacks_from(ksq, them); - checkSq[KNIGHT] = pos.attacks_from(ksq); - checkSq[BISHOP] = pos.attacks_from(ksq); - checkSq[ROOK] = pos.attacks_from(ksq); - checkSq[QUEEN] = checkSq[BISHOP] | checkSq[ROOK]; - checkSq[KING] = 0; + checkSquares[PAWN] = pos.attacks_from(ksq, them); + checkSquares[KNIGHT] = pos.attacks_from(ksq); + checkSquares[BISHOP] = pos.attacks_from(ksq); + checkSquares[ROOK] = pos.attacks_from(ksq); + checkSquares[QUEEN] = checkSquares[BISHOP] | checkSquares[ROOK]; + checkSquares[KING] = 0; } @@ -616,7 +616,7 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const { Square to = to_sq(m); // Is there a direct check? - if (ci.checkSq[type_of(piece_on(from))] & to) + if (ci.checkSquares[type_of(piece_on(from))] & to) return true; // Is there a discovered check? diff --git a/src/position.h b/src/position.h index e38020d5..516f3f1b 100644 --- a/src/position.h +++ b/src/position.h @@ -46,7 +46,7 @@ struct CheckInfo { Bitboard dcCandidates; Bitboard pinned; - Bitboard checkSq[PIECE_TYPE_NB]; + Bitboard checkSquares[PIECE_TYPE_NB]; Square ksq; }; diff --git a/src/psqt.cpp b/src/psqt.cpp index 5e42d76e..d4055260 100644 --- a/src/psqt.cpp +++ b/src/psqt.cpp @@ -109,7 +109,7 @@ void init() { for (Square s = SQ_A1; s <= SQ_H8; ++s) { - int edgeDistance = int(file_of(s) < FILE_E ? file_of(s) : FILE_H - file_of(s)); + int edgeDistance = file_of(s) < FILE_E ? file_of(s) : FILE_H - file_of(s); psq[BLACK][pt][~s] = -(psq[WHITE][pt][s] = v + Bonus[pt][rank_of(s)][edgeDistance]); } } diff --git a/src/search.cpp b/src/search.cpp index 152e9fd6..d419cd30 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -181,15 +181,15 @@ void Search::init() { } -/// Search::reset() clears all search memory, to restore a deterministic state +/// Search::reset() clears all search memory, to obtain reproducible search results void Search::reset () { - TT.clear(); - History.clear(); - CounterMovesHistory.clear(); - Gains.clear(); - Countermoves.clear(); + TT.clear(); + History.clear(); + CounterMovesHistory.clear(); + Gains.clear(); + Countermoves.clear(); } @@ -858,7 +858,7 @@ moves_loop: // When in check and at SpNode search starts from here captureOrPromotion = pos.capture_or_promotion(move); givesCheck = type_of(move) == NORMAL && !ci.dcCandidates - ? ci.checkSq[type_of(pos.piece_on(from_sq(move)))] & to_sq(move) + ? ci.checkSquares[type_of(pos.piece_on(from_sq(move)))] & to_sq(move) : pos.gives_check(move, ci); dangerous = givesCheck @@ -1281,7 +1281,7 @@ moves_loop: // When in check and at SpNode search starts from here assert(is_ok(move)); givesCheck = type_of(move) == NORMAL && !ci.dcCandidates - ? ci.checkSq[type_of(pos.piece_on(from_sq(move)))] & to_sq(move) + ? ci.checkSquares[type_of(pos.piece_on(from_sq(move)))] & to_sq(move) : pos.gives_check(move, ci); // Futility pruning diff --git a/src/tt.h b/src/tt.h index 48c5638a..f243a39d 100644 --- a/src/tt.h +++ b/src/tt.h @@ -43,34 +43,22 @@ struct TTEntry { void save(Key k, Value v, Bound b, Depth d, Move m, Value ev, uint8_t g) { - if (key16 != (k >> 48)) - { - key16 = (uint16_t)(k >> 48); - move16 = (uint16_t)m; - value16 = (int16_t)v; - eval16 = (int16_t)ev; - genBound8 = (uint8_t)(g | b); - depth8 = (int8_t)d; - } - else - { - // Preserve any existing move for the same position - if (m) - move16 = (uint16_t)m; - - // Don't overwrite more valuable values - if ( d + 2 > depth8 - || g != (genBound8 & 0xFC) - || b == BOUND_EXACT) - { - value16 = (int16_t)v; - genBound8 = (uint8_t)(g | b); - depth8 = (int8_t)d; - } - - if (ev != VALUE_NONE) - eval16 = (int16_t)ev; - } + // Preserve any existing move for the same position + if (m || (k >> 48) != key16) + move16 = (uint16_t)m; + + // Don't overwrite more valuable entries + if ( (k >> 48) != key16 + || d > depth8 - 2 + || g != (genBound8 & 0xFC) + || b == BOUND_EXACT) + { + key16 = (uint16_t)(k >> 48); + value16 = (int16_t)v; + eval16 = (int16_t)ev; + genBound8 = (uint8_t)(g | b); + depth8 = (int8_t)d; + } } private: -- 2.39.2