summaryrefslogtreecommitdiff
path: root/src/engine/moves
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/moves')
-rw-r--r--src/engine/moves/knight.c6
-rw-r--r--src/engine/moves/pawn.c16
2 files changed, 15 insertions, 7 deletions
diff --git a/src/engine/moves/knight.c b/src/engine/moves/knight.c
index 64e44ed..4aca3ac 100644
--- a/src/engine/moves/knight.c
+++ b/src/engine/moves/knight.c
@@ -68,9 +68,9 @@ bitboard_t knight_moves[64] = {
1128098930098176ULL,
2257297371824128ULL,
4796069720358912ULL,
- 4796069720358912ULL >> 1,
- 4796069720358912ULL >> 2,
- 4796069720358912ULL >> 3,
+ 4796069720358912ULL << 1,
+ 4796069720358912ULL << 2,
+ 4796069720358912ULL << 3,
4679521487814656ULL,
9077567998918656ULL,
};
diff --git a/src/engine/moves/pawn.c b/src/engine/moves/pawn.c
index 66b1d06..bc97fb7 100644
--- a/src/engine/moves/pawn.c
+++ b/src/engine/moves/pawn.c
@@ -34,6 +34,12 @@ bitboard_t get_black_pawn_forwardmoves(moves_t* moves, position_t position) {
return x;
}
+bool pawn_in_range(int to, int next_from, int ub_escape, int turn) {
+ if (next_from == ub_escape) return false;
+ if (turn == WHITE_TURN) return to > next_from && to - next_from <= 16;
+ return to < next_from && next_from - to <= 16;
+}
+
void get_pawn_moves(moves_t* moves, position_t position) {
assert_valid_position(position);
@@ -65,20 +71,23 @@ void get_pawn_moves(moves_t* moves, position_t position) {
bitboard_t item = file_masks[i] & forward_moves;
bitboard_t pawns = file_masks[i] & friendly_pawns;
+ int UB_ESCAPE = position.turn == WHITE_TURN ? 64 : -1;
+
int from = __builtin_ctzll(pawns);
pawns &= (pawns - 1);
int next_from = __builtin_ctzll(pawns);
- if (pawns == 0) next_from = 64; // to work around UB
+ if (pawns == 0) next_from = UB_ESCAPE;
while (item) {
int to = __builtin_ctzll(item);
- if (to > next_from) {
+ if (pawn_in_range(to, next_from, UB_ESCAPE, position.turn)) {
from = next_from;
pawns &= (pawns - 1);
next_from = __builtin_ctzll(pawns);
- if (pawns == 0) next_from = 64;
+ if (pawns == 0) next_from = UB_ESCAPE;
}
item &= (item - 1);
+
if (to >= 56 || to <= 7) {
add_promote_moves(0);
} else add_move(moves, from, to);
@@ -128,7 +137,6 @@ void get_pawn_moves(moves_t* moves, position_t position) {
}
}
-
bitboard_t ATTACK_MASK;
int shift;
if (position.turn == WHITE_TURN) {