Filter invalid moves early
authorFabian Fichter <ianfab@users.noreply.github.com>
Sun, 1 Sep 2019 18:50:36 +0000 (20:50 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Fri, 20 Sep 2019 12:57:34 +0000 (14:57 +0200)
No functional change.

src/movegen.cpp
src/position.cpp

index a3e0ab2..55c4d5a 100644 (file)
@@ -108,7 +108,7 @@ namespace {
     // Single and double pawn pushes, no promotions
     if (Type != CAPTURES)
     {
-        emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
+        emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces() & pos.board_bb());
 
         Bitboard b1 = shift<Up>(pawnsNotOn7)   & emptySquares;
         Bitboard b2 = pos.double_step_enabled() ? shift<Up>(b1 & TRank3BB) & emptySquares : Bitboard(0);
@@ -158,7 +158,7 @@ namespace {
     if (pawnsOn7)
     {
         if (Type == CAPTURES)
-            emptySquares = ~pos.pieces();
+            emptySquares = ~pos.pieces() & pos.board_bb();
 
         if (Type == EVASIONS)
             emptySquares &= target;
@@ -193,7 +193,7 @@ namespace {
             {
                 if (pos.count(Us, pt))
                     continue;
-                Bitboard b = (pos.attacks_from(Us, pt, from) & ~pos.pieces()) | from;
+                Bitboard b = (pos.attacks_from(Us, pt, from) & ~pos.pieces() & pos.board_bb()) | from;
                 if (Type == EVASIONS)
                     b &= target;
 
@@ -404,7 +404,7 @@ ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
      if (pt == PAWN)
          continue; // Will be generated together with direct checks
 
-     Bitboard b = pos.moves_from(us, pt, from) & ~pos.pieces();
+     Bitboard b = pos.moves_from(us, pt, from) & ~pos.pieces() & pos.board_bb();
 
      if (pt == KING)
          b &= ~PseudoAttacks[~us][QUEEN][pos.square<KING>(~us)];
@@ -440,7 +440,7 @@ ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
   }
 
   // Generate evasions for king, capture and non capture moves
-  Bitboard b = pos.attacks_from<KING>(us, ksq) & ~pos.pieces(us) & ~sliderAttacks;
+  Bitboard b = pos.attacks_from<KING>(us, ksq) & ~pos.pieces(us) & ~sliderAttacks & pos.board_bb();
   while (b)
       moveList = make_move_and_gating<NORMAL>(pos, moveList, us, ksq, pop_lsb(&b));
 
index 1f683d7..56f3a96 100644 (file)
@@ -750,10 +750,6 @@ bool Position::legal(Move m) const {
   assert(color_of(moved_piece(m)) == us);
   assert(!count<KING>(us) || piece_on(square<KING>(us)) == make_piece(us, KING));
 
-  // Illegal moves to squares outside of board
-  if (!(board_bb() & to))
-      return false;
-
   // Illegal checks
   if ((!checking_permitted() || (sittuyin_promotion() && type_of(m) == PROMOTION)) && gives_check(m))
       return false;
@@ -876,6 +872,10 @@ bool Position::pseudo_legal(const Move m) const {
   Square to = to_sq(m);
   Piece pc = moved_piece(m);
 
+  // Illegal moves to squares outside of board
+  if (!(board_bb() & to))
+      return false;
+
   // Use a fast check for piece drops
   if (type_of(m) == DROP)
       return   piece_drops()