]> git.sesse.net Git - stockfish/commitdiff
Pass CastlingFlag argument only
authorMarco Costalba <mcostalba@gmail.com>
Sun, 2 Mar 2014 10:53:17 +0000 (11:53 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 2 Mar 2014 12:21:19 +0000 (13:21 +0100)
Instead of Color and CastlingSide. Change functions API
accordingly.

No functional change.

src/movegen.cpp
src/pawns.cpp
src/position.cpp
src/position.h
src/types.h

index 2591473514a9068532c43d46a636befa30341b06..39c372924a322dcd676666e34f8222d5c7c74520 100644 (file)
                                          (mlist++)->move = make_move(to - (d), to); }
 namespace {
 
-  template<CastlingSide Side, bool Checks, bool Chess960>
+  template<CastlingFlag Cf, bool Checks, bool Chess960>
   ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {
 
-    if (pos.castling_impeded(us, Side) || !pos.can_castle(make_castling_flag(us, Side)))
+    static const bool KingSide = (Cf == WHITE_OO || Cf == BLACK_OO);
+
+    if (pos.castling_impeded(Cf) || !pos.can_castle(Cf))
         return mlist;
 
     // After castling, the rook and king final positions are the same in Chess960
     // as they would be in standard chess.
     Square kfrom = pos.king_square(us);
-    Square rfrom = pos.castling_rook_square(us, Side);
-    Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
+    Square rfrom = pos.castling_rook_square(Cf);
+    Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
     Bitboard enemies = pos.pieces(~us);
 
     assert(!pos.checkers());
 
-    const Square K = Chess960 ? kto > kfrom       ? DELTA_W : DELTA_E
-                              : Side == KING_SIDE ? DELTA_W : DELTA_E;
+    const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
+                              : KingSide    ? DELTA_W : DELTA_E;
 
     for (Square s = kto; s != kfrom; s += K)
         if (pos.attackers_to(s) & enemies)
@@ -262,13 +264,13 @@ namespace {
     {
         if (pos.is_chess960())
         {
-            mlist = generate_castling< KING_SIDE, Checks, true>(pos, mlist, Us, ci);
-            mlist = generate_castling<QUEEN_SIDE, Checks, true>(pos, mlist, Us, ci);
+            mlist = generate_castling<MakeCastling<Us,  KING_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
+            mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
         }
         else
         {
-            mlist = generate_castling< KING_SIDE, Checks, false>(pos, mlist, Us, ci);
-            mlist = generate_castling<QUEEN_SIDE, Checks, false>(pos, mlist, Us, ci);
+            mlist = generate_castling<MakeCastling<Us,  KING_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
+            mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
         }
     }
 
index f767f6dbcbf442af1b4455ff01571f093a8bc8cf..5a5ebb5d1ffa127f897abcb066086ac15269318f 100644 (file)
@@ -291,10 +291,10 @@ Score Entry::update_safety(const Position& pos, Square ksq) {
   Value bonus = shelter_storm<Us>(pos, ksq);
 
   // If we can castle use the bonus after the castling if it is bigger
-  if (pos.can_castle(make_castling_flag(Us, KING_SIDE)))
+  if (pos.can_castle(MakeCastling<Us, KING_SIDE>::flag))
       bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));
 
-  if (pos.can_castle(make_castling_flag(Us, QUEEN_SIDE)))
+  if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::flag))
       bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
 
   return kingSafety[Us] = make_score(bonus, -16 * minKPdistance[Us]);
index 0cea2d372faf9ed04ec813e17d7f89c269b31418..dcd3eb2485316441a986f785b761588cbe0b77d2 100644 (file)
@@ -304,23 +304,23 @@ void Position::set_castling_flag(Color c, Square rfrom) {
 
   Square kfrom = king_square(c);
   CastlingSide cs = kfrom < rfrom ? KING_SIDE : QUEEN_SIDE;
-  CastlingFlag cf = make_castling_flag(c, cs);
+  CastlingFlag cf = (c | cs);
 
   st->castlingFlags |= cf;
   castlingFlagsMask[kfrom] |= cf;
   castlingFlagsMask[rfrom] |= cf;
-  castlingRookSquare[c][cs] = rfrom;
+  castlingRookSquare[cf] = rfrom;
 
   Square kto = relative_square(c, cs == KING_SIDE ? SQ_G1 : SQ_C1);
   Square rto = relative_square(c, cs == KING_SIDE ? SQ_F1 : SQ_D1);
 
   for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); ++s)
       if (s != kfrom && s != rfrom)
-          castlingPath[c][cs] |= s;
+          castlingPath[cf] |= s;
 
   for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); ++s)
       if (s != kfrom && s != rfrom)
-          castlingPath[c][cs] |= s;
+          castlingPath[cf] |= s;
 }
 
 
@@ -353,16 +353,16 @@ const string Position::fen() const {
   ss << (sideToMove == WHITE ? " w " : " b ");
 
   if (can_castle(WHITE_OO))
-      ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE,  KING_SIDE)), false) : 'K');
+      ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE |  KING_SIDE)), false) : 'K');
 
   if (can_castle(WHITE_OOO))
-      ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE, QUEEN_SIDE)), false) : 'Q');
+      ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE | QUEEN_SIDE)), false) : 'Q');
 
   if (can_castle(BLACK_OO))
-      ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK,  KING_SIDE)),  true) : 'k');
+      ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK |  KING_SIDE)),  true) : 'k');
 
   if (can_castle(BLACK_OOO))
