]> git.sesse.net Git - stockfish/commitdiff
Cleanup work in misc.cpp
authorMarco Costalba <mcostalba@gmail.com>
Sat, 9 May 2015 09:09:06 +0000 (11:09 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 10 May 2015 07:42:26 +0000 (09:42 +0200)
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
src/misc.cpp
src/movegen.cpp
src/position.cpp
src/position.h
src/psqt.cpp
src/search.cpp
src/tt.h

index f76ed31c3c8263cd7cfb87698f658ed6caab8555..7ab798a39f64c6b74a92aa33e4b2822f7ee3b1cf 100644 (file)
@@ -201,17 +201,15 @@ void Bitboards::init() {
       PseudoAttacks[QUEEN][s1]  = PseudoAttacks[BISHOP][s1] = attacks_bb<BISHOP>(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]);
+          }
   }
 }
 
index 7df4287a3203df480b706672be1552493a35b627..c75f2ecf421f952dd1c13d25cea5228321b52ddc 100644 (file)
@@ -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); }
index 4a0a34ef4704f1d0f8d8e7bf50a26205d1904767..51adc5b4c0296d60914c3d97f371ada9a657439b 100644 (file)
@@ -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<Pt>(from) & target;
 
         if (Checks)
-            b &= ci->checkSq[Pt];
+            b &= ci->checkSquares[Pt];
 
         while (b)
             *moveList++ = make_move(from, pop_lsb(&b));
index 1aa476b6dea9529d5a68cc565c86716f44a6b27f..87e65d02fcf26cac93c5b935e67a08cfa9181262 100644 (file)
@@ -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<PAWN>(ksq, them);
-  checkSq[KNIGHT] = pos.attacks_from<KNIGHT>(ksq);
-  checkSq[BISHOP] = pos.attacks_from<BISHOP>(ksq);
-  checkSq[ROOK]   = pos.attacks_from<ROOK>(ksq);
-  checkSq[QUEEN]  = checkSq[BISHOP] | checkSq[ROOK];
-  checkSq[KING]   = 0;
+  checkSquares[PAWN]   = pos.attacks_from<PAWN>(ksq, them);
+  checkSquares[KNIGHT] = pos.attacks_from<KNIGHT>(ksq);
+  checkSquares[BISHOP] = pos.attacks_from<BISHOP>(ksq);
+  checkSquares[ROOK]   = pos.attacks_from<ROOK>(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?
index e38020d5b5a2188661103f46f4e12506ff1e48e8..516f3f1b0c1bda1d2c3f2ba47f3f342ca7f22740 100644 (file)
@@ -46,7 +46,7 @@ struct CheckInfo {
 
   Bitboard dcCandidates;
   Bitboard pinned;
-  Bitboard checkSq[PIECE_TYPE_NB];
+  Bitboard checkSquares[PIECE_TYPE_NB];
   Square   ksq;
 };
 
index 5e42d76e14bf14fa5b0b9f5338d863521daf61bb..d405526019fdf0773d15e0e65f4ce4ca8a962f51 100644 (file)
@@ -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]);
       }
   }
index 152e9fd68c4812854d0ce3d098581586fb56102f..d419cd30ede15e192142b040de9caf14d8b8005c 100644 (file)
@@ -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
index 48c5638a7d971d1ed3c581a37b7a87529948d462..f243a39d483335b61496418bf3f35d9ef03a34ff 100644 (file)
--- 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: