user11328: I've just finished translating my Java Engine to C++11, expecting great speed improvements in move generation. The code is very straightforward and uses a pre-generate bitboards approach. I also generate only legal moves, ie, inside generateMoves() I check to see if the king is in check.
As a quick test, I ran Perft (6) from the starting position. The Java version finishes the test on ~11 seconds. The C++ version takes ~8.5 seconds (my perft stops at depth 1, and zobrist keys are being updated on makeMove and undoMove).
The same test on Stockfish runs under 1 second.
I was expecting the C++ version to be at least twice as faster as the Java one, and of course, somewhat slower than Stockfish. 
After some investigation, it's clear that checking if the king is in check, for every move, is the major bottleneck. What can I do about this? I was trying to understand how to work with pinned pieces (https://chessprogramming.wikispaces.com/Checks+and+Pinned+Pieces+(Bitboards)), but I'm not sure how to follow. For example, what does obstructed() do in the link above?
NOTE: I already compiled it with the -O2 optimization flag.
EDIT: Here is some more details on the code:
Q: How did you generate sliding pieces?
I use the classfical bitboard approach, at program start I generate attack masks for all pieces:
void MoveGen::bishopMoves(Board& board, int side, MoveList& moves){
    int opp = side ^ 1;
    U64 bishops = board.bitboards[Board::BISHOP | side];
    U64 occup = board.bitboards[side] | board.bitboards[opp];
    U64 enemyOrEmpty = ~board.bitboards[side] & ~board.bitboards[Board::KING | opp];

    U64 *up_r = BitBoardGen::BITBOARD_DIRECTIONS[BitBoardGen::IDX_UP_RIGHT];
    U64 *up_l = BitBoardGen::BITBOARD_DIRECTIONS[BitBoardGen::IDX_UP_LEFT];
    U64 *down_r = BitBoardGen::BITBOARD_DIRECTIONS[BitBoardGen::IDX_DOWN_RIGHT];
    U64 *down_l = BitBoardGen::BITBOARD_DIRECTIONS[BitBoardGen::IDX_DOWN_LEFT];

    while (bishops != 0){
        int from = numberOfTrailingZeros(bishops);
        U64 upward =  upwardMoveTargetsFrom(from, occup, up_r, enemyOrEmpty) | upwardMoveTargetsFrom(from, occup, up_l, enemyOrEmpty);
        U64 downward =  downwardMoveTargetsFrom(from, occup, down_r, enemyOrEmpty) | downwardMoveTargetsFrom(from, occup, down_l, enemyOrEmpty);
        add_moves(board, from, upward | downward, Move::NO_FLAGS, moves);
        bishops &= bishops - 1;
    }
}

 //handle blocking
U64 MoveGen::upwardMoveTargetsFrom(int from, U64 occup, U64 dir_mask[], U64 enemyOrEmpty){
    U64 ray = dir_mask[from];
    U64 blocker = occup & ray;

    if (blocker != 0){
        int sq = numberOfTrailingZeros(blocker);
        ray^= dir_mask[sq];
    } 
    return ray & enemyOrEmpty;
}

Q: How did you generate legal moves?
I only generate legal moves, so I check if the king is in check while generating moves. This is probably one of the problems, since most moves don't leave the king in check.
The generateAllMoves code:
MoveList genLegalMoves()
   valid = ()
   moves = getPseudoLegalForAllPieces (may leave the king in check)
   for move in moves
      board.applyMove(move) //its a local makemove, no zorbist involved
      atCheck = isAtCheck()  //dumb code, be more selective here?
      board.removeMove(move)//its a local undomove, no zorbist involved

      if !atCheck:
         valid.add(move)

  atCheck = isAtCheck() //again for castling
  if (! atCheck and canCastleKS)
     valid.add(castleKS)
  if (! atCheck and canCastleQS)
     valid.add(castleQS)
  return valid   

Q: How did you undo your move?
My makeMove is like the following (this is fast):
int makeMove(int move):
    int undo = board.state
    incremental update zobrist key
    update board.board[]
    update board.bitboards
    update board.state
    return undo

void undoMove(int move, int undo):
    board.state = undo
    incremental undo zobrist key
    update board.board[]
    update board.bitboards

And finally, my isAtCheck() code goes like:
boolean isAtCheck(int side):
    locate king square
    get all opponent attacks mask 
    return kingSquare & attacksMask

EDIT 2: I have changed the code to a pseudo-legal generator. The problem now is that I have to go to depth 0 to count only legal moves, and now it's taking ~16 seconds.