-      ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK, QUEEN_SIDE)),  true) : 'q');
+      ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK | QUEEN_SIDE)),  true) : 'q');
 
   if (!can_castle(WHITE) && !can_castle(BLACK))
       ss << '-';
@@ -1393,14 +1393,12 @@ bool Position::pos_is_ok(int* failedStep) const {
       for (Color c = WHITE; c <= BLACK; ++c)
           for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1))
           {
-              CastlingFlag cf = make_castling_flag(c, s);
-
-              if (!can_castle(cf))
+              if (!can_castle(c | s))
                   continue;
 
-              if (  (castlingFlagsMask[king_square(c)] & cf) != cf
-                  || piece_on(castlingRookSquare[c][s]) != make_piece(c, ROOK)
-                  || castlingFlagsMask[castlingRookSquare[c][s]] != cf)
+              if (  (castlingFlagsMask[king_square(c)] & (c | s)) != (c | s)
+                  || piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK)
+                  || castlingFlagsMask[castlingRookSquare[c | s]] != (c | s))
                   return false;
           }
 
index 8d3393faaa18b20c0fdbcafba1a7a3cee8c25fb5..527e66e8b623073764f43d7569b7f305d0f15945 100644 (file)
@@ -100,10 +100,10 @@ public:
   template<PieceType Pt> const Square* list(Color c) const;
 
   // Castling
-  int can_castle(CastlingFlag f) const;
   int can_castle(Color c) const;
-  bool castling_impeded(Color c, CastlingSide s) const;
-  Square castling_rook_square(Color c, CastlingSide s) const;
+  int can_castle(CastlingFlag f) const;
+  bool castling_impeded(CastlingFlag f) const;
+  Square castling_rook_square(CastlingFlag f) const;
 
   // Checking
   Bitboard checkers() const;
@@ -198,8 +198,8 @@ private:
 
   // Other info
   int castlingFlagsMask[SQUARE_NB];
-  Square castlingRookSquare[COLOR_NB][CASTLING_SIDE_NB];
-  Bitboard castlingPath[COLOR_NB][CASTLING_SIDE_NB];
+  Square castlingRookSquare[CASTLING_FLAG_NB];
+  Bitboard castlingPath[CASTLING_FLAG_NB];
   StateInfo startState;
   uint64_t nodes;
   int gamePly;
@@ -281,12 +281,12 @@ inline int Position::can_castle(Color c) const {
   return st->castlingFlags & ((WHITE_OO | WHITE_OOO) << (2 * c));
 }
 
-inline bool Position::castling_impeded(Color c, CastlingSide s) const {
-  return byTypeBB[ALL_PIECES] & castlingPath[c][s];
+inline bool Position::castling_impeded(CastlingFlag f) const {
+  return byTypeBB[ALL_PIECES] & castlingPath[f];
 }
 
-inline Square Position::castling_rook_square(Color c, CastlingSide s) const {
-  return castlingRookSquare[c][s];
+inline Square Position::castling_rook_square(CastlingFlag f) const {
+  return castlingRookSquare[f];
 }
 
 template<PieceType Pt>
index 2317626e3d39cfe6604ce2fc41f7465a7ccbe8e1..f041e29db5102c7defa5ad8a250d20b2309d76c4 100644 (file)
@@ -116,6 +116,14 @@ enum MoveType {
   CASTLING  = 3 << 14
 };
 
+enum Color {
+  WHITE, BLACK, NO_COLOR, COLOR_NB = 2
+};
+
+enum CastlingSide {
+  KING_SIDE, QUEEN_SIDE, CASTLING_SIDE_NB = 2
+};
+
 enum CastlingFlag {  // Defined as in PolyGlot book hash key
   NO_CASTLING,
   WHITE_OO,
@@ -126,10 +134,10 @@ enum CastlingFlag {  // Defined as in PolyGlot book hash key
   CASTLING_FLAG_NB = 16
 };
 
-enum CastlingSide {
-  KING_SIDE,
-  QUEEN_SIDE,
-  CASTLING_SIDE_NB = 2
+template<Color C, CastlingSide S> struct MakeCastling {
+  static const CastlingFlag
+  flag = C == WHITE ? S == QUEEN_SIDE ? WHITE_OOO : WHITE_OO
+                    : S == QUEEN_SIDE ? BLACK_OOO : BLACK_OO;
 };
 
 enum Phase {
@@ -187,10 +195,6 @@ enum Piece {
   PIECE_NB = 16
 };
 
-enum Color {
-  WHITE, BLACK, NO_COLOR, COLOR_NB = 2
-};
-
 enum Depth {
 
   ONE_PLY = 2,
@@ -337,6 +341,10 @@ inline Square operator|(File f, Rank r) {
   return Square((r << 3) | f);
 }
 
+inline CastlingFlag operator|(Color c, CastlingSide s) {
+  return CastlingFlag(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
+}
+
 inline Value mate_in(int ply) {
   return VALUE_MATE - ply;
 }
@@ -349,10 +357,6 @@ inline Piece make_piece(Color c, PieceType pt) {
   return Piece((c << 3) | pt);
 }
 
-inline CastlingFlag make_castling_flag(Color c, CastlingSide s) {
-  return CastlingFlag(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
-}
-
 inline PieceType type_of(Piece p)  {
   return PieceType(p & 7);
 }