]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Use namespace in position.cpp
[stockfish] / src / position.cpp
index 3b7916bf4b2e9cc4e1602c60c81e06d22fc9d3b5..b5503cb501fbbcac919ae542231942203ec8f2eb 100644 (file)
 #include <sstream>
 
 #include "bitcount.h"
-#include "mersenne.h"
 #include "movegen.h"
 #include "movepick.h"
 #include "position.h"
 #include "psqtab.h"
+#include "rkiss.h"
 #include "san.h"
 #include "tt.h"
 #include "ucioption.h"
@@ -44,9 +44,35 @@ using std::string;
 using std::cout;
 using std::endl;
 
-static inline bool isZero(char c) { return c == '0'; }
 
-struct PieceLetters : public std::map<char, Piece> {
+////
+//// Position's static data definitions
+////
+
+Key Position::zobrist[2][8][64];
+Key Position::zobEp[64];
+Key Position::zobCastle[16];
+Key Position::zobSideToMove;
+Key Position::zobExclusion;
+
+Score Position::PieceSquareTable[16][64];
+
+// Material values used by SEE, indexed by PieceType
+const Value Position::seeValues[] = {
+    VALUE_ZERO,
+    PawnValueMidgame, KnightValueMidgame, BishopValueMidgame,
+    RookValueMidgame, QueenValueMidgame, QueenValueMidgame*10
+};
+
+
+namespace {
+
+  // Bonus for having the side to move (modified by Joona Kiiski)
+  const Score TempoValue = make_score(48, 22);
+
+  bool isZero(char c) { return c == '0'; }
+
+  struct PieceLetters : public std::map<char, Piece> {
 
     PieceLetters() {
 
@@ -69,36 +95,11 @@ struct PieceLetters : public std::map<char, Piece> {
         assert(false);
         return 0;
     }
-};
-
-
-////
-//// Constants and variables
-////
-
-/// Bonus for having the side to move (modified by Joona Kiiski)
-
-static const Score TempoValue = make_score(48, 22);
-
-
-Key Position::zobrist[2][8][64];
-Key Position::zobEp[64];
-Key Position::zobCastle[16];
-Key Position::zobSideToMove;
-Key Position::zobExclusion;
-
-Score Position::PieceSquareTable[16][64];
-
-static PieceLetters pieceLetters;
-
-// Material values used by SEE, indexed by PieceType
-const Value Position::seeValues[] = {
-  VALUE_ZERO, PawnValueMidgame, KnightValueMidgame, BishopValueMidgame,
-  RookValueMidgame, QueenValueMidgame, QueenValueMidgame*10
-};
+  } pieceLetters;
+}
 
 
-/// Constructors
+/// CheckInfo c'tor
 
 CheckInfo::CheckInfo(const Position& pos) {
 
@@ -121,13 +122,12 @@ CheckInfo::CheckInfo(const Position& pos) {
 /// or the FEN string, we want the new born Position object do not depend
 /// on any external data so we detach state pointer from the source one.
 
-Position::Position(int th) : threadID(th) {}
-
 Position::Position(const Position& pos, int th) {
 
   memcpy(this, &pos, sizeof(Position));
   detach(); // Always detach() in copy c'tor to avoid surprises
   threadID = th;
+  nodes = 0;
 }
 
 Position::Position(const string& fen, int th) {
@@ -382,7 +382,7 @@ const string Position::to_fen() const {
 
 
 /// Position::print() prints an ASCII representation of the position to
-/// the standard output. If a move is given then also the san is print.
+/// the standard output. If a move is given then also the san is printed.
 
 void Position::print(Move move) const {
 
@@ -523,6 +523,7 @@ bool Position::move_attacks_square(Move m, Square s) const {
   assert(move_is_ok(m));
   assert(square_is_ok(s));
 
+  Bitboard occ, xray;
   Square f = move_from(m), t = move_to(m);
 
   assert(square_is_occupied(f));
@@ -531,12 +532,11 @@ bool Position::move_attacks_square(Move m, Square s) const {
       return true;
 
   // Move the piece and scan for X-ray attacks behind it
-  Bitboard occ = occupied_squares();
-  Color us = color_of_piece_on(f);
-  clear_bit(&occ, f);
-  set_bit(&occ, t);
-  Bitboard xray = ( (rook_attacks_bb(s, occ) &  pieces(ROOK, QUEEN))
-                   |(bishop_attacks_bb(s, occ) & pieces(BISHOP, QUEEN))) & pieces_of_color(us);
+  occ = occupied_squares();
+  do_move_bb(&occ, make_move_bb(f, t));
+  xray = ( (rook_attacks_bb(s, occ)   & pieces(ROOK, QUEEN))
+          |(bishop_attacks_bb(s, occ) & pieces(BISHOP, QUEEN)))
+         & pieces_of_color(color_of_piece_on(f));
 
   // If we have attacks we need to verify that are caused by our move
   // and are not already existent ones.
@@ -752,6 +752,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
   assert(is_ok());
   assert(move_is_ok(m));
 
+  nodes++;
   Key key = st->key;
 
   // Copy some fields of old state to our new StateInfo object except the
@@ -765,7 +766,9 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
     Value npMaterial[2];
   };
 
-  memcpy(&newSt, st, sizeof(ReducedStateInfo));
+  if (&newSt != st)
+      memcpy(&newSt, st, sizeof(ReducedStateInfo));
+
   newSt.previous = st;
   st = &newSt;
 
@@ -1477,6 +1480,7 @@ void Position::clear() {
   memset(st, 0, sizeof(StateInfo));
   st->epSquare = SQ_NONE;
   startPosPlyCounter = 0;
+  nodes = 0;
 
   memset(byColorBB,  0, sizeof(Bitboard) * 2);
   memset(byTypeBB,   0, sizeof(Bitboard) * 8);
@@ -1561,7 +1565,7 @@ void Position::allow_ooo(Color c) {
 
 Key Position::compute_key() const {
 
-  Key result = Key(0ULL);
+  Key result = 0;
 
   for (Square s = SQ_A1; s <= SQ_H8; s++)
       if (square_is_occupied(s))
@@ -1586,7 +1590,7 @@ Key Position::compute_key() const {
 
 Key Position::compute_pawn_key() const {
 
-  Key result = Key(0ULL);
+  Key result = 0;
   Bitboard b;
   Square s;
 
@@ -1611,7 +1615,7 @@ Key Position::compute_pawn_key() const {
 
 Key Position::compute_material_key() const {
 
-  Key result = Key(0ULL);
+  Key result = 0;
   for (Color c = WHITE; c <= BLACK; c++)
       for (PieceType pt = PAWN; pt <= QUEEN; pt++)
       {
@@ -1703,7 +1707,7 @@ bool Position::is_draw() const {
 
 bool Position::is_mate() const {
 
-  MoveStack moves[256];
+  MoveStack moves[MOVES_MAX];
   return is_check() && (generate_moves(*this, moves) == moves);
 }
 
@@ -1713,7 +1717,7 @@ bool Position::is_mate() const {
 
 bool Position::has_mate_threat() {
 
-  MoveStack mlist[256], *last, *cur;
+  MoveStack mlist[MOVES_MAX], *last, *cur;
   StateInfo st1, st2;
   bool mateFound = false;
 
@@ -1756,19 +1760,20 @@ bool Position::has_mate_threat() {
 
 void Position::init_zobrist() {
 
+  RKISS RKiss;
   int i,j, k;
 
   for (i = 0; i < 2; i++) for (j = 0; j < 8; j++) for (k = 0; k < 64; k++)
-      zobrist[i][j][k] = Key(genrand_int64());
+      zobrist[i][j][k] = RKiss.rand<Key>();
 
   for (i = 0; i < 64; i++)
-      zobEp[i] = Key(genrand_int64());
+      zobEp[i] = RKiss.rand<Key>();
 
   for (i = 0; i < 16; i++)
-      zobCastle[i] = Key(genrand_int64());
+      zobCastle[i] = RKiss.rand<Key>();
 
-  zobSideToMove = Key(genrand_int64());
-  zobExclusion  = Key(genrand_int64());
+  zobSideToMove = RKiss.rand<Key>();
+  zobExclusion  = RKiss.rand<Key>();
 }