user3774: Disclaimer: I can't support my claims with numbers. I don't have your engine to profile. It is mostly gut feelings.
It looks like a performance killer is a castling test.
Testing for canCastle*S (it looks like it is about King and Rook didn't move, and nothing is in between) is way cheaper than testing for King becoming exposed, so swap the tests order. C++ will short-circuit logical and.
BTW, for castling you need to test against inCheck at three squares:
if (canCastleKS && !inCheck(e_file) && !inCheck(f_file) && !inCheck(g_file)) {
    ....
}


user825: Your code looks fine. We can't really give you the exact cause, but we can give you some hints.
Q: How did you generate sliding pieces?
Sliding piece generation is typically the slowest because we need to check the enemy pieces. Stockfish uses magic bitboard, which is also used by Houdini and Komodo.
Q: How did you generate legal moves?
Stockfish always generate pseudo-legal moves and only check when it's necessary.
Q: How did you undo your move?
Stockfish can easily undo a move by zorbrist hashing. Hash value can be updated efficiently.
------------------------ EDIT ------------------------
Your sliding generation and undo look okay to me. I think the bottleneck is your legal move generation. Look at this recent thread:
http://talkchess.com/forum/viewtopic.php?t=61797

How do you ensure legality:
  a) by making/unmaking each move and testing whether it leaves the king in check, or
  b) by only doing this for the small set of moves that could actually be illegal (king moves, check evasions, ep captures, and moves of pinned pieces), or
  c) by doing clever move generation that knows how to avoid generating illegal moves?
  Only in case of a) I'd say you should spend effort to improve speed, since that could be done an order of magnitude faster, e.g. like b). Otherwise I'd keep it as it is, provided it is correct Smile

Sven Schule is a respected engine programmer, we should trust his words. Your implementation looks like exactly his point a.

11328: <p>I've just finished translating my Java Engine to C++11, expecting great speed improvements in move generation. The code is very straightforward and uses a pre-generate bitboards approach. I also generate only legal moves, ie, inside generateMoves() I check to see if the king is in check.</p>

<p>As a quick test, I ran Perft (6) from the starting position. The Java version finishes the test on ~11 seconds. The C++ version takes ~8.5 seconds (my perft stops at depth 1, and zobrist keys are being updated on makeMove and undoMove).</p>

<p>The same test on Stockfish runs under 1 second.</p>

<p>I was expecting the C++ version to be at least twice as faster as the Java one, and of course, somewhat slower than Stockfish. </p>

<p>After some investigation, it's clear that checking if the king is in check, for every move, is the major bottleneck. What can I do about this? I was trying to understand how to work with pinned pieces (<a href="https://chessprogramming.wikispaces.com/Checks+and+Pinned+Pieces+(Bitboards)" rel="nofollow">https://chessprogramming.wikispaces.com/Checks+and+Pinned+Pieces+(Bitboards)</a>), but I'm not sure how to follow. For example, what does <code>obstructed()</code> do in the link above?</p>

<p>NOTE: I already compiled it with the -O2 optimization flag.</p>

<p>EDIT: Here is some more details on the code:</p>

<p><strong>Q: How did you generate sliding pieces?</strong></p>

<p>I use the classfical bitboard approach, at program start I generate attack masks for all pieces:</p>

<pre><code>void MoveGen::bishopMoves(Board&amp; board, int side, MoveList&amp; moves){
    int opp = side ^ 1;
    U64 bishops = board.bitboards[Board::BISHOP | side];
    U64 occup = board.bitboards[side] | board.bitboards[opp];
    U64 enemyOrEmpty = ~board.bitboards[side] &amp; ~board.bitboards[Board::KING | opp];

    U64 *up_r = BitBoardGen::BITBOARD_DIRECTIONS[BitBoardGen::IDX_UP_RIGHT];
    U64 *up_l = BitBoardGen::BITBOARD_DIRECTIONS[BitBoardGen::IDX_UP_LEFT];
    U64 *down_r = BitBoardGen::BITBOARD_DIRECTIONS[BitBoardGen::IDX_DOWN_RIGHT];
    U64 *down_l = BitBoardGen::BITBOARD_DIRECTIONS[BitBoardGen::IDX_DOWN_LEFT];

    while (bishops != 0){
        int from = numberOfTrailingZeros(bishops);
        U64 upward =  upwardMoveTargetsFrom(from, occup, up_r, enemyOrEmpty) | upwardMoveTargetsFrom(from, occup, up_l, enemyOrEmpty);
        U64 downward =  downwardMoveTargetsFrom(from, occup, down_r, enemyOrEmpty) | downwardMoveTargetsFrom(from, occup, down_l, enemyOrEmpty);
        add_moves(board, from, upward | downward, Move::NO_FLAGS, moves);
        bishops &amp;= bishops - 1;
    }
}

 //handle blocking
