user-1: I am currently developing a Bitboard chess engine using C++. I am in the process of creating the move generator function. I have completed most of it and only the casting part is left.
I have used Magic Bitboards for sliding pieces and precalculated the attacks for other pieces. I am trying to make everything Highly efficient, by avoiding too many if-statements. The part I am stuck at is castling. The most naive method would be to use a few if-statements for checking whether the squares are empty and castling is still available. But this wouldn't be efficient at all.
What would be the fastest method to generate castle-moves?

user15989: Castling checks seem easy to short-circuit to be efficient. For my own implementation (not blazing fast), I did the following checks in order:
Generate pseudo-legal move:

Castling rights: Booleans for each castling, flipped when relevant king/rook move for the first time.
Intervening squares vacant: precalculated bitboards for each castling; AND it with all pieces.

Test move:

Check that the 3 squares the king (not the rook) passes through are not attacked. I had a general isSquareAttacked method for that.

This already suffices for orthodox chess. No need to make and unmake the move.
For Chess960, if you're interested, you do need to make and unmake the move to see if the king is in check after castling. This is because of the following type of situation: wRb1, wKe1, bRa1; a-side castling is illegal for white, although the squares the king passes through (e1, d1, c1) are not under attack in the static position. This case only arises for Chess960, though.

user21129: I am not sure about the fastest way but I used pre-defined bitboards with 1:s on the bishop and knight squares (and queen if castling queen side) respectively and AND those together with the all pieces bitboard (there can't be any oppoennt pieces on the squares either). If the resulting bitboard is empty and you have castling rights, then you are allowed to make the move.
Edit: You also can't add the castling move if you are in check, which should be the first and easiest to check (since you probably already check if you are in check or not in your move gen routine).

user9684: It's been a long time since I've made any efficiency mods, but this can be reduced to one if statement, but many would be easier to read.
if
!bitboard_occupied  // easy to find by ORing with f1/g1 (or queenside)

!bitboard_attacked | [square e1/f1/g1 (or queenside)  // attacked already known

!bitboard_moved | (h1 (or a1) & e1)

Updating the moved bitboards would be more costly to maintain during each move and undo than the extra if statements in the relatively rare castling routine.

-1: <p>I am currently developing a Bitboard chess engine using C++. I am in the process of creating the move generator function. I have completed most of it and only the casting part is left.</p>
<p>I have used <a href="https://www.chessprogramming.org/Magic_Bitboards" rel="nofollow noreferrer">Magic Bitboards</a> for sliding pieces and precalculated the attacks for other pieces. I am trying to make everything Highly efficient, by avoiding too many if-statements. The part I am stuck at is castling. The most naive method would be to use a few if-statements for checking whether the squares are empty and castling is still available. But this wouldn't be efficient at all.</p>
<p>What would be the fastest method to generate castle-moves?</p>
 15989: <p>Castling checks seem easy to short-circuit to be efficient. For my own implementation (not blazing fast), I did the following checks in order:</p>
<p>Generate pseudo-legal move:</p>
<ul>
<li>Castling rights: Booleans for each castling, flipped when relevant king/rook move for the first time.</li>
<li>Intervening squares vacant: precalculated bitboards for each castling; AND it with all pieces.</li>
</ul>
<p>Test move:</p>
<ul>
<li>Check that the 3 squares the king (not the rook) passes through are not attacked. I had a general isSquareAttacked method for that.</li>
</ul>
<p>This already suffices for orthodox chess. No need to make and unmake the move.</p>
<p>For Chess960, if you're interested, you do need to make and unmake the move to see if the king is in check after castling. This is because of the following type of situation: wRb1, wKe1, bRa1; a-side castling is illegal for white, although the squares the king passes through (e1, d1, c1) are not under attack in the static position. This case only arises for Chess960, though.</p>
 21129: <p>I am not sure about the fastest way but I used pre-defined bitboards with 1:s on the bishop and knight squares (and queen if castling queen side) respectively and AND those together with the all pieces bitboard (there can't be any oppoennt pieces on the squares either). If the resulting bitboard is empty and you have castling rights, then you are allowed to make the move.</p>
<p>Edit: You also can't add the castling move if you are in check, which should be the first and easiest to check (since you probably already check if you are in check or not in your move gen routine).</p>
 9684: <p>It's been a long time since I've made any efficiency mods, but this can be reduced to one if statement, but many would be easier to read.</p>
<p>if</p>
<pre><code>!bitboard_occupied  // easy to find by ORing with f1/g1 (or queenside)

!bitboard_attacked | [square e1/f1/g1 (or queenside)  // attacked already known

!bitboard_moved | (h1 (or a1) &amp; e1)
</code></pre>
<p>Updating the moved bitboards would be more costly to maintain during each move and undo than the extra if statements in the relatively rare castling routine.</p>