U64 MoveGen::upwardMoveTargetsFrom(int from, U64 occup, U64 dir_mask[], U64 enemyOrEmpty){
    U64 ray = dir_mask[from];
    U64 blocker = occup &amp; ray;

    if (blocker != 0){
        int sq = numberOfTrailingZeros(blocker);
        ray^= dir_mask[sq];
    } 
    return ray &amp; enemyOrEmpty;
}
</code></pre>

<p><strong>Q: How did you generate legal moves?</strong></p>

<p>I only generate legal moves, so I check if the king is in check while generating moves. This is probably one of the problems, since most moves don't leave the king in check.</p>

<p>The generateAllMoves code:</p>

<pre><code>MoveList genLegalMoves()
   valid = ()
   moves = getPseudoLegalForAllPieces (may leave the king in check)
   for move in moves
      board.applyMove(move) //its a local makemove, no zorbist involved
      atCheck = isAtCheck()  //dumb code, be more selective here?
      board.removeMove(move)//its a local undomove, no zorbist involved

      if !atCheck:
         valid.add(move)

  atCheck = isAtCheck() //again for castling
  if (! atCheck and canCastleKS)
     valid.add(castleKS)
  if (! atCheck and canCastleQS)
     valid.add(castleQS)
  return valid   
</code></pre>

<p><strong>Q: How did you undo your move?</strong></p>

<p>My makeMove is like the following (this is fast):</p>

<pre><code>int makeMove(int move):
    int undo = board.state
    incremental update zobrist key
    update board.board[]
    update board.bitboards
    update board.state
    return undo

void undoMove(int move, int undo):
    board.state = undo
    incremental undo zobrist key
    update board.board[]
    update board.bitboards
</code></pre>

<p>And finally, my <code>isAtCheck()</code> code goes like:</p>

<pre><code>boolean isAtCheck(int side):
    locate king square
    get all opponent attacks mask 
    return kingSquare &amp; attacksMask
</code></pre>

<p>EDIT 2: I have changed the code to a pseudo-legal generator. The problem now is that I have to go to depth 0 to count only legal moves, and now it's taking ~16 seconds.</p>
 3774: <p>Disclaimer: I can't support my claims with numbers. I don't have your engine to profile. It is mostly gut feelings.</p>

<p>It looks like a performance killer is a castling test.</p>

<p>Testing for <code>canCastle*S</code> (it <em>looks</em> like it is about King and Rook didn't move, and nothing is in between) is way cheaper than testing for King becoming exposed, so swap the tests order. C++ will short-circuit logical <code>and</code>.</p>

<p>BTW, for castling you need to test against <code>inCheck</code> at three squares:</p>

<pre><code>if (canCastleKS &amp;&amp; !inCheck(e_file) &amp;&amp; !inCheck(f_file) &amp;&amp; !inCheck(g_file)) {
    ....
}
</code></pre>
 825: <p>Your code looks fine. We can't really give you the exact cause, but we can give you some hints.</p>

<p><strong>Q: How did you generate sliding pieces?</strong></p>

<p>Sliding piece generation is typically the slowest because we need to check the enemy pieces. Stockfish uses <strong>magic bitboard</strong>, which is also used by Houdini and Komodo.</p>

<p><strong>Q: How did you generate legal moves?</strong></p>

<p>Stockfish always generate <strong>pseudo-legal</strong> moves and <a href="https://stackoverflow.com/questions/7197369/chess-programming-no-ai-moves-validation/8589734#8589734">only check when it's necessary</a>.</p>

<p><strong>Q: How did you undo your move?</strong></p>

<p>Stockfish can easily undo a move by <strong>zorbrist hashing</strong>. Hash value can be updated efficiently.</p>

<p>------------------------ <strong>EDIT</strong> ------------------------</p>

<p>Your sliding generation and undo look okay to me. I think the bottleneck is your legal move generation. Look at this recent thread:</p>

<p><a href="http://talkchess.com/forum/viewtopic.php?t=61797" rel="nofollow noreferrer">http://talkchess.com/forum/viewtopic.php?t=61797</a></p>

<blockquote>
  <p>How do you ensure legality:<br>
  a) by making/unmaking each move and testing whether it leaves the king in check, or<br>
  b) by only doing this for the small set of moves that could actually be illegal (king moves, check evasions, ep captures, and moves of pinned pieces), or<br>
  c) by doing clever move generation that knows how to avoid generating illegal moves?<br>
  Only in case of a) I'd say you should spend effort to improve speed, since that could be done an order of magnitude faster, e.g. like b). Otherwise I'd keep it as it is, provided it is correct Smile</p>
</blockquote>

<p><strong>Sven Schule</strong> is a respected engine programmer, we should trust his words. Your implementation looks like <strong>exactly</strong> his point a.</p>
